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.
|
||||
#
|
||||
# How many hands does Player 1 win?
|
||||
|
||||
from __future__ import annotations
|
||||
import sys
|
||||
from typing import List, Optional
|
||||
from enum import IntEnum
|
||||
|
||||
from projecteuler import timing
|
||||
|
||||
|
||||
class Value(IntEnum):
|
||||
Dummy = 0
|
||||
Two = 1
|
||||
Three = 2
|
||||
Four = 3
|
||||
@ -67,16 +69,29 @@ class Value(IntEnum):
|
||||
King = 12
|
||||
Ace = 13
|
||||
|
||||
def next(self):
|
||||
v = self.value + 1
|
||||
|
||||
if v > 13:
|
||||
raise ValueError('Enumeration ended')
|
||||
|
||||
return Value(v)
|
||||
|
||||
|
||||
class Card():
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
def __init__(self) -> None:
|
||||
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':
|
||||
self.value = Value.Two
|
||||
elif card[0] == '3':
|
||||
@ -109,31 +124,34 @@ class Card():
|
||||
|
||||
class Hand():
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.cards: List[Card]
|
||||
self.cards = []
|
||||
|
||||
def sort(self):
|
||||
self.cards.sort(key=lambda x: x.value)
|
||||
def sort(self) -> None:
|
||||
self.cards.sort()
|
||||
|
||||
|
||||
class Game():
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.hand1: Optional[Hand]
|
||||
self.hand1 = None
|
||||
self.hand2: Optional[Hand]
|
||||
self.hand2 = None
|
||||
|
||||
def play(self):
|
||||
def play(self) -> int:
|
||||
# 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\
|
||||
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].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[4].suit:
|
||||
|
||||
return 1
|
||||
|
||||
# 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[3].suit and\
|
||||
self.hand2.cards[0].suit == self.hand2.cards[4].suit:
|
||||
|
||||
return -1
|
||||
|
||||
straightflush1 = 0
|
||||
@ -156,10 +175,11 @@ class Game():
|
||||
straightflush1 = 1
|
||||
|
||||
for i in range(1, 5):
|
||||
value = value + 1
|
||||
value = value.next()
|
||||
|
||||
if self.hand1.cards[i].value != value:
|
||||
straightflush1 = 0
|
||||
|
||||
break
|
||||
|
||||
# Check if player 2 has a straight flush.
|
||||
@ -170,10 +190,11 @@ class Game():
|
||||
straightflush2 = 1
|
||||
|
||||
for i in range(1, 5):
|
||||
value = value + 1
|
||||
value = value.next()
|
||||
|
||||
if self.hand2.cards[i].value != value:
|
||||
straightflush2 = 0
|
||||
|
||||
break
|
||||
|
||||
# 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
|
||||
|
||||
for i in range(1, 5):
|
||||
value = value + 1
|
||||
# value = value + 1
|
||||
value = value.next()
|
||||
|
||||
if self.hand1.cards[i].value != value:
|
||||
straight1 = 0
|
||||
|
||||
break
|
||||
|
||||
# Check if player 2 has a straight.
|
||||
value = self.hand2.cards[0].value
|
||||
|
||||
for i in range(1, 5):
|
||||
value = value + 1
|
||||
value = value.next()
|
||||
|
||||
if self.hand2.cards[i].value != value:
|
||||
straight2 = 0
|
||||
|
||||
break
|
||||
|
||||
# If player 1 has a straight and player 2 doesn't, player 1 wins.
|
||||
@ -443,12 +467,13 @@ class Game():
|
||||
|
||||
|
||||
@timing
|
||||
def p054():
|
||||
def p054() -> None:
|
||||
try:
|
||||
with open('p054_poker.txt', 'r', encoding='utf-8') as fp:
|
||||
games = fp.readlines()
|
||||
except FileNotFoundError:
|
||||
print('Error while opening file p054_poker.txt')
|
||||
|
||||
sys.exit(1)
|
||||
|
||||
count = 0
|
||||
|
Loading…
x
Reference in New Issue
Block a user