Created
February 5, 2015 23:46
-
-
Save anthonyburdi/86c66bb3bc9c6ef04f6b to your computer and use it in GitHub Desktop.
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
# Before your interview, write a program that lets two humans play a game of Tic Tac Toe in a terminal. | |
# The program should let the players take turns to input their moves. | |
# The program should report the outcome of the game. | |
# Anthony Burdi 2/5/2015 | |
import tictactoe_helper as th | |
class ticTacToeGame: | |
"""Store current game for display""" | |
gameState = [None,None,None,None,None,None,None,None,None] | |
class player: | |
"""Store player data""" | |
def __init__(self, name, number): | |
self.name = name | |
self.n = number | |
# Manually assign X and O: | |
if number == 1: | |
self.sym = "X" | |
else: | |
self.sym = "O" | |
# Initialize player's moves, used to check for win: | |
self.moves = [0,0,0,0,0,0,0,0,0] | |
if __name__ == '__main__': | |
""" Main program""" | |
print "TicTacToe - get ready!" | |
game = ticTacToeGame() | |
# Solicit players names: | |
player1 = player(raw_input("Player 1! Nice to meet you. What is your name? "), 1) | |
player2 = player(raw_input("Player 2! Nice to meet you. What is your name? "), 2) | |
# It is OK and allowed if players enter the same name, but it will be confusing. | |
# Iterate through set maximum number of turns, displaying current state and soliciting new moves: | |
for turn in range(1,10): | |
th.printGameState(game,player1,player2,turn) | |
if turn%2: | |
# Odd numbered turns, player 1 move | |
th.inputMove(player1, game) | |
if th.checkForWinner(player1): | |
print str(player1.name) + " wins!! Congratulations!" # use str() in case non-str entered as name | |
break | |
else: | |
# Player 2 move | |
th.inputMove(player2, game) | |
if th.checkForWinner(player2): | |
print str(player2.name) + " wins!! Congratulations!" | |
break | |
if not th.checkForWinner(player1) and not th.checkForWinner(player2): | |
print "Sorry, no one wins. Try again!" |
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
# Helper functions for tictactoe.py | |
# Anthony Burdi 2/5/2015 | |
def inputMove(player, game): | |
""" """ | |
while True: | |
# Find the available boxes and display them: | |
available = [] | |
for idx,box in enumerate(game.gameState): | |
if not box: | |
available.append(idx+1) | |
print "Available boxes: " + str(available) | |
# Solicit box number, and do some validation: | |
try: | |
input = int(raw_input(player.name + ", please enter the number of the box you wish to mark moving left to right, top to bottom: ")) | |
except ValueError: | |
print "Please enter a number between 1 and 9." # ValueError if not int() | |
continue | |
if 1 <= input <= 9: | |
if not game.gameState[input-1]: # Check if box is full (default for game.gameState is None) | |
game.gameState[input-1] = player.sym # Add player symbol instead | |
player.moves[input-1] = input # for later comparison with winning combinations | |
break | |
else: | |
print "Please choose an un-chosen box." | |
continue | |
def checkForWinner(player): | |
"""Compare current player moves with all winning combinations""" | |
tests = [[1,2,3],[4,5,6],[7,8,9],[1,4,7],[2,5,8],[3,6,9],[1,5,9],[3,5,7]] | |
for test in tests: | |
if set(test).issubset(set(player.moves)): | |
return True | |
return False | |
def printGameState(game,player1, player2, turn): | |
"""Print the game state in a way that is useful to the player.""" | |
print "Player 1 (" + str(player1.name) + ") is using X. Player 2 (" + str(player2.name) + ") is using O." | |
print "Here is how the game is shaping up so far on turn #: " + str(turn) | |
print "" | |
# Create a copy of game.gameState, then change all None to ' ' for clearer display | |
temp = list(game.gameState) | |
for idx,state in enumerate(temp): | |
if not state: | |
temp[idx] = ' ' | |
# Print the rows and a divider row after first two rows | |
for i in [0,3,6]: | |
print " " + str(temp[i]) + " | " + str(temp[i+1]) + " | " + str(temp[i+2]) | |
if i in [0,3]: | |
print "-----------" | |
print "" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment