background info A well known game: 2 opponents take turns taking bowling balls (
ID: 3625787 • Letter: B
Question
background info
A well known game: 2 opponents take turns taking bowling balls (cylinders) from a stack. In each move, a playa will choose how many bowling cylinders to take. He must must take at the very least 1 but at most 1/2 of the bowling cylinders. Then opponent #2 takes his turn. The playa who takes the final bowling cylinder loses.
Note: please use C++ format to answer follwoing 2 questions
Write a program in which computer(COMP) plays against an actual person (PER) opponent.
1. Create a random integer(INT) between the values of ten & one hundred to indicate the starting size of the pile.
2. Create a random INT between the values of zero & one to decide whether it’s an actual PER or a COMP acquires the initial turn.
The playas can be either: Dumb COMP, intelligent COMP or PER
Explanation / Answer
please rate - thanks
#include <iostream>
using namespace std;
int smartmove(int);
int dumbmove(int);
int person(int);
int main()
{bool smart;
bool turn;
int pins,move;
srand(time(0));
pins=rand()%91+10;
cout<<"We are smarting with "<<pins<<" pins ";
if(rand()%2==0)
smart=false;
else
smart=true;
if(smart)
cout<<"careful-I'm smart ";
else
cout<<"you've got a good change - I'm dumb ";
if(rand()%2==0)
{cout<<"I go first ";
turn=true;
}
else
{cout<<"you go first ";
turn=false;
}
while(pins>0)
{if(!turn)
move=person(pins);
else
if(smart)
move=smartmove(pins);
else
move=dumbmove(pins);
pins-=move;
cout<<"There are "<<pins<<" pins left ";
turn=!turn;
}
if(!turn)
cout<<"You win!!! ";
else
cout<<"Better luck next time-I won:) ";
system("pause");
return 0;
}
int smartmove(int p)
{int move;
int i,powers[]={2,4,8,16,32,64};
for(i=0;i<6;i++)
if(p==powers[i]-1)
return dumbmove(p);
i=0;
while(p>=powers[i])
i++;
i--;
move=p-powers[i]+1;
if(move>p/2)
return dumbmove(p);
cout<<"I take "<<move<<" pins ";
return move;
}
int dumbmove(int p)
{int move;
if(p<=2)
move=1;
else
move=rand()%(p/2)+1;
cout<<"I take "<<move<<" pins ";
return move;
}
int person(int p)
{int move;
int half;
if(p==1)
half=1;
else
half=p/2;
cout<<"Enter your move-must be between 1 and "<<half<<endl;
cin>>move;
if(p==1)
p=2;
while(move<1||move>p/2)
{cout<<"Invalid move ";
cout<<"Enter your move-must be between 1 and "<<half<<endl;
cin>>move;
}
return move;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.