Write a C++ program. Here is the project description. The game of "23" is a two-player game that begins with a pile of 23 toothpicks. Players take turns, withdrawing either 1, 2, or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays "23". The human should always move first. When it is the computer's turn, it should play according to the following rules:
1. If there are more than 4 toothpicks left, then the computer should with-draw 4 - X toothpicks, where X is the number of toothpicks the human withdrew on the previous turn.
2. If there are 2 to 4 toothpicks left, then the computer should withdraw enough toothpicks to leave 1.
3. If there is 1 toothpick left, then the computer has to take it and loses.
When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure that the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile.
Additional requirement
1. If the user picks illegal number of sticks, the program will prompt the user to pick again.
2. At the end of each game, the program will prompt user to choose if the user wants another game.
Hi, Please find the solution and comments inline. I thought it is best using stack for this. Hence implemented a simple one.
//
// main.cpp
// ToothPicks
//
#include <iostream>
#include "Stack.hpp"
using namespace std;
//if this function returns false then human lost. It will return true if there are still items in the stack,
//it will return true until there are items in stack;
static int previousTurnToothPics =0 ;
bool withDrawHuman(Stack *st){
int numberOfToothPicks=0;
cout<<endl<<"Enter the number of toothpicks you want to withdraw:";
cin>>numberOfToothPicks;
while(!(numberOfToothPicks==1 || numberOfToothPicks==2 ||numberOfToothPicks==3) ){
cout<<"Please enter a value within 1-3 inclusive;"<<endl;
cin>>numberOfToothPicks;
}
previousTurnToothPics = numberOfToothPicks;//keeping track of usernumberofToothpicks for computer turn
for (int i=0; i<numberOfToothPicks; i++) {
if(st->pop()==nullptr){
return false;
}
}
return true;//if control reaches here, means there are still toothpicks left
}
bool withDrawComputer(Stack *st){
int itemsInStack = st->countItemsInStack();
int withdraw = 0;
if(itemsInStack>4){
withdraw = 4-previousTurnToothPics;
}else if(itemsInStack>=2 && itemsInStack<=4){
withdraw = (itemsInStack-1);
}else if(itemsInStack==1){
withdraw = 1;
return false;
}
for (int i=0; i<withdraw; i++) {
if(st->pop()==nullptr){
return false;
}
}
if(st->countItemsInStack()==0){
return false;
}
return true;
}
int main(int argc, const char * argv[]) {
char choice = 'y';
Stack st;
do{
for (int i=0; i<23; i++) {
st.push(new Node(i));
}
while (st.countItemsInStack()!=0) {
if(!withDrawHuman(&st)){
cout<<"You Lost";
break;
}
// cout<<endl<<"After human";
// st.printStack();//can uncomment this
if(!withDrawComputer(&st)){
cout<<"You Won!Hurray!";
break;
}
// cout<<endl<<"After computer:"<<endl;
// st.printStack();//can uncomment this to see whats going on
}
st.clear();
cout<<"Do you want to play another game?Yes(y) or no(n)";
cin>>choice;
}while(choice=='y');
}
#ifndef Stack_hpp
#define Stack_hpp
#include <stdio.h>
#include "iostream"
class Node {
int val;
Node *next;
public:
Node(int val):val(val){
next = nullptr;
}
void setNext(Node *item){
next = item;
}
Node* getNext(){
return next;
}
int getVal(){
return val;
}
};
class Stack{
public:
Node *head;
void clear(){
head=nullptr;
}
void push(Node *newNode){
newNode->setNext(nullptr);
if(head==nullptr){
head=newNode;
return;
}
newNode->setNext(head);
head = newNode;
}
Node* pop(){
if(head==nullptr){
return nullptr;
}
Node *temp = head;
head = temp->getNext();
temp->setNext(nullptr);
return temp;
}
int countItemsInStack(){
Node *temp = head;
int count=0;
while(temp!=nullptr){
count++;
temp = temp->getNext();
}
return count;
}
void printStack(){
Node *temp = head;
std::cout<<std::endl;
while(temp!=nullptr){
std::cout<<temp->getVal()<<", ";
temp=temp->getNext();
}
std::cout<<std::endl;
}
};
#endif /* Stack_hpp */
nothing in Stack.cpp
Sample Run:
Please rate the solution. Thanks
Get Answers For Free
Most questions answered within 1 hours.