-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule_5
129 lines (99 loc) · 4.38 KB
/
module_5
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
def game():
from random import choice
win = False
player = ''
a = '_________'
def game_bord():
return '''
1 2 3
---------
3 | {} {} {} |
2 | {} {} {} |
1 | {} {} {} |
---------'''.format(a[0], a[1], a[2], a[3], a[4], a[5], a[6],a[7], a[8])
print(game_bord())
# this is to chose who will play first
# s = x's turn o = o's turn
o = 0
s = 0
turn = choice('so')
if turn == 'o':o += 1;print('O will play first')
elif turn == 's':s += 1;print('X will play first')
while True:
if o:player = 'O';o -= 1;s += 1
else:player = 'X';s -= 1;o += 1
def coordinate():
while player:
nonlocal a
coo_=input('Enter the coordinates {}:'.format(player))
#to count the number of inputs
reply = 'you should enter two coordinate, example:1 1 or 2 3 or 3 3'
if (len(coo_.split())) != 2:print(reply);continue
else:x,y = coo_.split()
# to check if our input coordinate is good to work with
# to check if our input(coordinate) are numbers
try:int(x) and int(y)
except ValueError:print('You should enter numbers!');continue
# to check if our input(coordinate) are in range 1 to 3
coo_range = 'Coordinates should be from 1 to 3!'
if int(x) not in range(1, 4) or int(y) not in range(1, 4):print(coo_range);continue
def occupied_cell():
print('This cell is occupied! Choose another one!');coordinate()
# if the input is a good coordinate to work with, processing the coordinate/position
a_ =[x, y]
if a_ == list('13'):
if a[0] == '_':a = list(a);a[0] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('23'):
if a[1] == '_':a = list(a);a[1] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('33'):
if a[2] == '_':a = list(a);a[2] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('12'):
if a[3] == '_':a = list(a);a[3] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('22'):
if a[4] == '_':a = list(a);a[4] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('32'):
if a[5] == '_':a = list(a);a[5] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('11'):
if a[6] == '_':a = list(a);a[6] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('21'):
if a[7] == '_':a = list(a);a[7] = player;''.join(a);print(game_bord())
else:occupied_cell()
elif a_ == list('31'):
if a[8] == '_':a = list(a);a[8] = player;''.join(a);print(game_bord());
else:occupied_cell()
break
coordinate()
# wining conditions
def winner():
if win:print(player, "wins")
# vertical win
#a = list(a)
if a[0] == a[3] == a[6] == player:win = True;winner();break
elif a[1] == a[4] == a[7] == player:win = True;winner();break
elif a[2] == a[5] == a[8] == player:win = True;winner();break
# horizontal win
elif a[0] == a[1] == a[2] == player:win = True;winner();break
elif a[3] == a[4] == a[5] == player:win = True;winner();break
elif a[6] == a[7] == a[8] == player:win = True;winner();break
# diagonal win
elif a[6] == a[4] == a[2] == player:win = True;winner();break
elif a[0] == a[4] == a[8] == player:win = True;winner();break
# for draw conditions
blanks = a.count('_')
if blanks == 0:
if not win:print('Draw');break
continue
game()
while True:
ans = input('Would you like to play again ? Enter y for Yes and n for No: ')
print(ans)
if ans == 'y':game()
elif ans == 'n':break
else:continue