Feb 5, 2017 at 7:03pm Feb 5, 2017 at 7:03pm UTC
In my function and prototype - void battle - I was pleasantly surprised that this worked. I really didn't think it would because I'm using both a char and int's. As I understood it, you can only have one type being passed. Why does this work?
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
void intro_text ();
int get_zombie_num ();
int get_human_num ();
bool ask_to_play_again ();
void end_text (int num_humans_start, int num_of_humans, int humans_killed,
int num_zombies_start, int num_of_zombies, int zombies_killed);
void battle (char turn, int& num_of_humans, int& human_health, int& current_human_health, int& humans_killed,
int& num_of_zombies, int& zombie_health, int& current_zombie_health, int& zombies_killed);
int main ()
{
bool continue_game = true;
char turn = 'H';
// human number and health
int num_of_humans = 0;
int human_health = 15;
int current_human_health = human_health;
// zombie number and health
int num_of_zombies = 0;
int zombie_health = 10;
int current_zombie_health = zombie_health;
intro_text ();
while (continue_game == true)
{
int humans_killed = 0;
int zombies_killed = 0;
num_of_zombies = get_zombie_num ();
num_of_humans = get_human_num ();
int num_humans_start = num_of_humans;
int num_zombies_start = num_of_zombies;
battle (turn, num_of_humans, human_health, current_human_health, humans_killed,
num_of_zombies, zombie_health, current_zombie_health, zombies_killed);
end_text (num_humans_start, num_of_humans, humans_killed,
num_zombies_start, num_of_zombies, zombies_killed);
continue_game = ask_to_play_again();
}
return 0;
}
void intro_text ()
{
cout << "\t--- Humans vs Zombies ---" << endl << endl;
cout << "Zombies are slowly walking towards the town." << endl;
cout << "Send your soldiers out to fend off the zombies." << endl;
cout << "Choose how many zombies there are, and how many soldiers you send." << endl << endl;
cout << "Your soldiers will be stronger than the zombie army." << endl << endl << endl;
}
int get_zombie_num ()
{
int a = 0;
cout << "How many zombies do you see?: ";
cin >> a;
cout << endl;
return a;
}
int get_human_num ()
{
int b = 0;
cout << "How many soldiers will you send?: ";
cin >> b;
cout << endl << endl;
return b;
}
bool ask_to_play_again ()
{
char input = 0;
bool yes_no = 0;
cout << "Play again?: ";
cin >> input;
cout << endl << endl;
if ((input == 'Y') || (input == 'y'))
{
yes_no = true;
cout << "More zombies are shambling towards the town..." << endl << endl;
}
else
{
yes_no = false;
cout << "The zombie army shambles towards the town as your army retreats." << endl << endl;
}
return yes_no;
}
void end_text (int num_humans_start, int num_of_humans, int humans_killed,
int num_zombies_start, int num_of_zombies, int zombies_killed)
{
if (num_of_humans > 0)
{
cout << "You win!" << endl << endl;
cout << "Soldiers started with: " << num_humans_start << endl;
cout << "Soldiers remaining: " << num_humans_start - humans_killed << endl;
cout << "Soldiers died: " << humans_killed << endl << endl;
cout << "Zombies that came: " << num_zombies_start << endl;
cout << "Zombies remaining: " << num_zombies_start - zombies_killed << endl;
cout << "Zombies you destroyed: " << zombies_killed << endl << endl;
}
else
{
cout << "Zombies win!" << endl << endl;
cout << "Soldiers started with: " << num_humans_start << endl;
cout << "Soldiers remaining: " << num_humans_start - humans_killed << endl;
cout << "Soldiers died: " << humans_killed << endl << endl;
cout << "Zombies that came: " << num_zombies_start << endl;
cout << "Zombies remaining: " << num_zombies_start - zombies_killed << endl;
cout << "Zombies you destroyed: " << zombies_killed << endl << endl;
}
}
void battle (char turn, int& num_of_humans, int& human_health, int& current_human_health, int& humans_killed,
int& num_of_zombies, int& zombie_health, int& current_zombie_health, int& zombies_killed)
{
mt19937 ran_num_gen (time(0));
uniform_int_distribution<int> human_attack (3, 8);
uniform_int_distribution<int> zombie_attack (1, 6);
while ((num_of_humans > 0) && (num_of_zombies > 0))
{
if (turn == 'H')
{
current_zombie_health -= human_attack (ran_num_gen);
turn = 'Z';
{
if (current_zombie_health < 1)
{
num_of_zombies --;
zombies_killed ++;
current_zombie_health = zombie_health;
}
}
}
else
{
current_human_health -= zombie_attack (ran_num_gen);
turn = 'H';
{
if (current_human_health < 1)
{
num_of_humans --;
humans_killed ++;
current_human_health = human_health;
}
}
}
}
}
Feb 5, 2017 at 8:20pm Feb 5, 2017 at 8:20pm UTC
each parameter can have its own type in C++.
so
foo(int x, double y, char* z)
is perfectly fine!
also, char IS an int in c++. it can hold only 8 bits worth, but its otherwise an integer. (0-255 or -127 to 128)
Feb 5, 2017 at 8:55pm Feb 5, 2017 at 8:55pm UTC
Thanks a lot for the quick reply and the info!