-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.py
182 lines (155 loc) · 8.77 KB
/
Board.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
class Game:
def random():
ai = random.randrange(1,9)
return ai
def play():
pos = ['1','2','3','4','5','6','7','8','9']
p1, p2 = user1.role()
draw = True
friend = user1.vs()
ai_nums = Game.random()
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
if friend == True:
for i in range(5):
user = int(input("Player 1 Select your position : "))
char = str(user)
position_check = True
if char == pos[user-1]:
pos[user-1] = p1
position_check = False
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
while position_check == True:
user = int(input("Player 1) please select emply position : "))
char = str(user)
if char == pos[user-1]:
pos[user-1] = p1
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
break
if (p1 == pos[0]) and (p1== pos[1]) and (p1== pos[2]) or (p1 == pos[3]) and (p1== pos[4]) and (p1== pos[5]):
print("Player 1) Wins")
draw = False
break
elif (p1 == pos[6]) and (p1== pos[7]) and (p1== pos[8]) or (p1 == pos[0]) and (p1== pos[3]) and (p1== pos[6]) or (p1 == pos[1]) and (p1 == pos[4]) and (p1 == pos[7]):
print("Player 1) Wins")
draw = False
break
elif (p1 == pos[0]) and (p1== pos[4]) and (p1== pos[8]) or (p1 == pos[2]) and (p1== pos[4]) and (p1== pos[6]) or (p1 == pos[2]) and (p1== pos[5]) and (p1== pos[8]):
print("Player 1) Wins")
draw = False
break
else:
if i < 4:
user2 = int(input("Player 2 select your position : "))
char2 = str(user2)
position_check = True
if char2 == pos[user2-1]:
pos[user2-1] = p2
position_check = False
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
while position_check == True:
user2 = int(input("Player 2) please select emply position : "))
char2 = str(user2)
if char2 == pos[user2-1]:
pos[user2-1] = p2
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
break
if (p2 == pos[0]) and (p2== pos[1]) and (p2== pos[2]) or (p2 == pos[3]) and (p2== pos[4]) and (p2== pos[5]):
print("Player 2) Wins")
draw = False
break
elif (p2 == pos[6]) and (p2== pos[7]) and (p2== pos[8]) or (p2 == pos[0]) and (p2== pos[3]) and (p2== pos[6]) or (p2 == pos[1]) and (p2 == pos[4]) and (p2 == pos[7]):
print("Player 2) Wins")
draw = False
break
elif (p2 == pos[0]) and (p2== pos[4]) and (p2== pos[8]) or (p2 == pos[2]) and (p2== pos[4]) and (p2== pos[6]) or (p2 == pos[2]) and (p2== pos[5]) and (p2== pos[8]):
print("Player 2) Wins")
draw = False
break
if i == 4 and draw == True:
print("Draw")
else:
for i in range(5):
user = int(input("Player 1 Select your position : "))
char = str(user)
position_check = True
if char == pos[user-1]:
pos[user-1] = p1
position_check = False
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
while position_check == True:
user = int(input("Player 1) please select emply position : "))
char = str(user)
if char == pos[user-1]:
pos[user-1] = p1
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
break
if (p1 == pos[0]) and (p1== pos[1]) and (p1== pos[2]) or (p1 == pos[3]) and (p1== pos[4]) and (p1== pos[5]):
print("Player 1) Wins")
draw = False
break
elif (p1 == pos[6]) and (p1== pos[7]) and (p1== pos[8]) or (p1 == pos[0]) and (p1== pos[3]) and (p1== pos[6]) or (p1 == pos[1]) and (p1 == pos[4]) and (p1 == pos[7]):
print("Player 1) Wins")
draw = False
break
elif (p1 == pos[0]) and (p1== pos[4]) and (p1== pos[8]) or (p1 == pos[2]) and (p1== pos[4]) and (p1== pos[6]) or (p1 == pos[2]) and (p1== pos[5]) and (p1== pos[8]):
print("Player 1) Wins")
draw = False
break
else:
if i < 4:
Game.random()
num = Game.random()
a = Game.random()
char2 = str(a)
position_check = True
if char2 == pos[num-1]:
pos[Game.random()-1] = p2
position_check = False
print("AI Turn")
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
while position_check == True:
Game.random()
num = Game.random()
a = Game.random()
char2 = str(a)
if char2 == pos[num-1]:
pos[num-1] = p2
print("AI Turn")
print(pos[0]+" | "+pos[1]+" | "+pos[2]+"\
\n--|---|--\n"+pos[3]+" | "+pos[4]+" | "+pos[5]+"\
\n--|---|--\n"+pos[6]+" | "+pos[7]+" | "+pos[8]+"\n\n")
break
if (p2 == pos[0]) and (p2== pos[1]) and (p2== pos[2]) or (p2 == pos[3]) and (p2== pos[4]) and (p2== pos[5]):
print("AI Wins")
draw = False
break
elif (p2 == pos[6]) and (p2== pos[7]) and (p2== pos[8]) or (p2 == pos[0]) and (p2== pos[3]) and (p2== pos[6]) or (p2 == pos[1]) and (p2 == pos[4]) and (p2 == pos[7]):
print("AI Wins")
draw = False
break
elif (p2 == pos[0]) and (p2== pos[4]) and (p2== pos[8]) or (p2 == pos[2]) and (p2== pos[4]) and (p2== pos[6]) or (p2 == pos[2]) and (p2== pos[5]) and (p2== pos[8]):
print("AI Wins")
draw = False
break
if i == 4 and draw == True:
print("Draw")
ai_f = input("Please select whether you wnat to play with AI or Friend: ").upper()
x_or_o = input("Player1 please select to be O's or X's : ").upper()
user1 = Assigned(ai_f,x_or_o)
Game.play()
Re_run.test_re_play()
print(user1.vs())