Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 75% + Suggestions for Nim program Nim is a more complex program than you have done before, so here are some ideas on how

image text in transcribed
image text in transcribed
1 75% + Suggestions for Nim program Nim is a more complex program than you have done before, so here are some ideas on how you might start working on it. Algorithm 1) Do any initial setup work 2) While the game is going on do for each player in turn a. Display the board b. Get a new move C. Validate the move d. Update the count of sticks e. Check for game over If not over, swap player 3) When game concludes, congratulate winner Commented out example program f #include using namespace std; int main() A initialization 1/ define variables // set number of sticks W see if computer or two people bool playing = true; int player = 1; while playing) { display board I should use for loop 1 W2 - got and validate input // integer between 1 and 3 Il not greater than romaining sticks // 3 - update board // subtract move from number of sticks remaining 4 - see if done Wit number of sticks is zero, set playing to false W5 - not done, alternate player if player 1. go to player 2 Wir player 2. go to player 1 > Wannounce winner using player number or name return 0; CS 161 Programming Lab 5 Problem description You will write a program that implements the following version of the game of Nim. Nim is a mathematical strategy game, which dates back to 16th century China. There are many variations on this game, but the version we'll be playing starts with a row of 11 sticks On their turn a player may remove 1.2 or 3 sticks from the pile. Players alternate taking turns. and the play removing the last stick wins. Your game should feature two players alternating turns. First, your program should print out a welcome statement and brief instructions. Then. you should display the current game state and prompt player 1 for the number of sticks they should remove: Welcome to Nim! The player Players will take tums removing 1.2 or 3 sticks from the initial 11 removing the last stick wins! Player 1. it's your turn! The game currently looks like this: How many sticks would you like to remove? (1. 2. or 3) After making sure they entered a valid response, you should remove the appropriate number of sticks If there are now zero sticks remaining, you should declare that player the winner and end the program. Otherwise the other player should take a turn Program Requirements Your program should validate that the user entered a legal move (1,2,3) but not more than the current number of sticks. You should also verify that it is an integer and clean up the buffer if it is not You should display the number of sticks using a for loop. Your program should not have duplicate code, that is you should not have code for player1 and player2 but should use the same code for both with a variable indicating which player it is After the game is over, ask if they want to play again (yor n). If they respond with a y, start the game over. Optional Program Enhancements For a more interesting game, implement a computer opponent. A simple opponent would pick randomly between 1 and 3 sticks per move, while a more advanced player could play perfectly. See the document on suggestions for more information. Another variation would be a version with more than one row of sticks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 2005 Databases

Authors: Karli Watson

1st Edition

0470044063, 978-0470044063

More Books

Students also viewed these Databases questions

Question

(b) Show that f(n) = 4 log n + log log n = ?(log n)

Answered: 1 week ago