-
-
Save mikezink/d6d9c7e98b393bc000a0ecee52bbd880 to your computer and use it in GitHub Desktop.
HW1 Q3 Fall 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""tictactoe game for 2 players""" | |
choices = [] ## Err1: add the declaration for array choices. | |
for x in range (1, 10) : ## Err2: range incorrect. range (1, 9) only contains number from 1 to 8 | |
choices.append(str(x)) ## Err3: x is an integer, you need to convert it to string when append to the list | |
playerOneTurn = True ## Err4: it should be = instead of == | |
winner = False | |
def printBoard(): | |
print( '\n -----') | |
print( '|' + choices[0] + '|' + choices[1] + '|' + choices[2] + '|') | |
print( ' -----') | |
print( '|' + choices[3] + '|' + choices[4] + '|' + choices[5] + '|') | |
print( ' -----') | |
print( '|' + choices[6] + '|' + choices[7] + '|' + choices[8] + '|') | |
print( ' -----\n') | |
while not winner: ## Err5: logic error. The condition should be not winner | |
printBoard() ## Err6: name error. python variable name is case sensitive | |
if playerOneTurn : | |
print( "Player 1:") | |
else : | |
print( "Player 2:") | |
try: | |
choice = int(input(">> ")) | |
except: | |
print("please enter a valid field") | |
continue | |
if choices[choice - 1] == 'X' or choices [choice-1] == 'O': ## Err7: first = should be == | |
## Err8: logic error. The second choice should be choice-1 | |
print("illegal move, please try again") | |
continue | |
if playerOneTurn : | |
choices[choice - 1] = 'X' ## Err9: string should be wrapped with '' | |
else : | |
choices[choice - 1] = 'O' ## Err9: string should be wrapped with '' | |
playerOneTurn = not playerOneTurn | |
for x in range (0, 3) : | |
y = x * 3 | |
if (choices[y] == choices[(y + 1)] and choices[y] == choices[(y + 2)]) : | |
winner = True | |
printBoard() | |
if (choices[x] == choices[(x + 3)] and choices[x] == choices[(x + 6)]) : | |
winner = True | |
printBoard() | |
if((choices[0] == choices[4] and choices[0] == choices[8]) or | |
(choices[2] == choices[4] and choices[4] == choices[6])) : | |
winner = True ## Err10: True should be capitalized for the first letter | |
printBoard() | |
print ("Player " + str(int(playerOneTurn + 1)) + " wins!\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment