Created
March 14, 2016 12:45
-
-
Save devpruthvi/d25ccd5fb0f0915d6c29 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
#!/usr/bin/env python | |
#Author: Pruthvi Raj ( [email protected] ) | |
#Date: 10-March-2016 | |
import re, copy, random | |
class TicTacToe: | |
def __init__(self): | |
self.magic_square = [8, 1, 6, 3, 5, 7, 4, 9, 2] | |
self.player_marker = 'X' | |
self.computer_marker = 'O' | |
self.board = [' '] * 9 | |
self.board_design = "| %s | | %s | | %s |\n\n| %s | | %s | | %s |\n\n| %s | | %s | | %s |\n" | |
self.player_playing = "n" | |
def print_board(self): | |
self.boardValues = [x for x in tuple(self.board) if x != '0'] | |
print(self.board_design) % tuple(self.board) | |
def start_game(self): | |
self.player_playing = raw_input("Are you making the first move (y/n) :") | |
while self.player_playing not in ['y', 'n']: | |
print('Please enter either "y" or "n"') | |
self.player_playing = raw_input("Are you making the first move (y/n) :") | |
print('Your moves are %ss' % self.player_marker) | |
self.print_board() | |
print("Welcome to the Tic Tac Toe Game") | |
self.game_end = False | |
while not self.game_end: | |
if self.player_playing == "y": | |
self.get_player_move() | |
else: | |
self.get_computer_move() | |
self.print_board() | |
player_magic_matrix = [self.magic_square[x] if self.board[x] == 'X' else 0 for x in | |
range(0, len(self.board))] | |
computer_magic_matrix = [self.magic_square[x] if self.board[x] == 'O' else 0 for x in | |
range(0, len(self.board))] | |
if self.check_win_magic_matrix(player_magic_matrix): | |
print("You won!!") | |
self.game_end = True | |
elif self.check_win_magic_matrix(computer_magic_matrix): | |
print("Computer won") | |
self.game_end = True | |
elif self.is_board_full(): | |
print("Game Drawn!") | |
self.game_end = True | |
def is_board_full(self): | |
if self.board.count(' ') == 0: | |
return True | |
else: | |
return False | |
def check_win_magic_matrix(self, matrix): | |
vals = [sum(matrix[0:3]), sum(matrix[3:6]), sum(matrix[6:9]), | |
sum([matrix[0], matrix[4], matrix[8]]) | |
, sum([matrix[2], matrix[4], matrix[6]]) | |
, sum([matrix[0], matrix[3], matrix[6]]) | |
, sum([matrix[1], matrix[4], matrix[7]]) | |
, sum([matrix[2], matrix[5], matrix[8]])] | |
if 15 in vals: | |
return True | |
else: | |
return False | |
def get_computer_move(self): | |
print("Computer made a move:") | |
move_made = [False] | |
for i in range(0, len(self.board)): | |
board_copy = copy.deepcopy(self.board) | |
if self.is_not_used(i): | |
board_copy[i] = self.computer_marker | |
board_copy_magic_matrix = [self.magic_square[x] if board_copy[x] == 'O' else 0 for x in | |
range(0, len(board_copy))] | |
if self.check_win_magic_matrix(board_copy_magic_matrix): | |
self.board[i] = self.computer_marker | |
move_made[0] = True | |
break | |
if not move_made[0]: | |
for i in range(0, len(self.board)): | |
board_copy = copy.deepcopy(self.board) | |
if self.is_not_used(i): | |
board_copy[i] = self.player_marker | |
board_copy_magic_matrix = [self.magic_square[x] if board_copy[x] == 'X' else 0 for x in | |
range(0, len(board_copy))] | |
if self.check_win_magic_matrix(board_copy_magic_matrix): | |
self.board[i] = self.computer_marker | |
move_made[0] = True | |
break | |
if not move_made[0]: | |
move_made[0] = self.make_random_move([0, 2, 6, 8]) | |
if not move_made[0]: | |
move_made[0] = self.make_random_move([4]) | |
if not move_made[0]: | |
move_made[0] = self.make_random_move([1, 3, 5, 7]) | |
self.player_playing = "y" | |
def make_random_move(self, spaces_to_check): | |
free_spaces = [x for x in spaces_to_check if self.board[x] == ' '] | |
if len(free_spaces) > 0: | |
random_free_space = random.choice(free_spaces) | |
self.board[random_free_space] = self.computer_marker | |
return True | |
return False | |
def is_not_used(self, index): | |
return self.board[index] == ' ' | |
def get_player_move(self): | |
valid_player_move = False | |
while not valid_player_move: | |
player_move = raw_input("Enter your move [1-9]: ") | |
if re.match('\d+', player_move): | |
player_move_integer = int(player_move) | |
if player_move_integer < 1 or player_move_integer > 9: | |
print("Please enter a valid move") | |
elif self.board[player_move_integer - 1] != ' ': | |
print("Please enter a valid move") | |
else: | |
self.board[player_move_integer - 1] = self.player_marker | |
valid_player_move = True | |
else: | |
print("Only integers [1-9] are allowed\nPlease enter a valid move") | |
self.player_playing = "n" | |
if __name__ == "__main__": | |
Game = TicTacToe() | |
Game.start_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment