Created
December 21, 2019 22:06
-
-
Save tonytan4ever/a65a15c10a98b5d3186502d2a741ae2e to your computer and use it in GitHub Desktop.
Tic-tac-toe
This file contains hidden or 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
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