Python-based tictoe game example
This article describes the Tic Tac Toe game implemented by Python. We will share this with you for your reference. The details are as follows:
Description
I used python to implement the game player. The entire framework was self-conceived and satisfied. In addition, I wrote more than 90% codes one by one.
The minimax algorithm is not fully understood yet, so I have referenced the code here and made modifications.
Features
You can choose one of the four combat modes: Everyone, man/machine, man/machine, and machine.
Computer player AI uses the minimax algorithm with apha-beta pruning
During thinking, computer players always have an imaginary enemy ". To make the minimax algorithm run
Code
# Author: hhh5460 # Date: August 1, June 26, 2017 # Board class Board (object): def _ init _ (self): # self. _ board = '-' * 9 # pitfall !! Self. _ board = ['-' for _ in range (9)] self. _ history = [] # chess set # press the specified action and place the pawn def _ move (self, action, take): if self. _ board [action] = '-': self. _ board [action] = take self. _ history. append (action, take) # add to the chess set # undo action, remove the pawn set def _ unmove (self, action): self. _ board [action] = '-' self. _ history. pop () # checkboard snapshot def get_board_snapshot (self): return self. _ board [:] # obtain the valid walk def get_legal_actions (self): actions = [] For I in range (9): if self. _ board [I] = '-': actions. append (I) return actions # determine whether the walk method is legal def is_legal_action (self, action): return self. _ board [action] = '-' # terminate detection def teminate (self): board = self. _ board lines = [board [0: 3], board [3: 6], board [6], board [0: 3], board [1: 3], board [2:: 3], board [0: 4], board [] if ['X'] * 3 in lines or ['O'] * 3 in lines or '-' not in board: return True else: Return False # def get_winner (self): board = self. _ board lines = [board [0: 3], board [3: 6], board [6], board [0: 3], board [1: 3], board [2:: 3], board [0: 4], board [:7:2] if ['X'] * 3 in lines: return 0 elif ['O'] * 3 in lines: return 1 else: return 2 # print the checkboard def print_ B (self): board = self. _ board for I in range (len (board): print (board [I], end = '') if (I + 1) % 3 = 0: print () # print the chess spectrum def print_history (s Elf): print (self. _ history) # Player class Player (object): ''' the Player only does two things: Think, fall into the Child 1. think --> get the result 2. sub-accounts --> execute the Walk Method and change the checker '''def _ init _ (self, take = 'X'): # The default take = 'X' self. take = take def think (self, board): pass def move (self, board, action): board. _ move (action, self. take) # human Player class HumanPlayer (Player): def _ init _ (self, take): super (). _ init _ (take) def think (self, board): while True: action = input ('P Lease input a num in 0-8: ') if len (action) = 1 and action in '123' and board. is_legal_action (int (action): return int (action) # COMPUTER Player class AIPlayer (Player): def _ init _ (self, take): super (). _ init _ (take) def think (self, board): print ('ai is thinking... ') take = ['x', 'O'] [self. take = 'X'] player = AIPlayer (take) # hypothetical competitor !!! _, Action = self. minimax (board, player) # print ('OK') return action # extremely small search, α-β pruning def minimax (self, board, player, depth = 0 ): ''' reference: else if self. take = "O": bestVal =-10 else: bestVal = 10 if board. teminate (): if board. get_winner () = 0: return-10 + depth, None elif board. get_winner () = 1: return 10-depth, None elif board. get_winner () = 2: return 0, None for action in board. get_legal_actions (): # traverse the valid routing board. _ move (action, self. take) val, _ = player. minimax (board, self, depth + 1) # Switch to hypothetical competitor !!! Board. _ unmove (action) # undo walk, backtrack if self. take = "O": if val> bestVal: bestVal, bestAction = val, action else: if val <bestVal: bestVal, bestAction = val, action return bestVal, bestAction # Game class Game (object): def _ init _ (self): self. board = Board () self. current_player = None # generate player def mk_player (self, p, take = 'X'): # p in [0, 1] if p = 0: return HumanPlayer (take) else: return AIPlayer (take )# Switch player def switch_player (self, player1, player2): if self. current_player is None: return player1 else: return [player1, player2] [self. current_player = player1] # print winner def print_winner (self, winner): # Winner in [0, 1, 2] print (['winner is player1', 'winner is player2 ', 'draw'] [winner]) # run the game def run (self): ps = input ("Please select two player's type: \ n \ t0.Human \ n \ t1.AI \ nSuch as: 0 0 \ n ") p1, p2 = [int (P) for p in ps. split ('')] player1, player2 = self. mk_player (p1, 'x'), self. mk_player (p2, 'O') # execute X first, and then execute O print ('\ nGame start! \ N') self. board. print_ B () # display the chessboard while True: self. current_player = self. switch_player (player1, player2) # Switch the current player action = self. current_player.think (self. board) # after the current player thinks about the board, get the trick self. current_player.move (self. board, action) # The current player moves to change the board self. board. print_ B () # display the current Board if self. board. teminate (): # determine whether to terminate winner = self based on the current Board. board. get_winner () # Get winner 0, 1, 2 break self. print_winner (winner) print ('G Ame over! ') Self. board. print_history () if _ name _ =' _ main _ ': Game (). run ()
It is the result of a battle for everyone