-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_09_20_tic_tac.py
189 lines (147 loc) · 5.11 KB
/
01_09_20_tic_tac.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
'''
author: Moshood Olawale
credit: Christoper T.
project: Tic-Tac-Toe
'''
import os
import random
os.system("clear")
class Board():
def __init__(self):
self.cells = ["","","","","","","","","",""]
def display(self):
print(" %s | %s | %s" %(self.cells[1], self.cells[2], self.cells[3]))
print("-----------")
print(" %s | %s | %s" %(self.cells[4], self.cells[5], self.cells[6]))
print("-----------")
print(" %s | %s | %s" %(self.cells[7], self.cells[8], self.cells[9]))
def update_cell(self, cell_no, player):
if self.cells[cell_no] == "":
self.cells[cell_no] = player
#need update for player not to loss turn
def is_winner(self, player):
for strategy in [[1,2,3], [4,5,6], [7,8,9],[1,5,9],[3,5,7],[1,4,7],[2,5,8],[3,6,9]]:
result = True
for cell_no in strategy:
if self.cells[cell_no] != player:
result = False
if result == True:
return True
return False
# # if self.cells[1] == player and self.cells[2] == player and self.cells[3] == player:
# # return True
# if self.cells[4] == player and self.cells[5] == player and self.cells[6] == player:
# return True
# if self.cells[7] == player and self.cells[8] == player and self.cells[9] == player:
# return True
# if self.cells[1] == player and self.cells[5] == player and self.cells[9] == player:
# return True
# if self.cells[3] == player and self.cells[5] == player and self.cells[7] == player:
# return True
# if self.cells[1] == player and self.cells[4] == player and self.cells[7] == player:
# return True
# if self.cells[2] == player and self.cells[5] == player and self.cells[8] == player:
# return True
# if self.cells[3] == player and self.cells[6] == player and self.cells[9] == player:
# return True
# return False
def is_tie(self):
used_cells = 0
for cell in self.cells:
if cell != "":
used_cells += 1
if used_cells == 9:
return True
else:
return False
def reset(self):
self.cells = ["","","","","","","","","",""]
def ai_move(self, player):
#if the center is open, choose that
if player == "X":
enemy = "O"
if player == "O":
enemy = "X"
if self.cells[5] == "":
self.update_cell(5, player)
else:
while True:
cell_no = random.randint(1,9)
if self.cells[cell_no] == "":
self.update_cell(cell_no, player)
break
#ai can win
# for i in range(1, 10):
# if self.cells[i] == "":
# self.cells[i] = player
# if self.is_winner(player):
# return i
# else:
# self.cells[i] == ""
#ai blocks check for the center and defense the strategy down
#choose random - with quick the other player is at advantage 9-3-6/7
# for i in range(1,10):
# if self.cells[i] == "":
# self.update_cell(i, player)
# break
board = Board()
board.display()
def print_header():
print("Welcome to Tic-Tac_Toe\n")
def refresh_screen():
os.system("clear")
print_header()
board.display()
# def is_board_full(board):
# if " " in board:
# return True
# else:
# return False
while True:
refresh_screen()
# is_board_full(board)
#get X input
x_choice = int(input("\nX) Please choose 1 - 9. >")) #diff raw_input and input
#update the board
board.update_cell(x_choice, "X")
#refresh screen
refresh_screen()
#check for X win
if board.is_winner("X"):
print("\nX wins! \n")
play_again = input("Would you like to play again? (Y/N): ").upper()
if play_again == "Y":
board.reset()
continue
else:
break
if board.is_tie():
print("\n Ties! \n")
play_again = input("Would you like to play again? (Y/N): ").upper()
if play_again == "Y":
board.reset()
continue
else:
break
#get X input
# o_choice = int(input("\nO) Please choose 1 - 9. >")) #diff raw_input and input
board.ai_move("O")
#update the board
# board.update_cell(o_choice, "O") ai enabled
#check for X win
if board.is_winner("O"):
print("\nO wins! \n")
play_again = input("Would you like to play again? (Y/N): ").upper()
if play_again == "Y":
board.reset()
continue
else:
break
if board.is_tie():
print("\n Tie! \n")
play_again = input("Would you like to play again? (Y/N): ").upper()
if play_again == "Y":
board.reset()
continue
else:
break