[pycon] warGame
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- face = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
- suit = ["Clubs", "Diamonds", "Hearts", "Spades"]
- import random
- class Card:
- # Constructor method for Card.
- # Takes as input a face and suit value.
- # If they are not found in the global variables above, the card will be set to a 2 of clubs
- def __init__(self, the_face, the_suit):
- global face, suit
- if (the_face in face and the_suit in suit):
- self.face = the_face
- self.suit = the_suit
- else:
- self.face = -1
- self.suit = "ILLEGAL CARD"
- # Retuns the suit value of the calling card
- def get_suit(self):
- return self.suit
- # Returns the face value of the calling card
- def get_face(self):
- return self.face
- # Compares the face and suit attributes of other_card to those possessed by the calling card
- def __eq__(self, other_card):
- return (self.face == other_card.get_face()) and (self.suit == other_card.get_suit())
- # Returns the value of self > other_card
- # The first comparison is on face value. If the faces are different, we return the result of
- # self.face > other_card.get_face()
- # If they are tied, we return the result of self.suit > other_card.get_suit()
- def __gt__(self, other_card):
- if self.face > other_card.get_face():
- return True
- elif (self.face == other_card.get_face()):
- return self.suit > other_card.get_suit()
- else:
- return False
- # Card tostring. Will return the card in the format "Face of Suit"
- def __str__(self):
- return "%s of %s" % (self.face, self.suit)
- class Deck:
- # The constructor method for the Deck.
- # It takes no parameters.
- # It fills a deck with 52 unique card, and then uses random.shuffle to randomly order the deck
- # The counter will be used to indicate which card is at the "top" of the deck
- # i.e. all cards above counter will have been dealt
- def __init__(self):
- self.deck = []
- self.counter = 0
- global face
- global suit
- for the_face in face:
- for the_suit in suit:
- self.deck.append(Card(the_face, the_suit))
- for i in range(7):
- random.shuffle(self.deck)
- # Returns the top card of the deck if it exists (if we have not previously dealt 52 cards)
- # We could add in a method to automatically shuffle the deck if we reach this point
- def deal(self):
- if self.counter < 52:
- result = self.deck[self.counter]
- self.counter += 1
- return result
- # Randomly shuffles the deck array seven times.
- def shuffle(self):
- self.counter = 0
- for i in range(7):
- random.shuffle(self.deck)
- # tostring method for deck class.
- # Prints out all 52 cards in the deck, one per line.
- # We indicate with an X cards that have been dealt
- # << Current Top Card indicates which card is the current top of the deck.
- def __str__(self):
- result = ""
- for i in range(52):
- if i == self.counter:
- result += "%s << Current Top Card\n" % self.deck[i]
- elif i < self.counter:
- result += "%s X\n" % self.deck[i]
- else:
- result += "%s\n" % self.deck[i]
- return result
- class Player_War:
- # For our Player, we need two attributes guaranteed, the current hand and the discard pile
- # Depending on game design, we could have a data point to represent wins.
- def __init__(self):
- self.hand = []
- self.discard = []
- # A method to allow cards to be added to the hand. Will be utilized by the game code
- def add_card(self, card):
- self.hand.append(card)
- # A method to allow cards to be added to the discard pile. Will be utilized by the game code
- def add_discard(self, card):
- self.discard.append(card)
- # Returns the number of cards currently in the player's hand
- def hand_size(self):
- return len(self.hand)
- # Returns the number of cards currently in the player's discard pile
- def discard_size(self):
- return len(self.discard)
- # Returns the top card of the player's hand. However, if the hand is empty,
- # we will shuffle the discard pile in with the helper method.
- # We will post an error message if the hand is totally empty.
- # NOTE: Perhaps in our gameplay loop we decide to remove the automatic shuffle
- def play_card(self):
- if len(self.hand) > 0:
- return self.hand.pop()
- elif len(self.discard) > 0:
- self.shuffle_discard()
- return self.hand.pop()
- else:
- return Card(-1, "HAND EMPTY")
- # Takes all cards from our discard pile and places them into the hand
- def shuffle_discard(self):
- while len(self.discard) > 0:
- self.hand.append(self.discard.pop())
- random.shuffle(self.hand)
- # Prints out both the hand and the discard pile for the current player
- def __str__(self):
- result = "Current Hand:"
- for item in self.hand:
- result += "\t%s" % item
- result += "\nDiscard Pile:"
- for item in self.discard:
- result += "\t%s" % item
- return result
- # A class which defines the Game of War.
- # The rules of war are as follows:
- # 2 Players
- # 1 Deck of cards (52 unique cards)
- # Could change/expand the base datapoints
- # Deal the cards
- # Cycle of Play
- # Round by round/turn by turn
- # Each player draws a card, and presents it (c1 and c2)
- # If c1 > c2, P1 wins, and gets both cards into their discard
- # If c2 > c1, P2 wins, ...
- # There is no possibility of tie (This will change)
- # Play continues until no cards are in the hand (at the start, 26 rounds/turns will go)
- # shuffle the discard pile into the hand, and play continues
- # Win Condition:
- # Default is one of the players has all 52 cards
- # We'll look at two possibilities:
- # X Turns of have passed, the player with more cards wins
- # Once a player has X number of cards total (hand + discard) (Look at this style next week)
- class War_Game:
- # The init method. It defines both players and the deck.
- # We will be adding to this in the future, with the addition of win counters
- def __init__(self):
- self.player_one = Player_War()
- self.player_two = Player_War()
- self.player_three = Player_War()
- self.player_four = Player_War()
- self.p1_victories = 0
- self.p2_victories = 0
- self.p3_victories = 0
- self.p4_victories = 0
- self.deck = Deck()
- # Gives us the base setup for our War Game.
- # Redeclares both players (perhaps we update Player_War to have a reset method)
- # Shuffles the deck
- # Deals the appropriate number of cards to each player
- def start_game(self):
- self.player_one = Player_War()
- self.player_two = Player_War()
- self.player_three = Player_War()
- self.player_four = Player_War()
- self.deck.shuffle()
- for i in range(13):
- self.player_one.add_card(self.deck.deal())
- self.player_two.add_card(self.deck.deal())
- self.player_three.add_card(self.deck.deal())
- self.player_four.add_card(self.deck.deal())
- def p1_win(self):
- t1 = self.player_one.discard_size() + self.player_one.hand_size()
- t2 = self.player_two.discard_size() + self.player_two.hand_size()
- t3 = self.player_three.discard_size() + self.player_three.hand_size()
- t4 = self.player_four.discard_size() + self.player_four.hand_size()
- return ((t2 == 0 or t3 == 0 or t4 == 0) and (t1 > t2 and t1 > t3 and t1 > t4))
- def p2_win(self):
- t1 = self.player_one.discard_size() + self.player_one.hand_size()
- t2 = self.player_two.discard_size() + self.player_two.hand_size()
- t3 = self.player_three.discard_size() + self.player_three.hand_size()
- t4 = self.player_four.discard_size() + self.player_four.hand_size()
- return ((t1 == 0 or t3 == 0 or t4 == 0) and (t2 > t1 and t2 > t3 and t2 > t4))
- def p3_win(self):
- t1 = self.player_one.discard_size() + self.player_one.hand_size()
- t2 = self.player_two.discard_size() + self.player_two.hand_size()
- t3 = self.player_three.discard_size() + self.player_three.hand_size()
- t4 = self.player_four.discard_size() + self.player_four.hand_size()
- return ((t1 == 0 or t2 == 0 or t4 == 0) and (t3 > t1 and t3 > t2 and t3 > t4))
- def p4_win(self):
- t1 = self.player_one.discard_size() + self.player_one.hand_size()
- t2 = self.player_two.discard_size() + self.player_two.hand_size()
- t3 = self.player_three.discard_size() + self.player_three.hand_size()
- t4 = self.player_four.discard_size() + self.player_four.hand_size()
- return ((t1 == 0 or t2 == 0 or t3 == 0) and (t4 > t1 and t4 > t2 and t4 > t3))
- def simulation(self, num_games):
- # What exactly /is/ a simulation?
- # We want to play a full game (and play it several times)
- # Likely many calls to play round!
- # What ends the game? What is the winning condition?
- # Is it when no cards remain?
- # X rounds have passed and whomever has most cards wins
- # End when one player has X number of cards total <----
- # Reset the game somewhere!
- game_count = 1
- while (game_count <= num_games):
- # First thing is to reset the game state
- self.start_game()
- num_rounds = 0
- game_won = False
- while (not game_won):
- num_rounds += 1
- # Play a round
- self.play_round()
- # Check to see if the game is over
- if (self.p1_win()):
- print("Player 1 has won game #%s! It took %s rounds" % (game_count, num_rounds))
- game_won = True
- self.p1_victories += 1
- elif (self.p2_win()):
- print("Player 2 has won game #%s! It took %s rounds" % (game_count, num_rounds))
- game_won = True
- self.p2_victories += 1
- elif (self.p3_win()):
- print("Player 3 has won game #%s! It took %s rounds" % (game_count, num_rounds))
- game_won = True
- self.p3_victories += 1
- elif (self.p4_win()):
- print("Player 4 has won game #%s! It took %s rounds" % (game_count, num_rounds))
- game_won = True
- self.p4_victories += 1
- game_count += 1
- # A method to play a singular round of War.
- # Both players deal a card using the play_card method of Player_War
- # The higher valued card wins the hand, and both cards are placed into the winning player's
- # discard pile.
- # Shuffling of discard into hand is handled by the Player_War object
- def play_round(self):
- c1 = self.player_one.play_card()
- c2 = self.player_two.play_card()
- c3 = self.player_three.play_card()
- c4 = self.player_four.play_card()
- if ((c1.get_face() < 0) or (c2.get_face() < 0) or (c3.get_face() < 0) or (c4.get_face() < 0 )):
- return False
- t1 = self.player_one.discard_size() + self.player_one.hand_size()
- t2 = self.player_two.discard_size() + self.player_two.hand_size()
- t3 = self.player_three.discard_size() + self.player_three.hand_size()
- t4 = self.player_four.discard_size() + self.player_four.hand_size()
- if (t1 == 0):
- if (t2 > t3 and t2 > t4):
- self.p2_win()
- if (t3 > t2 and t3 > t4):
- self.p3_win()
- if (t4 > t2 and t4 > t3):
- self.p4_win()
- elif (t2 == 0):
- if (t1 > t3 and t1 > t4):
- self.p1_win()
- if (t3 > t1 and t3 > t4):
- self.p3_win()
- if (t4 > t1 and t4 > t3):
- self.p4_win()
- elif (t3 == 0):
- if (t1 > t2 and t1 > t4):
- self.p1_win()
- if (t2 > t1 and t2 > t4):
- self.p2_win()
- if (t4 > t2 and t4 > t1):
- self.p4_win()
- elif (t4 == 0):
- if (t2 > t3 and t2 > t1):
- self.p2_win()
- if (t3 > t2 and t3 > t1):
- self.p3_win()
- if (t1 > t2 and t1 > t3):
- self.p1_win()
- print("Player 1's Hand + Discard Count: %s" % t1)
- print("Player 2's Hand + Discard Count: %s" % t2)
- print("Player 3's Hand + Discard Count: %s" % t3)
- print("Player 4's Hand + Discard Count: %s" % t4)
- print("")
- print("Playing %s vs %s vs %s vs %s" % (c1, c2, c3, c4))
- if ((c1 > c2) and (c1 > c3) and (c1 > c4)):
- # Player 1 won!
- self.player_one.add_discard(c1)
- self.player_one.add_discard(c2)
- self.player_one.add_discard(c3)
- self.player_one.add_discard(c4)
- print( "Player One wins with %s!\n" % c1)
- return True
- elif((c2 > c1) and (c2 > c3) and (c2 > c4)):
- # Player 2 won!
- self.player_two.add_discard(c1)
- self.player_two.add_discard(c2)
- self.player_two.add_discard(c3)
- self.player_two.add_discard(c4)
- print( "Player Two wins with %s!\n" % c2)
- return False
- elif((c3 > c1) and (c3 > c2) and (c3 > c4)):
- # Player 3 won!
- self.player_three.add_discard(c1)
- self.player_three.add_discard(c2)
- self.player_three.add_discard(c3)
- self.player_three.add_discard(c4)
- print( "Player Three wins with %s!\n" % c3)
- return False
- elif((c4 > c1) and (c4 > c2) and (c4 > c3)):
- # Player 4 won!
- self.player_four.add_discard(c1)
- self.player_four.add_discard(c2)
- self.player_four.add_discard(c3)
- self.player_four.add_discard(c4)
- print( "Player Four wins with %s!\n" % c4)
- return False
- # Tostring for War_Game
- # Currently it prints out both player objects, with a static number for wins.
- def __str__(self):
- result = "Player 1\tWins %s\n" % self.p1_victories
- result += "%s\n" % self.player_one
- result += "Player 2\tWins %s\n" % self.p2_victories
- result += "%s\n" % self.player_two
- result = "Player 3\tWins %s\n" % self.p3_victories
- result += "%s\n" % self.player_three
- result += "Player 4\tWins %s\n" % self.p4_victories
- result += "%s\n" % self.player_four
- return result
- def main():
- print("Starting Game of War!\n")
- my_game = War_Game()
- my_game.simulation(1)
- print("\nGame Over!")
- main()
Editor
You can edit this paste and save as new: