-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
175 lines (154 loc) · 6.34 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
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
"""
Simple TicTacToe v1.1
"""
#Defining the board. 3 rows with 3 cells
board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
#Player icons
p1_icon = "X"
p2_icon = "O"
#Player input
p_input_row = 0
p_input_cell = 0
#Player text
p1_won = "Player 1 won!"
p2_won = "Player 2 won!"
#String for invalid answer
invalid = "Invalid answer! Try again.\n"
#Function for printing the board
def print_board():
print("\n-------")
print("|" + str(board[0][0]) + "|" + str(board[0][1]) + "|" + str(board[0][2]) + "|")
print("-------")
print("|" + str(board[1][0]) + "|" + str(board[1][1]) + "|" + str(board[1][2]) + "|")
print("-------")
print("|" + str(board[2][0]) + "|" + str(board[2][1]) + "|" + str(board[2][2]) + "|")
print("-------\n")
#Function for checking the board
def check_board():
"""
A function that checks the board. If no player won, the function will return false, meaning game isn't over.
If player won, the function will return true, meaning game is over.
"""
#Check vertical rows
#First vertical row
if board[0][0] == p1_icon and board[0][1] == p1_icon and board[0][2] == p1_icon:
print(p1_won)
return True
elif board[0][0] == p2_icon and board[0][1] == p2_icon and board[0][2] == p2_icon:
print(p2_won)
return True
#Second vertical row
elif board[1][0] == p1_icon and board[1][1] == p1_icon and board[1][2] == p1_icon:
print(p1_won)
return True
elif board[1][0] == p2_icon and board[1][1] == p2_icon and board[1][2] == p2_icon:
print(p2_won)
return True
#Third vertical row
elif board[2][0] == p1_icon and board[2][1] == p1_icon and board[2][2] == p1_icon:
print(p1_won)
return True
elif board[2][0] == p2_icon and board[2][1] == p2_icon and board[2][2] == p2_icon:
print(p2_won)
return True
#Check horizontal rows
#First horizontal row
if board[0][0] == p1_icon and board[1][0] == p1_icon and board[2][0] == p1_icon:
print(p1_won)
return True
elif board[0][0] == p2_icon and board[1][0] == p2_icon and board[2][0] == p2_icon:
print(p2_won)
return True
#Second horizontal row
elif board[0][1] == p1_icon and board[1][1] == p1_icon and board[2][1] == p1_icon:
print(p1_won)
return True
elif board[0][1] == p2_icon and board[1][1] == p2_icon and board[2][1] == p2_icon:
print(p2_won)
return True
#Third horizontal row
elif board[0][2] == p1_icon and board[1][2] == p1_icon and board[2][2] == p1_icon:
print(p1_won)
return True
elif board[0][2] == p2_icon and board[1][2] == p2_icon and board[2][2] == p2_icon:
print(p2_won)
return True
#Check diagonal rows:
#Upper left to down right
if board[0][0] == p1_icon and board[1][1] == p1_icon and board[2][2] == p1_icon:
print(p1_won)
return True
elif board[0][0] == p2_icon and board[1][1] == p2_icon and board[2][2] == p2_icon:
print(p2_won)
return True
#Down left to upper left
elif board[2][0] == p1_icon and board[1][1] == p1_icon and board[0][2] == p1_icon:
print(p1_won)
return True
elif board[2][0] == p2_icon and board[1][1] == p2_icon and board[0][2] == p2_icon:
print(p2_won)
return True
#Check if board is full:
if board[0][0] != 0 and board[0][1] != 0 and board[0][2] != 0 and board[1][0] != 0 and board[1][1] != 0 and board[1][2] != 0 and board[2][0] != 0 and board[2][1] != 0 and board[2][2] != 0:
print("Board is full!")
return True
return False
#Function for player input
def player_turn(player):
while True: #Loop for player turn
if player == "p1" or player == "p2": #A player is chosen | p1 = Player 1 & p2 = Player 2
if player == "p1":
print("Player 1's turn")
elif player == "p2":
print("Player 2's turn")
try: #Ask for player input
p_input_row = int(input("Row: "))
if p_input_row in [1, 2, 3]: #Correct row-input
p_input_cell = int(input("Cell: "))
if p_input_cell in [1, 2, 3]: #Correct cell-input
if board[(p_input_row - 1)][(p_input_cell - 1)] != p1_icon and board[(p_input_row - 1)][(p_input_cell - 1)] != p2_icon: #Check if cell is empty
if player == "p1": #Player 1 is chosen
board[(p_input_row - 1)][(p_input_cell - 1)] = p1_icon #Assign player icon to cell
print_board() #Print board
break #Break loop
elif player == "p2": #Player 2 is chosen
board[(p_input_row - 1)][(p_input_cell - 1)] = p2_icon
print_board()
break
else: #Cell is not empty
print(invalid)
else: #Invalid cell-input
print(invalid)
else: #Invalid row-input
print(invalid)
except ValueError: #Invalid player input
print(invalid)
else: #Invalid player chosen
print("[" + str(player) + "] is not a valid player!")
break
#Display introduction message
print("Simple TicTacToe Project v1.1\n")
print("The board is created by 3 rows which includes 3 cells each.")
print("You have to choose a row and a cell to insert icon to board.\n")
print("Board:")
print(" ----------------------------")
print("Row 1 | Cell 1 | Cell 2 | Cell 3 |")
print(" ----------------------------")
print("Row 2 | Cell 1 | Cell 2 | Cell 3 |")
print(" ----------------------------")
print("Row 3 | Cell 1 | Cell 2 | Cell 3 |")
print(" ----------------------------")
print("Example: Left upper corner = Row: 1, Cell: 1\t\tCenter = Row: 2, Cell: 2\t Middle third row = Row: 3, Cell: 2\n")
#Main game-loop
while True:
#Player 1's turn
player_turn(player = "p1")
game_over = check_board() #Check board
if game_over == True: #Check game-state. Is game over?
break #Exit main game-loop
#Player 2's turn
player_turn(player = "p2")
game_over = check_board() #Check board
if game_over == True:
break
print("Game over!")