Question

Write a C++ program. Here is the project description. The game of "23" is a two-player...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask...
R-S-P Requirement: - write one C++ program that mimics the Rock-Scissor-Paper game. This program will ask user to enter an input (out of R-S-P), and the computer will randomly pick one and print out the result (user wins / computer wins). - User's input along with computer's random pick will be encoded in the following format: -- user's rock: 10 -- user's scissor:          20 -- user's paper:            30 -- comp's rock:            1 -- comp's scissor:        2 -- comp's...
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can...
(Game: scissor, rock, paper) Write a program that plays the popular scissor-rockpaper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws.
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional...
Write a program that allows two players to play a game of tic-tac-toe. Use a twodimensional char array with three rows and three columns as the game board. Each element in the array should be initialized with an asterisk (*). The program should run a loop that: • Displays the contents of the board array. • Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code!...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code! Thanks! The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in...
*PYTHON 3 * Coin taking game Game rules a) The game starts with 22 coins in a common pool b) The goal of the game is to take the last coin c) Players take turns removing 1, 2, or 3 coins from the pool d) A player cannot take more coins than remain in the pool e) After each turn, the number of coins that remain in the pool is announced f) When the last coin is taken, the winner...
Write a program that plays a number guessing game with a human user. The human user...
Write a program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
Write a Python program that plays a number guessing game with a human user. The human...
Write a Python program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with...
One variation of the game of Nim, described in Chapter 5 Exercise 12, is played with four piles of stones. Initially, the piles contain 1, 2, 3, and 4 stones. On each turn, a player may take 1, 2, or 3 stones from a single pile. The player who takes the last stone loses. a) Write a program that allows two players to play Nim. Be sure to allow only legal moves. The program should be written so that it...
PLEASE POST IN C++ : Thank you!! 5.27 LAB*: Program: Soccer team roster (Vectors) This program...
PLEASE POST IN C++ : Thank you!! 5.27 LAB*: Program: Soccer team roster (Vectors) This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team. (1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int vector and the ratings in another int vector. Output these vectors (i.e., output...
in C++, Program 4: Conversion between Bases Write a program that will convert a decimal number...
in C++, Program 4: Conversion between Bases Write a program that will convert a decimal number to binary, octal, and hexadecimal (base 2, 8, and 16). Program Requirements: Prompt the user to input an integer value in decimal (base 10) and display the value in base 2, 8 and 16. Do NOT use built-in conversion methods for the conversion. You must do your own math to compute the values. Repeat the prompt and conversion until the user enters a sentinel...