![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
Neatening some Python code
OK, I wrote a simple script that will deal you some cards.
Basically it takes an argument of how many cards you want to be drawn, and then it shows you those cards. It doesn't draw the same card twice. The cards can be put back into the pack by calling: shuffle(). It works fine, but what i am interested in is shortening the code and making it more efficient. Here it is: #! /usr/bin/python
#Card Dealer
import random
class cards:
def __init__(self):
self.suits = ["Hearts", "Diamonds", "Spades", "Clubs"]
self.numbers = ["Ace", 2, 3, 4, 5 , 6, 7, 8, 9, 10, "Jack", "Queen", "King"]
self.pack = ["52 Card Pack"]
for x in self.suits:
for y in self.numbers:
z = "%s of %s" % (y, x)
self.pack.append(z)
self.shuffle()
def shuffle(self):
self.choices = [1]
for n in range(2,53): self.choices.append(n)
def draw_a_card(self):
a = random.choice(self.choices)
card_drawn = self.pack[a]
self.choices.remove(a)
return card_drawn
def deal(self, number_of_cards):
b = self.draw_a_card()
self.playercards = [b]
for i in range(1, number_of_cards):
b = self.draw_a_card()
self.playercards.append(b)
print "Your hand: %s" % (self.playercards)
newgame = cards()
deal = "y"
while deal == "y":
newgame.deal(5)
deal = raw_input("Deal again? (y/n)")Its about 35 lines without comments. I can't think of any way that i could shorten this code, how would you go about shortening it?
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
I'm rushing to catch the bus right now, but if you wait 2 hours till win my Tech starts, I'll have your answer.
See ya! |
|
|
|
|
|
#3 |
|
Expert Programmer
|
ty Sane
i'll be on AIM, MSN and IRC. But you coudl post in this topic incase anyone else needs to find this sort of thing in the future. ty.EDIT: haha you look wackey in your avatar ![]()
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
The first thing I'd do to shorten it would be to use list comprehensions:
import random
# (By convention, classes should ideally have their first letter capitalised)
class Cards:
def __init__(self):
suits = ["Hearts", "Diamonds", "Spades", "Clubs"]
numbers = ["Ace"] + range(2, 11) + ["Jack", "Queen", "King"]
self.pack = ["%s of %s" % (n, s) for n in numbers for s in suits]
self.shuffle() def shuffle(self):
random.shuffle(self.pack) def draw_a_card(self):
card = random.choice(self.pack)
self.pack.remove(card)
return card def draw_a_card(self):
return self.pack.pop() def deal(self, number_of_cards):
print "Your hand: %s" % [self.draw_a_card() for i in range(number_of_cards)] def deal(self, number_of_cards):
print "Your hand: %s" % [self.pack.pop() for i in range(number_of_cards)]So, assuming we're not too fussed about shuffling more than once, and assuming we're not worried about exceptions occuring when running out of cards (as in the original code), we can cut the program down to 20 lines: #! /usr/bin/python
# Card Dealer
import random
class Cards:
def __init__(self):
suits = ["Hearts", "Diamonds", "Spades", "Clubs"]
numbers = ["Ace"] + range(2, 11) + ["Jack", "Queen", "King"]
self.pack = ["%s of %s" % (n, s) for n in numbers for s in suits]
random.shuffle(self.pack)
def deal(self, number_of_cards):
print "Your hand: %s" % [self.pack.pop() for i in range(number_of_cards)]
newgame = Cards()
deal = "y"
while deal == "y":
newgame.deal(5)
deal = raw_input("Deal again? (y/n)") |
|
|
|
|
|
#5 |
|
Expert Programmer
|
Wow. Arevos thats great!
Thats the exact response i was looking for thanks. One main thing that got me, was the fact that i didn't think about actually shuffling the pack, but shuffling the indexes. Its looking pretty good now, tyvm! EDIT: i've set it to reshuffle when thw number of cards to deal is less than the number of cards left in the pack.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
Last edited by coldDeath; Oct 31st, 2005 at 7:18 AM. Reason: typos |
|
|
|
|
|
#6 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
Also, you might want to think about representing the cards as objects, rather than strings: class Card:
def __init__(self, suite, number):
self.suite = suite
self.number = number
def __str__(self):
return "%s of %s" % (self.number, self.suite)remaining_hearts = [card for card in self.pack if card.suite = "Hearts"] |
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
I see, i might mess around with that, ty.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
This may sound a bt newbie-ish, but here goes:
If i was to make the cards into objects, how would i make a list containing each card? Because i'm not sure how to make an instance of an object from the code without typing out each card, which would mean 52 lines of code just to set up the pack.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Assuming you want a different object for each card, rather than a different class (which might be overkill):
self.pack = [Card(s, n) for n in numbers for s in suites] |
|
|
|
|
|
#10 |
|
Expert Programmer
|
I don't really understand how you could call the __str__ function for each car in the pack.
Say you got dealt a hand of five cards, and they were in a list, how would you print out what each card is?
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|