944,045 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 5948
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 24th, 2006
0

Class - Shuffle()

Expand Post »
Hi,

I have a card class and deck class. The cards are in
suitList
rankList.

I call the cards from a file cards.txt and split them into the above.

I need to import the cards.txt and then shuffle them. I have the following code:

def shuffle(self):
import random
nCards = len(self.cards)
# Swap randomly selected card i with randomly selected card j
for i in range(nCards):
j = random.randrange(i, nCards)
[self.cards[i], self.cards[j]] = [self.cards[j], self.cards[i]]

How do I get the cards shuffled and then use the shuffled deck to display the cards using Deck().

macca1111
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 24th, 2006
0

Re: Class - Shuffle()

Check the other threads about the card class and such. Once you have a deck in a list, you can use a shuffle() function and it will randomly switch the items around.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 24th, 2006
0

Re: Class - Shuffle()

Click to Expand / Collapse  Quote originally posted by LaMouche ...
Check the other threads about the card class and such. Once you have a deck in a list, you can use a shuffle() function and it will randomly switch the items around.
Hi,

I have my cards in suitList and rankList.


When I use:

deck=Deck()
deck.shuffle()
print deck

It shuffles the ranks but returns all Hearts. How do I make the shuffle() function shuffle both rankList and suitList???

macca1111
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 24th, 2006
0

Re: Class - Shuffle()

Could you copy your entire code with code tags, please? Sorry my first post didn't really say anything at all.
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006
Oct 24th, 2006
0

Re: Class - Shuffle()

Click to Expand / Collapse  Quote originally posted by LaMouche ...
Could you copy your entire code with code tags, please? Sorry my first post didn't really say anything at all.
Python Syntax (Toggle Plain Text)
  1. import string
  2. list1 = open('H:\cards.txt').read()
  3. class Card:
  4. suitList = list1[1::2]
  5. rankList = list1[0::2]
  6.  
  7. def __init__(self,suit,rank):
  8. self.suit = suit
  9. self.rank = rank
  10.  
  11. def __repr__(self):
  12. return str(self)
  13.  
  14. def __str__(self):
  15. return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])
  16.  
  17. def __cmp__(self,other):
  18. if self.suit > other.suit: return 1
  19. if self.suit < other.suit: return -1
  20. if self.rank > other.rank: return 1
  21. if self.rank < other.rank: return -1
  22. return 0
  23. class Deck:
  24. def __init__(self):
  25. self.cards = []
  26. for suit in range(4):
  27. for rank in range(1,14):
  28. self.cards.append(Card(suit,rank))
  29. def printDeck(self):
  30. for card in self.cards:
  31. print card
  32.  
  33. def __str__(self):
  34. s = ""
  35. for i in range(len(self.cards)):
  36. s = s + " " + str(self.cards[i]) + "\n"
  37. return s #= s + " " + str(self.cards[i]) + '\n'
  38. def shuffle(self):
  39. import random
  40. nCards = len(self.cards)
  41. # Swap randomly selected card i with randomly selected card j
  42. for i in range(nCards):
  43. j = random.randrange(i, nCards)
  44. [self.cards[i], self.cards[j]] = [self.cards[j], self.cards[i]]
  45. def removeCard(self, card):
  46. if card in self.cards:
  47. self.cards.remove(card)
  48. return 1
  49. else: return 0
  50. # return the top ca
  51. def popCard(self):
  52. return self.cards.pop()
  53.  
  54. def main():
  55. list2 = list1.split()
  56. Card.suitList = list2[1::2]
  57. Card.rankList = list2[0::2]
  58. deck = Deck()
  59. deck.shuffle()
  60.  
  61. # replace suit with full word
  62. for index, suit in enumerate(Card.suitList):
  63. if suit == 'h':
  64. Card.suitList[index] = "Hearts"
  65. elif suit == 'c':
  66. Card.suitList[index] = "Clubs"
  67. elif suit == 's':
  68. Card.suitList[index] = "Spades"
  69. elif suit == 'd':
  70. Card.suitList[index] = "Diamonds"
  71. list3 = open('H:\cardsS.txt','w')
  72. list3.writeline(list2,)
  73. list3.close()
  74. print deck
  75.  
  76.  
  77. main()

Ultimately what i'm trying to achieve is:

1.
Create a random file from a file with the cards in order. So read an ordered file (I can do). Then randonize it (only the rank are changing - not the suit).

(This is how the file is to be ordered)
2 h
3 d
Ace d
...

2.
Then deal a card from the top of the deck and remove it from the deck. I think I have the class in order, yet taking the card is not working.

3.
Then return the number of cards left.

4. Finally test it by dealing n number of cards and display them.

I am almost there, yet most of the problems I think lay with the syntax which is confusing me.

The last time I programmed anything was a C64 20 years ago.

Kind Regards
macca1111
Last edited by vegaseat; Oct 24th, 2006 at 3:43 am. Reason: Added requested code tags!!!!!!!!!!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 24th, 2006
0

Re: Class - Shuffle()

Sorry,

I'm very new to this. I even had to do a search to find out what code tags are...
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 24th, 2006
0

Re: Class - Shuffle()

Why do you want to keep two lists, a card has both a rank and a suit, they belong together to make the card. If you just shuffle the rank list, then you lost the connection with the suit list. As I mentioned before, keep your cards in a list of (rank, suit) tuples. That list will by default sort by rank (item 0 of the tuple) and keep the suit along. The same goes for the random.shuffle().
Last edited by Ene Uran; Oct 24th, 2006 at 8:02 pm.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Oct 30th, 2006
0

Re: Class - Shuffle()

Click to Expand / Collapse  Quote originally posted by Ene Uran ...
Why do you want to keep two lists, a card has both a rank and a suit, they belong together to make the card. If you just shuffle the rank list, then you lost the connection with the suit list. As I mentioned before, keep your cards in a list of (rank, suit) tuples. That list will by default sort by rank (item 0 of the tuple) and keep the suit along. The same goes for the random.shuffle().
The following code is suppose to print a pack of cards in order, then shuffle them and reprint them again. Using Deck to print them it's not quite how I would like it.

This is how its printing...

[['1', 'h'], ['2', 'h'], ['3', 'h']...

I would like it to print:

Ace of Hearts
2 of Hearts
...

my file is saved like...

1 of h
2 of h
...

[php]
import string

class Card:


def __init__(self,suit,rank):
self.suit = suit
self.rank = rank
suitList = []
rankList = []

def __repr__(self):
return str(self)

def __str__(self):
return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])

class Deck():
def __init__(self):
self.cards = []
for suit in range(4):
for rank in range(1,14):
self.cards.append(Card(suit,rank))

def printDeck(self):
for card in self.cards:
print card

def __str__(self):
s = ""
for i in range(len(self.cards)):
s = s + " " + str(self.cards[i]) + '\n'
return s

def shuffle(self):
import random
nCards = len(self.cards)
for i in range(nCards):
j = random.randrange(i, nCards)
[self.cards[i], self.cards[j]] = [self.cards[j], self.cards[i]]

def main():
a = open('cards.txt','r')
line = a.readlines()
a.close()

deck = Deck()

cards =[]
for i in line:
cards.append(i.split())

print deck

#replace suit with full word
for index, suit in enumerate(Card.suitList):
if suit == 'h':
Card.suitList[index] = "Hearts"
elif suit == 'c':
Card.suitList[index] = "Clubs"
elif suit == 's':
Card.suitList[index] = "Spades"
elif suit == 'd':
Card.suitList[index] = "Diamonds"
print[/php]
Reputation Points: 10
Solved Threads: 0
Light Poster
macca1111 is offline Offline
36 posts
since Oct 2006
Oct 30th, 2006
0

Re: Class - Shuffle()

I still don't understand your file structure or the reason for a file. Will this help ...
python Syntax (Toggle Plain Text)
  1. import random
  2.  
  3. def show_hand(hand):
  4. """give drawn cards more detailed descriptions and then display"""
  5. for item in hand:
  6. rank = item[0]
  7. suit = item[1]
  8.  
  9. if rank == 1:
  10. rank = "Ace"
  11. elif rank == 11:
  12. rank = 'Jack'
  13. elif rank == 12:
  14. rank = 'Queen'
  15. elif rank == 13:
  16. rank = 'King'
  17.  
  18. if suit == 'h':
  19. suit = "Hearts"
  20. elif suit == 'c':
  21. suit = "Clubs"
  22. elif suit == 's':
  23. suit = "Spades"
  24. elif suit == 'd':
  25. suit = "Diamonds"
  26.  
  27. print "%s of %s" % (rank, suit)
  28.  
  29.  
  30. card_list = [ (rank, suit) for suit in "hcds" for rank in range(1, 14)]
  31. # test
  32. print card_list
  33.  
  34. print
  35.  
  36. # pull 5 cards
  37. hand = card_list[:5]
  38. # show the hand
  39. show_hand(hand)
  40.  
  41. """
  42. result =
  43. Ace of Hearts
  44. 2 of Hearts
  45. 3 of Hearts
  46. 4 of Hearts
  47. 5 of Hearts
  48. """
  49.  
  50. print "-"*30
  51.  
  52. random.shuffle(card_list)
  53. # pull 5 cards
  54. hand = card_list[:5]
  55. # sort the hand by rank
  56. hand.sort()
  57. # show the hand
  58. show_hand(hand)
  59.  
  60. """
  61. possible result =
  62. 2 of Diamonds
  63. 5 of Hearts
  64. Queen of Clubs
  65. Queen of Diamonds
  66. King of Diamonds
  67. """
Last edited by vegaseat; Oct 30th, 2006 at 9:52 pm. Reason: updated rank
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 30th, 2006
0

Re: Class - Shuffle()

So are classes a good choice for a card game or not?
Featured Poster
Reputation Points: 83
Solved Threads: 39
Posting Whiz in Training
LaMouche is offline Offline
263 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: global or function?
Next Thread in Python Forum Timeline: Python for Birthday (but having tech difficulties)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC