Game of Nim

0 comments

Objective

To be able to solve problems using while and do-while loops.

 

Overview

Complete Programming Project P6.5 The Game of Nim on page 302 of the textbook. You must implement the variant described in the textbook except that the client decides if the computer should play smart, and also decides who plays first.

 

Start-up code for your Nim class is provided. It includes the entire public interface, as well declarations of the instance variables and a stub for the helper method play(). The helper is invoked by the constructor to play the constructed game to completion. You must complete these methods and provide additional suitable helper methods.

 

Specific Requirements

1.The play() method must be implemented using a while loop. It must use two other helper methods, one for the computer’s play, and another for the human’s play. It must also maintain a current transcript of the game. For example, here’s the final transcript of a completed game:

NIM Initial Pile Size 15

CYBER plays SMART

HUMAN Plays First

 

HUMAN Takes 3 Pile Size 12

CYBER Takes 5 Pile Size 7

HUMAN Takes 3 Pile Size 4

CYBER Takes 1 Pile Size 3

HUMAN Takes 1 Pile Size 2

CYBER Takes 1 Pile Size 1

HUMAN Takes 1 Pile Size 0

 

CYBER Wins

2.      The helper method for the human’s play must prompt the player for the number of marbles that the player wishes to take; use JOptionPane.showInputDialog().The input should repeated in a do-while loop until the player enters a legal move. The prompt must show sufficient information to enable the player to choose a winning move.

3.      The helper method for the computer’s play should use two additional helpers: for random play, and for smart play.

4.      Modify the client to prompt the player whether the computer should play smart, and if the player wishes to take the first turn; use JOptionPane.showConfirmDialog() calls.

5.      Modify the client to allow several games to be played. Use a do-while loop that gives the player the option to play another game or to quit.

 

 

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}