SoFunction
Updated on 2025-04-11

Teach you how to train Goko Chess Ai using pytorch

There are 4 files

Gozi Chess Game

Neural Network Model

Training code

Players and the Goji chess battle

 
class Game:
    def __init__(self, h, w):
        # Number of rows         = h
        # Number of columns         = w
        # Chessboard         = [['-' for _ in range(w)] for _ in range(h)]
        # Current player - means empty X first and then O         = 'X'
        # Game winner        self.win_user = None
 
    # Check whether there is any win after this step. y is the row and x is the column. Return True to mean winning.    def check_win(self, y, x):
        directions = [
            # Horizontal, vertical, two diagonal directions            (1, 0), (0, 1), (1, 1), (1, -1)
        ]
        player = [y][x]
        for dy, dx in directions:
            count = 0
            # Check for consecutive identical pieces in four directions            for i in range(-4, 5):  # Check the range of -4 to 4, because the five-piece beads require 5 pieces                ny, nx = y + i * dy, x + i * dx
                if 0 <= ny <  and 0 <= nx <  and [ny][nx] == player:
                    count += 1
                    if count == 5:
                        return True
                else:
                    count = 0
        return False
 
    # Check if you can go down here. Y row x column. Return True to indicate that you can go down.    def check(self, y, x):
        return [y][x] == '-' and self.win_user is None
 
    # Print the board to visualize it    def __str__(self):
        # Determine the width of the row and column numbers        row_width = len(str( - 1))
        col_width = len(str( - 1))
        
        # Generate a chessboard string representation with line and column numbers        result = []
        # Add column number title        (' ' * (row_width + 1) + ' '.join(f'{i:>{col_width}}' for i in range()))
        # Add divider (optional)        (' ' * (row_width + 1) + '-' * (col_width * ))
        # Add a chessboard line        for y, row in enumerate():
            # Add line number            (f'{y:>{row_width}} ' + ' '.join(f'{cell:>{col_width}}' for cell in row))
        return '\n'.join(result)
 
    # One-step chess    def set(self, y, x):
        if self.win_user or not (y, x):
            return False
        [y][x] = 
        if self.check_win(y, x):
            self.win_user = 
            return True
         = 'X' if  == 'O' else 'O'
        return True
    #All-round    def heqi(self):
        for y in range():
            for x in range():
                if [y][x]=='-':
                    return False
        return True
    

#Players go down by themselvesdef run_game01():
    g = Game(15, 15)
    while not g.win_user:
        # Print the current board status        while 1:
            print(g)
            try:
                y,x=input(+':').split(',')
                x=int(x)
                y=int(y)
                if (y,x):
                    break
            except Exception as e:
                print(e)
    print(g)
    print('winner',g.win_user)
 
 
 

import torch
import  as nn
import  as optim
from game import Game

class MyMod():
    def __init__(self, input_channels=1, output_size=15*15):
        super(MyMod, self).__init__()
        
        # Define a convolutional layer for extracting features        self.conv1 = nn.Conv2d(input_channels, 32, kernel_size=3, padding=1)  # Output 32 x 15 x 15        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)  # Output 64 x 15 x 15        self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)  # Output 128 x 15 x 15        
        # Define a fully connected layer for final score prediction        self.fc1 = (128 * 15 * 15, 1024)  # After flattening, pass in full connection layer        self.fc2 = (1024, output_size)  # Output 15*15 score prediction
    def forward(self, x):
        # Convolutional layer -> Activation function -> Maximum pooling        x = (self.conv1(x))
        x = (self.conv2(x))
        x = (self.conv3(x))
        
        # Flatten the output of the convolution layer into one dimension        x = ((0), -1)
        
        # Full connection layer        x = (self.fc1(x))
        x = self.fc2(x)
        return x
    # Save model weights    def save(self, path):
        (self.state_dict(), path)

    # Loading model weights    def load(self, path):
        self.load_state_dict((path))

#Improve output The probability of places with chess pieces = 0 to avoid going down these places# Enter the Game object and the MyMod object to get the most likely dropping point (row y, column x)def input_qi(g: Game, m: MyMod):
    # Get the current board status    board_state =   # Use to get the status of the current board (a 2D list of 15x15)    
    # Convert the board state to PyTorch's Tensor and add a dimension (batch_size = 1)    board_tensor = ([[1 if cell == 'X' else -1 if cell == 'O' else 0 for cell in row] for row in board_state], 
                                 dtype=torch.float32).unsqueeze(0).unsqueeze(0)  # The shape becomes (1, 1, 15, 15)    
    # Pass in to get the score for each position    output = m(board_tensor)
    
    # Convert the output to a probability value (can be normalized using softmax)    probabilities = (output, dim=-1).view(, ).detach().numpy()  # becomes (15, 15) size    
    # Set the probability of the position of the existing chess piece to -inf to avoid selecting these positions    for y in range():
        for x in range():
            if board_state[y][x] != '-':
                probabilities[y, x] = -float('inf')  # Set the probability of a place where there are already chess pieces to -inf    
    # Find the most likely place    max_prob_pos = divmod((), )  # Get the row and column coordinates of the maximum probability    
    # Make sure that the returned legal location    y, x = max_prob_pos
    
    return (y, x), output  # Return coordinates and model output


import os
import torch
import  as optim
import  as F
from mod import MyMod, input_qi, Game

# Two weight files represent X and O respectivelyMX = 'MX'
MO = 'MO'

# Load the model, initialize if the file does not existdef load_model(model, path):
    if (path):
        (path)
        print(f"Loaded model from {path}")
    else:
        print(f"{path} not found, initializing new model.")
        # Here you can add some code to initialize the model, for example:        # (init_weights) If initialization of weights is required
# Initialize the modelmodx = MyMod()
load_model(modx, MX)

modo = MyMod()
load_model(modo, MO)

# Define an optimizerlr=0.001
optimizer_x = ((), lr=lr)
optimizer_o = ((), lr=lr)

# Loss function: adjust the loss according to the game resultsdef compute_loss(winner: int, player: str, model_output):
    # Convert the target value to the corresponding tensor    if player == "X":
        if winner == 1:  # X Win            target = (1.0, dtype=torch.float32)
        elif winner == 0:  #Grading game            target = (0.5, dtype=torch.float32)
        else:  # X Lost            target = (0.0, dtype=torch.float32)
    else:
        if winner == -1:  # O Win            target = (1.0, dtype=torch.float32)
        elif winner == 0:  #Grading game            target = (0.5, dtype=torch.float32)
        else:  # O Lose            target = (0.0, dtype=torch.float32)

    # Ensure that the shape of the target value is consistent with model_output, assuming that model_output is a single value    target = (0).unsqueeze(0)  # The shape becomes (1, 1)
    # Calculate using mean square error loss    return F.mse_loss(model_output, target)



# The process of training the modeldef train_game():
    ()
    ()

    # Create a new game instance    game = Game(15, 15)  # The default is 15x15 chessboard    # Backpropagation and optimization    optimizer_x.zero_grad()
    optimizer_o.zero_grad()

    while not game.win_user:  # The game is not over        # X Fang Luozi        x_move, x_output = input_qi(game, modx)  # Get the drop position and model output (x_output is the output of the model)        (x_move[0], x_move[1])  # X Play Chess        
        if game.win_user:
            break
        
        # O Fang Luozi        
        o_move, o_output = input_qi(game, modo)  # Get the drop position and model output (o_output is the output of the model)        #print(o_move,game)
        (o_move[0], o_move[1])  # O Play Chess       

    # Get the results of the match    winner = 0 if () else (1 if game.win_user == 'X' else -1)  # 1 is X win, -1 is O win, 0 is tie
    # Calculate the loss    loss_x = compute_loss(winner, "X", x_output)  # Pass the model output to the calculation loss function    loss_o = compute_loss(winner, "O", o_output)  # Pass the model output to the calculation loss function
    # Calculate the loss and perform backpropagation    loss_x.backward()
    loss_o.backward()

    # Update weight    optimizer_x.step()
    optimizer_o.step()
    print(game)
    return loss_x.item(), loss_o.item()


# Train multiple roundsdef train(num_epochs,n):
    k=0
    for epoch in range(num_epochs):
        loss_x, loss_o = train_game()
        print(f"Epoch [{epoch+1}/{num_epochs}], Loss X: {loss_x}, Loss O: {loss_o}")
        k+=1
        if k==n:
            ('MO')
            ('MX')
            print('saved')
            k=0

# Start trainingtrain(50000,1000)

from game import Game
from mod import MyMod,input_qi


#Players down X ai down Odef playX():
    m=MyMod()
    ('MO')
    g=Game(15,15)
    while 1:
        print(g)
        if () or g.win_user:
            break
        while 1:
            try:
                r=input('X:')
                y,x=(',')
                y=int(y)
                x=int(x)
                if (y,x):
                    break
            except Exception as e:
                print(e)
        if () or g.win_user:
            break
        while 1:
            (y,x),_=input_qi(g,m)
            if (y,x):
                break
    print(g)
    print('winner',g.win_user)


#Players go down O ai go down Xdef playO():
    m=MyMod()
    ('MX')
    g=Game(15,15)
    while 1:
        
        if () or g.win_user:
            break
        while 1:
            (y,x),_=input_qi(g,m)
            if (y,x):
                break
        if () or g.win_user:
            break
        print(g)
        while 1:
            try:
                r=input('O:')
                y,x=(',')
                y=int(y)
                x=int(x)
                if (y,x):
                    break
            except Exception as e:
                print(e)
    print(g)
    print('winner',g.win_user)

playX()

Summarize

This is the end of this article about training Gozi Ai with pytorch. For more related content on training Gozi Ai, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!