-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (99 loc) · 3.57 KB
/
main.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
124
125
import random
from enum import Enum
class Winner(Enum):
player = 1
computer = -1
draw = 0
TEXT_WINNERS = {
Winner.draw: 'DRAW',
Winner.computer: 'Computer won',
Winner.player: 'Player won'
}
class UiText(Enum):
welcome = 0
rules = 1
controls = 2
computer = 3
player = 4
warning_input = 5
game_winner_draw = 6
game_winner_computer = 7
game_winner_player = 8
input_request = 9
TEXT_UI = {
UiText.welcome: 'Welcome! This is a rock-paper-scissors game.',
UiText.rules: 'Rules are simple: paper beats rock; rock beats scissors, scissors beat paper. '
'The winner is chosen after 5 rounds.',
UiText.controls: 'Enter the name of an object play a round.',
UiText.computer: 'Computer',
UiText.player: 'Player',
UiText.warning_input: 'Choose one of the following: rock, paper or scissors!',
UiText.game_winner_draw: 'WHOA! It\'s a TOTAL draw!',
UiText.game_winner_player: 'Congratulations! You\'ve WON',
UiText.game_winner_computer: 'Uh-oh... It\'s not your lucky game. Try again!',
UiText.input_request: 'Type in your choice for this round: ',
}
# stores valid computer and player choices
text_choice = ['rock', 'paper', 'scissors']
# prints out initial texts on application launch
def welcome():
print(TEXT_UI[UiText.welcome])
print(TEXT_UI[UiText.rules])
print(TEXT_UI[UiText.controls])
# rolls computer turn
def computer_choice():
computer_roll = random.choice(text_choice)
return computer_roll
# clears player input from some shit and lower cases it
def clean_input():
player_input = input(TEXT_UI[UiText.input_request])
player_input.strip(' ,.')
return player_input.casefold()
# shows status message at the end of the round
# and returns value for score
def round_end(player, computer, winner):
print(f'{player} VS {computer}. {TEXT_WINNERS[winner]}!')
return winner.value
def one_round():
player_input = clean_input()
computer_input = computer_choice()
# spellcheck of cleaned input
# checks if player_input is one of these: 'rock', 'paper', 'scissors'
while player_input not in text_choice:
print(TEXT_UI[UiText.warning_input])
player_input = clean_input()
# chooses the winner for the round based on input
# returns score adjustment: 0 for draw; 1 for player win, -1 for computer win
if player_input == computer_input:
print(f'{player_input} VS {computer_input}. DRAW!')
return 0
elif player_input == 'rock':
if computer_input == 'paper':
return round_end(player_input, computer_input, Winner.computer)
else:
return round_end(player_input, computer_input, Winner.player)
elif player_input == 'paper':
if computer_input == 'scissors':
return round_end(player_input, computer_input, Winner.computer)
else:
return round_end(player_input, computer_input, Winner.player)
elif player_input == 'scissors':
if computer_input == 'rock':
return round_end(player_input, computer_input, Winner.computer)
else:
return round_end(player_input, computer_input, Winner.player)
# game logic: 5 rounds played,
# then winner is determined based on score
def game():
score = 0
for game_round in range(1, 6):
score += one_round()
if score > 0:
print(TEXT_UI[UiText.game_winner_player])
elif score < 0:
print(TEXT_UI[UiText.game_winner_computer])
else:
print(TEXT_UI[UiText.game_winner_draw])
if __name__ == '__main__':
welcome()
game()