Skip to content

Instantly share code, notes, and snippets.

@tonytan4ever
Created December 21, 2019 22:06
Show Gist options
  • Save tonytan4ever/a65a15c10a98b5d3186502d2a741ae2e to your computer and use it in GitHub Desktop.
Save tonytan4ever/a65a15c10a98b5d3186502d2a741ae2e to your computer and use it in GitHub Desktop.
Tic-tac-toe
class AlreadyTakenException(Exception):
pass
class GameEndException(Exception):
pass
class TicTacToe:
"""
@return: nothing
"""
def __init__(self):
self._cur_player = 'O'
self.board = [['.'] * 3 for _ in range(3)]
def move(self, x, y):
if self.board[x][y] != '.'
raise AlreadyTakenException
player = self.getCurrentPlayer()
self.board[x][y] = player
if self.is_game_end(self):
print("{} player wins!")
raise GameEndException({} player )
def getCurrentPlayer(self):
if self._cur_player == 'O':
self._cur_player = 'X'
else:
self._cur_player = 'O'
return self._cur_player
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment