#include<iostream>
#include<ctime>
#include<cstdlib>
int main(void)
{
using std::cout;
using std::cin;
using std::srand;
using std::time;
using std::rand;
using std::string;
srand((unsignedint)time(0));
bool end=false;
bool lost;
string name;
int archers=50;
int catapults=25;
int swordmen=100;
int g_archers=rand()%(51)+20;
int g_catapults=rand()%(41)+10;
int g_swordmen=rand()%(101)+50;
cout<<"Welcome!\nWhat's your name?\n";
cin>>name;
cout<<"Well,welcome "<<name<<"\nThis is the Roman Commander Game!";
int archers_sent=0;
int catapults_sent=0;
int swordmen_sent=0;
int menu_choice;
while(!end);
{
cout<<"You have"<<archers<<"archers,"<<catapults<<"catapults,"<<swordmen<<"swordmen\n";
cout<<"Gemania have"<<g_archers<<"archers,"<<g_catapults<<"catapults,"<<g_swordmen<<"swordmen\n";
int fight_menu;
do
{
int i=1;
int archers_menu;
int catapults_menu;
int swordmen_menu;
if (archers>0 && (archers-archers_sent)!=0)
{
archers_menu=i;
cout<<"["<<i<<"] send archers\n";
i++;
}
else archers_menu=0;
if (catapults>0 && (catapults-catapults_sent)!=0)
{
catapults_menu=i;
cout<<"["<<catapults_menu<<"] send catapults\n";
i++;
}
else catapults_menu=0;
if (swordmen>0 && (swordmen-swordmen_sent)!=0)
{
swordmen_menu=i;
cout<<"["<<swordmen_menu<<"] send swordmen\n";
i++;
}
else swordmen_menu=0;
fight_menu=i;
cout<<"["<<fight_menu<<"] Go fight!\n";
cin>>menu_choice;
if (menu_choice==archers_menu)
{
do{
cout<<"\nHow many archers do you want to send?\n";
cin>>archers_sent;
}while (!(archers_sent>-1 && archers_sent<=archers));
}
elseif (menu_choice==catapults_menu)
{
do{
cout<<"\nHow many catapults do you want to send?\n";
cin>>catapults_sent;
}while (!(catapults_sent>-1 && catapults_sent<=catapults));
}
elseif (menu_choice==swordmen_menu)
{
do{
cout<<"How many swordmen do you want to send?\n";
cin>>swordmen_sent;
}while(!(swordmen_sent>-1 && swordmen_sent<=swordmen));
}
}while(menu_choice!=fight_menu);
cout<<"\nEntering Battle!\n";
int archers_dead,catapults_dead,swordmen_dead;
int g_archers_dead,g_catapults_dead,g_swordmen_dead;
archers_dead=2*g_catapults;
catapults_dead=g_swordmen;
swordmen_dead=3*g_archers;
g_archers_dead=2*catapults_sent;
g_catapults_dead=swordmen_sent;
g_swordmen_dead=3*archers_sent;
archers=(archers>archers_dead)?archers-archers_dead:0;
catapults=(catapults>catapults_dead)?catapults-catapults_dead:0;
swordmen=(swordmen>swordmen_dead)?swordmen-swordmen_dead:0;
g_archers(g_archers>g_archers_dead)?g_archers-g_archers_dead:0;
g_catapults=(g_catapults>g_catapults_dead)?g_catapults-g_catapults_dead:0;
g_swordmen=(g_swordmen>g_swordmen_dead)?g_swordmen-g_swordmen_dead:0;
cout<<"It was a long journey"
<<archers_dead<<"archers died,"
<<catapults_dead<<"catapults died,"
<<swordmen_dead<<"swordmen died.\n";
if((archers+catapults+swordmen)==0)
{
end=lost =true;
}
elseif((g_archers+g_catapults+g_swordmen)==0)
{
end=true;
lost=false;
}
if(lost)
{
cout<<"You lost!\n";
return 0;
}
cout<<"You win!!\n";
return 0;
}
}
Output:
Welcome Adventurer, what is your name?
MarcusWell, Marcus welcome to the Roman Commander Game.
You are the commander of the Roman Army attacking German
You have 50 archers, 25 catapults, and 100 swordsmen.
Germania has 61 archers, 50 catapults, and 52 swordsmen.
[1] Send Archers
[2] Send Catapults
[3] Send Swordsmen
[4] Go Fight
1
How many archers would you like to send?
50
[1] Send Catapults
[2] Send Swordsmen
[3] Go Fight
1How many catapults would you like to send?
25
[1] Send Swordsmen
[2] Go Fight
1
How many swordsmen would you like to send?
100
[1] Go Fight
1
Entering Battle...
It was a long battle.
100 archers died.
52 catapults died.
183 swordsmen died.
You lost. Try again next time.
The output may not look like the exactly the same as my code,but since i only change some sentences,so it doesn't matter..
Thank you!!
Well ?: is the only ternary operator of c/c++ but it's not equivalent to if/else in all cases.
The ?: is an expression and if-then-else is a statement so they can always be used in the same place.
Example in initialization list of base classes only the ternary operator can be used. Generally in conditional assignment situations only the ternary operator can be used.