-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe2.py
61 lines (54 loc) · 1.84 KB
/
tictactoe2.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
class Tictactoe:
def __init__(self):
self._board=[[' ']*3 for j in range(3)]
self._player="X"
def mark(self,i,j):
if not 0<=i<=2 and 0<=j<=2:
raise ValueError("Index out of bounds")
elif self._board[i][j]!=' ':
raise ValueError("Position is already occupied")
elif self.winner() is not None:
raise ValueError("Game is already completed")
else:
self._board[i][j]=self._player
if self._player=="X":
self._player="O"
else:
self._player="X"
def _is_win(self,mark):
board=self._board
return(mark==board[0][0]==board[0][1]==board[0][2] or
mark==board[1][0]==board[1][1]==board[1][2] or
mark==board[2][0]==board[2][1]==board[2][2] or
mark==board[0][0]==board[1][0]==board[2][1] or
mark==board[0][1]==board[1][1]==board[2][1] or
mark==board[0][2]==board[1][2]==board[2][2] or
mark==board[0][0]==board[1][1]==board[2][2] or
mark==board[0][2]==board[1][1]==board[2][0])
def checker(self):
for mark in "XO":
if self._is_win(mark):
return True
else:
return False
def winner(self):
for mark in "XO":
if self._is_win(mark):
return mark
return None
def __str__(self):
rows=['|'.join(self._board[r]) for r in range(3)]
return '\n-----\n'.join(rows)
game=Tictactoe()
#code to play it on the terminal
y=0
while y==0:
a,b=input("Enter your next move:").split()
game.mark(int(a),int(b))
print(game)
if game.checker():
print(game.winner(),"wins this game")
y=1
else:
pass
print()