-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.py
More file actions
181 lines (138 loc) · 3.95 KB
/
Copy pathtictactoe.py
File metadata and controls
181 lines (138 loc) · 3.95 KB
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
#Joshua Prpic
#tictactoe
import random
def chkWinner(board):
winner = False
for i in range(0, 3, 1):
# print(i)
if(board[i] == "x" and board[i + 3] == "x" and board[i + 6] == "x"):
print ("x wins!")
winner = True
break
if(board[i] == "o" and board[i + 3] == "o" and board[i + 6] == "o"):
print ("o wins!")
winner = True
break
for i in range(0, 9, 3):
if(board[i] == "x" and board[i + 1] == "x" and board[i + 2] == "x"):
print ("x wins!")
winner = True
break
if(board[i] == "o" and board[i + 1] == "o" and board[i + 2] == "o"):
print ("o wins!")
winner = True
break
# if(winner):
# return True
if(board[0] == "x" and board[4] == "x" and board[8] == "x"):
print ("x wins!")
return True
if(board[2] == "x" and board[4] == "x" and board[6] == "x"):
print ("x wins!")
return True
if(board[0] == "o" and board[4] == "o" and board[8] == "o"):
print ("o wins!")
return True
if(board[2] == "o" and board[4] == "o" and board[6] == "o"):
print ("o wins!")
return True
return winner
def ai(board):
# first iteration is checking win, second is to check blocks
# 3 conditions per row x x o | x o x | o x x
place = -1
for i in range(0, 2, 1):
turn = "o"
if(i == 1):
turn = "x"
for j in range(0, 3, 1):
# print(j)
if(board[j] == turn and board[j + 3] == turn and empty(board, (j + 6))):
place = j + 6
break
if(board[j] == turn and board[j + 6] == turn and empty(board, (j + 3))):
place = j + 3
break
if(board[j + 3] == turn and board[j + 6] == turn and empty(board, j)):
place = j
break
for j in range(0, 9, 3):
# print(j)
if(board[j] == turn and board[j + 1] == turn and empty(board, (j + 2))):
place = j + 2
break
if(board[j] == turn and board[j + 2] == turn and empty(board, (j + 1))):
place = j + 1
break
if(board[j + 1] == turn and board[j + 2] == turn and empty(board, j)):
place = j
break
if(place == -1):
if(board[0] == turn and board[4] == turn and empty(board, 8)):
place = 8
if(board[0] == turn and board[8] == turn and empty(board, 4)):
place = 4
if(board[4] == turn and board[8] == turn and empty(board, 0)):
place = 0
if(board[2] == turn and board[4] == turn and empty(board, 6)):
place = 6
if(board[2] == turn and board[6] == turn and empty(board, 4)):
place = 4
if(board[4] == turn and board[6] == turn and empty(board, 2)):
place = 2
if(place != -1):
return place
new = True
while(new):
num = random.randint(0,8)
if(board[num] == "x" or board[num] == "o"):
new = True
else:
new = False
# print("Rand")
return num
def empty(board, spot):
taken = True
if(board[spot] == "x" or board[spot] == "o"):
taken = False
return taken
print("Welcome to tick tack toe.")
board = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
print(board[0] + " | " + board[1] + " | " + board[2])
print("---------")
print(board[3] + " | " + board[4] + " | " + board[5])
print("---------")
print(board[6] + " | " + board[7] + " | " + board[8] + "\n")
#turn: True -> x first False -> o first
turn = True
tie = True
count = 9
for x in range(count):
if(turn):
print("User: ")
uInput = int(input())
uInput -= 1
else:
uInput = ai(board)
display = uInput + 1
print("CPU: \n" + str(display))
if(board[uInput] == "x" or board[uInput] == "o"):
count += 1
else:
if(turn):
board[uInput] = "x"
turn = False
else:
board[uInput] = "o"
turn = True
print(board[0] + " | " + board[1] + " | " + board[2])
print("---------")
print(board[3] + " | " + board[4] + " | " + board[5])
print("---------")
print(board[6] + " | " + board[7] + " | " + board[8] + "\n")
if(chkWinner(board)):
# print("things")
tie = False
break
if(tie):
print("Tie game")