Objective: The main purpose of this assignment is to get familiar with

COSC1306
Spring 2017
Assignment 5: Function
[1] Objective: The main purpose of this assignment is to get familiar with function definition and
invocation. It also reinforce the syntax on loops and IF statements.
[2] Description: In this assignment you will create a simple two-player game. (This version is for
two players, not for you to play against the computer as mentioned in class. Sorry.) This game
between two players as follows:
Game set up: There is a stack of sticks on the table. It has a random number of sticks
between 5 and 10. Use the random number generator to determine the number.
Game objective: To take the final stick from the stack.
Game play: Each player takes turn picking a number of sticks form that stack. A player
may take either ONE stick or TWO sticks from the stack. After a player has removed a
number of sticks from the stack, play is then passed to the other player. Play continues until
one person takes the final stick from the stack. That (last) player is the winner.
We can model the game as a simple integer number (the number of current stick on the table). The
payers can be modeled as 0 or 1. To help you with this program, I am giving you the outline of the
“main” program.
Initialization of player (player 1 goes first) and seed
stack = initStack()
print message that the game starts with x number of sticks
while stack!=0:
displayStack(stack, player)
stack, player = nextMove(stack, player)
print message that the game is over and declare the winner
You may need another function getSticks(stack,player) to prompt the user for the input.
It will be called by the nextMove function. Let’s also add a data validation to the program.
Since a player is only allowed to remove 1 or 2 sticks, add that restriction to the program. If a
player enters an incorrect number, repeat the prompt again.
Optional part: You may add a loop to ask the player whether he/she wants to play again.
[3] Input: All input will be interactive. The program will ask the current player how many sticks they
wish to remove..
[4] Output: Each stack of sticks will be draw visually by the ‘|’ character. See the sample screen shot
below. The instructor will demonstrate an implementation of this assignment in class.
Game Starts with
6
sticks.
6
| | | | | |
Player 1 's turn
How many sticks to remove (1 or 2)? 2
4
| | | |
Player 0 's turn
How many sticks to remove (1 or 2)? 1
3
| | |
Player 1 's turn
How many sticks to remove (1 or 2)? 2
1
|
Player 0 's turn
How many sticks to remove (1 or 2)? 2
You are not allowed to remove 2 stick(s).
How many sticks to remove (1 or 2)? 1
Game Over
Player
0
wins.
[5] Deadline: Monday, March 5, 2017 (May be extended)