-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
executable file
·250 lines (231 loc) · 6.5 KB
/
functions.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# TicTacToe/functions.py
import os
from variables import *
def clearScreen():
# @description: function for clearing the terminal window using cls on
# windows and clear on linux
# clearScreen() adapted from http://stackoverflow.com/questions/517970
os.system(['clear','cls'][os.name == 'nt'])
print "Enter 'Quit' At Any Prompt To Quit The Program\n"
def buffInput(question):
# @description: function for checking if input is quit, otherwise return
# the input (custom input function)
# @return: the raw_input
raw_input_var = raw_input(question).lower()
if (raw_input_var == "quit"):
os._exit(0) # quits the program with exit code 0
else:
try:
return raw_input_var[0]
except:
return "" # if raw_input_var was empty return ""
def printIntro():
# @description: function for printing Introduction message
clearScreen()
print clearLineLarge
print "Program: TiCTaCToE"
print "Version: 2.0" # i wanted to make it look stable :-)
print "Author: Micah Thomas"
print clearLineLarge
print "This Is A 2 Player Game, Find Another Player Now!"
def printMenu():
# @description: function for printing the Basic Menu for starting the Game
# @return: 1: if ans == y/Y
# 2: exceptions
ans = ""
while (ans not in ["y","n"]):
ans = buffInput("(ENTER: y/n) => ").lower()
if (ans == "y"):
return 1
return 0
def printError(error):
# @description: function for a uniform and distinguishable formatting for
# for errors and also to make sure that errors dont fill the screen by
# calling drawBoard() before printing errors.
drawBoard()
print "\n\t" + error + "\n"
def drawBoard():
# @description: function for printing the board in a uniform manner
# Used while loop and if statements to ouptut the correct seperators
clearScreen()
i = 0
while ((i < 9) & (i >= 0)):
print boxes[i],
if ((i+1)%3): #i+1 because that makes the index number = the box number
print " | ",
else:
if (i < 6):
print "\n" + clearLineSmall
i += 1
print "\n"
def boxIsMarked(box, player):
# @description: function for checking if a box is already filled by the
# player's mark
# @params: box: the number of the box
# player: the number of the player
# @return: 1: if the box is marked
# 0: exceptions
box -= 1
if (player == 1):
mark = player1["symbol"]
else:
mark = player2["symbol"]
if (boxes[box] == mark):
return 1
else:
return 0
def boxIsEmpty(box):
# @description: function for checking if box is empty
# @params: box: the number of the boxes
# @return: 1: if box is not Empty
# 0: if box is empty
if (boxes[box-1] in [player1["symbol"], player2["symbol"]]):
return 1
else:
return 0
def markBox(box, player):
# @description: function for marking the box if its not already marked
# @params: box: the number of the box
# player: the number of the player
# @return: 1: successfull
# 0: exceptions
if (player == 1):
mark = player1["symbol"]
player = 2
else:
mark = player2["symbol"]
player = 1
if (not boxIsEmpty(box)):
box -= 1
boxes[box] = mark
return 1
else:
printError("Invalid Selection:\n\t\tBox No.%s Is Already Marked!\n\tPlease Select An Empty Box." % box)
return 0
def gameOver(player):
# @description: the function for checking if the game is over by checking
# combinations of winning possibilities.
# @params: player: this is the variable passed over to boxIsMarked()
# @return: 1: if game is over
# 0: exceptions
if (boxIsMarked(3, player)):
if (boxIsMarked(2, player)):
if (boxIsMarked(1, player)):
#top row
return 1
else:
if (boxIsMarked(5, player)):
if(boxIsMarked(7, player)):
#top right diagonal
return 1
else:
if (boxIsMarked(6, player)):
if (boxIsMarked(9, player)):
#right column
return 1
if (boxIsMarked(5, player)):
if (boxIsMarked(8, player)):
if (boxIsMarked(2, player)):
#middle column
return 1
else:
if (boxIsMarked(6, player)):
if (boxIsMarked(4, player)):
#middle row
return 1
else:
if (boxIsMarked(9, player)):
if (boxIsMarked(1, player)):
#top left diagonal
return 1
if (boxIsMarked(7, player)):
if (boxIsMarked(8, player)):
if (boxIsMarked(9, player)):
#bottom row
return 1
else:
if (boxIsMarked(4, player)):
if (boxIsMarked(1, player)):
#left column
return 1
return 0
def ask(player):
# @description: the function that asks the question and if the answer is
# integer, it saves the value.
# @params: player: the player number
# @return: ans: raw_input if int, exceptions: 0
ans = 0
# check if input is int:
# http://stackoverflow.com/questions/5424716
try:
ans = int(buffInput("Player %s: Please ENTER The Number Of The BOX You Would Like To Mark => " % player))
except:
printError("Invalid Input:\n\n\t\tInput Was Not An Integer!")
return ans
def questionLoop():
# @description: the loop that draws the board and initializes the question
# loop untill game is over
# @return: 1: if the winner is player 1
# 2: if the winner is player 2
drawBoard()
runs = 0
while (runs < 10):
marked = 0
while (marked == 0):
ans = 0
while (not (ans > 0 and ans < 10)):
ans = ask(1)
if (not (ans > 0 and ans < 10)):
printError("Invalid Input:\n\n\t\tInput Was Not In The Range Of 1 And 9!\n\n\tSelect A Valid Box Number")
marked = markBox(ans, 1)
player1["turns"] += 1
runs += 1
if (runs == 9):
drawBoard()
return 3
drawBoard()
if (gameOver(1)):
return 1
marked = 0
while (not marked):
ans = 0
while (not (ans > 0 and ans < 10)):
ans = ask(2)
if (not (ans > 0 and ans < 10)):
printError("Invalid Input:\n\n\t\tInput Was Not In The Range Of 1 And 9!\n\n\tSelect A Valid Box Number")
marked = markBox(ans, 2)
player2["turns"] += 1
runs += 1
drawBoard()
if (gameOver(2)):
return 2
return 3
def printWinner(winner):
# @description: the function for printing the info regarding game.
# @params: winner: the player who won
drawBoard()
print "The Winner Is Player %s. \nPlayer 1:\n\tTurns Taken: %s\nPlayer 2:\n\tTurns Taken: %s" % (winner, player1["turns"], player2["turns"])
def game():
# @description: the function that defines the game loop
winner = questionLoop()
if (winner == 3):
print "The Game Is A Draw!"
print "Would You Like To Play Again?"
else:
printWinner(winner)
def resetVariables():
# @description: the function for resetting the Variables
global boxes
global player1
global player2
boxes = ["1","2","3","4","5","6","7","8","9"]
player1 = {
"id":1,
"symbol":"x",
"turns":0,
}
player2 = {
"id":2,
"symbol":"o",
"turns":0,
}