Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 54 additions & 47 deletions TicTacToe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ def playAgain():
return input().lower().startswith('y')

def makeMove(board, letter, move):
board[move] = letter
if isSpaceFree(board,move):
board[move] = letter
else:
raise Exception("makeMove: the field is not empty!")

def isWinner(bo, le):
# Given a board and a player's letter, this function returns True if that player has won.
Expand All @@ -64,7 +67,7 @@ def getBoardCopy(board):

def isSpaceFree(board, move):
# Return true if the passed move is free on the passed board.
return board[move] == ' '
return board[move].isdigit()

def getPlayerMove(board):
# Let the player type in his move.
Expand All @@ -82,7 +85,7 @@ def chooseRandomMoveFromList(board, movesList):
if isSpaceFree(board, i):
possibleMoves.append(i)

if len(possibleMoves) != 0:
if len(possibleMoves) > 0:
return random.choice(possibleMoves)
else:
return None
Expand Down Expand Up @@ -130,53 +133,57 @@ def isBoardFull(board):
return False
return True


print('Welcome to Tic Tac Toe!')

while True:
# Reset the board
theBoard = [' '] * 10
playerLetter, computerLetter = inputPlayerLetter()
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
gameIsPlaying = True

while gameIsPlaying:
if turn == 'player':
# Player's turn.
drawBoard(theBoard)
move = getPlayerMove(theBoard)
makeMove(theBoard, playerLetter, move)

if isWinner(theBoard, playerLetter):
def main():
print('Welcome to Tic Tac Toe!')
random.seed()
while True:
# Reset the board
theBoard = [' '] * 10
for i in range(9,0,-1):
theBoard[i] = str(i)
playerLetter, computerLetter = inputPlayerLetter()
turn = whoGoesFirst()
print('The ' + turn + ' will go first.')
gameIsPlaying = True

while gameIsPlaying:
if turn == 'player':
# Player's turn.
drawBoard(theBoard)
print('Hooray! You have won the game!')
gameIsPlaying = False
else:
if isBoardFull(theBoard):
move = getPlayerMove(theBoard)
makeMove(theBoard, playerLetter, move)

if isWinner(theBoard, playerLetter):
drawBoard(theBoard)
print('The game is a tie!')
break
print('Hooray! You have won the game!')
gameIsPlaying = False
else:
turn = 'computer'

else:
# Computer's turn.
move = getComputerMove(theBoard, computerLetter)
makeMove(theBoard, computerLetter, move)

if isWinner(theBoard, computerLetter):
drawBoard(theBoard)
print('The computer has beaten you! You lose.')
gameIsPlaying = False
if isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
break
else:
turn = 'computer'

else:
if isBoardFull(theBoard):
# Computer's turn.
move = getComputerMove(theBoard, computerLetter)
makeMove(theBoard, computerLetter, move)

if isWinner(theBoard, computerLetter):
drawBoard(theBoard)
print('The game is a tie!')
break
print('The computer has beaten you! You lose.')
gameIsPlaying = False
else:
turn = 'player'

if not playAgain():
break

if isBoardFull(theBoard):
drawBoard(theBoard)
print('The game is a tie!')
break
else:
turn = 'player'

if not playAgain():
break

if __name__ == "__main__":
main()