Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions 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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education