-
-
Save xmonader/b94bcb90605d79b508f4addf2405306a to your computer and use it in GitHub Desktop.
tictactoe.py
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
| import copy | |
| import sys | |
| from random import choice | |
| NEXT_PLAYER = {'X':'O', 'O':'X'} | |
| class board(list): | |
| WINS = ((0,1,2), (3,4,5), (6,7,8), (0, 3, 6), (1,4,7), (2,5,8), (0,4,8), (2,4,6)) | |
| def done(self): | |
| for w in board.WINS: | |
| # print("SELF.BOARD_CHECK: ", self.board[w[0]], self.board[w[1]], self.board[w[2]]) | |
| if self[w[0]] == self[w[1]] == self[w[2]]: | |
| if self[w[0]] == 'X': | |
| return True, 'X' | |
| elif self[w[0]] == 'O': | |
| return True, 'O' | |
| if all([x in ['O', 'X'] for x in self]) == True: | |
| return True, 'TIE' | |
| else: | |
| return False, 'GOING' | |
| def printboard(self): | |
| rows = self[0:3], self[3:6], self[6:] | |
| print("\n") | |
| for row in rows: | |
| for cell in row: | |
| print(cell, end=" | ") | |
| print("\n--------------") | |
| def empty_spots(self): | |
| return [int(i) for i in self if i.isdigit()] | |
| class Move: | |
| def __init__(self, score=0, idx=0): | |
| self.score = score | |
| self.idx = idx | |
| class Game(): | |
| def __init__(self, player='X', difficulty=9): | |
| self.current_player = 'X' | |
| self.difficulty = difficulty | |
| self.board = board([str(i) for i in range(9)]) | |
| self.AI_PLAYER = False | |
| # 0 1 2 | |
| # 3 4 5 | |
| # 6 7 8 | |
| def change_player(self): | |
| self.current_player = NEXT_PLAYER[self.current_player] | |
| def getbestmove(self, board, player): | |
| done, winner = board.done() | |
| # print("BOARD: ", board) | |
| if done is True: | |
| if winner == self.AI_PLAYER: | |
| return Move(10, None) | |
| elif winner != 'TIE': #human | |
| return Move(-10, None) | |
| else: | |
| return Move(0, None) | |
| empty_spots = board.empty_spots() | |
| # print("EMPTY INDICES: ", empty_spots) | |
| moves = [] | |
| for idx in empty_spots: | |
| newboard = copy.copy(board) | |
| newboard[idx] = player | |
| move = Move() | |
| move.score = self.getbestmove(newboard, NEXT_PLAYER[player]).score | |
| move.idx = idx | |
| moves.append(move) | |
| if player == self.AI_PLAYER: | |
| return max(moves, key=lambda m: m.score) | |
| else: | |
| return min(moves, key=lambda m: m.score) | |
| def startgame(self): | |
| while True: | |
| self.board.printboard() | |
| # print(self.AI_PLAYER, self.current_player) | |
| if self.AI_PLAYER != self.current_player: | |
| move = input("Enter move: ") | |
| self.board[int(move)] = self.current_player | |
| else: | |
| if self.current_player == self.AI_PLAYER: | |
| if len(self.board.empty_spots()) <= self.difficulty: | |
| print("AI MOVE..") | |
| move = self.getbestmove(self.board, self.AI_PLAYER) | |
| self.board[move.idx] = self.AI_PLAYER | |
| else: | |
| print("RANDOM GUESS") | |
| self.board[choice(self.board.empty_spots())] = self.AI_PLAYER | |
| self.change_player() | |
| done, winner = self.board.done() | |
| if done is True: | |
| self.board.printboard() | |
| if winner == 'TIE': | |
| print("TIE") | |
| else: | |
| print("WINNER IS :", winner ) | |
| break | |
| def main(): | |
| g = Game(player='X') | |
| g.AI_PLAYER = 'O' | |
| g.startgame() | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment