-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample.py
92 lines (68 loc) · 3.21 KB
/
Sample.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
import random
l = [[" "," "," "], [" "," "," "] , [" "," "," "]]
def update_board(l):
print(f" {l[0][0]} | {l[0][1]} | {l[0][2]}\n_______________\n {l[1][0]} | {l[1][1]} | {l[1][2]}\n_______________\n {l[2][0]} | {l[2][1]} | {l[2][2]}\n")
def check(l):
if((l[0][0] == l[0][1] == l[0][2] or l[1][0] == l[2][0] == l[0][0] )and (l[0][0] != " ")):
return " Player " + l[0][0] + " Wins "
elif((l[1][0] == l[1][1] == l[1][2] or l[0][2] == l[1][1] == l[2][0] or l[1][1] == l[2][2] == l[0][0] or l[1][0] == l[2][0] == l[0][0]) and l[1][1] != " " ):
return " Player " + l[1][1] + " Wins "
elif((l[2][0] == l[2][1] == l[2][2] or l[2][0] == l[2][1] == l[2][2])and (l[2][0] != " ")):
return " Player " + l[2][0] + " Wins "
elif(l[0][0] != " " and l [0][1] != " " and l[0][2] != " " and l[1][0] != " " and l [1][1] != " " and l[1][2] != " " and l[2][0] != " " and l [2][1] != " " and l[2][2] != " "):
return " Draw "
else:
return None
def playervplayer(l):
a = 0
update_board(l)
while (check(l) == None):
player = "x" if a%2 == 0 else "o"
a += 1
indices = [int(i) for i in input(f"For player {player} Enter the Co-od [(0,0)format]: ").split(",")]
while(l[indices[0]][indices[1]] != " "):
print("That place is already filled")
update_board(l)
indices = [int(i) for i in input(f"For player {player} Enter the Co-od [(0,0)format]: ").split(",")]
l[indices[0]][indices[1]] = player
update_board(l)
def playervcomp(l):
comp = random.choice(["x" , "o"])
player = "x" if(comp == "o") else "o"
a = 2
start = a
print(f"Computer : ",comp , " Player : ",player)
while True:
if(a % 2 == 0):
turn = comp
a+=1
else:
turn = player
a+=1
if(turn == player):
update_board(l)
ask = [int(i) for i in input(f"For player {player} Enter the Co-od [(0,0)format]: ").split(",")]
while(l[ask[0]][ask[1]] != " "):
print("That place is already filled")
update_board(l)
ask = [int(i) for i in input(f"For player {player} Enter the Co-od [(0,0)format]: ").split(",")]
l[ask[0]][ask[1]] = turn
update_board(l)
elif(turn == comp):
if(a == start+1):
moves = [[1,1] , [0,0] , [2,2] , [0,2] , [2,0]]
toplay = random.choice(moves)
l[toplay[0]][toplay[1]] = turn
update_board(l)
print("Computer Played")
else:
temp = l
for one in range(0,len(temp)):
for ten in (0,len(temp[one])):
another_temp = temp[one][ten]
temp[one][ten] = "x"
if(check(temp)):
print([one],[ten])
break
temp[one][ten] = another_temp
playervcomp(l)