Created
February 20, 2017 21:32
-
-
Save ANich/8a090d5493be4f3081867d9be2c738f7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Game: | |
| GAME_PIECES = { 1: 'X', 2: 'O' } | |
| WINNING_SEQUENCES = [ | |
| (0, 3, 6), #first column | |
| (1, 4, 7), #second column | |
| (2, 5, 8), #third column | |
| (0, 1, 2), #first row | |
| (3, 4, 5), #second row | |
| (6, 7, 8), #third row | |
| (0, 4, 8), #forward diagonal | |
| (2, 4, 6) #backward diagonal | |
| ] | |
| def __init__(self, game_state=None): | |
| self.game_state = [0] * 9 if game_state == None else game_state | |
| # Our game state is a list where each index is of format: 0 for empty, 1 for player one, 2 for player two | |
| def print_board(self): | |
| board = """ | |
| ------------- | |
| | 0 | 1 | 2 | | |
| ------------- | |
| | 3 | 4 | 5 | | |
| ------------- | |
| | 6 | 7 | 8 | | |
| ------------- | |
| """ #Default Board | |
| for i in range(0, 9): | |
| # We can replace the numbers in our board string using the indexes in our game_state | |
| state = self.game_state[i] | |
| if state != 0: | |
| board = board.replace(str(i), self.GAME_PIECES.get(self.game_state[i])) | |
| print board | |
| def check_for_winner(self, player): | |
| for sequence in self.WINNING_SEQUENCES: | |
| corresponding_game_state = [self.game_state[i] for i in sequence] | |
| if corresponding_game_state[0] == player and corresponding_game_state[1] == player and corresponding_game_state[2] == player: | |
| return True | |
| return False | |
| def check_draw(self): | |
| return reduce(lambda x, y: x != 0 and y != 0, self.game_state) # If no empties left, we draw! | |
| def player_move(self, player): | |
| valid_moves = [index for index, state in enumerate(self.game_state) if state == 0] | |
| is_valid_move = False | |
| while not is_valid_move: | |
| move = raw_input("Player " + str(player) + " please enter your move: ") | |
| try: | |
| move = int(move) | |
| if move in valid_moves: | |
| self.game_state[move] = player | |
| is_valid_move = True | |
| else: print 'Valid moves: ' + str(valid_moves) | |
| except ValueError: | |
| print 'Valid moves: ' + str(valid_moves) | |
| def game_start(self): | |
| self.print_board() | |
| game_playing = True | |
| while game_playing: | |
| self.player_move(1) | |
| self.print_board() | |
| player_one_wins = self.check_for_winner(1) | |
| draw = self.check_draw() | |
| if player_one_wins: | |
| print "Player One Wins!" | |
| game_playing = False | |
| elif draw: | |
| print "Draw Game!" | |
| game_playing = False | |
| else: | |
| self.player_move(2) | |
| self.print_board() | |
| player_two_wins = self.check_for_winner(2) | |
| draw = self.check_draw() | |
| if player_two_wins: | |
| print "Player Two Wins!" | |
| game_playing = False | |
| elif draw: | |
| print "Draw Game!" | |
| game_playing = False | |
| game = Game() | |
| game.game_start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment