-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
177 lines (128 loc) · 4.23 KB
/
tic_tac_toe.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
import sys
game_state = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
def draw_a_frame():
print(''.ljust(1, ' '), ''.ljust(1, '{0}'.format(*game_state)), ''.ljust(1, ' '), ''.ljust(1, '|'),
''.ljust(1, ' '),
''.ljust(1, '{1}'.format(*game_state)), ''.ljust(1, ' '), ''.ljust(1, '|'), ''.ljust(1, ' '),
''.ljust(1, '{2}'.format(*game_state)), ''.ljust(1, ' '))
print(''.ljust(21, '-'))
print(''.ljust(1, ' '), ''.ljust(1, '{3}'.format(*game_state)), ''.ljust(1, ' '), ''.ljust(1, '|'),
''.ljust(1, ' '),
''.ljust(1, '{4}'.format(*game_state)), ''.ljust(1, ' '), ''.ljust(1, '|'), ''.ljust(1, ' '),
''.ljust(1, '{5}'.format(*game_state)), ''.ljust(1, ' '))
print(''.ljust(21, '-'))
print(''.ljust(1, ' '), ''.ljust(1, '{6}'.format(*game_state)), ''.ljust(1, ' '), ''.ljust(1, '|'),
''.ljust(1, ' '),
''.ljust(1, '{7}'.format(*game_state)), ''.ljust(1, ' '), ''.ljust(1, '|'), ''.ljust(1, ' '),
''.ljust(1, '{8}'.format(*game_state)), ''.ljust(1, ' '))
draw_a_frame()
def switch_player(iter):
if iter % 2 == 0:
player = ['X']
else:
player = ['O']
return player
def get_data(player):
while True:
field_num = input(f"Player {player} select a field (1-9): ")
if not field_num.isnumeric():
print("Field value not allowed", file=sys.stderr)
continue
elif field_num not in ("1", "2", "3", "4", "5", "6", "7", "8", "9"):
print("Field value not allowed", file=sys.stderr)
continue
else:
break
return field_num
def save_game():
if player[0] == 'X':
for i in playerX:
if i in game_state:
game_state[game_state.index(i)] = 'X'
elif player[0] == 'O':
for i in playerO:
if i in game_state:
game_state[game_state.index(i)] = 'O'
else:
print("Error")
def check_game_save(player):
while True:
x = get_data(player)
if player[0] == 'X':
if x in playerX or x in playerO:
print('Field taken')
continue
else:
playerX.append(x)
save_game()
return x
elif player[0] == "O":
if x in playerX or x in playerO:
print('Field taken')
continue
else:
playerO.append(x)
save_game()
return x
winning_sets1 = ['1', '2', '3']
winning_sets2 = ['4', '5', '6']
winning_sets3 = ['7', '8', '9']
winning_sets4 = ['1', '4', '7']
winning_sets5 = ['2', '5', '8']
winning_sets6 = ['3', '6', '9']
winning_sets7 = ['1', '5', '9']
winning_sets8 = ['3', '5', '7']
sets = [winning_sets1, winning_sets2, winning_sets3, winning_sets4, winning_sets5, winning_sets6, winning_sets7,
winning_sets8]
def check_the_result():
if player[0] == "X":
player_list = playerX
else:
player_list = playerO
for x in range(len(sets)):
bingo = 0
for _ in sets[x]:
if _ in player_list:
bingo += 1
if bingo == 3:
return player[0]
else:
continue
def return_winning_set():
if player[0] == "X":
player_list = playerX
else:
player_list = playerO
for x in range(len(sets)):
bingo = 0
for _ in sets[x]:
if _ in player_list:
bingo += 1
if bingo == 3:
return sets[x]
else:
continue
def draw():
if len(playerX) + len(playerO) == 9:
return True
else:
return
playerX = []
playerO = []
iter = 1
while True:
player = switch_player(iter)
x = check_game_save(player)
winner = check_the_result()
winning_set = return_winning_set()
print("\n")
draw_a_frame()
print("\n")
if winner:
print(f"Player {player} has won!!! Congrats!")
break
if draw():
print("Draw! Try again.")
break
print("\n")
iter += 1