I have spent the last few very late nights trying to figure out this code. My instructor for my intro to programming course is having us create a game for our final project. The game is pretty basic and he wanted us to use a variety different functions and loops. It is a game about Ants and mostly uses random number generators to decide the outcome. For the most part, it is currently exactly the way he wanted it.
I am mainly having difficulties with the getMyFood() function. I first noticed the problem when the function would run twice within the same loop pass. I basically added a final else command with a cout message at the bottom of my function. I should never receive the message because I am only generating a random number between 1&3. I test the random number to see if its 1,2 or 3, and if its not my last else cout command kicks in. This only happens about every other game or so which has me even more confused that it is not a consistent error.
switch (dayNum)
{
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
case 7:
day = "Sunday";
default:
break;
}
return day;
}
void beginGame()
{
string currentDay;
string myFoodType;
string enemyFoodType;
int myAnts = 100;
int enemyAnts = 100;
int myBabies = 100;
int enemyBabies = 100;
int myFood = 0;
int enemyFood = 0;
for (int i=1; i<8; i+=1)
{
currentDay = dayOfWeek(i);
gameStats(myAnts, enemyAnts, currentDay);
hatchBabies(myAnts, enemyAnts, myBabies, enemyBabies);
cout<<endl;
cout<<myBabies<<" ants were hatched in my colony today. My colony now has " <<myAnts<< " ants."<<endl;
cout<<enemyBabies<<" ants were hatched in enemy colony today. Enemy colony now has " <<enemyAnts<< " ants."<<endl;
cout<<endl;
getMyFood(myFood, myFoodType);
if (myFoodType =="nothing")
{
myAnts = myAnts - myFood;
cout <<"My colony searches for food and finds "<<myFoodType<<"."<<endl;
cout <<myFood<<" ants die of starvation.";
cout <<"The colony now has "<<myAnts<<" ants."<<endl;
cout <<endl;
}
else if (myFoodType =="dead bug")
{
myAnts = myAnts + myFood;
cout <<"My colony searches for food and finds "<<myFoodType<<"."<<endl;
cout <<"Crunchy! They devour it and add "<<myFood<<" ants due to the nutrition. "<<endl;
cout <<"The colony now has "<<myAnts<<" ants."<<endl;
cout <<endl;
}
else
{
myAnts = myAnts + myFood;
cout <<"My colony searches for food and finds "<<myFoodType<<"."<<endl;
cout <<"Sweet! They devour it and add "<<myFood<<" ants due to a sugar rush. "<<endl;
cout <<"The colony now has "<<myAnts<<" ants."<<endl;
cout <<endl;
}
getEnemyFood(enemyFood, enemyFoodType);
if (enemyFoodType =="nothing")
{
enemyAnts = enemyAnts - enemyFood;
cout <<"The enemy colony searches for food and finds "<<enemyFoodType<<"."<<endl;
cout <<enemyFood<<" ants die of starvation."<<endl;
cout <<"The enemy colony now has "<<enemyAnts<<" ants."<<endl;
cout <<endl;
}
else if (enemyFoodType =="dead bug")
{
enemyAnts = enemyAnts + enemyFood;
cout <<"The enemy colony searches for food and finds "<<enemyFoodType<<"."<<endl;
cout <<"Crunchy! They devour it and add "<<enemyFood<<" ants due to the nutrition. "<<endl;
cout <<"The enemy colony now has "<<enemyAnts<<" ants."<<endl;
cout <<endl;
}
else if (enemyFoodType == "candy bar")
{
enemyAnts = enemyAnts + enemyFood;
cout <<"The enemy colony searches for food and finds "<<enemyFoodType<<"."<<endl;
cout <<"Sweet! They devour it and add "<<enemyFood<<" ants due to a sugar rush. "<<endl;
cout <<"The enemy colony now has "<<enemyAnts<<" ants."<<endl;
cout <<endl;
}
else{cout<<"this is a mistake"<<endl;}
if (willMeet()==true)
{
cout<<"The colonies run into each other while returning home. What do you want to do?"<<endl;
willFight(myAnts, enemyAnts);
}
cout<<"My colony returns safely home for the night."<<endl;
do {
cout << "(F)ight or (R)un away: "<<endl;
cin >> choice;
choice = toupper(choice);
if (choice == 'F')
{
cout << "My colony attacks and causes damage of "<<myDamage<<endl;
cout << "The enemy colony attacks and causes damage of "<<enemyDamage<<endl;
if (myDamage > enemyDamage)
{
cout << "My colony wins the fight. The enemy colony loses 10 ants in the battle."<<endl;
enemyAnts = enemyAnts - 10;
}
else if (myDamage < enemyDamage)
{
cout << "My colony loses the fight, and loses 10 ants in the battle."<<endl;
myAnts = myAnts - 10;
}
else
{
cout << "We fight to a draw."<<endl;
}
}
if (choice == 'R')
{
cout << "My colony retreats, but we lose 5 ants while running away."<<endl;
myAnts = myAnts - 5;
}