Below is the code for a Python game I have almost finished, entitled “Heap Of Beans”; in which a user competes against the computer to draw the final “bean” in the pile of sixteen. This program is still under development, and will be complete shortly.

# Problem 1

#

# Program Explanation: This program is the "Easy" function for the "Heap of Beans" program, and makes the computer player predictable to the user by

# only allowing the computer to deduct one bean each turn.

#

# Test Case 1:

# Please enter the difficulty you would like to utilize. Easy

# There are 16 beans in the pile.

# The amount of beans subtracted by the computer player is: 1.

# There are 15 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 1.

# There are 11 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 1.

# There are 8 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 1.

# There are 4 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 1.

# There are 1 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 1

# You won!!

#

#

# Test Case 2:

# Please enter the difficulty you would like to utilize. Easy

# There are 16 beans in the pile.

# The amount of beans subtracted by the computer player is: 1.

# There are 15 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 1.

# There are 12 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 1.

# There are 8 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 1.

# There are 4 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 1

# The amount of beans subtracted by the computer player is: 1.

# There are 2 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 1

# You lost!!

# The amount of beans subtracted by the computer player is: 1.

import random

def Easy(beans):

print "There are 16 beans in the pile. "

computerSubtract = 1

while beans >= 0:

beans -= computerSubtract

if beans >0:

print "The amount of beans subtracted by the computer player is: "+str(computerSubtract)+"."

if beans >0:

print "There are "+str(beans)+" beans remaining."

if beans >0:

playerSubtract = input("Please enter the number of beans you would like to remove from the heap. ")

beans-=playerSubtract

if beans == 0:

print "You won!!"

if beans ==1:

print "You lost!!"

print "The amount of beans subtracted by the computer player is: "+str(computerSubtract)+"."

return beans

# Problem 2

#

# Program Explanation: This function expands upon Problem 1, allowing the computer player to choose any random (integer) number of beans, between 1 and 3.

#

#

# Test Case 1:

# Please enter the difficulty you would like to utilize. Medium

# There are 16 beans in the pile.

# The amount of beans subtracted by the computer player is: 2.

# There are 14 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 3.

# There are 8 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 1.

# There are 5 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 1.

# There are 1 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 1

# You won!!

#

#

# Test Case 2:

# Please enter the difficulty you would like to utilize. Medium

# There are 16 beans in the pile.

# The amount of beans subtracted by the computer player is: 1.

# There are 15 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 2.

# There are 11 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 3.

# There are 5 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 3.

# You lost!

def Medium(beans):

print "There are 16 beans in the pile. "

while beans > 0:

computerSubtract = random.randint(1,3)

while computerSubtract > beans:

computerSubtract = random.randint(1,3)

beans-=computerSubtract

if beans >0:

print "The amount of beans subtracted by the computer player is: "+str(computerSubtract)+"."

if beans ==0:

print "The amount of beans subtracted by the computer player is: "+str(computerSubtract)+"."

print "You lost!"

if beans > 0:

print "There are "+str(beans)+" beans remaining."

playerSubtract = input("Please enter the number of beans you would like to remove from the heap. ")

beans -= playerSubtract

if beans == 0:

print "You won!!"

if beans <0:

print "You lost!"

return beans

# Problem 3

#

# Program Explanation: This function further expands upon the "Medium" difficulty, through the use of cascading "if-statements" to attempt to "guess" the user's input on the next turn,

# and a means of winning the game for the Computer player.

#

# Test Case 1:

# Please enter the difficulty you would like to utilize. Hard

# There are 16 beans in the pile.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 3.

# There are 10 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 3.

# There are 5 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 2.

# You lost!

#

#

# Test Case 2:

# Please enter the difficulty you would like to utilize. Hard

# There are 16 beans in the pile.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 3.

# There are 10 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 2

# The amount of beans subtracted by the computer player is: 3.

# There are 5 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 1

# The amount of beans subtracted by the computer player is: 3.

# There are 1 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 1

# You won!!

#

#

# Test Case 3:

# Please enter the difficulty you would like to utilize. Hard

# There are 16 beans in the pile.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 3.

# There are 10 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 2.

# There are 5 beans remaining.

# Please enter the number of beans you would like to remove from the heap. 3

# The amount of beans subtracted by the computer player is: 2.

# You lost!

def Hard(beans):

print "There are 16 beans in the pile. "

while beans > 0:

playerSubtract = input("Please enter the number of beans you would like to remove from the heap. ")

beans -= playerSubtract

if beans == 0:

print "You won!!"

if beans <0:

print "You lost!"

if beans==3:

computerSubtract=3

if beans == 2:

computerSubtract = 2

if beans == 1:

computerSubtract = 1

if beans >=8 and beans <=14:

computerSubtract = 3

elif beans >=7 and beans >=2:

computerSubtract = 2

beans-=computerSubtract

if beans >0:

print "The amount of beans subtracted by the computer player is: "+str(computerSubtract)+"."

if beans > 0:

print "There are "+str(beans)+" beans remaining."

if beans ==0:

print "The amount of beans subtracted by the computer player is: "+str(computerSubtract)+"."

print "You lost!"

return beans

if __name__ == "__main__":

beans = 16

difficultySelect = raw_input("Please enter the difficulty you would like to utilize. ")

if str(difficultySelect)=="Easy":

beans = Easy(beans)

elif str(difficultySelect)=="Medium":

beans = Medium(beans)

elif str(difficultySelect)=="Hard":

beans = Hard(beans)

Advertisements