-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
96 lines (74 loc) · 2.71 KB
/
main.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
from itertools import combinations
row = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
points = [2, 7, 6, 9, 5, 1, 4, 3, 8]
# Initializes a board and is updated later on to show where a sign was put in.
def current_board():
string = ""
for j in range(9):
string += f"|{row[j]}"
if j == 2 or j == 5 or j == 8:
string += "|\n" + "--+-+--\n"
return string
# Check for correct inputs. Prevents signs to be overwritten.
def input_is_not_correct(x):
if int(x) not in points:
print(
"Your input is incorrect, try again. It should be 'X'.Range (1-9). "
)
return True
if row[int(x) - 1] != " ":
print("This spot is already filled. Please choose other one. ")
return True
# Defines which players turn is it.
def where_to_input_sign(player):
# Ask for current player's sign:
x = input(f"Where do you want to put your {player}. Range(1-9): ")
return x
# Should be cleaned up. Game flow is going through here.
def tic_tac_toe():
# Players' collected points
points_x_lst = []
points_o_lst = []
print(current_board())
game_is_on = True
while game_is_on:
# When true it is "X" turn
is_turn_x = True
if is_turn_x:
player = "X"
# Asks where to put "O" sign
x = where_to_input_sign(player)
sign_on_board(x, player)
# Appends player's collected points.
points_x_lst.append(points[int(x) - 1])
if check_score(points_x_lst, player):
return True
# Switches turn to "O" player
is_turn_x = False
# Player's "O" turn
if not is_turn_x:
player = "O"
# Asks wgere to put "O" sign
x = where_to_input_sign(player)
sign_on_board(x, player)
# Appends player's collected points.
points_o_lst.append(points[int(x) - 1])
if check_score(points_o_lst, player):
return True
# Put sign onto the board.
def sign_on_board(x, player):
while input_is_not_correct(x):
x = where_to_input_sign(player)
row[int(x) - 1] = player
def check_score(point_list, player):
print(current_board())
if len(point_list) >= 3:
# Goes through possible combinations of points in threes. And sums them up to see if they add up to 15. If they do. Player wins.
if any(sum(comb) == 15 for comb in combinations(point_list, 3)):
print(f"Player with {player} won!")
return True
# If game is not resolved untill board is filled it is a draw.
if len(point_list) >= 5:
print(f"It's a draw!")
return True
tic_tac_toe()