1
1
# Tic Tac Toe
2
2
3
3
import random
4
+ import sys
5
+
6
+ def get_input ():
7
+ if sys .version_info >= (3 , 0 ):
8
+ return input ()
9
+ else :
10
+ return raw_input ()
4
11
5
12
def drawBoard (board ):
6
13
# This function prints out the board that it was passed.
@@ -18,7 +25,7 @@ def inputPlayerLetter():
18
25
letter = ''
19
26
while not (letter == 'X' or letter == 'O' ):
20
27
print ('Do you want to be X or O?' )
21
- letter = input ().upper ()
28
+ letter = get_input ().upper ()
22
29
23
30
# the first element in the tuple is the player's letter, the second is the computer's letter.
24
31
if letter == 'X' :
@@ -36,7 +43,7 @@ def whoGoesFirst():
36
43
def playAgain ():
37
44
# This function returns True if the player wants to play again, otherwise it returns False.
38
45
print ('Do you want to play again? (yes or no)' )
39
- return input ().lower ().startswith ('y' )
46
+ return get_input ().lower ().startswith ('y' )
40
47
41
48
def makeMove (board , letter , move ):
42
49
if isSpaceFree (board ,move ):
@@ -74,7 +81,7 @@ def getPlayerMove(board):
74
81
move = ' '
75
82
while move not in '1 2 3 4 5 6 7 8 9' .split () or not isSpaceFree (board , int (move )):
76
83
print ('What is your next move? (1-9)' )
77
- move = input ()
84
+ move = get_input ()
78
85
return int (move )
79
86
80
87
def chooseRandomMoveFromList (board , movesList ):
@@ -186,4 +193,4 @@ def main():
186
193
break
187
194
188
195
if __name__ == "__main__" :
189
- main ()
196
+ main ()
0 commit comments