-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictac_test.py
219 lines (175 loc) · 6.47 KB
/
tictac_test.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import string
field = []
player_x_win = 0
player_o_win = 0
def print_field(): # print empty field with nested lists
alphabet = string.ascii_lowercase
print("\n" * 50)
for i in range(field_size):
field.append([" "])
for j in range(field_size):
field[i].append(" ")
line = "| "
letters = " "
for i in range(field_size):
letters += alphabet[i] + " "
print(letters)
for j in range(field_size):
for i in range(field_size):
line = line + str(field[i][j]) + " | "
line = line + str(j + 1)
print(line)
line = "| "
def move_interpreter(m):
alphabet = string.ascii_lowercase
x = int(alphabet.index(m[0]))
y = int(m[1]) - 1
coordinates = [x, y]
return coordinates
def check_empty(m):
if m == "start":
check_empty = False
else:
check_empty = False
coordinates = move_interpreter(m)
if field[coordinates[0]][coordinates[1]] == " ":
check_empty = True
return check_empty
def full_check():
full = True
for i in range(field_size):
for j in range(field_size):
if field[i][j] == " ":
full = False
return full
def move(player, m):
coordinates = move_interpreter(m)
field[coordinates[0]][coordinates[1]] = player
def win_check():
winner = " "
won = False
for i in range(field_size):
for j in range(field_size):
if field[i][j] == "X" or field[i][j] == "O":
try:
if field[i][j] == field[i+1][j] and field[i][j] == field[i+2][j]:
won = True
elif field[i][j] == field[i][j+1] and field[i][j] == field[i][j+2]:
won = True
elif field[i][j] == field[i+1][j+1] and field[i][j] == field[i+2][j+2]:
won = True
elif field[i][j] == field[i+1][j-1] and field[i][j] == field[i+2][j-2]:
won = True
except IndexError:
continue
return won
def clear_board():
for i in range(field_size):
for j in range(field_size):
field[i][j] = " "
def load_game():
global field_size
global player_x_win
global player_o_win
while True:
field_size = input("\nSet field size (3x3 - 8x8). Enter only one digit: ")
if field_size.isnumeric() == True:
field_size = int(field_size)
if field_size in range(3, 9):
break
else:
print("Please enter a valid value!")
continue
else:
print("Please enter a digit between 3 and 8!")
continue
print_field()
winner = " "
m = "start"
while True:
while check_empty(m) == False:
while True:
m = str(input(player_x + " move? (X) -- for exit press \"r\" "))
if m == "r":
print("\nThe final score is: " + player_x + ": " + str(player_x_win) + " - " + player_o + ": " + str(player_o_win))
print("Thank you for playing. Good bye!")
exit()
else:
if len(m) == 2 and m[0].isalpha() and m[1].isnumeric():
coordinates = move_interpreter(m)
if coordinates[0] < field_size and coordinates[1] < field_size:
break
else:
print("Please enter a valid field position!")
continue
else:
print("Please enter a valid field position!")
print("This position is already taken. Please choose another one.")
move("X", m)
print_field()
if win_check() == True:
player_x_win += 1
print(player_x + " won!")
print(player_x + ": " + str(player_x_win) + " - " + player_o + ": " + str(player_o_win))
clear_board()
display_menu()
if full_check() == True:
print("DRAW")
clear_board()
display_menu()
while check_empty(m) == False:
while True:
m = str(input(player_o + " move? (O) -- for exit press \"r\" "))
if m == "r":
print("\nThe final score is: " + player_x + ": " + str(player_x_win) + " - " + player_o + ": " + str(player_o_win))
print("Thank you for playing. Good bye!")
exit()
else:
if len(m) == 2 and m[0].isalpha() and m[1].isnumeric():
coordinates = move_interpreter(m)
if coordinates[0] < field_size and coordinates[1] < field_size:
break
else:
print("Please enter a valid field position!")
continue
else:
print("Please enter a valid field position!")
print("This position is already taken. Please choose another one.")
move("O", m)
print_field()
if win_check() == True:
player_o_win += 1
print(player_o + " won!")
print(player_x + ": " + str(player_x_win) + " - " + player_o + ": " + str(player_o_win))
clear_board()
display_menu()
if full_check() == True:
print("Draw.")
clear_board()
display_menu()
def display_menu():
while True:
print("\ns: Start game")
print("q: Quit game\n")
user_input = str(input("Select menu item: "))
if user_input == "s":
load_game()
elif user_input == "q":
print("\nThe final score is: " + player_x + ": " + str(player_x_win) + " - " + player_o + ": " + str(player_o_win))
print("Good bye!")
exit()
elif user_input == "r":
continue
def add_name():
global player_x
global player_o
player_x = input("Please enter Player X name: ")
while True:
player_o = input("Please enter Player O name: ")
if player_o == player_x:
print("This name is occuped. Please enter another one.")
continue
else:
break
display_menu()
add_name()