Browse Source

initial commit

john melesky 2 years ago
parent
commit
710e8f385e
3 changed files with 155 additions and 0 deletions
  1. 130 0
      high_card/high_card.py
  2. 7 0
      poetry.lock
  3. 18 0
      pyproject.toml

+ 130 - 0
high_card/high_card.py

@@ -0,0 +1,130 @@
+from random import shuffle
+
+
+class Card:
+    suits = ["spades",
+             "hearts",
+             "diamonds",
+             "clubs"]
+    
+    values = [None, None,"2", "3",
+              "4", "5", "6", "7",
+              "8", "9", "10",
+              "Jack", "Queen",
+              "King", "Ace"]
+
+    def __init__(self, v, s):
+        """suit + value are ints"""
+        self.value = v
+        self.suit = s
+
+    def __lt__(self, c2):
+        if self.value < c2.value:
+            return True
+        if self.value == c2.value:
+            if self.suit < c2.suit:
+                return True
+            else:
+                return False
+        return False
+
+    def __gt__(self, c2):
+        if self.value > c2.value:
+            return True
+        if self.value == c2.value:
+            if self.suit > c2.suit:
+                return True
+            else:
+                return False
+        return False
+
+    def __repr__(self):
+        v = self.values[self.value] +\
+            " of " + \
+            self.suits[self.suit]
+        return v
+
+
+class Deck:
+    def __init__(self):
+        self.cards = []
+        for i in range(2, 15):
+            for j in range(4):
+                self.cards\
+                    .append(Card(i,
+                                 j))
+        shuffle(self.cards)
+
+    def rm_card(self):
+        if len(self.cards) == 0:
+            return
+        return self.cards.pop()
+
+class Player:
+   def __init__(self, name):
+       self.wins = 0
+       self.card = None
+       self.name = name
+
+class Game:
+    def __init__(self):
+        name1 = input("p1 name ")
+        name2 = input("p2 name ")
+        self.deck = Deck()
+        self.p1 = Player(name1)
+        self.p2 = Player(name2)
+
+    def wins(self, winner):
+        w = "{} wins this round"
+        w = w.format(winner)
+        print(w)
+
+    def draw(self, p1n, p1c, p2n, p2c):
+        d = "{} drew {} {} drew {}"
+        d = d.format(p1n,
+                     p1c,
+                     p2n,
+                     p2c)
+        print(d)
+
+    def play_game(self):
+        cards = self.deck.cards
+        print("beginning War!")
+        while len(cards) >= 2:
+            m = "q to quit. Any " + \
+                "key to play:"
+            response = input(m)
+            if response == 'q':
+                break
+            p1c = self.deck.rm_card()
+            p2c = self.deck.rm_card()
+            p1n = self.p1.name
+            p2n = self.p2.name
+            self.draw(p1n,
+                      p1c,
+                      p2n,
+                      p2c)
+            if p1c > p2c:
+                self.p1.wins += 1
+                self.wins(self.p1.name)
+            else:
+                self.p2.wins += 1
+                self.wins(self.p2.name)
+
+        win = self.winner(self.p1,
+                         self.p2)
+        print("War is over.{} wins"
+              .format(win))
+
+    def winner(self, p1, p2):
+        if p1.wins > p2.wins:
+            return p1.name
+        if p1.wins < p2.wins:
+            return p2.name
+        return "It was a tie!"
+
+
+def main():
+    game = Game()
+    game.play_game()
+

+ 7 - 0
poetry.lock

@@ -0,0 +1,7 @@
+# This file is automatically @generated by Poetry and should not be changed by hand.
+package = []
+
+[metadata]
+lock-version = "2.0"
+python-versions = "^3.11"
+content-hash = "81b2fa642d7f2d1219cf80112ace12d689d053d81be7f7addb98144d56fc0fb2"

+ 18 - 0
pyproject.toml

@@ -0,0 +1,18 @@
+[tool.poetry]
+name = "high-card"
+version = "0.1.0"
+description = ""
+authors = ["john melesky <code@phaedrusdeinus.org>"]
+readme = "README.md"
+packages = [{include = "high_card"}]
+
+[tool.poetry.dependencies]
+python = "^3.11"
+
+[tool.poetry.scripts]
+high-card = "high_card.high_card:main"
+
+
+[build-system]
+requires = ["poetry-core"]
+build-backend = "poetry.core.masonry.api"