-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython_Tictactoe.py
164 lines (127 loc) · 3.91 KB
/
Python_Tictactoe.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
# Tic Tac Toe project
# Show the playboard to the user
import random
def welcome():
print("WELCOME TO TIC TAC TOE!!\n\n")
def display_board(matrix):
print(matrix[0])
print(matrix[1])
print(matrix[2])
def start_turn():
turn = random.randint(1, 2)
print(f"USER {turn} will begin! \n")
return turn
def user_symbol(number):
us_x = "A"
while us_x not in ["X", "O"]:
us_x = input(f"USER {number}:Pick your symbol \
between X and O ").upper()
if us_x not in ["X", "O"]:
print("Sorry, invalid choice, try again")
if us_x == "X":
return ("X", "O")
else:
return ("O", "X")
# User(number) chooses a row in the list
def user_row(number, matrix):
row = "row"
while row not in ["0", "1", "2"] or check_row(matrix, int(row)):
row = input(f"USER {number}: Pick row 0, 1 or 2 \n")
if row not in ["0", "1", "2"] or check_row(matrix, int(row)):
print("Sorry, invalid choice")
return int(row)
# check for filled position
def check_pos(matrix, row, pos):
while matrix[row][pos] in ["X", "O"]:
return True
else:
return False
# check for filled row
def check_row(matrix, row):
for i in range(0, 3):
while matrix[row][i] not in ["X", "O"]:
return False
else:
return True
# check for a FULL board
def check_full(matrix):
for i_list in range(0, 3):
for i_item in range(0, 3):
while not check_pos(matrix, i_list, i_item):
return False
else:
return True
# User(number) chooses a position in the list
def user_pos(matrix, number, row):
pos = "wrong"
while pos not in ["0", "1", "2"] or check_pos(matrix, row, int(pos)):
pos = input(f"USER {number}: Pick a position \
in row {row} between 0 1 and 2 \n")
if pos not in ["0", "1", "2"] or check_pos(matrix, row, int(pos)):
print("Sorry, invalid choice")
return int(pos)
# Replacement value for chosen position
def replacement_value(turn, matrix, us_a, us_b, row, pos):
if turn == 1:
matrix[row][pos] = us_a
elif turn == 2:
matrix[row][pos] = us_b
return (matrix)
# check for winner:
def check_winner(matrix, us1, us2, turn):
if turn == 1:
us_symbol = us1
else:
us_symbol = us2
for row in matrix:
i = matrix.index(row)
for item in row:
h = row.index(item)
if matrix[i][0] == us_symbol and\
matrix[i][1] == us_symbol and matrix[i][2] == us_symbol:
return True
elif matrix[0][h] == us_symbol and\
matrix[1][h] == us_symbol and matrix[2][h] == us_symbol:
return True
elif matrix[0][0] == us_symbol and\
matrix[1][1] == us_symbol and matrix[2][2] == us_symbol:
return True
else:
return False
# switch turns between players
def switch_turn(turn):
if turn == 1:
turn = turn+1
elif turn == 2:
turn = turn-1
return turn
# Base of the game
game_on = True
# initial game list
matrix = [["", "", ""], ["", "", ""], ["", "", ""]]
welcome()
us_a, us_b = user_symbol(1)
turn = start_turn()
# While the game is on
while game_on:
# show the current playboard
display_board(matrix)
print("\n")
# the actual player will choose a row
row = user_row(turn, matrix)
# the actual player chooses a position
pos = user_pos(matrix, turn, row)
# we update the game list
matrix = replacement_value(turn, matrix, us_a, us_b, row, pos)
if check_winner(matrix, us_a, us_b, turn):
display_board(matrix)
print(f"\n USER {turn} wins!")
break
elif check_full(matrix):
display_board(matrix)
print("\n End of game, no one wins")
break
else:
print("\n"*5)
# we update game_on value
turn = switch_turn(turn)