Weekly Challenge #12 – Basic Nim

Nim is a mathematical game of strategy in which objects (Such as matchsticks) are organised into a series of rows. Two players take it in turns to remove one or more objects from a single row. The player who removes the final object is the loser of the game.

In this challenge, you will be creating a basic, playable version of Nim. 

Bronze

Write a subroutine which outputs the state of the Nim board at the start of the game. By default, the board should have four rows, each containing 1, 3, 5 and 7 objects.

|
| | |
| | | | |
| | | | | | |
      |
    | | |
  | | | | | 
| | | | | | |

Silver

Add functionality to your game so that users are able to take turns removing objects from rows. The game should end once the final object has been removed, and the program should output which player has won.

|
| | |
| | | | |
| | | | | | |
Player One: Enter a row: 2
Player One: How many objects should be removed? 2

|
|
| | | | |
| | | | | | |
Player Two: Enter a row:

Gold

Extend your program so that it now allows the user the option to play against the computer. The computer should randomly decide which row to remove objects from, and should remove a random number of objects from the selected row. 

Basic Nim Game
1) Play a Friend
2) Play vs Computer
Please enter your choice: 2

|
| | |
| | | | |
| | | | | | |
Player One: Enter a row: 2
Player One: How many objects should be removed? 2

|
|
| | | | |
| | | | | | |
Computer removes 4 objects from row 3

|
| | |
|
| | | | | | |
Player One: Enter a row:

Diamond

Modify your program further so that rather than playing every game with a fixed layout of 4 rows (Containing 1, 3, 5, 7 objects), the program randomly generates the number of rows and the number of objects contained within each row. 

Ensure your program contains sufficient validation throughout to prevent users entering invalid options (Such as removing too many objects, or selecting a row which doesn’t exist)