How to resolve the algorithm Nim game step by step in the Plain English programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Nim game step by step in the Plain English programming language

Table of Contents

Problem Statement

Nim is a simple game where the second player─if they know the trick─will always win.

The game has only 3 rules:

To win every time,   the second player simply takes 4 minus the number the first player took.   So if the first player takes 1,   the second takes 3; if the first player takes 2,   the second should take 2; and if the first player takes 3,   the second player will take 1. Design a simple Nim game where the human player goes first, and the computer always wins. The game should enforce the rules.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Nim game step by step in the Plain English programming language

Source code in the plain programming language

To run:
Start up.
Play the game of Nim.
Write "The computer wins! Press esc to exit." on the console.
Wait for the escape key.
Shut down.

A piece is a number.

To play the game of Nim:
Put 12 into a piece count.
Loop.
If the piece count is 0, exit.
Write "There are " then the piece count then " pieces remaining." on the console.
Ask the player to take some pieces.
Subtract the pieces from the piece count.
Ask the computer to take some other pieces given the pieces.
Subtract the other pieces from the piece count.
Repeat.

To ask the player to take some pieces:
Write "Would you like to take 1, 2, or 3 pieces? " on the console without advancing.
Read a count from the console.
If the count is not between 1 and 3, repeat.
Format the count and "piece" or "pieces" into a string.
Write "You took " then the string then "." on the console.
Put the count into the pieces.

To ask the computer to take some pieces given some other pieces:
Put 4 minus the other pieces into a count.
Format the count and "piece" or "pieces" into a string.
Write "The computer took " then the string then "." on the console.
Put the count into the pieces.

  

You may also check:How to resolve the algorithm Arithmetic numbers step by step in the BASIC programming language
You may also check:How to resolve the algorithm Bitmap/Read an image through a pipe step by step in the Perl programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the Maple programming language
You may also check:How to resolve the algorithm Idiomatically determine all the lowercase and uppercase letters step by step in the Arturo programming language
You may also check:How to resolve the algorithm Rot-13 step by step in the NewLISP programming language