MONT 105S, Spring 2009
Home | | Course Schedule | | Assignments | | Lecture NotesLaboratory 7
Problem 1: The Game of Eight
You must write two functions to make the program work correctly.
Function 1: isLegal(move, lastMove)
Function 2: getWinner(player, sum)
The rest of the program.
The following are examples of the output of a program for playing the game of eight:
Sample program 1 Sample program 2
What To Turn In. Reminder. Be sure to save a copy of
each program on your
Due at the end of today's class
The game of eight is a game in which two players take turns choosing a number
between 1 and 3. The number chosen cannot be the same as the previous number
chosen. For example, if player 1 chooses 1, player 2 can only choose 2 or 3
on the next move. The score is the sum of all the numbers chosen so far.
As long as the score is less than 8, the game continues. If
a player chooses a number that makes the score equal to 8, that player wins.
If a player chooses a number that makes the score go over 8, that player loses.
See the end of this lab for sample programs.
The isLegal( ) function determines whether a given move is legal.
It takes two parameters, move and lastMove, that give the values of the current
move and the previous move. It checks whether the current move is legal as follows:
The getWinner( ) function determines who the winner is. If there is no winner
yet, it returns zero. If player 1 is the winner, it returns 1 and if player 2 is the
winner, it returns 2. It takes two parameters, player and sum, that give the
current player and the current sum of all the moves played so far.
It determines the winner as follows:
You should use the following program skeleton for your program.
#Program: gameOfEight.py
#Your program prologue goes here
def isLegal(move, lastMove):
#your code for the isLegal( ) function goes here
def getWinner(player, sum):
#your code for the getWinner( ) function goes here
def getMove(lastMove):
move = input("Please make a move (enter a number between 1 and 3): ")
while isLegal(move, lastMove) == False:
print "That is not a legal move."
move = input("Please enter a number between 1 and 3: ")
return move
#The main program starts here.
print "Welcome to the Game of Eight!"
# Initialize variables
lastMove = 0
sum = 0
player = 1
#Player 1 gets the first move
move = getMove(lastMove)
sum = sum + move
print "The score is", sum
print
winner = getWinner(player, sum)
while winner == 0: #As long as there is no winner yet
if player == 1: #Change the player
print "Player 2's turn."
player = 2
else:
print "Player 1's turn."
player = 1
lastMove = move #save the latest move
move = getMove(lastMove) #Get a new move from the current player
sum = sum + move #Keep track of the sum of moves.
print "The score is", sum
print
winner = getWinner(player, sum) #Check for a winner
print "Game Over! The winner is player", winner
Welcome to the Game of Eight!
Please make a move (enter a number between 1 and 3): 2
The score is 2
Player 2's turn.
Please make a move (enter a number between 1 and 3): 3
The score is 5
Player 1's turn.
Please make a move (enter a number between 1 and 3): 4
That is not a legal move.
Please enter a number between 1 and 3: 2
The score is 7
Player 2's turn.
Please make a move (enter a number between 1 and 3): 1
The score is 8
Game Over! The winner is player 2
Welcome to the Game of Eight!
Please make a move (enter a number between 1 and 3): 1
The score is 1
Player 2's turn.
Please make a move (enter a number between 1 and 3): 2
The score is 3
Player 1's turn.
Please make a move (enter a number between 1 and 3): 1
The score is 4
Player 2's turn.
Please make a move (enter a number between 1 and 3): 2
The score is 6
Player 1's turn.
Please make a move (enter a number between 1 and 3): 2
Your move may not be the same as the last move.
That is not a legal move.
Please enter a number between 1 and 3: 1
The score is 7
Player 2's turn.
Please make a move (enter a number between 1 and 3): 2
The score is 9
Game Over! The winner is player 1