Skip to content

Instantly share code, notes, and snippets.

@kpmcc
Created January 20, 2022 17:48
Show Gist options
  • Save kpmcc/fab112986c5ba446a201101346fda551 to your computer and use it in GitHub Desktop.
Save kpmcc/fab112986c5ba446a201101346fda551 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
import argparse
class Game(object):
def __init__(self):
self.board = [0 for _ in range(9)]
self.value_dict = {1: "X", 2: "O"}
self.turn_taker = 1
self.triads = self.get_triads()
self.num_turns = 0
self.has_winner = False
self.winner = None
self.winning_triad = None
def get_triads(self):
triads = []
# add row triads
for i in range(3):
triads.append([3*i + j for j in range(3)])
# add column triads
for c in range(3):
triads.append([c + 3*j for j in range(3)])
# add diagonal triads:
triads.append([0, 4, 8])
triads.append([2, 4, 6])
return triads
def check_slot_triad(self, slot_triad):
if slot_triad[0] != 0:
if slot_triad[0] == slot_triad[1] and slot_triad[1] == slot_triad[2]:
return True
return False
def check_for_winner(self):
for t in self.triads:
slot_triad = [self.board[i] for i in t]
if self.check_slot_triad(slot_triad):
self.winner = self.value_dict[slot_triad[0]]
self.winning_triad = t
return True
return False
def get_slot_char(self, slot_index):
slot_value = self.board[slot_index]
if slot_value != 0:
# slot value is not none, so return mapping of value to char
return self.value_dict[slot_value]
else:
# return the index on the board
return "%d" % slot_index
def get_board(self):
column_str = ""
for slot in range(0, 9):
if slot % 3 == 0:
print("\t" + column_str)
print("\t" + "-"*len(column_str))
column_str = ""
else:
column_str += "|"
column_str += self.get_slot_char(slot)
column_str += "\n"
print("\t" + column_str)
def apply_turn(self, turn_taker, slot_char):
if slot_char not in [str(i) for i in range(9)]:
print("> Please enter a number corresponding to an empty slot.")
return False
slot = int(slot_char)
if self.board[slot]:
print("> Slot %d is already taken, please choose again." % slot)
return False
else:
self.board[slot] = turn_taker
return True
def prompt_for_turn(self):
input_prompt = "> %s's turn, please enter the number for an available slot: " % \
self.value_dict[self.turn_taker]
slot_index = input(input_prompt)
return slot_index
def play(self):
while not (self.has_winner or self.num_turns == 9):
self.get_board()
slot_index = self.prompt_for_turn()
if self.apply_turn(self.turn_taker, slot_index):
self.turn_taker = 3 - self.turn_taker
self.num_turns += 1
winner = self.check_for_winner()
self.has_winner = winner
self.get_board()
if self.has_winner:
print("> %s Wins with combination %s!" % (self.winner, self.winning_triad))
else:
assert self.num_turns == 9
print("> Game is a tie!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A simple, two player tic tac toe game.")
args = parser.parse_args()
new_game = Game()
new_game.play()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment