Class - Shuffle()

Reply

Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Class - Shuffle()

 
0
  #1
Oct 24th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Class - Shuffle()

 
0
  #2
Oct 24th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Class - Shuffle()

 
0
  #3
Oct 24th, 2006
Originally Posted by LaMouche View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Class - Shuffle()

 
0
  #4
Oct 24th, 2006
Could you copy your entire code with code tags, please? Sorry my first post didn't really say anything at all.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Class - Shuffle()

 
0
  #5
Oct 24th, 2006
Originally Posted by LaMouche View Post
Could you copy your entire code with code tags, please? Sorry my first post didn't really say anything at all.
  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!!!!!!!!!!!!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Class - Shuffle()

 
0
  #6
Oct 24th, 2006
Sorry,

I'm very new to this. I even had to do a search to find out what code tags are...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 169
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Class - Shuffle()

 
0
  #7
Oct 24th, 2006
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.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 36
Reputation: macca1111 is an unknown quantity at this point 
Solved Threads: 0
macca1111 macca1111 is offline Offline
Light Poster

Re: Class - Shuffle()

 
0
  #8
Oct 30th, 2006
Originally Posted by Ene Uran View Post
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]
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Class - Shuffle()

 
0
  #9
Oct 30th, 2006
I still don't understand your file structure or the reason for a file. Will this help ...
  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
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 128
Reputation: LaMouche is on a distinguished road 
Solved Threads: 19
LaMouche's Avatar
LaMouche LaMouche is offline Offline
Junior Poster

Re: Class - Shuffle()

 
0
  #10
Oct 30th, 2006
So are classes a good choice for a card game or not?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC