443,818 Members | 1,223 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

# Calling an object from a list

 P: 14 I don't know how clear my title is, but I can't think of how to title this problem. I am writing a single card game of War. It is very simple. Every round the deck is populated and shuffled. A single card is dealt to each of two players. The card with the higher value wins, and the player gets a point. If there is a tie, nobody earns the point, and a new round begins. I am reusing code from a blackjack program provided in the book I am working out of. It uses a list to organize each of the players that would be in the blackjack game. I kept the list around so I could use the functions associated with it. However, in the Blackjack game, it compared each players hand value to a dealer. In my program, I want to compare the value of the card the player holds to each other's hand. the player object is defined like this: Expand|Select|Wrap|Line Numbers class War_Player(War_Hand):     """ A War Player. """     wins = int(0)       def lose(self):         print self.name, "loses."       def win(self):         print self.name, "wins."         wins += 1       def tie(self):         print self.name, "ties."   and then there is a War_Game object with this code: Expand|Select|Wrap|Line Numbers class War_Game(object):     """ A War Game. """     def __init__(self, names):               self.players = []         for name in names:             player = War_Player(name)             self.players.append(player)           self.dealer = War_Dealer("Dealer")           self.deck = War_Deck()         self.deck.populate()         self.deck.shuffle()       def play(self):         # Make sure deck is populated.         self.deck.clear(self)         self.deck.populate(self)         self.deck.shuffle(self)         # deal card to everyone         self.deck.deal(self.players, per_hand = 1)         for player in self.players:             print player           # compare each player still playing to each other         for player in self.players             if player.total > self.dealer.total:                 player.win()             elif player.total < self.dealer.total:                 player.lose()             else:                 player.push()           # remove everyone's cards         for player in self.players:             player.clear()         self.dealer.clear()   the bold, italicized, underlined portions are the remnants of the Blackjack code that I can't figure out how to change to fit this. I need to change it to compare each players hand to the other players hand rather than the dealer(which i have removed from the program completely). If you could explain to me how to do it, and why it works that way, that would be great! I am frustrated that I can't seem to figure this out. Thanks in advance for any help you can give! May 24 '07 #1
9 Replies

 Expert 5K+ P: 6,596 Ok. We can do this, but you left out the parent class of War_Player() which is where some of this is going to take place (it looks like). Presumably, one thing that War_Hand() does is Expand|Select|Wrap|Line Numbers def __init__(maybeName, whatEver):     self.name = maybyName     # etc. In the sub-class there is one problem that I can see right off: Expand|Select|Wrap|Line Numbers class War_Player(War_Hand):     """ A War Player. """     ### This is a "class variable, shared by all instances ###     wins = int(0)     ### To keep track of this players wins, you need:     self.wins = 0     ### do the same thing for losses and ties       def lose(self):         print self.name, "loses."       def win(self):         print self.name, "wins."         ### Change here, too         self.wins += 1       def tie(self):         print self.name, "ties."   I don't know how clear my title is, but I can't think of how to title this problem. I am writing a single card game of War. It is very simple. Every round the deck is populated and shuffled. A single card is dealt to each of two players. The card with the higher value wins, and the player gets a point. If there is a tie, nobody earns the point, and a new round begins. I am reusing code from a blackjack program provided in the book I am working out of. It uses a list to organize each of the players that would be in the blackjack game. I kept the list around so I could use the functions associated with it. However, in the Blackjack game, it compared each players hand value to a dealer. In my program, I want to compare the value of the card the player holds to each other's hand. the player object is defined like this: Expand|Select|Wrap|Line Numbers class War_Player(War_Hand):     """ A War Player. """     wins = int(0)       def lose(self):         print self.name, "loses."       def win(self):         print self.name, "wins."         wins += 1       def tie(self):         print self.name, "ties."   and then there is a War_Game object with this code: Expand|Select|Wrap|Line Numbers class War_Game(object):     """ A War Game. """     def __init__(self, names):               self.players = []         for name in names:             player = War_Player(name)             self.players.append(player)           self.dealer = War_Dealer("Dealer")           self.deck = War_Deck()         self.deck.populate()         self.deck.shuffle()       def play(self):         # Make sure deck is populated.         self.deck.clear(self)         self.deck.populate(self)         self.deck.shuffle(self)         # deal card to everyone         self.deck.deal(self.players, per_hand = 1)         for player in self.players:             print player           # compare each player still playing to each other         for player in self.players             if player.total > self.dealer.total:                 player.win()             elif player.total < self.dealer.total:                 player.lose()             else:                 player.push()           # remove everyone's cards         for player in self.players:             player.clear()         self.dealer.clear()   the bold, italicized, underlined portions are the remnants of the Blackjack code that I can't figure out how to change to fit this. I need to change it to compare each players hand to the other players hand rather than the dealer(which i have removed from the program completely). If you could explain to me how to do it, and why it works that way, that would be great! I am frustrated that I can't seem to figure this out. Thanks in advance for any help you can give![/quote] May 24 '07 #2

 Expert 5K+ P: 6,596 Not too much code for the forum. Just too much for me at the moment (not enough coffee). Back soon. May 29 '07 #4

 Expert Mod 2.5K+ P: 2,851 Add a __cmp__ method to Player to enable sorting a list of Players on the Players' scores: Expand|Select|Wrap|Line Numbers class Player(object):     """ A player for a game. """     def __init__(self, name, score = 0):         self.name = name         self.score = score       def __str__(self):         rep = self.name + ":\t" + str(self.score)         return rep       def __cmp__(self, other):         return cmp(self.score, other.score) You will have to update each Players' 'score' attribute based on the rank of the cards dealt. Sort self.players in War_Game.play(): Expand|Select|Wrap|Line Numbers self.players.sort() If the last player's score (self.players[-1].score) is equal to the next to last player's score, there is a tie and no one wins. Otherwise, the last player wins and everyone else loses. I have not tested this, but I think it will work. May 29 '07 #5

 P: 14 wow. Thanks! I'm not at a machine with python on it, so I cannot test that out, but it sounds like it could work. I am not familiar with the _cmp_ method, though, and I don't know what that is doing. If you could explain that to me, that would be nice! May 29 '07 #6

 Expert Mod 2.5K+ P: 2,851 wow. Thanks! I'm not at a machine with python on it, so I cannot test that out, but it sounds like it could work. I am not familiar with the _cmp_ method, though, and I don't know what that is doing. If you could explain that to me, that would be nice! '__cmp__' is a special method used by the comparison operators to compare two objects (self, other). If undefined, objects are compared by identity. Alternatively, rich comparison operations can be defined such as '__lt__', '__le__', '__eq__', etc. May 29 '07 #7

 P: 14 '__cmp__' is a special method used by the comparison operators to compare two objects (self, other). If undefined, objects are compared by identity. Alternatively, rich comparison operations can be defined such as '__lt__', '__le__', '__eq__', etc. Thanks alot. I'm actually trying to teach myself python out of a book, and I'm familiar with __str__ and __init__, but the __cmp__ special method is new to me. Expand|Select|Wrap|Line Numbers def __cmp__(self, other):         return cmp(self.score, other.score)   When I use this, and say the self.score is 10 while the other.score is 8, what would the returned value be? May 29 '07 #8

 Expert Mod 2.5K+ P: 2,851 Thanks alot. I'm actually trying to teach myself python out of a book, and I'm familiar with __str__ and __init__, but the __cmp__ special method is new to me. Expand|Select|Wrap|Line Numbers def __cmp__(self, other):         return cmp(self.score, other.score)   When I use this, and say the self.score is 10 while the other.score is 8, what would the returned value be? -1, 0, 1 if <, ==, > respectively. The list sort method interprets the return value transparently. If you want it to sort in reverse order, simply negate the return value. May 29 '07 #9

 P: 14 Thanks. That's really neat. I'll let you know how it works out for me! May 30 '07 #10