-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
190 lines (153 loc) · 7.31 KB
/
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
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
import os
import curses
from curses.textpad import rectangle
class tic_tac_toe:
def __init__(self) -> None:
self.players = [{"name" : "", "sign" : 'X'}, {"name" : "", "sign" : 'O'}]
self.play_board = ["---", "---", "---"]
self.option = [int(0), int(0)]
def start(self, stdscr):
os.system("clear")
options = ["New Game", "Quit "]
while (True):
attributes = {}
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
attributes['normal'] = curses.color_pair(1)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
attributes['highlighted'] = curses.color_pair(2)
c = 0
option = 0
while c != 10:
curses.curs_set(0)
stdscr.addstr(" ▄ ▀ ▄ ▄ \n")
stdscr.addstr(" ▄▄█▄▄ ▄▄▄ ▄▄▄ ▄▄█▄▄ ▄▄▄ ▄▄▄ ▄▄█▄▄ ▄▄▄ ▄▄▄ \n")
stdscr.addstr(" █ █ █▀ ▀ █ ▀ █ █▀ ▀ █ █▀ ▀█ █▀ █ \n")
stdscr.addstr(" █ █ █ ▀▀▀ █ ▄▀▀▀█ █ ▀▀▀ █ █ █ █▀▀▀▀ \n")
stdscr.addstr(" ▀▄▄ ▄▄█▄▄ ▀█▄▄▀ ▀▄▄ ▀▄▄▀█ ▀█▄▄▀ ▀▄▄ ▀█▄█▀ ▀█▄▄▀ \n\n")
for i in range(0, 2):
if i == option:
attr = attributes['highlighted']
else:
attr = attributes['normal']
stdscr.addstr("| ", curses.A_BOLD)
stdscr.addstr(options[i] + '\n', attr | curses.A_BOLD)
c = stdscr.getch()
if c == curses.KEY_UP and option > 0:
option -= 1
elif c == curses.KEY_DOWN and option < len(options) - 1:
option += 1
stdscr.erase()
if (option == 0):
self.option = [int(0), int(0)]
self.master(stdscr)
else:
return
def reset(self):
self.play_board = ["---", "---", "---"]
def is_filled(self, stdscr)->bool:
for row in self.play_board:
c = row.find('-')
if (row.find('-') != int(-1)):
return False
return True
def insert(self, stdscr, player):
while (True):
attributes = {}
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
attributes['normal'] = curses.color_pair(1)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
attributes['highlighted'] = curses.color_pair(2)
c = 0
while c != 10:
if (c == 'q' or c == 'Q'):
return [False, 0]
stdscr.erase()
stdscr.addstr("Player " + str(player + 1)+ "'s turn:\n\n\n", curses.A_BOLD)
for i in range(0, 3):
stdscr.addstr(" ")
for j in range(0, 3):
if ([i, j] == self.option):
attr = attributes['highlighted']
else:
attr = attributes['normal']
stdscr.addstr(" " + self.play_board[i][j] + " ", attr | curses.A_BOLD)
stdscr.addstr(" ")
stdscr.addstr(" ")
stdscr.addstr("\n\n\n")
rectangle(stdscr, 2, 0, 4, 4)
rectangle(stdscr, 2, 6, 4, 10)
rectangle(stdscr, 2, 12, 4, 16)
rectangle(stdscr, 5, 0, 7, 4)
rectangle(stdscr, 5, 6, 7, 10)
rectangle(stdscr, 5, 12, 7, 16)
rectangle(stdscr, 8, 0, 10, 4)
rectangle(stdscr, 8, 6, 10, 10)
rectangle(stdscr, 8, 12, 10, 16)
curses.curs_set(0)
# stdscr.addstr("\n\n")
c = stdscr.getch()
if c == curses.KEY_UP and self.option[0] > 0:
self.option[0] -= 1
elif c == curses.KEY_DOWN and self.option[0] < 2:
self.option[0] += 1
elif c == curses.KEY_LEFT and self.option[1] > 0:
self.option[1] -= 1
elif c == curses.KEY_RIGHT and self.option[1] < 2:
self.option[1] += 1
if (self.play_board[self.option[0]][self.option[1]] != '-'):
continue
else:
if (player == 0):
list1 = list(self.play_board[self.option[0]])
list1[self.option[1]] = 'X'
self.play_board[self.option[0]] = ''.join(list1)
else:
list1 = list(self.play_board[self.option[0]])
list1[self.option[1]] = '0'
self.play_board[self.option[0]] = ''.join(list1)
return [True, self.option[0] * 3 + self.option[1]]
def master(self, stdscr):
self.reset()
stdscr.clear()
# self.get_name(stdscr)
player_idx = int(0)
while (True):
curr_cond = self.insert(stdscr, player_idx)
if (curr_cond[0] is not True):
stdscr.erase()
return
if (self.game_cond(int(curr_cond[1])) is True):
stdscr.erase()
stdscr.addstr("Player " + str(player_idx + 1) + " Wins!\n\n", curses.A_ITALIC | curses.A_BOLD )
return
if (self.is_filled(stdscr) is True):
stdscr.erase()
stdscr.addstr("It ended in a draw!\n\n", curses.A_ITALIC | curses.A_BOLD)
return
player_idx = (player_idx + 1) % 2
def get_name(self, stdscr):
stdscr.addstr("Name of Player 1: ")
self.players[0]["name"] = stdscr.getstr()
stdscr.addstr("Name of Player 2: ")
self.players[1]["name"] = stdscr.getstr()
def game_cond(self, index):
row = int(index / 3)
col = int(index % 3)
if (are_equal(self.play_board[row][0], self.play_board[row][1], self.play_board[row][2])):
return True
if (are_equal(self.play_board[0][col], self.play_board[1][col], self.play_board[2][col])):
return True
if (are_equal(self.play_board[0][0], self.play_board[1][1], self.play_board[2][2])):
return True
if (are_equal(self.play_board[0][2], self.play_board[1][1], self.play_board[2][0])):
return True
return False
def are_equal(val1, val2, val3):
return (val1 == val2) and (val1 == val3) and (val1 != '-')
def main(stdscr):
game = tic_tac_toe()
game.start(stdscr)
stdscr.erase()
stdscr.addstr("Press any key to exit", curses.A_ITALIC | curses.A_BOLD)
stdscr.getch()
curses.wrapper(main)