Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

INF 120 Project #7 (Worth 200 pts) The Nim game. For this project, you will write a program for a two-player version of the game

INF 120 Project #7 (Worth 200 pts)

The Nim game. For this project, you will write a program for a two-player version of the game Nim: a human player will play against the computer. The game is simple: players take turns removing from 1 to 4 sticks from a pile of 13 sticks; the player who picks up the last stick wins the game.

Heres some pseudocode for the Nim game.

Start by initializing some variables that will allow you to keep track of the state of the game:

One variable should record how many sticks there are left in the pile; initially, this variable must be initialized to 13.

There are two players in the game, who take turns. To remember whos turn is next, use a variable with two states (= values). Say the variable is 1 if the human player is next, and it is 2 if the computers turn is next. Pick randomly the player who should start the game.

Then, start the main loop of the game: the game should continue for as long as there still are sticks (at least one) to pick. In each repetition of the game loop, one player will play their turn:

First, print the current board in this version, the whole game will be played in the console. The board will simply be a representation of the sticks that are left to play; you should also state textually how many sticks there are left. Heres the board at the beginning of the game:

| | | | | | | | | | | | | 13 sticks remaining

Before you let the next player play, determine what is the maximum number of sticks that can be removed in this turn: that is 4, if there are at least 4 sticks left, or the number of remaining sticks, if less than 4 sticks remain. The minimum number of sticks to remove is always 1.

Next, one player plays their turn. If its the human players turn, show them a message asking how many sticks they want to pick (from 1 to the maximum that you determined before), and get their input. Validate the input; this means you must check if the player inputs a correct number, in the allowed range. If they didnt input correctly, then keep asking for their input until it is correct.

Note: to read input in the console, use the input() function. This function works similarly to requestInteger(), only in the console: it will show a prompt message and will return the value that the user types in before they hit the Enter key.

If its the computers turn, then have your program pick a random number of sticks to play in the allowed range, as established before. Show a message in the console to inform the human player of how the computer plays its turn. Note: this isnt actually a very smart strategy to play the game, and the human will almost certainly always win.

Once the current player human or computer has picked the number of sticks to remove, those sticks will be removed from the remaining stack.

If there still are sticks to play, switch whos turn it is next. This means that you will need to switch between the two possible values for the variable that keeps track of the current

player; for example, if it was 1 in this turn, it should be changed to 2 for the next turn. You need to do this so that you know whos turn it is the next time through the game loop.

When the game loop completes, meaning that all sticks have been removed from the initial stack, announce the winner in a console message.

Extra credit (20 points). Once you get this version of the game working, to make it more interesting, you can create a second version of the game where you use graphic elements for drawing the board instead of showing the board in the console. Also, user interaction (input and output) will be handled through dialog windows, not in the console (see below).

Keep in mind the description above is not detailed pseudocode, and it does not describe the program in its entirety; you will need to do a lot of coding on your own.

Here are some pictures from one run of the program the console version:

(Could not include on Chegg )

What to turn in:

Upload the final version(s) on Canvas.

General requirements:

This is an individual assignment.

Write user-friendly code this includes showing clear messages to the user.

Besides the preamble of the program (the header stating the program name, your name etc.), you should also start each paragraph of code with a # comment stating what that code accomplishes. 5% of your grade will depend on your programming style and comments.

Start early, and start with a simplified version first, gradually adding features. Do NOT try to write the entire program at once! Write code to accomplish one single thing at a time (such as picking the player that starts the game, or determining the maximum number of sticks to remove during a turn etc.), then test it. When it works ok, move on to writing and testing the next thing in the program, and so on.

Before submitting your work, always test your program by running it to see if it works and if you get the correct results.

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_2

Step: 3

blur-text-image_3

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

Oracle Database 10g Insider Solutions

Authors: Arun R. Kumar, John Kanagaraj, Richard Stroupe

1st Edition

0672327910, 978-0672327919

More Books

Students also viewed these Databases questions

Question

What is job enlargement ?

Answered: 1 week ago

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago