[Tutor] Question : Creating cribbage game
Tim Goddard
timgoddardsemail at gmail.com
Mon Dec 7 17:08:44 CET 2009
> Message: 2
> Date: Mon, 7 Dec 2009 02:30:30 -0400
> From: Christopher schueler <chris_schueler at hotmail.com>
> To: <tutor at python.org>
> Subject: [Tutor] Question : Creating cribbage game
> Message-ID: <COL115-W23640CB7712629D3A205FEE0900 at phx.gbl>
> Content-Type: text/plain; charset="iso-8859-1"
>
>
> My name is Chris Schueler and i am having some troubles with my Python programming
>
>
>
> Our current project is to create the game of cribbage from scratch.
>
> The only problem is we are not allowed to use classes, only user-defind functions and arrays. I was wondering if anybody could give me tips or pointers on adding codes or modifying some of my program
>
>
>
> Here is my Program so far
>
> I will also include a .py file of it incase this doesnt look legible
>
>
>
> from random import*
>
>
>
>
> def DisplayTitle():
> print
> print "Welcome to Tech-Sauve Cribbage"
> print "--------------------------------------------"
> print " Insctructions "
> print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
> print "1) Only played with two players (for now) "
> print "2) The program starts with a full deck of 52 cards"
> print "3) Deals out 6 cards to each player with a Suit letter"
> print "4) Then asks each player what 2 cards they want to discard to the crib"
> print "5) Then the program saves the crib in a temporary deck"
> print "6) Players start showing cards to get an ammount equal to 31"
> print "7) Once all the cards have been played, program counts the score"
> print "8) Then the program will count all possible scores in each hand"
> print " And it will add the players points to their total score"
> print "9) First player to reach a score of 121 wins the game"
> #Gets players names
> def GetPlayer1():
> print
> Player1 = str(raw_input("Player 1's name "))
> return Player1
> def GetPlayer2():
> print
> Player2 = str(raw_input("Player 2's name "))
> return Player2
> #Building the deck
> def Build_Deck():
> for R in range (0,52):
> cardnumb = numbers[R]
> cardsuit = suits[R]
> card = str(numbers[R])+str(suits[R])
> Deck.append(card)
> return Deck,numbers,suits,card,cardnumb,cardsuit
>
>
> #Variables Needed
> numbers = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"]*4
> suits = ["H","C","S","D"]*13
> suits.sort()
> Deck = []
> P1hand = []
> P2hand = []
> Crib = []
> Cribcard = []
> Cribsuit = []
> P1_score = 0
> P2_score = 0
> Winner = 121
> ele = 52
> Deck,numbers,suits,card,cardnumb,cardsuit = Build_Deck()
> for X in range(0,6):
> Y = randint(0,ele)
> draw = Deck[Y]
> P1hand.append(draw)
> Deck.pop(Y)
> ele -= 1
> for X2 in range (0,6):
> Y1 = randint(0,ele)
> draw2 = Deck[Y1]
> P2hand.append(draw2)
> Deck.pop(Y1)
> ele -= 1
> print
> Top = randint(0,47)
> Topcard = Deck[Top]
> print
> for count in range(0,2):
> print P1hand
> print
> option = str(raw_input("Player 1,what CARD would you like to add to the crib? CARDS 1 thru 6 "))
> if option == "1":
> Crib.append(P1hand[0])
> P1hand.pop(0)
> elif option == "2":
> Crib.append(P1hand[1])
> P1hand.pop(1)
> elif option == "3":
> Crib.append(P1hand[2])
> P1hand.pop(2)
> elif option == "4":
> Crib.append(P1hand[3])
> P1hand.pop(3)
> elif option == "5":
> Crib.append(P1hand[4])
> P1hand.pop(4)
> elif option == "6":
> Crib.append(P1hand[5])
> P1hand.pop(5)
> print
> for c2 in range(0,2):
> print P2hand
> print
> option1 = str(raw_input("Player 2, what CARD would you like to add to the crib? CARDS 1 thru 6 "))
> if option1 == "1":
> Crib.append(P2hand[0])
> P2hand.pop(0)
> elif option1 == "2":
> Crib.append(P2hand[1])
> P2hand.pop(1)
> elif option1 == "3":
> Crib.append(P2hand[2])
> P2hand.pop(2)
> elif option1 == "4":
> Crib.append(P2hand[3])
> P2hand.pop(3)
> elif option1 == "5":
> Crib.append(P2hand[4])
> P2hand.pop(4)
> elif option1 == "6":
> Crib.append(P2hand[5])
> P2hand.pop(5)
>
> print Deck
> print "The TOP CARD is ",Topcard
> print "Player 1's Hand is ",P1hand
> print "Player 2's Hand is ",P2hand
> print "The 4 cards in the Crib are ",Crib
>
Unfortunately I had to read a few wiki pages of cribbage first, so my
understanding of the game is weak.
My suggestions:
Start with an outline of play (more to help us understand cribbage)
>From my quick lesson, it sounds like you have so far:
Get player names (two players)
Create deck
Ask player which cards to put in crib
So for what you have now here are some suggestions:
You are creating variables "numbers" and "suits" in your global
namespace. Then you use them in your Build_Deck function which is
fine, but then you are returning them at the end of the function,
overwriting the original variable definition. I don't think it would
mess up your code but it is messy. I also don't see where you are
using card, cardnumb, or cardsuit elsewhere.
I see your technique for chooosing cards at random, however the random
module includes a shuffle function so you could create a shuffled deck
with:
import from random *
#shuffle deck
Deck = shuffle(Deck)
# your randomly determined top card would then be:
top_card = Deck[0]
# removing the top card using your method is easy
Deck.pop(0)
Lastly, your lines:
> Top = randint(0,47)
> Topcard = Deck[Top]
comes after you've reduced the Deck by 12 cards so I think your
randint(0,47) is incorrect? should it be 52-12 = 40?
Good luck with the rest of it. Think about writing functions for
repetitive procedures (i.e. drawing cards from a deck) which is
loosely the DRY principle (Don't repeat yourself)
More information about the Tutor
mailing list