Threesuns

Email the Instructor

Contact

All Semesters

Semesters

All Courses

Courses

2017Summer

06.05.17 - 07.30.17

C Language

CSCI2473

Tuesday Thursday
09:30 AM - 12:20 PM

NIM

Lab04

Objective: Practice using control structures

pastdue

Submit Your Solution

Lab04

Request a Copy of your Solution

Lab04

Requirements

Program compiles and executes without errors10 pts
Program allows required user inputs15 pts
Program performs required input error checking25 pts
Game executes play correctly and without error25 pts
Computer player uses optimal strategy correctly25 pts
Total100 pts

Instructions

The assignment is to write a program to play the well-known game of Nim. The game starts with some number of stones in a pile. The players take turns removing either one or two stones at each move. The player who takes the last stone loses.

The game should be played between the program and the user. The game should be setup to be friendly to the user, letting them know on each move how many stones are left in the pile and how many stones the computer removes on each of its turns.

Optimal Strategy

It is easy to program an optimal strategy for Nim: Take the remainder of dividing the number of stones left by three; this will be 0, 1, or 2. If it is 0 then take 2 stones; if it is 2, take one stone. If the remainder is 1, then the game cannot be won assuming the opponent is also playing the optimal strategy; however, the program should still put up a good show of it and take one stone (delaying the inevitable and giving the opponent more chances to make a mistake).

Game Play

Your program should start out by asking the user for the number of stones to start with, and then asking who moves first (0 = program, 1 = user). It then runs in a loop, alternating turns between the program and the user. The program chooses one or two stones according to the above strategy, and prompts the user for how many stones they wish to take. When the number of stones reaches zero, the program display a message announcing the winner.

Note:

An important part of this assignment is input error checking. Your program should check for the following conditions:

  • Initial number of stones must be greater than zero.
  • "Who moves first" must be zero or one.
  • Number of stones removed by the user at each move must be one or two, and not more than the number of stones remaining in the pile.

If an error is detected, the program should output an appropriate error message and repeat the input request. For this assignment, it is not necessary to check for non-integer input.

Extras