Skip to content

Instantly share code, notes, and snippets.

@h2rashee
Created February 2, 2022 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h2rashee/05440df5e55c243e4e0f472ee77deb87 to your computer and use it in GitHub Desktop.
Save h2rashee/05440df5e55c243e4e0f472ee77deb87 to your computer and use it in GitHub Desktop.
Build Tic Tac Toe
import copy
BOARD_FORMAT = '{} | {} | {}\n-----------\n{} | {} | {}\n-----------\n{} | {} | {}\n-----------\n'
class TicTacToe:
def __init__(self):
self.board = [[None, None, None], [None, None, None], [None, None, None]]
self.cur_turn = 'X'
def __repr__(self):
printable_board = copy.deepcopy(self.board[0])
printable_board.extend(self.board[1])
printable_board.extend(self.board[2])
formatted_board = ['-' if x is None else x for x in printable_board]
return BOARD_FORMAT.format(*formatted_board)
def play(self, x, y):
if self.get_game_winner() is not None:
raise Exception('Cannot play because game has ended')
if x < 0 or x > 2 or y < 0 or y > 2:
raise Exception('Invalid coordinates given')
if self.board[x][y] is not None:
raise Exception('This position has already been played')
self.board[x][y] = self.whose_turn_is_it()
self.change_turn()
def change_turn(self):
if self.cur_turn == 'X':
self.cur_turn = 'O'
else:
self.cur_turn = 'X'
def whose_turn_is_it(self):
return self.cur_turn
def get_game_winner(self):
# Horiztonal win condition
if self.board[0][0] == self.board[0][1] and self.board[0][1] == self.board[0][2]:
if self.board[0][0] is not None:
return self.board[0][0]
if self.board[1][0] == self.board[1][1] and self.board[1][1] == self.board[1][2]:
if self.board[1][0] is not None:
return self.board[1][0]
if self.board[2][0] == self.board[2][1] and self.board[2][1] == self.board[2][2]:
if self.board[2][0] is not None:
return self.board[2][0]
# Vertical win condition
if self.board[0][0] == self.board[1][0] and self.board[1][0] == self.board[2][0]:
if self.board[0][0] is not None:
return self.board[0][0]
if self.board[0][1] == self.board[1][1] and self.board[1][1] == self.board[2][1]:
if self.board[0][1] is not None:
return self.board[0][1]
if self.board[0][2] == self.board[1][2] and self.board[1][2] == self.board[2][2]:
if self.board[0][2] is not None:
return self.board[0][2]
if self.board[0][0] == self.board[1][1] and self.board[1][1] == self.board[2][2]:
if self.board[0][0] is not None:
return self.board[0][0]
if self.board[0][2] == self.board[1][1] and self.board[1][1] == self.board[2][0]:
if self.board[0][2] is not None:
return self.board[0][2]
return None
def has_game_ended(self):
if self.get_game_winner() is not None:
return True
for row in self.board:
if None in row:
return False
return True
def reset(self):
self.board = [[None, None, None], [None, None, None], [None, None, None]]
def main():
game = TicTacToe()
while (not game.has_game_ended()):
print(game)
print('It\'s {}\'s turn'.format(game.whose_turn_is_it()))
try:
x = input('Please enter x co-ordinate of the position you want to play: ')
y = input('Please enter y co-ordinate of the position you want to play: ')
game.play(int(x), int(y))
except Exception as e:
print('ERROR: {}'.format(str(e)))
continue
winner = game.get_game_winner()
if winner is None:
print('Game ended in stalemate')
else:
print('{} won the game!'.format(winner))
game.reset()
main()
# t = TicTacToe()
# print(t)
# t.play(0,0)
# print(t)
# t.play(1,0)
# print(t)
# t.play(2,0)
# print(t)
# t.play(1,1)
# print(t)
# t.play(0,2)
# print(t)
# # t.play(1,2)
# # print(t)
# # print(t.get_game_winner())
# t.play(2,2)
# print(t)
# t.play(2,1)
# print(t)
# t.play(0,1)
# print(t)
# t.play(1,2)
# print(t)
# print(t.get_game_winner())
# print(t.has_game_ended())
# t.play(0,0)
# t.play(3, 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment