Created
May 1, 2013 04:03
-
-
Save PuercoPop/5493673 to your computer and use it in GitHub Desktop.
Grid best viewd in monospaced font
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
# -*- coding: utf-8 -*- | |
class IllegalMove(Exception): | |
pass | |
class TicTacToe(object): | |
player_1_symbol = "O" | |
player_2_symbol = "X" | |
empty_symbol = " " | |
def __init__(self): | |
self.board = [ self.empty_symbol, self.empty_symbol, | |
self.empty_symbol, self.empty_symbol, | |
self.empty_symbol, self.empty_symbol, | |
self.empty_symbol, self.empty_symbol, | |
self.empty_symbol, ] | |
self.current_player = self.player_1_symbol | |
@property | |
def has_ended(self): | |
return self.empty_symbol not in self.board | |
@property | |
def has_winner(self): | |
return (self.player_has_won(self.player_1_symbol) or | |
self.player_has_won(self.player_2_symbol)) | |
@property | |
def has_tied(self): | |
return self.has_ended and not self.has_winner | |
@property | |
def winner(self): | |
if self.player_has_won(self.player_1_symbol): | |
return self.player_1_symbol | |
elif self.player_has_won(self.player_2_symbol): | |
return self.player_2_symbol | |
else: | |
return None | |
def move(self, position): | |
if self.is_square_empty(position): | |
self.board[self.translate_move(position)] = self.current_player | |
if self.current_player == self.player_1_symbol: | |
self.current_player = self.player_2_symbol | |
else: | |
self.current_player = self.player_1_symbol | |
else: | |
raise IllegalMove | |
def is_square_empty(self, position): | |
return self.board[self.translate_move(position)] == self.empty_symbol | |
def player_has_won(self, symbol): | |
if (self.board[0] == symbol and | |
self.board[1] == symbol and | |
self.board[2] == symbol): | |
return True | |
elif (self.board[3] == symbol and | |
self.board[4] == symbol and | |
self.board[5] == symbol): | |
return True | |
elif (self.board[6] == symbol and | |
self.board[7] == symbol and | |
self.board[8] == symbol): | |
return True | |
elif (self.board[0] == symbol and | |
self.board[3] == symbol and | |
self.board[6] == symbol): | |
return True | |
elif (self.board[1] == symbol and | |
self.board[4] == symbol and | |
self.board[7] == symbol): | |
return True | |
elif (self.board[2] == symbol and | |
self.board[5] == symbol and | |
self.board[8] == symbol): | |
return True | |
elif (self.board[0] == symbol and | |
self.board[4] == symbol and | |
self.board[8] == symbol): | |
return True | |
elif (self.board[2] == symbol and | |
self.board[4] == symbol and | |
self.board[6] == symbol): | |
return True | |
else: | |
return False | |
def translate_move(self, position): | |
if position == "TL": | |
return 0 | |
elif position == "TC": | |
return 1 | |
elif position == "TR": | |
return 2 | |
elif position == "ML": | |
return 3 | |
elif position == "MC": | |
return 4 | |
elif position == "MR": | |
return 5 | |
elif position == "BL": | |
return 6 | |
elif position == "BC": | |
return 7 | |
elif position == "BR": | |
return 8 | |
def __repr__(self): | |
return " {0} | {1} | {2} \n———————\n {3} | {4} | {5} \n———————\n {6} | {7} | {8} \n".format(self.board[0], self.board[1], self.board[2], self.board[3], self.board[4], self.board[5], self.board[6], self.board[7], self.board[8], ) | |
if __name__ == "__main__": | |
game = TicTacToe() | |
while not game.has_ended: | |
print game | |
print "{0}'s turn".format(game.current_player) | |
move = raw_input("Input Move: Top Left (TL), Top Center (TC), Top " | |
"Right (TR), Middle Left (ML), Middle Center (MC), " | |
"Middle Right (MR), Bottom Left (BL), Bottom Center " | |
"(BC), Bottom Right (BR)") | |
if move not in ("TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"): | |
print "Invalid Input" | |
continue | |
try: | |
game.move(move) | |
except IllegalMove: | |
print "Illegal Move {0}. Try Again".format(move) | |
continue | |
if game.has_winner: | |
print "{0} Wins!!! Congrats!".format(game.winner) | |
break | |
print game | |
if game.has_tied: | |
print "No Winner. Maybe next time." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment