-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
123 lines (80 loc) · 3.66 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import time
from typing import *
from card import Card
from deck import Deck
from player import Player
class Game:
def __init__(self) -> None:
self.player_one = Player(1)
self.player_two = Player(2)
self.deck = Deck()
self._deal_cards_to_players()
def _deal_cards_to_players(self) -> None:
self.deck.shuffle_deck()
self.player_one.cards.extend(self.deck.cards[:27])
self.player_two.cards.extend(self.deck.cards[27:])
def get_current_winner(self) -> None:
score_diff = self.get_score_diff()
if score_diff == 0:
print("\nYou are tied!")
elif score_diff > 0:
print(f"\nYou are up by {score_diff}")
else:
print(f"\nComputer is up by {abs(score_diff)}")
def get_score_diff(self) -> int:
return len(self.player_one.cards) - len(self.player_two.cards)
def get_final_winner(self) -> None:
score_diff = self.get_score_diff()
if score_diff == 0:
print("\nYou are tied!")
elif score_diff > 0:
print(f"\nYOU WIN!")
else:
print(f"\nCOMPUTER WINS!")
@staticmethod
def _generate_player_played_str(player_one_card: Card, player_two_card: Card) -> Tuple[str, str]:
player_one_play_str, player_two_play_str = f"\nYou play {player_one_card}!", f"Computer plays {player_two_card}!"
return player_one_play_str, player_two_play_str
def play_turn(self) -> None:
player_one_card, player_two_card = self.player_one.play_card(), self.player_two.play_card()
winnings: List[Card] = [player_one_card, player_two_card]
player_one_play_str, player_two_play_str = self._generate_player_played_str(player_one_card, player_two_card)
print(player_one_play_str)
print(player_two_play_str)
# TODO: check if this logic can happen when one player is almost out of cards
while player_one_card.value == player_two_card.value:
player_one_face_down_cards: List[Card] = [self.player_one.play_card() for _ in range(3)]
player_two_face_down_cards: List[Card] = [self.player_two.play_card() for _ in range(3)]
player_one_card: Card = self.player_one.play_card()
player_two_card: Card = self.player_two.play_card()
winnings.extend([*player_one_face_down_cards, *player_two_face_down_cards, player_one_card, player_two_card])
player_one_play_str, player_two_play_str = self._generate_player_played_str(player_one_card, player_two_card)
print(player_one_play_str)
print(player_two_play_str)
if player_one_card.value > player_two_card.value:
print("\nYou win this turn!")
self.player_one.cards.extend(winnings)
else:
print("\nComputer wins this turn!")
self.player_two.cards.extend(winnings)
self.get_current_winner()
if __name__ == '__main__':
play_game = True
game = Game()
print("Hi welcome to War! Would you like to play this game in interactive mode? [Enter] or N: ")
im = input()
interactive_mode = False if im.upper() == 'N' else True
while play_game:
game.play_turn()
# set play_game to False if one player had 0 cards
if game.get_score_diff() == abs(52):
play_game = False
if interactive_mode:
print('\nPlay another turn? [Enter] or N: ')
play_again = input()
play_game = False if play_again.upper() == 'N' else True
else:
time.sleep(3.5)
os.system('cls' if os.name == 'nt' else 'clear')
game.get_final_winner()