Feb 17, 2013 at 8:14am Feb 17, 2013 at 8:14am UTC
So I have a game in which you insert how much items are in the pile. Then the player takes 1-3 items from the pile. Then the PC takes a random count of items again 1-3. The winner is the one who takes the last item of the pile. So here is the code with P1 vs. P2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include<iostream>
using namespace std;
int main(){
long long items,taken_from_P_one,taken_from_P_two,which_player;
cout<<"How much items are in the pile?" <<endl;
cin>>items;
while (items>0){
which_player=1;
cout<<"P1 take 1-3 items." <<endl;
cin>>taken_from_P_one;
items-=taken_from_P_one;
cout<<"There are " <<items<<" items left." <<endl;
if (items<=0){
goto end;
}
which_player=2;
cout<<"P2 take 1-3 items." <<endl;
cin>>taken_from_P_two
items-=taken_from_P_two
cout<<"There are" <<items<<" items left." <<endl;
}
end:
if (which_player==1){
cout<<"P1 wins" ;
}else {
if (which_player==2){
cout<<"P2 wins" ;
}
}
return 0;
}
and here is the random number generator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <cstdlib>
#include <ctime>
#include <iostream>
long long getRandom( long long min, long long max )
{
long long range = max-min+1 ;
return rand() % range + min ;
}
int main()
{
srand(std::time(0)) ;
std::cout << getRandom(1, 3) << "\n" ;
}
how could I put the random number generator code inside the game so P2 is the computer and takes a random count of items still 1-3?
Last edited on Feb 17, 2013 at 8:34am Feb 17, 2013 at 8:34am UTC