Created
March 6, 2019 04:12
-
-
Save tillyt/9b8c2a1c27871d0765e3fe69763b097c to your computer and use it in GitHub Desktop.
dead simple CLI tic tac toe game written in python 3
This file contains 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 TicTacToeGame: | |
def __init__(self, player1=None, player2=None): | |
# initialize an empty board | |
# standard boards have dimension 3: 3 columns and 3 rows | |
self.dimension = 3 | |
self._board = [None for n in range(self.dimension**2)] | |
self.empty = "⬜" | |
self.player1 = player1 or "❌" | |
self.player2 = player2 or "⭕" | |
self.turn = 0 | |
self.is_game_over = False | |
self.winning_player = None | |
def __str__(self): | |
def formatSquare(squareValue: int) -> str: | |
if squareValue is None: | |
return self.empty | |
elif squareValue == 1: | |
return self.player1 | |
elif squareValue == 2: | |
return self.player2 | |
rows = [self._board[i:i+self.dimension] | |
for i in range(0, len(self._board), self.dimension)] | |
return "\n".join(" ".join(map(formatSquare, row)) + "\n" for row in rows) | |
@property | |
def current_player(self) -> int: | |
# player 1 goes on even turns and player 2 on odd turns | |
return 1 if self.turn % 2 == 0 else 2 | |
def play(self): | |
prompt = "Player {}: Pick a square to play (between 1 and 9) where the square numbers are increasing from top left across the rows and then down the columns \n" | |
prompt = prompt.format(self.current_player) | |
square_to_play = None | |
while not square_to_play or square_to_play < 1 or square_to_play > 9 or self.is_occupied(square_to_play): | |
square_to_play = input(prompt) or 0 | |
square_to_play = int(square_to_play) | |
self.mark_square(square_to_play) | |
if self.is_won_game(): | |
self.is_game_over = True | |
self.winning_player = self.current_player | |
self.turn += 1 | |
def is_occupied(self, square: int) -> bool: | |
board_idx = square - 1 # square 1 is index 0 etc | |
return self._board[board_idx] is not None | |
def mark_square(self, square: int): | |
board_idx = square - 1 | |
self._board[board_idx] = self.current_player | |
@property | |
def is_full_board(self) -> bool: | |
return all([n is not None for n in self._board]) | |
@property | |
def is_over(self) -> bool: | |
return self.is_full_board or self.is_game_over | |
def is_won_game(self) -> bool: | |
horizontal_win_indices = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | |
vertical_win_indices = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] | |
diagonal_win_indices = [[1, 5, 9], [3, 5, 7]] | |
for win in horizontal_win_indices + vertical_win_indices + diagonal_win_indices: | |
vals = [self._board[i-1] for i in win] | |
if all([x is not None and x == vals[0] for x in vals]): | |
return True | |
return False | |
if __name__ == "__main__": | |
print("New game!") | |
print("Time to choose your pieces") | |
player1piece = str(input( | |
"Player 1: Pick your piece. Use any emoji or letter. Or, just hit enter to use the default: ❌")) | |
player2piece = str(input("Player 2: Pick your piece. Use any emoji or letter. Or, just hit enter to use the default: ⭕")) | |
game = TicTacToeGame(player1=player1piece, player2=player2piece) | |
print(game) | |
while not game.is_over: | |
game.play() | |
print(game) | |
if game.is_won_game(): | |
print("PLAYER {} WON".format(game.winning_player)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment