Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

background info A well known game: 2 opponents take turns taking bowling balls (

ID: 3625788 • 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 ball loses.

Write a program in which computer(COMP) plays against an actual person (PER) opponent.

Please note: the program must be written in C++ format


1. Create a random integer(INT) between the values zero and one to pick whether the computer (COMP) start game as dumb or intelligent. In the "dumb" scenario, the COMP will take a random legal value (between one and n/2) from the stack whenever it takes a turn.

2. In “intelligent” scenario the COMP takes off enough marbles to make the size of the pile a power of two minus one – I.e. 3, 7, or 63. This move is legal ( the only exception is if the size of the stack is currently 1 less than a power of 2). In that case, the COMP makes a random legal move.

The playas can be either: Dumb COMP, intelligent COMP or PER(person)

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;
}