I am working in visual studios and need to write this program in C++. The game o
ID: 3606874 • Letter: I
Question
I am working in visual studios and need to write this program in C++.
The game of matchsticks is a two-player game played with 21 matchsticks (or any other object). Players take turns removing matchsticks from a centralized pile. On any given turn, a player can remove 1, 2, or 3 matchsticks. Turns pass from player to player. The winner of the game is the player who takes the last matchstick. It is against the rules to put matchsticks back into a pile, or skip a turn.
Hint: If you get to go first and know the "algorithm" you can always win. To "see" the algorithm look carefully at the number of matchsticks that remain after each move.
The Secret: Whenever there is a chance, adjust the remaining number of sticks so that number is evenly divisible by 4. If you do, you will win. If you don't, the opponent's next move will use this strategy, and then you cannot win.
Program:
For this programming assignment, you will create a computerized game of matchsticks that allows for a human player and a simulated computer player. The simulated computer play should exhibit perfect play. Your game should have the following options.
1) Allow a choice of which player goes first.
2) Allow changing of the number of matchsticks with which the players begin the game from the default value of 21.
Requirements:
"Functionalize" your program as much as possible, that is, decompose the program into reasonable modules (functions). Main should pretty much consist of variable declarations, overall control structure, and function invocations. Anything substantial should be relegated to a function, either void or non-void, as appropriate. Remember to comment your functions to indicate their purpose within the program.
Verify that the user is making a legal move. In other words, your program should check that the number of matchsticks requested by the user is between 1 and 3 and does not exceed the number of matchsticks remaining.
Use at least one void function that passes back more than one parameter by reference in the construction of your program.
The game should allow the player to start a new game after the game is complete. At the end of each game, the program should output the total number of games won so far by the user and by the computer along with the player’s winning percentage.
You may not use any global variables in the development of your game.
Test your program thoroughly!
Bonus:
Frequently, a computerized opponent that plays optimally is not a lot of fun for the human player. Modify your program so that there is an additional AI that plays randomly. Incorporate a menu to allow the user to select the level of difficulty.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
class MatchstickGame
{
//Matchstick Game class
public:
string username;
string firstPlayer;
string currentPlayer;
int sticks;
MatchstickGame()
{
cout<<"Enter name of the player ";
cin>>username;
string welcomeMessage="Welcome "+username+" ";
cout<<welcomeMessage;
}
/**
Function removes stick from pile
**/
void removeStick(int removeSticks);
/**
Initialises pile with matchsticks
**/
void initialise();
/**
Gives current status of game
uses pass by reference for the same
**/
void getCurrentStatus(int &noOfSticks,string &nextPlayer);
/**
returns if the given move is a valid move
or not
**/
bool isValid(int removeSticks);
/**
Computer takes its turn optimally if possible
**/
void computerTurn();
/**
Computer takes its turn based on difficulty set(1-5)
**/
void computerTurn(int difficulty);
/**
User takes turn and removes some sticks
**/
void userTurn(int removeSticks);
/**
Checks if game has ended
**/
int hasEnded();
/**
Returns the winner of the game
**/
string getWinner();
};
void MatchstickGame::removeStick(int removeSticks)
{
sticks-=removeSticks;
}
void MatchstickGame::initialise()
{
int option;
cout<<"Enter number of sticks ";
cin>>sticks;
printf("Which player to go first ");
cout<<"Enter 1 for computer"<<" and 2 for "<<username<<" ";
cin>>option;
if(option==1)
{
firstPlayer="computer";
}
else
{
firstPlayer=username;
}
cout<<firstPlayer<<" will go first ";
currentPlayer=firstPlayer;
}
void MatchstickGame::getCurrentStatus(int &noOfStick,string &nextPlayer)
{
noOfStick=sticks;
nextPlayer=currentPlayer;
}
bool MatchstickGame::isValid(int removeSticks)
{
if(removeSticks>3 || removeSticks<0)
{
return false;
}
if(sticks-removeSticks>=0)
{
return true;
}
else
{
return false;
}
}
void MatchstickGame::userTurn(int sticksToRemove)
{
removeStick(sticksToRemove);
currentPlayer="computer";
}
void MatchstickGame::computerTurn()
{
bool foundOptimalStep=false;
if((sticks-1)%4==0)
{
cout<<"Computer Removed 1 stick ";
removeStick(1);
foundOptimalStep=true;
}
else if((sticks-2)%4==0)
{
cout<<"Computer Removed 2 sticks ";
removeStick(2);
foundOptimalStep=true;
}
if((sticks-3)%4==0)
{
cout<<"Computer Removed 3 sticks ";
removeStick(3);
foundOptimalStep=true;
}
if(!foundOptimalStep)
{
int randNum=(rand()%3)+1;
cout<<"Computer Removed "<<randNum<<" sticks ";
removeStick(randNum);
}
currentPlayer=username;
}
void MatchstickGame::computerTurn(int difficulty)
{
int optimalstep=0;
int randomstep=0;
bool foundOptimalStep=false;
if((sticks-1)%4==0)
{
foundOptimalStep=true;
optimalstep=1;
}
else if((sticks-2)%4==0)
{
foundOptimalStep=true;
optimalstep=2;
}
if((sticks-3)%4==0)
{
foundOptimalStep=true;
optimalstep=3;
}
int randNum=(rand()%3)+1;
randomstep=randNum;
randNum=(rand()%5)+1;
/**
If difficulty is 5 then it will always be greater than equal random number between 1-5
hence computer will take an optimal step.
if difficulty is 4 it will be greater than equal random number with probability 4/5 hence
it will have higher changes of giving optimal step
If difficulty is 1 it will have most probability(1/5) of giving random step.
**/
if(difficulty>=randNum)
{
cout<<"Computer Removed "<<optimalstep<<" sticks ";
removeStick(optimalstep);
}
else
{
cout<<"Computer Removed "<<randomstep<<" sticks ";
removeStick(randomstep);
}
currentPlayer=username;
}
int MatchstickGame::hasEnded()
{
return (sticks==0);
}
string MatchstickGame::getWinner()
{
if(hasEnded()==false)
{
return "Game hasnt ended";
}
if(currentPlayer==username)
{
return "computer";
}
else
{
return username;
}
}
float percentage(float gamesWon,float totalGames)
{
return (gamesWon/totalGames)*100.00;
}
int main()
{
int totalGames=0,gamesWon=0;
string ans;
MatchstickGame game;
srand(time(NULL));
do
{
game.initialise();
int noOfSticks;
string nextPlayer=game.firstPlayer;
while(!game.hasEnded())
{
int removeSticks;
if(nextPlayer==game.username)
{
bool valid=false;
do
{
cout<<"Enter number of sticks to remove: ";
cin>>removeSticks;
valid=game.isValid(removeSticks);
if(!valid)
{
cout<<"Please make a valid move ";
}
}
while(!valid);
game.userTurn(removeSticks);
}
else
{
game.computerTurn();
}
game.getCurrentStatus(noOfSticks,nextPlayer);
cout<<"No of Sticks Remaining: "<<noOfSticks<<" next Player: "<<nextPlayer<<endl;
}
cout<<game.getWinner()<<" is the Winner ";
if(game.getWinner()==game.username)
{
gamesWon++;
}
totalGames++;
cout<<"Total games: "<<totalGames<<endl;
cout<<"Games Won by User: "<<gamesWon<<endl;
cout<<"Games Won by Computer: "<<totalGames-gamesWon<<endl;
cout<<"Percentage Games won by user: "<<percentage(gamesWon,totalGames)<<endl;
cout<<"Want to play more(y/n): ";
cin>>ans;
}
while(ans=="y");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.