CISC1600: Computer Science I Lab 4

Purpose: In this lab assignment of CIS1600, we will continue to get familiar with the programming environment on storm, and practice using if/else and if statement and loop.

Instruction: We will write a program that allows the user to play the game of 23 with the computer. The game of 23 is a two-player game that begins with a pile of 23 toothpicks. Plyer take turns, wihtdrawing either 1, 2 or 3 toothpicks at a time. The player to withdraw the last toothpick loses the game. Write a human vs. computer program that plays this game. The human should always move first. When it is the computer's turn, it should play according to the following rules:

When the human player enters the number of toothpicks to withdraw, the program should perform input validation. Make sure that the entered number is between 1 and 3 and that the player is not trying to withdraw more toothpicks than exist in the pile

An example run of your program is as follows, where the user inputs are underlined, and the program output are shown in regular format:

$ ./a.out
Let's play the game of 23!
Current toothpicks number: 23
--------------------
Human's turn (enter a number between 1 and 3, and no more than 23):0
Cannot withdraw less than 1. Try again:3
Human's move: 3
Toothpicks left: 20
--------------------
Computer's move: 1
Toothpicks left: 19
--------------------
Human's turn (enter a number between 1 and 3, and no more than 19):4
Cannot withdraw more than 3. Try again:2
Human's move: 2
Toothpicks left: 17
--------------------
Computer's move: 2
Toothpicks left: 15
--------------------
Human's turn (enter a number between 1 and 3, and no more than 15):3
Human's move: 3
Toothpicks left: 12
--------------------
Computer's move: 1
Toothpicks left: 11
--------------------
Human's turn (enter a number between 1 and 3, and no more than 15):1
Human's move: 1
Toothpicks left: 10
--------------------
Computer's move: 3
Toothpicks left: 7

....

--------------------
Computer's move: 1
Toothpicks left: 0
Computer loses!  
--------------------
If the human loses, the program displays:
 
Human loses! 
The program stops when either computer wins or human player loses, when it has to withdraw the last toothpick based upon the rule.

Pseudocode (Program Structure) : One possible way (not the only way) to structure your program is as follows (as discussed in class):

Initialize the number of toothpicks to 23

Repeat until game over (i.e., toothpicks is 0)
{
     //Human's turn
     Prompt the user to enter a number 
     read in the human move

     repeat until the human move is valid
     {
         display the error message depending on the error 
	    "Cannot withdraw more than 3.", "Cannot withdraw less than 1", 
	    "Cannot withdraw more than what's left"
         Prompt "Try again:"
	 read in the human move
     }

     display the human's move
     display the toothpicks left after human move
     if computer loses, display a message saying so
     display the divider line (made up of 20 dashes)

     if the game is not over
     {
          //Computer's turn
	  Calcualte the computer's move based upon the rule

	  display the computer's move
	  display the toothpick left
	  if computer loses, display a message saying so
          display the divider line (made up of 20 dashes)
     }
}
Note that there are other ways to structure your program. Regardless, please comment your code to show the ideas.

Coding Style Requirement:

  1. Please indent your code in as required.
  2. Declare a named constant for 23, so that this can be easily changed.
  3. Use descriptive names for variables
  4. Use blank lines to separate the code into appropriate blocks
  5. Be sure to include comments to help others understand your program, and help yourself think!

How to submit

The project shall be submitted onto autograder. The link will be set up and added below later in the week. Submit your gameof23.cpp file here.

The autograder will compile your code。 If your program does not compile, the submission will be rejected. The correctness of the code will be graded by TA and the instructor.