-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXOwithAI.py
200 lines (175 loc) · 5.24 KB
/
XOwithAI.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
191
192
193
194
195
196
197
198
199
200
import random
import time
# Global ----
turn = ''
shown_turn = ''
winner= ''
check_if_win = False
check_if_tie = False
AI_win = False
board_copy = []
Remaining_plays = []
corners = []
board = ["-", "-", "-",
"-", "-", "-",
"-", "-", "-"]
def Play_game():
global turn
while check_if_win == False or check_if_tie == False:
check_trun = False
print_board = board[0] + " | " + board[1] + " | " + board[2] +"\n"+\
board[3] + " | " + board[4] + " | " + board[5] +"\n"+\
board[6] + " | " + board[7] + " | " + board[8]
print(print_board)
CPU1 = 0
cpu_play= 0
Check_Winner()
check_tie()
if winner == 'X' or 'O' and check_if_win == True:
print("Yay " + winner + " won.")
break
elif check_if_tie == True:
print("Tie :(")
break
if shown_turn == 'X' or shown_turn == '':
Player = input("Enter a number between 1 to 9")
while Player not in ["1","2","3","4","5","6","7","8","9"]:
Player= input("only numbers between 1 to 9")
while check_trun == False:
if board[int(Player) -1 ] == '-':
check_trun = True
board[int(Player) - 1] = Switch_Turn(turn)
else:
print("Used Space try again")
break
#AI turn
elif shown_turn == 'O' and check_if_win == False:
time.sleep(0.3)
CPU1 = biscuit(cpu_play)
AI_play = CPU1 +1
print("AI played on "+str(AI_play) +" slot")
board[int(CPU1)] = Switch_Turn(turn)
def biscuit(play):
global corners
global Remaining_plays
board_copy = board[:]
Remaining_plays = []
# check for posible losing/wining spots
ai_play = 0
loop_number = 0
for xo in ['O', 'X']:
loop_number = 0
board_copy = board[:]
#add all remaing empty stop to an arry for the next loop
for i in board_copy:
if board_copy[loop_number] == '-':
Remaining_plays.append(loop_number)
loop_number += 1
#loop using through all the raiming plays and check if one is losing / wining.
for i in Remaining_plays:
board_copy = board[:]
board_copy[i] = xo
if AI_check_for_win(board_copy):
ai_play = i
Remaining_plays = []
return ai_play
#add loop through corners and return one randomly if there is any
corners = []
for i in [0, 2, 6, 8]:
if board[i] == '-':
corners.append(i)
if len(corners) > 0:
return random.choice(corners)
#if non of the up loops returned any check the middle spot
if board[4] == '-':
return 4
#if nothing is posible loop and return anything
Remaining_plays= []
loop_number = 0
for i in board:
if board[loop_number] == '-':
Remaining_plays.append(loop_number)
loop_number += 1
for i in Remaining_plays:
if board[i] == '-':
return i
def Switch_Turn(turn1):
global turn
global shown_turn
if turn1 == 'X':
turn = 'O'
shown_turn = 'X'
return 'O'
else:
turn = 'X'
shown_turn = 'O'
return 'X'
def Check_Winner():
global winner
global check_if_win
#Rows --
if board[0] == board[1] == board[2] != "-":
winner = board[0]
check_if_win = True
return
elif board[3] == board[4] == board[5] != "-":
winner= board[3]
check_if_win = True
return
elif board[6] == board[7] == board[8] != "-":
winner = board[6]
check_if_win = True
return
#Col --
if board[0] == board[3] == board[6] != "-":
winner = board[0]
check_if_win = True
return
elif board[1] == board[4] == board[7] != "-":
winner = board[1]
check_if_win = True
return
elif board[2] == board[5] == board[8] != "-":
winner = board[2]
check_if_win = True
return
#digno --
if board[0] == board[4] == board[8] != "-":
winner = board[0]
check_if_win = True
return
elif board[2] == board[4] == board[6] != "-":
winner = board[2]
check_if_win = True
return
else:
return
def AI_check_for_win(c_board):
#Rows --
if c_board[0] == c_board[1] == c_board[2] != "-" :
return True
elif c_board[3] == c_board[4] == c_board[5] != "-" :
return True
elif c_board[6] == c_board[7] == c_board[8] != "-" :
return True
#Col --
if c_board[0] == c_board[3] == c_board[6] != "-" :
return True
elif c_board[1] == c_board[4] == c_board[7] != "-":
return True
elif c_board[2] == c_board[5] == c_board[8] != "-":
return True
#digno --
if c_board[0] == c_board[4] == c_board[8] != "-":
return True
elif c_board[2] == c_board[4] == c_board[6] != "-":
return True
else:
return False
def check_tie():
global check_if_tie
for i in board:
if i == '-':
return
check_if_tie = True
Play_game()