Basic referee code for Spoons (but no plumbing yet)
I'm still working on being able to test my game referee python code with full Volity plumbing - I'm running into what looks to be a network error or something - but because the mechanics of the game of spoons are so simple, a large portion of the rest of the game referee code is ready and has passed some basic testing. Now all that's needed before I can get it up and running is to be able to hook it up to the Volity RPC stuff. (Next week, I hope...)
# This python code is copyright (c) 2006 by Craig Kasper
# It is provided here for educational use and may be used freely
# for any non-commercial purpose.
# First steps in implementing Spoons for voility:
# passing cards around between hands with python, and grabbing spoons
# At this point, basic card passing and spoon grabbing functionality is present.
# However, much of the error-checking and all of the volity-related plumbing is missing
# because I still don't have all of the packages for developing for voility with python
# downloaded and ready for use in testing.
# Use a list for each hand and a list to hold all of the hands
import random
# Declare our global list of hands.
Hand_list = []
# Have a separate list to hold cards passed from hand to hand
Passed_list = ["","","",""]
# For the spoons, add players to the list as they grab the spoons
Spoons_list = []
# We need an actual player count to handle grabbing of the spoons; we'll set it to zero for now
# and set it at the start of the actual game.
Player_count = 0
def start_game(count_param):
# intialize the card stack
# Novice python programmer pitfall: access to global variables within a function definition
# usually requires use of the "global" pitfall
global Player_count
global Hand_list
Player_count = count_param # Set the global player count
card_count = count_param * 4 # We need to count off four cards for each player before shuffling
master_card_list = ["1C", "1D", "1H", "1S", "2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S", "4C", "4D", "4H", "4S", "5C", "5D", "5H", "5S"]
#Enough cards for 5 players so far, but we can up this later
card_list=master_card_list[:card_count]
# Slice off the exact number of cards we need from the start of the card array
random.shuffle(card_list)
# Python's random class makes the shuflfing easy
# start with an empty hand list
Hand_list=[]
while len(card_list) > 0: # While there are cards left to deal
Hand_list.append(card_list[:4]) # Add the first four cards to the list of hands as a list
card_list=card_list[4:] # remove the first four cards from the card list so they can't get dealt again
def pass_card(player_num, card_num):
# Pass one of the four cards (indexed from 0 to 3) in a player's hand to the next player
# Returns 0 if successful or -1 if the pass could not be completed.
# In theory, we only need full error checking in only the referee code or only the player code;
# In practice, we're going to put it into both sets of code to make it more robust
if card_num < 0 or card_num > 3: # Bounds check to ensure that the card number is valid
return -1
else:
if Passed_list[player_num - 1] <> "": # Has this player passed a card that hasn't been picked up yet?
return -1
#the player is not allowed to pass more than one card at a time
else:
if len(Hand_list[player_num - 1]) <4:
# The player is not allowed to pass a card twice in a row without picking up in between
return -1
else:
Passed_list[player_num - 1]=Hand_list[player_num - 1][card_num]
# Copy the passed card into the passed card list
del Hand_list[player_num - 1][card_num]
# Erase the passed card from the player's hand
return 0
def receive_card(player_num):
#Receive the card passed by the previous player
#Returns 0 if successful or -1 if the pick-up could not be completed
if Passed_list[player_num - 2] == "":
return -1
#the player cannot receive a card if none is available from a previous player passing it
else:
if len(Hand_list[player_num - 1]) >3:
# The player is not allowed to receive a card with a full hand of 4 cards
return -1
else:
Hand_list[player_num - 1].append(Passed_list[player_num - 2])
# add the passed card to the player's hand
Passed_list[player_num - 2]=""
# erase the passed card from the passed card list
return 0
#The above functions should successfully pass cards from one hand to another
#with an intermediate stop on the table in between, as with the real game of spoons
#To do: bounds checking on the player number
def end_of_round():
print "End of game round here"
def end_of_game():
print "End of game code here"
def grab_spoon(player_num):
# We're going to grab spoons by adding the grabbing player to a list of spoon-grabbers
# rather than by removing the spoons from a list because this approach allows us to
# keep track of who has a spoon (for if and when that player tries to return the spoon)
if player_num in Spoons_list:
#One spoon per customer
return -1
else:
if len(Spoons_list) == Player_count - 1:
return -1
else:
Spoons_list.append(player_num)
print Spoons_list, Player_count
if len(Spoons_list) == Player_count - 1:
end_of_round()
def return_spoon(player_num):
if len(Spoons_list) == Player_count - 1:
return -1
else:
if player_num in Spoons_list:
del Spoons_list[Spoons_list.index(player_num)]
return 0
else:
return -1
# This python code is copyright (c) 2006 by Craig Kasper
# It is provided here for educational use and may be used freely
# for any non-commercial purpose.
# First steps in implementing Spoons for voility:
# passing cards around between hands with python, and grabbing spoons
# At this point, basic card passing and spoon grabbing functionality is present.
# However, much of the error-checking and all of the volity-related plumbing is missing
# because I still don't have all of the packages for developing for voility with python
# downloaded and ready for use in testing.
# Use a list for each hand and a list to hold all of the hands
import random
# Declare our global list of hands.
Hand_list = []
# Have a separate list to hold cards passed from hand to hand
Passed_list = ["","","",""]
# For the spoons, add players to the list as they grab the spoons
Spoons_list = []
# We need an actual player count to handle grabbing of the spoons; we'll set it to zero for now
# and set it at the start of the actual game.
Player_count = 0
def start_game(count_param):
# intialize the card stack
# Novice python programmer pitfall: access to global variables within a function definition
# usually requires use of the "global" pitfall
global Player_count
global Hand_list
Player_count = count_param # Set the global player count
card_count = count_param * 4 # We need to count off four cards for each player before shuffling
master_card_list = ["1C", "1D", "1H", "1S", "2C", "2D", "2H", "2S", "3C", "3D", "3H", "3S", "4C", "4D", "4H", "4S", "5C", "5D", "5H", "5S"]
#Enough cards for 5 players so far, but we can up this later
card_list=master_card_list[:card_count]
# Slice off the exact number of cards we need from the start of the card array
random.shuffle(card_list)
# Python's random class makes the shuflfing easy
# start with an empty hand list
Hand_list=[]
while len(card_list) > 0: # While there are cards left to deal
Hand_list.append(card_list[:4]) # Add the first four cards to the list of hands as a list
card_list=card_list[4:] # remove the first four cards from the card list so they can't get dealt again
def pass_card(player_num, card_num):
# Pass one of the four cards (indexed from 0 to 3) in a player's hand to the next player
# Returns 0 if successful or -1 if the pass could not be completed.
# In theory, we only need full error checking in only the referee code or only the player code;
# In practice, we're going to put it into both sets of code to make it more robust
if card_num < 0 or card_num > 3: # Bounds check to ensure that the card number is valid
return -1
else:
if Passed_list[player_num - 1] <> "": # Has this player passed a card that hasn't been picked up yet?
return -1
#the player is not allowed to pass more than one card at a time
else:
if len(Hand_list[player_num - 1]) <4:
# The player is not allowed to pass a card twice in a row without picking up in between
return -1
else:
Passed_list[player_num - 1]=Hand_list[player_num - 1][card_num]
# Copy the passed card into the passed card list
del Hand_list[player_num - 1][card_num]
# Erase the passed card from the player's hand
return 0
def receive_card(player_num):
#Receive the card passed by the previous player
#Returns 0 if successful or -1 if the pick-up could not be completed
if Passed_list[player_num - 2] == "":
return -1
#the player cannot receive a card if none is available from a previous player passing it
else:
if len(Hand_list[player_num - 1]) >3:
# The player is not allowed to receive a card with a full hand of 4 cards
return -1
else:
Hand_list[player_num - 1].append(Passed_list[player_num - 2])
# add the passed card to the player's hand
Passed_list[player_num - 2]=""
# erase the passed card from the passed card list
return 0
#The above functions should successfully pass cards from one hand to another
#with an intermediate stop on the table in between, as with the real game of spoons
#To do: bounds checking on the player number
def end_of_round():
print "End of game round here"
def end_of_game():
print "End of game code here"
def grab_spoon(player_num):
# We're going to grab spoons by adding the grabbing player to a list of spoon-grabbers
# rather than by removing the spoons from a list because this approach allows us to
# keep track of who has a spoon (for if and when that player tries to return the spoon)
if player_num in Spoons_list:
#One spoon per customer
return -1
else:
if len(Spoons_list) == Player_count - 1:
return -1
else:
Spoons_list.append(player_num)
print Spoons_list, Player_count
if len(Spoons_list) == Player_count - 1:
end_of_round()
def return_spoon(player_num):
if len(Spoons_list) == Player_count - 1:
return -1
else:
if player_num in Spoons_list:
del Spoons_list[Spoons_list.index(player_num)]
return 0
else:
return -1
0 Comments:
Post a Comment
<< Home