python tic-tac-toe-text version (part 2)

The last article python Tic-Tac-Toe-Text Version (Part 1) The chess strategy on the computer side is random. If there are any positions that can be played, a random position is selected;

In reality, there are no such stupid opponents. It is still necessary to give the computer a normal IQ:

With such a few simple strategies, the computer has a little AI effect.

Under very obvious circumstances, a computer will not lose; in most cases, it can remain unbeaten.

Complete code

If you want to do better, you can modify the prompt description, gameplay description, etc.

import random

def display_instruct():print("game rules")

def new_board():return[" "]*9

def display_board(board):print(
  f"""
  |{ board[0]}|{board[1]}|{board[2]}||{board[3]}|{board[4]}|{board[5]}||{board[6]}|{board[7]}|{board[8]}|"""
    )

def pieces():
 is_go_first =input("Do you want to go first? (y/n): ").lower()if is_go_first =="y":
  human,computer ="X","O"else:
  human, computer ="O","X"return human, computer
    
def legal_moves(board):
 moves =[]for move inrange(9):if board[move]==" ":
   moves.append(move)return moves

def human_move(board):
 legal =legal_moves(board)
 move =int(input("Where are you going? (0 - 8):"))while move not in legal:
  move =int(input("Where are you going? (0 - 8):"))return move

def computer_move(board, computer, human):
 # Copy chess board
 board = board[:]
 # Optimal location
 best_moves =(4,0,2,6,8,1,3,5,7)
    
 # If the computer can win, go to that position
 for move inlegal_moves(board):
  board[move]= computer
  ifwinner(board)== computer:return move
  # The test fails, cancel and restart
  board[move]=" "
    
 # If the player can win, move to that position
 for move inlegal_moves(board):
  board[move]= human
  ifwinner(board)== human:return move
  # The test fails, cancel and restart
  board[move]=" "

 # If both sides cannot, choose the best position to play
 for move in best_moves:if move inlegal_moves(board):return move

def next_turn(turn):if turn =="X":return"O"else:return"X"

def winner(board):
 WAYS_TO_WIN =((0,1,2),(3,4,5),(6,7,8),(0,3,6),(1,4,7),(2,5,8),(0,4,8),(2,4,6))for row in WAYS_TO_WIN:if board[row[0]]== board[row[1]]== board[row[2]]!=" ":
   winner = board[row[0]]return winner

 if" " not in board:return"draw"return None

def congrat_winner(winner,human,computer):if winner!="draw":print(winner,"won!\n")else:print("这是一场draw")if winner == human:print("Congratulations on your victory! !")
 elif winner == computer:print("you lose!")display_instruct()
human,computer =pieces()
turn ="X"
board =new_board()display_board(board)while not winner(board):if turn ==human:
  move =human_move(board)
  board[move]=human 
 else:
  move =computer_move(board, computer, human)
  board[move]= computer
 display_board(board)
 turn =next_turn(turn)
 the_winner =winner(board)congrat_winner(the_winner,human,computer)

Of course, this is only for Tic Tac Toe, which has relatively limited steps;

If it is Gobang, then the computer will need to calculate more steps, and the judgment of winning will be more complicated. Those who are interested can think about the realization of Gobang game;

  1. What kind of data structure does the chessboard adopt?
  2. How to judge win or lose?
  3. Computer chess strategy?

Part II-Python Gobang (Part 1)

The effect of my code is as follows:

x won

The specific board realization and the realization of winning or losing will be explained in the next article.

( (End of full text)

Recommended Posts

python tic-tac-toe-text version (part 2)
python Tic-Tac-Toe-text version (on)
Ubuntu16.04 switch python version
PyCharm set Python version
Concurrent requests in Python (part 2)
Python implements text version minesweeper
Python version of OpenCV installation
Python version business card management system
Install TensorFlow (python2.7 version) on Ubuntu
Install the latest Python 3.6 version on Ubuntu
Which version of python does windows support
Python implements AI automatic version greedy snake