Skip to content

Instantly share code, notes, and snippets.

@apsicle
Created October 10, 2016 22:18
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 apsicle/4ba41869569ca72d2ad201d1b064ce55 to your computer and use it in GitHub Desktop.
Save apsicle/4ba41869569ca72d2ad201d1b064ce55 to your computer and use it in GitHub Desktop.
# Written by: Ryan Yan
# Date: October 10th, 2016
#
# This program lets two players play tic tac toe in the console using keyboard input.
class Player:
'''Player objects keep track of the spaces they control on the grid (add_square and get_squares)
and has their symbol, default X's and O's as their string representation'''
def __init__(self, symbol):
self._squares = set() #Set of controlled spaces. Holds indices from 0-8 held by player in Board._grid
self._symbol = symbol
def add_square(self, ind):
self._squares.add(ind)
def get_squares(self):
return self._squares
def __str__(self):
return self._symbol
class Board:
'''Tictactoe game class. Contains the grid to be printed, a list of available spaces, and makes moves
occur and determines the victor.'''
def __init__(self):
self._grid = list(range(1, 10, 1)) #Grid to be printed. filled with X and Os
self._squares = list(range(9)) #List of available spaces. Holds indices from 0-8 open in _grid
self._win_conditions = [{0,1,2}, {3,4,5}, {6,7,8}, #rows
{0,3,6}, {1,4,7}, {2,5,8}, #columns
{0,4,8}, {2,4,6}] #diagonals
self._victor = None
def clear_grid(self):
self._grid = [""] * 9
def make_move(self, player):
#Asks for input and updates all the square lists in Player and in Board, then checks win condition
while(True):
#Checks and validates input
try:
move = int(input("%s's move: " % str(player))) - 1
self._squares.index(move)
break
except ValueError:
print("Please enter an integer between 1 and 9")
#Updates lists containing squares and checks victory conditions.
self._squares.remove(move)
player.add_square(move)
self._grid[move] = str(player)
self.check_win(player)
def check_win(self, player):
#Checks if any victorious set of squares is a subset of squares held by a particular player
held_squares = player.get_squares()
for condition in self._win_conditions:
if condition.issubset(held_squares):
self._victor = player
return
def play(self):
#Sets up the game
player_one = Player("X")
player_two = Player("O")
players = [player_one, player_two]
current_player = 0
print(self)
self.clear_grid()
print("Use integers 1-9 to specify the location on the board")
#Gameplay
while(self._victor == None):
self.make_move(players[current_player%2])
print(self)
current_player = current_player + 1
print("The %s's have it!" % str(self._victor))
def __str__(self):
#Prints out the game board
for row in range(3):
adjust = 3 * row
print("%2s%2s%2s%2s%2s" % (self._grid[adjust+0], "|", self._grid[adjust+1], "|", self._grid[adjust+2]))
if(row != 2):
print("___|___|___")
else:
print("%4s%4s" % ("|", "|"))
return ""
if __name__ == "__main__":
new_game = Board()
new_game.play()
@apsicle
Copy link
Author

apsicle commented Jun 27, 2017

This is a tic-tac-toe game I wrote in python to be played by two human players in the console.

As I saw it, this would be a good place for an OOP approach with the work divided into two classes:

  1. Player: Keeps track of held squares and their team's symbol. Exposes an "add_square" method to allow Board object to update Players after a move is made.
  2. Board: Implements a game loop which instantiates Player objects. Additionally, implements a method to make player moves by prompting for input and updating the squares in properties based on user input prompts to make moves. This is called repeatedly in the game loop until a victor condition is met. Lastly, since this is console tic-tac-toe, the Board keeps a string representation of the playing board, printing to the console with each update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment