NutrientAmount FoodAmount::getScaledCaloriesFromNutrientId (const QString& nutrId) const { if (getFood() == NULL) { throw std::logic_error("Attempted to scale the nutrients of an undefined food."); } return getFood()->getCaloriesFromNutrientId(nutrId) * getScaleFactor(); }
double FoodAmount::getAmount(const QSharedPointer<const Unit>& otherUnit) const { if (!otherUnit || !isDefined() || otherUnit->getDimension() == getUnit()->getDimension()) { return Amount<Food, FoodAmount>::getAmount(otherUnit); } else { // Unlike generic Amounts, which cannot meaningfully convert from units in // one dimension to units in another, foods can be specified in multiple // dimensions, allowing for such a conversion. For example, if a food's // entry describes (equivalently) 2 C or 100 g, then it should be possible // to determine how many grams 3 Tbsp of the food weigh. // This is done by first finding the conversion factor from the unit that // the amount is already in to the base unit of the dimension that we // wish to convert to. Then, we do the normal intra-dimension conversion // from the base unit of the target dimension to the ultimately desired unit. FoodAmount baseAmountInSourceDimension = getFood()->getBaseAmount(getUnit()->getDimension()); // baseAmountInSourceDimension must be defined, or something is already // seriously wrong, and there should have been an exception already. FoodAmount baseAmountInTargetDimension = getFood()->getBaseAmount(otherUnit->getDimension()); if (baseAmountInTargetDimension.isDefined()) { double dimensionConversionFactor = baseAmountInTargetDimension.getAmount() / baseAmountInSourceDimension.getAmount(); FoodAmount amountInTargetBaseUnit (getSubstanceNonConst(), getAmount() * dimensionConversionFactor, baseAmountInTargetDimension.getUnit()); return amountInTargetBaseUnit.getAmount(otherUnit); } else { throw std::logic_error("Attempted to get an amount of a food in an inappropriate dimension."); } } }
void snake::moveStep() { if(createFood_lock == true) { cout<<"creating food"<<endl; return; } switch(direct) { case UP: if(MAP[head.x-1][head.y]==NOTHING) { gotoXY(head.x-1,head.y); } else if(MAP[head.x-1][head.y]==FOOD) getFood(); else gameOver=true; break; case DOWN: if(MAP[head.x+1][head.y]==NOTHING) { gotoXY(head.x+1,head.y); } else if(MAP[head.x+1][head.y]==FOOD) getFood(); else gameOver=true; break; case LEFT: if(MAP[head.x][head.y-1]==NOTHING) { gotoXY(head.x,head.y-1); } else if(MAP[head.x][head.y-1]==FOOD) getFood(); else gameOver=true; break; case RIGHT: if(MAP[head.x][head.y+1]==NOTHING) { gotoXY(head.x,head.y+1); } else if(MAP[head.x][head.y+1]==FOOD) getFood(); else gameOver=true; break; default:break; } }
QVector<FoodAmount> FoodAmount::getScaledComponents() const { // See getScaledNutrients for logic description if (getFood() == NULL) { throw std::logic_error("Attempted to scale the components of an undefined food."); } QVector<FoodAmount> components = getFood()->getComponentAmounts(); double scaleFactor = getScaleFactor(); for (QVector<FoodAmount>::iterator i = components.begin(); i != components.end(); ++i) { (*i) *= scaleFactor; } return components; }
QMap<QString, NutrientAmount> FoodAmount::getScaledNutrients() const { // TODO: Cache this instead of recomputing every time if (getFood() == NULL) { throw std::logic_error("Attempted to scale the nutrients of an undefined food."); } QMap<QString, NutrientAmount> nutrients = getFood()->getNutrients(); double scaleFactor = getScaleFactor(); for (QMap<QString, NutrientAmount>::iterator i = nutrients.begin(); i != nutrients.end(); ++i) { (*i) *= scaleFactor; } return nutrients; }
static void next_level() { food = getFood(); draw_block( food.x, food.y, 0b11101000); if(snake.len < MAX_SNAKE_LEN-2) snake.len++; if(snake.speed >= MAX_SPEED) snake.speed--; setTextColor(0xff,0b11100000); DoString(0,0,IntToStr(++points,6,0)); }
static void next_level() { points++; food = getFood(); if(snake.len < MAX_SNAKE_LEN-1) snake.len++; if(snake.speed > MAX_SPEED) snake.speed--; render_level(); }
void Core::loopBegin(const IGui *gui, Food food, t_key key) { int score; score = 0; if (gui) { displayBegin(gui, food); while ((key != ESCAPE_KEY) && (collisionWithWall(_pos[0].getPosX(), _pos[0].getPosY())) && (collisionWithSnake(_pos[0].getPosX(), _pos[0].getPosY())) && (posFood(food))) { if (getFood()) { food.putFood(_pos, _width, _height); gui->displayFood(food.getPosX(), food.getPosY()); score += 100; } if (!getFood()) { gui->displayAfterFood(_pos[_pos.size() - 1].getPosX(), _pos[_pos.size() - 1].getPosY()); _pos.pop_back(); } key = gui->manageKey(); if (key == ESCAPE_KEY) std::cout << "You're a poor loser, you leave the game !" << std::endl; if (key == SPEEDP_KEY || key == SPEEDL_KEY) setSpeed(key); else setDirectionSnake(key); moveDirection(); gui->displayBody(_pos[0].getPosX(), _pos[0].getPosY()); gui->displayScore(score); if (_speed <= 0) _speed += -(_speed); usleep(_speed); } } }
double FoodAmount::getScaleFactor() const { if (getFood() == NULL) { throw std::logic_error("Attempted to scale the an undefined food."); } // This FoodAmount object represents x amount of food in units of Foo. // In order to scale the nutrients, we need to know what fraction of the entry // for this food this x Foo amount represents. // To determine this, we need to get the base amount for the food entry in terms // of some unit Bar that is of the same dimension as Foo (e.g. Foo and Bar must // both be weights, or both volumes, etc.) // Once the base amount b Bar is determined, then the scale factor y can be computed // as: y = (x Foo) / (b Bar * (Foo/Bar)) // So we approach this by first obtaining baseAmount (b Bar), and then converting // it to be in terms of Foo units by calling baseAmount.getAmount(unit). Then the // division is performed to get the scale factor. const QSharedPointer<const Unit> unit = getUnit(); const QSharedPointer<const Food> food = getFood(); FoodAmount baseAmount = food->getBaseAmount(unit->getDimension()); if (!baseAmount.isDefined()) { throw std::logic_error("Attempted to scale based on a dimension for which " "the food does not have a base amount."); } double scaleFactor = getAmount() / baseAmount.getAmount(unit); if (!bIncludesRefuse) { scaleFactor /= ((100 - getFood()->getPercentRefuse()) / 100); } return scaleFactor; }
static void reset() { int i; // setup the screen lcdClear(); for (i=MIN_X; i<MAX_X; i++) { lcdSetPixel(i,MIN_Y,0b000101011); lcdSetPixel(i,MAX_Y,0b000101011); } for (i=MIN_Y; i<MAX_Y; i++) { lcdSetPixel(MIN_X,i,0b000101011); lcdSetPixel(MAX_X,i,0b000101011); } snake.speed = MIN_SPEED; snake.len = 3; snake.dir = 0; snake.t_start = 2; points = 0; food = getFood(); // create snake in the middle of the field snake.tail[0].x = SIZE_X/2; snake.tail[0].y = SIZE_Y/2; snake.tail[1].x = SIZE_X/2 +1; snake.tail[1].y = SIZE_Y/2; snake.tail[2].x = SIZE_X/2 +2; snake.tail[2].y = SIZE_Y/2; // print initail tail draw_block(snake.tail[0].x, snake.tail[0].y, 0b00011000); draw_block(snake.tail[1].x, snake.tail[1].y, 0b00011000); draw_block(snake.tail[2].x, snake.tail[2].y, 0b00011000); // switch to level one render_level(); }