|
| 1 | +from random import shuffle |
| 2 | + |
| 3 | + |
| 4 | +class Card: |
| 5 | + suits = ["spades", |
| 6 | + "hearts", |
| 7 | + "diamonds", |
| 8 | + "clubs"] |
| 9 | + |
| 10 | + values = [None, None,"2", "3", |
| 11 | + "4", "5", "6", "7", |
| 12 | + "8", "9", "10", |
| 13 | + "Jack", "Queen", |
| 14 | + "King", "Ace"] |
| 15 | + |
| 16 | + def __init__(self, v, s): |
| 17 | + """suit + value are ints""" |
| 18 | + self.value = v |
| 19 | + self.suit = s |
| 20 | + |
| 21 | + def __lt__(self, c2): |
| 22 | + if self.value < c2.value: |
| 23 | + return True |
| 24 | + if self.value == c2.value: |
| 25 | + if self.suit < c2.suit: |
| 26 | + return True |
| 27 | + else: |
| 28 | + return False |
| 29 | + return False |
| 30 | + |
| 31 | + def __gt__(self, c2): |
| 32 | + if self.value > c2.value: |
| 33 | + return True |
| 34 | + if self.value == c2.value: |
| 35 | + if self.suit > c2.suit: |
| 36 | + return True |
| 37 | + else: |
| 38 | + return False |
| 39 | + return False |
| 40 | + |
| 41 | + def __repr__(self): |
| 42 | + v = self.values[self.value] +\ |
| 43 | + " of " + \ |
| 44 | + self.suits[self.suit] |
| 45 | + return v |
| 46 | + |
| 47 | + |
| 48 | +class Deck: |
| 49 | + def __init__(self): |
| 50 | + self.cards = [] |
| 51 | + for i in range(2, 15): |
| 52 | + for j in range(4): |
| 53 | + self.cards\ |
| 54 | + .append(Card(i, |
| 55 | + j)) |
| 56 | + shuffle(self.cards) |
| 57 | + |
| 58 | + def rm_card(self): |
| 59 | + if len(self.cards) == 0: |
| 60 | + return |
| 61 | + return self.cards.pop() |
| 62 | + |
| 63 | + |
| 64 | +class Player: |
| 65 | + def __init__(self, name): |
| 66 | + self.wins = 0 |
| 67 | + self.card = None |
| 68 | + self.name = name |
| 69 | + |
| 70 | + |
| 71 | +class Game: |
| 72 | + def __init__(self): |
| 73 | + name1 = input("p1 name ") |
| 74 | + name2 = input("p2 name ") |
| 75 | + self.deck = Deck() |
| 76 | + self.p1 = Player(name1) |
| 77 | + self.p2 = Player(name2) |
| 78 | + |
| 79 | + def wins(self, winner): |
| 80 | + w = "{} wins this round" |
| 81 | + w = w.format(winner) |
| 82 | + print(w) |
| 83 | + |
| 84 | + def draw(self, p1n, p1c, p2n, p2c): |
| 85 | + d = "{} drew {} {} drew {}" |
| 86 | + d = d.format(p1n, |
| 87 | + p1c, |
| 88 | + p2n, |
| 89 | + p2c) |
| 90 | + print(d) |
| 91 | + |
| 92 | + def play_game(self): |
| 93 | + cards = self.deck.cards |
| 94 | + print("beginning War!") |
| 95 | + while len(cards) >= 2: |
| 96 | + m = "q to quit. Any " + \ |
| 97 | + "key to play:" |
| 98 | + response = input(m) |
| 99 | + if response == 'q': |
| 100 | + break |
| 101 | + p1c = self.deck.rm_card() |
| 102 | + p2c = self.deck.rm_card() |
| 103 | + p1n = self.p1.name |
| 104 | + p2n = self.p2.name |
| 105 | + self.draw(p1n, |
| 106 | + p1c, |
| 107 | + p2n, |
| 108 | + p2c) |
| 109 | + if p1c > p2c: |
| 110 | + self.p1.wins += 1 |
| 111 | + self.wins(self.p1.name) |
| 112 | + else: |
| 113 | + self.p2.wins += 1 |
| 114 | + self.wins(self.p2.name) |
| 115 | + |
| 116 | + win = self.winner(self.p1, |
| 117 | + self.p2) |
| 118 | + print("War is over.{} wins" |
| 119 | + .format(win)) |
| 120 | + |
| 121 | + def winner(self, p1, p2): |
| 122 | + if p1.wins > p2.wins: |
| 123 | + return p1.name |
| 124 | + if p1.wins < p2.wins: |
| 125 | + return p2.name |
| 126 | + return "It was a tie!" |
| 127 | + |
| 128 | +game = Game() |
| 129 | +game.play_game() |
0 commit comments