-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
194 lines (161 loc) · 5.72 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import random as r
class TicTacToe:
def __init__(self):
# self.board = [" " for _ in range(3)]
# self.current_winner = None
self.board = {'7': ' ', '8': ' ', '9': ' ',
'4': ' ', '5': ' ', '6': ' ',
'1': ' ', '2': ' ', '3': ' '}
def print_board(self):
print(self.board['7'] + ' | ' + self.board['8'] + ' | ' + self.board['9'])
print('--+---+--')
print(self.board['4'] + ' | ' + self.board['5'] + ' | ' + self.board['6'])
print('--+---+--')
print(self.board['1'] + ' | ' + self.board['2'] + ' | ' + self.board['3'])
class human_player(TicTacToe):
def __init__(self):
super().__init__()
def get_user_input(self):
entry = input("Where do you want to mark?")
return entry
def get_computer_input(self):
r1 = r.randint(1, 9)
for i in range(10):
if r1 not in comp_inp_list:
break
else:
r1 = r.randint(1, 9)
comp_inp_list.append(r1)
return str(r1)
def human_player_turn(self, user_input):
end_loop = True
while end_loop:
if self.board[user_input] == ' ':
end_loop = False
self.board[user_input] = "X"
self.print_board()
else:
user_input = input("Already filled, use a different place to mark?")
# print("Already filled, use a different place to mark")
def computer_turn(self, user_input):
end_loop = True
while end_loop:
if self.board[user_input] == ' ':
end_loop = False
self.board[user_input] = "O"
self.print_board()
else:
print("Looks like its already filled, I am choosing a different place to mark")
user_input = self.get_computer_input()
def find_winner(self,turn):
if self.board['7'] == self.board['8'] == self.board['9'] != ' ': # across the top
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['4'] == self.board['5'] == self.board['6'] != ' ': # across the middle
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['1'] == self.board['2'] == self.board['3'] != ' ': # across the bottom
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['1'] == self.board['4'] == self.board['7'] != ' ': # down the left side
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['2'] == self.board['5'] == self.board['8'] != ' ': # down the middle
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['3'] == self.board['6'] == self.board['9'] != ' ': # down the right side
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['7'] == self.board['5'] == self.board['3'] != ' ': # diagonal
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
elif self.board['1'] == self.board['5'] == self.board['9'] != ' ': # diagonal
# self.print_board()
print("\nGame Over.\n")
print(" **** " + turn + " won. ****")
winner = True
return winner
tk = TicTacToe()
comp_inp_list = []
# def get_user_input():
# entry = input("Where do you want to mark?")
# return entry
#
# def get_computer_input():
# r1 = r.randint(1,9)
# for i in range(10):
# if r1 not in comp_inp_list:
# break
# else:
# r1 = r.randint(1,9)
#
# comp_inp_list.append(r1)
# return str(r1)
print("Assume you are X, O is played by the computer")
starter = ""
hp = human_player()
while starter != "X" or starter != "O":
starter = str(input("Who wants to start X or O?"))
if starter == "X" or starter == "O":
break
turns = 1
result = False
while turns<10:
if starter == "X":
if turns%2 != 0:
print("Xs turn")
inp = hp.get_user_input()
hp.human_player_turn(inp)
result = hp.find_winner("X")
if result is True:
break
turns = turns + 1
else:
print("Os turn")
cp_inp = hp.get_computer_input()
whose_turn = "O"
hp.computer_turn(cp_inp)
result = hp.find_winner("O")
if result is True:
break
turns = turns+1
else:
if turns % 2 != 0:
print("Os turn")
cp_inp = hp.get_computer_input()
hp.computer_turn(cp_inp)
result = hp.find_winner("O")
if result is True:
break
turns = turns + 1
else:
print("Xs turn")
inp = hp.get_user_input()
hp.human_player_turn(inp)
result = hp.find_winner("X")
if result is True:
break
turns = turns + 1
if turns == 10:
print("It is a tie")