Add type hints in problem 54
This commit is contained in:
parent
491eb89201
commit
ac15f7ddf2
@ -45,14 +45,16 @@
|
|||||||
# and in each hand there is a clear winner.
|
# and in each hand there is a clear winner.
|
||||||
#
|
#
|
||||||
# How many hands does Player 1 win?
|
# How many hands does Player 1 win?
|
||||||
|
from __future__ import annotations
|
||||||
import sys
|
import sys
|
||||||
|
from typing import List, Optional
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
|
|
||||||
from projecteuler import timing
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
class Value(IntEnum):
|
class Value(IntEnum):
|
||||||
|
Dummy = 0
|
||||||
Two = 1
|
Two = 1
|
||||||
Three = 2
|
Three = 2
|
||||||
Four = 3
|
Four = 3
|
||||||
@ -67,16 +69,29 @@ class Value(IntEnum):
|
|||||||
King = 12
|
King = 12
|
||||||
Ace = 13
|
Ace = 13
|
||||||
|
|
||||||
|
def next(self):
|
||||||
|
v = self.value + 1
|
||||||
|
|
||||||
|
if v > 13:
|
||||||
|
raise ValueError('Enumeration ended')
|
||||||
|
|
||||||
|
return Value(v)
|
||||||
|
|
||||||
|
|
||||||
class Card():
|
class Card():
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
self.value: Value
|
||||||
|
self.value = Value.Dummy
|
||||||
|
self.suit: str
|
||||||
|
self.suit = ''
|
||||||
|
|
||||||
self.value = None
|
|
||||||
self.suit = None
|
|
||||||
|
|
||||||
def set_card(self, card):
|
def __lt__(self, other: Card) -> bool:
|
||||||
|
return self.value < other.value
|
||||||
|
|
||||||
|
|
||||||
|
def set_card(self, card: str) -> None:
|
||||||
if card[0] == '2':
|
if card[0] == '2':
|
||||||
self.value = Value.Two
|
self.value = Value.Two
|
||||||
elif card[0] == '3':
|
elif card[0] == '3':
|
||||||
@ -109,31 +124,34 @@ class Card():
|
|||||||
|
|
||||||
class Hand():
|
class Hand():
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
self.cards: List[Card]
|
||||||
|
|
||||||
self.cards = []
|
self.cards = []
|
||||||
|
|
||||||
def sort(self):
|
def sort(self) -> None:
|
||||||
self.cards.sort(key=lambda x: x.value)
|
self.cards.sort()
|
||||||
|
|
||||||
|
|
||||||
class Game():
|
class Game():
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
self.hand1: Optional[Hand]
|
||||||
|
|
||||||
self.hand1 = None
|
self.hand1 = None
|
||||||
|
self.hand2: Optional[Hand]
|
||||||
self.hand2 = None
|
self.hand2 = None
|
||||||
|
|
||||||
def play(self):
|
def play(self) -> int:
|
||||||
# If player 1 has a Royal Flush, player 1 wins.
|
# If player 1 has a Royal Flush, player 1 wins.
|
||||||
|
assert self.hand1 is not None
|
||||||
|
assert self.hand2 is not None
|
||||||
|
|
||||||
if self.hand1.cards[4].value == Value.Ace and self.hand1.cards[3].value == Value.King and\
|
if self.hand1.cards[4].value == Value.Ace and self.hand1.cards[3].value == Value.King and\
|
||||||
self.hand1.cards[2].value == Value.Queen and self.hand1.cards[1].value == Value.Jack and\
|
self.hand1.cards[2].value == Value.Queen and self.hand1.cards[1].value == Value.Jack and\
|
||||||
self.hand1.cards[0] == Value.Ten and self.hand1.cards[0].suit == self.hand1.cards[1].suit and\
|
self.hand1.cards[0] == Value.Ten and self.hand1.cards[0].suit == self.hand1.cards[1].suit and\
|
||||||
self.hand1.cards[0].suit == self.hand1.cards[2].suit and\
|
self.hand1.cards[0].suit == self.hand1.cards[2].suit and\
|
||||||
self.hand1.cards[0].suit == self.hand1.cards[3].suit and\
|
self.hand1.cards[0].suit == self.hand1.cards[3].suit and\
|
||||||
self.hand1.cards[0].suit == self.hand1.cards[4].suit:
|
self.hand1.cards[0].suit == self.hand1.cards[4].suit:
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
# If player 2 has a Royal Flush, player 2 wins.
|
# If player 2 has a Royal Flush, player 2 wins.
|
||||||
@ -143,6 +161,7 @@ class Game():
|
|||||||
self.hand2.cards[0].suit == self.hand2.cards[2].suit and\
|
self.hand2.cards[0].suit == self.hand2.cards[2].suit and\
|
||||||
self.hand2.cards[0].suit == self.hand2.cards[3].suit and\
|
self.hand2.cards[0].suit == self.hand2.cards[3].suit and\
|
||||||
self.hand2.cards[0].suit == self.hand2.cards[4].suit:
|
self.hand2.cards[0].suit == self.hand2.cards[4].suit:
|
||||||
|
|
||||||
return -1
|
return -1
|
||||||
|
|
||||||
straightflush1 = 0
|
straightflush1 = 0
|
||||||
@ -156,10 +175,11 @@ class Game():
|
|||||||
straightflush1 = 1
|
straightflush1 = 1
|
||||||
|
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
value = value + 1
|
value = value.next()
|
||||||
|
|
||||||
if self.hand1.cards[i].value != value:
|
if self.hand1.cards[i].value != value:
|
||||||
straightflush1 = 0
|
straightflush1 = 0
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# Check if player 2 has a straight flush.
|
# Check if player 2 has a straight flush.
|
||||||
@ -170,10 +190,11 @@ class Game():
|
|||||||
straightflush2 = 1
|
straightflush2 = 1
|
||||||
|
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
value = value + 1
|
value = value.next()
|
||||||
|
|
||||||
if self.hand2.cards[i].value != value:
|
if self.hand2.cards[i].value != value:
|
||||||
straightflush2 = 0
|
straightflush2 = 0
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# If player 1 has a straight flush and player 2 doesn't, player 1 wins
|
# If player 1 has a straight flush and player 2 doesn't, player 1 wins
|
||||||
@ -278,20 +299,23 @@ class Game():
|
|||||||
value = self.hand1.cards[0].value
|
value = self.hand1.cards[0].value
|
||||||
|
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
value = value + 1
|
# value = value + 1
|
||||||
|
value = value.next()
|
||||||
|
|
||||||
if self.hand1.cards[i].value != value:
|
if self.hand1.cards[i].value != value:
|
||||||
straight1 = 0
|
straight1 = 0
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# Check if player 2 has a straight.
|
# Check if player 2 has a straight.
|
||||||
value = self.hand2.cards[0].value
|
value = self.hand2.cards[0].value
|
||||||
|
|
||||||
for i in range(1, 5):
|
for i in range(1, 5):
|
||||||
value = value + 1
|
value = value.next()
|
||||||
|
|
||||||
if self.hand2.cards[i].value != value:
|
if self.hand2.cards[i].value != value:
|
||||||
straight2 = 0
|
straight2 = 0
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
# If player 1 has a straight and player 2 doesn't, player 1 wins.
|
# If player 1 has a straight and player 2 doesn't, player 1 wins.
|
||||||
@ -443,12 +467,13 @@ class Game():
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p054():
|
def p054() -> None:
|
||||||
try:
|
try:
|
||||||
with open('p054_poker.txt', 'r', encoding='utf-8') as fp:
|
with open('p054_poker.txt', 'r', encoding='utf-8') as fp:
|
||||||
games = fp.readlines()
|
games = fp.readlines()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('Error while opening file p054_poker.txt')
|
print('Error while opening file p054_poker.txt')
|
||||||
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
|
Loading…
x
Reference in New Issue
Block a user