Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In python The Nim game. Write a program for a two-player version of the game Nim: a human player will play against the computer. The

In python

The Nim game. 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.

Here’s 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 who’s 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 computer’s 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. Here’s 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 it’s the human player’s 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 input a correct number, in the allowed range. If they didn’t input correctly, then keep asking for their input until it is correct.
  • If it’s the computer’s turn, then have your program pick a random number of sticks to play if the number of sticks is greater than 4 on the board. If it is less than 4 have the computer pick them up and win the game. This is a very rudimentary strategy and the human should always win. Show a message in the console to inform the human player of how the computer plays its turn.
  • 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 who’s 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 whose turn it is the next time through the game loop.



Step by Step Solution

3.42 Rating (146 Votes )

There are 3 Steps involved in it

Step: 1

Answer import random initialize variables sticksleft 13 currentplayer 1 1 for human 2 for computer m... 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

Discrete Mathematics and Its Applications

Authors: Kenneth H. Rosen

7th edition

0073383090, 978-0073383095

More Books

Students also viewed these Programming questions

Question

Would another approach to the decision have worked better?

Answered: 1 week ago