This repository has been archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2_tic_tac_toe.py
164 lines (129 loc) · 4.61 KB
/
task2_tic_tac_toe.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
#! /usr/bin/env python
from unittest.mock import patch
from io import StringIO
import unittest
class TicTacToe:
def __init__(self):
self.__grid = [
0, 0, 0,
0, 0, 0,
0, 0, 0
]
self.__end = False
@property
def grid(self):
return self.__grid
@grid.setter
def grid(self, value):
assert(isinstance(value, list))
assert(len(value) == 9)
self.__grid = value
def draw_grid(self):
print("Current grid: Model:")
for i in range(3):
cell1 = self.__grid[i*3] if self.__grid[i*3] else " "
cell2 = self.__grid[i*3 + 1] if self.__grid[i*3 + 1] else " "
cell3 = self.__grid[i*3 + 2] if self.__grid[i*3 + 2] else " "
cell1_t = i*3 if not self.__grid[i*3] else " "
cell2_t = i*3 + 1 if not self.__grid[i*3 + 1] else " "
cell3_t = i*3 + 2 if not self.__grid[i*3 + 2] else " "
strf = f" {cell1} █ {cell2} █ {cell3} {cell1_t} █ {cell2_t} █ {cell3_t}"
print(strf)
if i < 2:
print("■■■█■■■█■■■ ■■■█■■■█■■■")
def check_win(self):
res = False
options = [
(0, 1, 2),
(0, 3, 6),
(0, 4, 8),
(6, 7, 8),
(6, 4, 2),
(2, 5, 8)
]
for option in options:
res = res or self.check_pos(*option)
return res
def check_pos(self, pos0, pos1, pos2):
mark = self.__grid[pos0]
return mark != 0 and mark == self.__grid[pos1] and mark == self.__grid[pos2]
def check_draw(self):
res = True
for cell in self.__grid:
res = res and cell
return bool(res)
def play(self):
player1_mark = ''
draw = False
while player1_mark != 'X' and player1_mark != 'O':
player1_mark = input("Choose player1's mark['X' or 'O']: ")
if player1_mark != 'X' and player1_mark != 'O':
print("___________ERROR: Wrong mark")
mark = player1_mark
while not self.__end:
self.draw_grid()
#self.draw_input_grid()
if mark == player1_mark:
cell = input(f"Player1 '{mark}' choose your cell: ")
else:
cell = input(f"Player2 '{mark} choose your cell: ")
try:
cell = int(cell)
except ValueError:
print(f"__________ERROR: Wrong input: {cell}")
continue
if not (0 <= cell < 9) or self.__grid[cell] != 0:
print(f"__________ERROR: Wrong number: {cell}")
continue
print("__________________________________________________")
self.__grid[cell] = mark
if self.check_win():
self.__end = True
elif self.check_draw():
self.__end = True
draw = True
else:
mark = 'X' if mark == 'O' else 'O'
if draw:
print("_____DRAW!_____")
elif mark == player1_mark:
print(f"_____PLAYER1 '{mark}' WINS!_____")
else:
print(f"_____PLAYER2 '{mark}' WINS!_____")
self.draw_grid()
class TestTicTacToe(unittest.TestCase):
def test_draw_win(self):
game = TicTacToe()
game.grid = [
'X', 'X', 'O',
'O', 'X', 'X',
'X', 'O', 'O'
]
self.assertEqual(game.check_draw(), True)
self.assertEqual(game.check_win(), False)
game.grid = [
'X', 'X', 'X',
0, 0, 0,
0, 0, 0
]
self.assertEqual(game.check_draw(), False)
self.assertEqual(game.check_win(), True)
def test_pos(self):
game = TicTacToe()
game.grid = [
'X', 'O', 'X',
'O', 'X', 'O',
'X', 'O', 'X'
]
self.assertEqual(game.check_pos(0, 1, 2), False)
self.assertEqual(game.check_pos(0, 3, 6), False)
self.assertEqual(game.check_pos(0, 4, 8), True)
def test_play(self):
inp = ['X', 0, 1, 2, 3, 4, 5, 6, 7]
with patch('builtins.input', side_effect=inp), patch('sys.stdout', new=StringIO()) as fake:
game = TicTacToe()
game.play()
if __name__ == "__main__":
#unittest.main()
Game = TicTacToe()
Game.play()