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 pseudocod

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education