Homework: Ruby Nim

Purpose

Preparation

Lesson

In this lesson you will implement the game of Nim. Some high-level requirements:

The list of potential “computer players” must be generated using reflection. How will this work? One option is to represent the “computer player” as simply a method that knows how to take a turn. To identify these methods via reflection, I had each method include computer_player in its name. There are other options. For example, you might have a Nim player class, then select all classes that are subclasses. Or select all classes that “respond to” a particular method.

A sample execution of the game is shown below.

Welcome to Nim!
1: [1, 3, 5, 7]
2: [4, 3, 7]
Select board configuration (1-2): 2

1: Smart Computer Player
2: Dumb Computer Player 
Select computer player (1-2): 1

Row 1: XXXX
Row 2: XXX
Row 3: XXXXXXX
Select the row (1-3): 3
Select the number of sticks to take (1-7): 7

Smart computer took 1 stick(s) from row 1.

Row 1: XXX
Row 2: XXX
Row 3: 
Select the row (1-3): 3

No sticks left in that row.

Select the row (1-3): 2
Select the number of sticks (1-3): 4

The number of sticks must be between 1 and 3.

Select the number of sticks (1-3): 3

Smart computer took 3 stick(s) from row 1.

Smart Computer Player wins the game!
Thank you for playing!

For the “face off” between the two computer players, you should create a separate Ruby class named nim_play_tester.rb. This program should simply contain a loop that plays the game 20 times. Instead of prompting for the board configuration and computer players, you will just set the board configuration (choose either board), set the dumb computer player as the first player, and set the smart computer player as the second player. Then begin the game. You should not display the board status, but you should display the sticks being taken, as shown in the example output below (partial execution). Be sure to include a message indicating who won.

Dumb computer took 7 stick(s) from row 4.

Smart computer took 3 stick(s) from row 3.

Dumb computer took 1 stick(s) from row 1.

Smart computer took 1 stick(s) from row 2.

Dumb computer took 1 stick(s) from row 3.

Smart computer took 1 stick(s) from row 2.

Dumb computer took 1 stick(s) from row 3.

Smart computer took 1 stick(s) from row 2.

Smart Computer Player wins the game!
Thank you for playing!

Dumb computer took 1 stick(s) from row 1.

Smart computer took 1 stick(s) from row 2.

Dumb computer took 7 stick(s) from row 4.

Smart computer took 3 stick(s) from row 3.

Dumb computer took 2 stick(s) from row 2.

Smart computer took 2 stick(s) from row 3.

Smart Computer Player wins the game!
Thank you for playing!

The grader will redirect the output of your game into a file and use grep to easily show the 20 game outcomes, as shown in the following example output.

$ ruby nim_play_tester.rb > results

$ grep 'wins' results
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!
Smart Computer Player wins the game!

Rubric

This assignment is worth 75 points.

Normal Game Play

Testing

Hint: There are a number of sites that discuss the strategy for Nim game, but the one I followed most closely is:

Submission

For ease of grading, please:

Zip your files and submit on Canvas. If you worked with a partner, submit only one copy, and be sure to include both names in the comments.