-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXO_game.py
65 lines (56 loc) · 2.21 KB
/
XO_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
class Game(object):
cells = [0,1,2,3,4,5,6,7,8]
def put(self, where, what):
self.cells[where] = what
def show(self):
for y in range(3):
print(self.cells[3*y], self.cells[3*y+1], self.cells[3*y+2], sep=" | ")
if(y < 2):
print("-"*9)
def ask(self, which):
user_n = input(f'{which}-player, choose a cell for your move and write its number\n')
try:
n = int(user_n)
except ValueError:
print("Please, write an integer number")
return self.ask(which)
if(n < 0 or n > 8):
print("Your number should be between 0 and 8")
return self.ask(which)
if self.cells[n] == "O" or self.cells[n] == "X":
print("This cell has already taken. Try another one")
return self.ask(which)
self.put(n,which)
def result(self):
if self.cells[0] == self.cells[1] == self.cells[2]:
return f"{self.cells[0]}-player wins"
if self.cells[3] == self.cells[4] == self.cells[5]:
return f"{self.cells[3]}-player wins"
if self.cells[6] == self.cells[7] == self.cells[8]:
return f"{self.cells[6]}-player wins"
if self.cells[0] == self.cells[3] == self.cells[6]:
return f"{self.cells[0]}-player wins"
if self.cells[1] == self.cells[4] == self.cells[7]:
return f"{self.cells[1]}-player wins"
if self.cells[2] == self.cells[5] == self.cells[8]:
return f"{self.cells[2]}-player wins"
if self.cells[0] == self.cells[4] == self.cells[8]:
return f"{self.cells[0]}-player wins"
if self.cells[2] == self.cells[4] == self.cells[6]:
return (f'{self.cells[2]}-player wins')
if any(map(lambda x : isinstance(x, int), self.cells)):
return "not end"
return "Draw"
def play(self):
whose_turn = "X"
while self.result() == "not end":
self.show()
self.ask(whose_turn)
if whose_turn == "X":
whose_turn = "O"
else:
whose_turn = "X"
self.show()
print(self.result())
# first_game = Game()
# first_game.play()