This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
TicTacToe_2016.py
198 lines (147 loc) · 5.97 KB
/
TicTacToe_2016.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
# Pam Qian 2016 Fall CS 112 Python Midterm Project II
# Tic Tack Toe
def intro():
# This function introduces the rules of the game Tic Tac Toe
print("Hello! Welcome to Pam's Tic Tac Toe game!")
print("\n")
print("Rules: Player 1 and player 2, represented by X and O, take turns "
"marking the spaces in a 3*3 grid. The player who succeeds in placing "
"three of their marks in a horizontal, vertical, or diagonal row wins.")
print("\n")
input("Press enter to continue.")
print("\n")
def create_grid():
# This function creates a blank playboard
print("Here is the playboard: ")
board = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
return board
def sym():
# This function decides the players' symbols
symbol_1 = input("Player 1, do you want to be X or O? ")
if symbol_1 == "X":
symbol_2 = "O"
print("Player 2, you are O. ")
else:
symbol_2 = "X"
print("Player 2, you are X. ")
input("Press enter to continue.")
print("\n")
return (symbol_1, symbol_2)
def startGamming(board, symbol_1, symbol_2, count):
# This function starts the game.
# Decides the turn
if count % 2 == 0:
player = symbol_1
elif count % 2 == 1:
player = symbol_2
print("Player "+ player + ", it is your turn. ")
row = int(input("Pick a row:"
"[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:"))
column = int(input("Pick a column:"
"[left column: enter 0, middle column: enter 1, right column enter 2]"))
# Check if players' selection is out of range
while (row > 2 or row < 0) or (column > 2 or column < 0):
outOfBoard(row, column)
row = int(input("Pick a row[upper row:"
"[enter 0, middle row: enter 1, bottom row: enter 2]:"))
column = int(input("Pick a column:"
"[left column: enter 0, middle column: enter 1, right column enter 2]"))
# Check if the square is already filled
while (board[row][column] == symbol_1)or (board[row][column] == symbol_2):
filled = illegal(board, symbol_1, symbol_2, row, column)
row = int(input("Pick a row[upper row:"
"[enter 0, middle row: enter 1, bottom row: enter 2]:"))
column = int(input("Pick a column:"
"[left column: enter 0, middle column: enter 1, right column enter 2]"))
# Locates player's symbol on the board
if player == symbol_1:
board[row][column] = symbol_1
else:
board[row][column] = symbol_2
return (board)
def isFull(board, symbol_1, symbol_2):
count = 1
winner = True
# This function check if the board is full
while count < 10 and winner == True:
gaming = startGamming(board, symbol_1, symbol_2, count)
pretty = printPretty(board)
if count == 9:
print("The board is full. Game over.")
if winner == True:
print("There is a tie. ")
# Check if here is a winner
winner = isWinner(board, symbol_1, symbol_2, count)
count += 1
if winner == False:
print("Game over.")
# This is function gives a report
report(count, winner, symbol_1, symbol_2)
def outOfBoard(row, column):
# This function tells the players that their selection is out of range
print("Out of boarder. Pick another one. ")
def printPretty(board):
# This function prints the board nice!
rows = len(board)
cols = len(board)
print("---+---+---")
for r in range(rows):
print(board[r][0], " |", board[r][1], "|", board[r][2])
print("---+---+---")
return board
def isWinner(board, symbol_1, symbol_2, count):
# This function checks if any winner is winning
winner = True
# Check the rows
for row in range (0, 3):
if (board[row][0] == board[row][1] == board[row][2] == symbol_1):
winner = False
print("Player " + symbol_1 + ", you won!")
elif (board[row][0] == board[row][1] == board[row][2] == symbol_2):
winner = False
print("Player " + symbol_2 + ", you won!")
# Check the columns
for col in range (0, 3):
if (board[0][col] == board[1][col] == board[2][col] == symbol_1):
winner = False
print("Player " + symbol_1 + ", you won!")
elif (board[0][col] == board[1][col] == board[2][col] == symbol_2):
winner = False
print("Player " + symbol_2 + ", you won!")
# Check the diagonals
if board[0][0] == board[1][1] == board[2][2] == symbol_1:
winner = False
print("Player " + symbol_1 + ", you won!")
elif board[0][0] == board[1][1] == board[2][2] == symbol_2:
winner = False
print("Player " + symbol_2 + ", you won!")
elif board[0][2] == board[1][1] == board[2][0] == symbol_1:
winner = False
print("Player " + symbol_1 + ", you won!")
elif board[0][2] == board[1][1] == board[2][0] == symbol_2:
winner = False
print("Player " + symbol_2 + ", you won!")
return winner
def illegal(board, symbol_1, symbol_2, row, column):
print("The square you picked is already filled. Pick another one.")
def report(count, winner, symbol_1, symbol_2):
print("\n")
input("Press enter to see the game summary. ")
if (winner == False) and (count % 2 == 1 ):
print("Winner : Player " + symbol_1 + ".")
elif (winner == False) and (count % 2 == 0 ):
print("Winner : Player " + symbol_2 + ".")
else:
print("There is a tie. ")
def main():
# The main function
introduction = intro()
board = create_grid()
pretty = printPretty(board)
symbol_1, symbol_2 = sym()
full = isFull(board, symbol_1, symbol_2) # The function that starts the game is also in here.
# Call Main
if __name__ == "__main__":
main()