You are on page 1of 6

2018 4th International Conference on Green Technology and Sustainable Development (GTSD)

Design A Chess Movement Algorithm and Detect the Movement by


Images Classification Using Support Vector Machine Classifier*
Ngo Luong Thanh Tra, Phung Tri Cong, Nguyen Duy Anh


Abstract - A game-playing AI (artificial intelligence) has been


Table 1 show position evaluation, which have been done
developed rapidly through the years. Chess is usually chosen to
be a basic game to verify the AI algorithm. This paper presents a before applying the Minimax algorithm. This evaluation is
basic heuristic algorithm, which is Minimax algorithm and the key of this algorithm because it decides which movement
Alpha–beta pruning. In addition, the chess movement can be have been chosen.
detected by an image, which is taken by a camera at the top, and
after that make the best counter’s move. Image processing
A search tree is created which can choose the best move,
method combined with SVM (support vector machine) model was which means the best point for each player at the time, by
used to improve the detection of the chess moves. A computer using the Minimax algorithm. Moreover, the alpha-beta
vision program opened separate MATLAB window to run totally pruning is used to cut down the time of searching.
in the background.
After studying a step-by-step to build a chess AI [1], the
Keywords: Chess program; Image processing; MATLAB; algorithm is created, which shows in figure 1. Moreover, the
support vector machine value of chessboard matrix and a given depth will be
I. INTRODUCTION mentioned in section: “Image processing”.

Not only the chess movement algorithm but the moving


detection by image had been developed many years ago.
“Canny” filter was used to determine the chess pieces on the
chess board. However, the biggest problem is: “How to
reduce the disturbance cause of the light?”. Firstly, the
algorithm is created by using minimax algorithm and Alpha-
beta pruning. A heuristic is a core of the knowledge which is
mainly used in all the algorithm. After designing the
algorithm, an opponent move is detected by using image
processing. In some previous research, a “canny” threshold
was used to identify an alteration. SVM classifier solves a
problem of the light intensity of the environment. If the light
change, the SVM model will be trained again. After some
training (limited), a computer can recognize more correctly
the chess move. In this study, the chess algorithm, the
detection, and SVM model are built in MATLAB program.
II. METHODOLOGY
A. The chess AI
TABLE 1. POSITION EVALUATION
Chess pieces Position evaluation
Pawn 1
Knight 3
Bishop 4
Rook 5
Queen 9
King 100

* This work was supported by Autoss company.


Ngo Luong Thanh Tra is a senior at Faculty of Mechanical
Engineering, Ho Chi Minh City University of Technology. His major is Figure 1. The chess AI algorithm
mechatronic engineering.
Phung Tri Cong is lecturer in departments of Mechatronic, Faculty of
The most important one is the Minimax algorithm so we
Mechanical Engineering, Ho Chi Minh City University of Technology. introduce a search tree and how to use it in Figure 2. The
Duy Anh Nguyen is lecturer in departments of Mechatronic, Faculty of position evaluation of intermediate nodes does not have to be
Mechanical Engineering, Ho Chi Minh City University of Technology. considered in all the program. The reason is intermediate
(e-mail: duyanhnguyen@hcmut.edu.vn)

978-1-5386-5126-1/18/$31.00 ©2018 IEEE 335


2018 4th International Conference on Green Technology and Sustainable Development (GTSD)

nodes can be quantified by final nodes which is evaluated


after choosing the given depth.

Figure 2. The search tree [1]

The Minimax algorithm describes in figure 3 and all legal Figure 4. The α-pruning
moves are explored to the given depth and the position is
evaluated at the ending “leaves” of the tree. Then, we As figure 4 shows that if value of one of these nodes,
calculate either the smallest or the largest value of child which are calculated after C node, is more than C node’s
nodes to the parent node, depending on whether it’s a white value, the α-pruning works and it goes back to the parent
or black move and a human or robot move. The minimize or node. The notation is established but the input does not be
mention. So, in the next section, the image and the
maximize value have to be found at each level.
chessboard matrix are processed.
Moreover, the time of reckoning based on the given depth
so the value of this one must be chosen carefully. In one turn, B. Image processing
player have approximated 30 legal moves so if the given
depth is choosing at 10 for the search tree, final nodes
explore 3010 nodes. It is too much so it has to be cut down.

Figure 5. Chessboard image

Camera having 640x480 pixel resolution is used to


snapshot a picture of the chess board. Because the chess
board is square we have to crop the image to 480x480 pixel
resolution. The setup board can be seen in figure 5.

Figure 3. The Minimax algorithm

At the beginning of this section, the Alpha-Beta pruning


is noticed to cut down the time. Figure 4 shows the α-pruning
at the maximum position. In the order hand, it turns to the β-
pruning. As this pruning, we can save a lot of time, what
spend on calculate either the maximum or the minimum Figure 6. Experimental setup
value.
Logitech C170 camera is used to identify the chess move
and the USB port is also used to connect the camera and the

336
2018 4th International Conference on Green Technology and Sustainable Development (GTSD)

computer together. As many previous project, the camera is black and the “1” for white, otherwise, squares have no chess
hang on a bracket, which is fixed with the table of the chess piece have the “2” value as figure 9 shows.
board, and we recognize that it cumbered the robot’s working
space.
We decide that hang the camera on the robot tool - the
gripper. By this method, the robot can move flexibility
everywhere without concern about the bracket’s zone. The
experimental setup in figure 6 made by 3D printing
technology.
In this paper, we do not define a single chess piece to find
the movement. However, a capture image’s function is built
around Matlab software. Because the pixel resolution of the
Figure 9. The “white-black” matrix
webcam is not square like a chessboard, the snapshot image
must be cropped and divided into 64 “mini” squares, which In reference papers [1] and [2], the simple move (like
have 60x60 pixel resolution in each square. In addition, if the move the pawn in the example) can be easily recognized by
picture has three color channel R, B, G like every snapshot calculate the delta “having-chess” matrix between before and
picture, it is give us many disadvantages to find which square after movement. They work perfectly with the “en passant”
have a chess piece. To solve this problem, a copy picture move, the castle move. The problem appears when we try to
from the original one but in grayscale is set and show in recognized the “take a piece” move. The solution is
figure 7. calculated the delta “black-white” matrix between before and
after movement (Figure 10 and Figure 11).

Figure 7. Capture image in grayscale

Figure 10. The chessboard matrix before the movement

Figure 8. The “having-chess” matrix

The training file, which is created in SVM model,


determine which a chess board’s square have a chess pieces
and what the color is, which will be mentioned in the next
section. After having all of these things, we start to obtain a
chess move. An 8x8 matrix, which can be call a “having-
chess” matrix, is created , which likes an 8x8 chessboard, and
the value of “1” stand for which square have a chess piece, Figure 11. The chessboard matrix after the movement
otherwise, the value is “0” . These are represented in figure 8.
After that we calculate the delta of each kind of matrix
Moreover, by the grayscale image, another 8x8 matrix, which
and got the value in figure 12. In the delta of 2 “having-
can be call a “white-black” matrix, is created with the “0” for

337
2018 4th International Conference on Green Technology and Sustainable Development (GTSD)

chess” matrix, “-1” element describes the chess move


beginning point. In the delta of 2 “white-black” matrix, the
other “-1” element form the previous one describes the chess
move ending point. All the movement can be convert to
notation in [1].

Figure 12. The delta of 2 “having-chess” matrix (right) and 2 “white-


black” matrix (left)

The running time of the process is 2,5 second. It is from Figure 14. A having chess piece square
capture image to the natation of the chess. In the next section,
the way using to define the movement is concerned. After all of these things, the program can work smoothly.
The experimental model is combined all three sections.
C. SVM classifier Chapter III will show the way robot work and check the
SVM classifier is an algorithm which is a supervised position error.
machine learning. It can be used for classification. A HOG
III. EXPERIMENTAL MODEL
vector, which uses for establishing the “having-chess”
matrix, and a color vector, which uses for establishing the Application software is built by MATLAB and Sysmac
“white-black” matrix, are created to take a specific and save Studio. Using Yaskawa robot in figure 15 to check the study.
to a text file with a label. This is an important training step
prepare for image processing.
Histogram of Oriented Gradients vector or HOG vector to
make shorter is a feature descriptor. It was used in computer
vision, image processing, detecting object. A grayscale
cropped image is converted to a gradient image. Each pixel
of this image calculated the change in intensity of that same
position in the original image with a given direction. To get
the full range of direction, gradient images in the x and y
directions are computed.
In the square have no a chess piece, the HOG vector has
many horizontal and vertical vector in figure 13, otherwise, a
chess piece makes the vector have many directions in figure
14.
Figure 15. MA1440 Yaskawa robot

The 6-axis MOTOMAN MA1440 is designed to work


with high demands of arc welding applications. Autoss
company, which have a cooperation with Nguyen Duy Anh,
have one so this advantage must be taken. It has six axes so it
can work flexible like human hand. This table below shows
the specifications of this robot [4].

TABLE 2. SPECIFICATIONS
Controller DX200
Controlled Axis 6
Payload 6kg
Vertical reach 2511mm
Horizontal reach 1440mm
Repeatability ±0.08mm
Mounting Floor, wall, ceiling
S-axis (turning): -170° - +170°
Range of motion L-axis (lower arm): -90° - +155°
Figure 13. A none chess piece square U-axis (upper arm): -175° - +240°

338
2018 4th International Conference on Green Technology and Sustainable Development (GTSD)

R-axis (arm roll): -150° - +150° x E5:


‫ ݔ‬ൌ ͵ǡͷǤͶͺ ൌ ͳ͸ͺ ݉݉
Range of motion B-axis (wrist bend): -135° - +90° ቄ
‫ ݕ‬ൌ ͵ǡͷǤͶͺ ൌ ͳ͸ͺ݉݉
T-axis (tool flange): -210° - +210°
A square with length is 48mm so letter “E” stand for three
S-axis (swivel base): 230°/s
and a half square and so on. The Z value depend on what
L-axis (lower arm): 200°/s chess that the robot grip. Wrong height can make a lot of
U-axis (upper arm): 230°/s damage to the chess and the gripper. Table 3, which is
Maximum speed measured from bottom, is created to prevent this cause.
R-axis (arm roll): 430°/s
B-axis (wrist bend): 430°/s TABLE 3. HEIGHT VALUE
T-axis (tool flange): 630°/s Chess pieces Height value
Mass 130kg
Pawn 26mm
Power requirements 1.5kVA Knight 16mm
Bishop 35mm
The interface in figure 16 is made by GUI (Graphical Rook 41mm
User Interface) Matlab. When push the “Start” button, Queen 47mm
program will start. If the human takes the white piece, they King 47mm
have to push the “White Done” button to take a snapshot and
calculate the notation of the robot – the black piece. The In the order hand, a time from the beginning to the end is
“Notation” rectangle shows the movement of each “Player to the most important that this study focus on. The robot delay
turn”. one second each time the robot stops to pick and place the
chess because it need to finish this work before moving order
place. Table 4 describes the total time that the computer and
the robot need to finish one move. Base on this table, the time
can be reduced by increase speed of robot.
After the trajectory is explored [3], all positions to the
robot variable are transferred and run the function that
already have in the robot arm. All systems show in Figure 19.

Figure 16. Image of the interface

Checking the image processing is the first step, which is


mentioned in section “Image process”. Secondly, the position
error must be check carefully. The user coordinate system of
the robot in figure 17 helps us control easily.

O
x

Figure 18. Position E7

TABLE 4. TIME TABLE


Movement Time to Number Delay Speed Total
recognize of robot time to of time
Figure 17. The setup coordinate the movement grip robot
notation
For example, the E7E5 nation have the coordinate below: Basic 2,5s 5 2s 15% 10s
“Take a 2,5s 9 4s 15% 15s
x E7: piece”
‫ ݔ‬ൌ ͵ǡͷǤͶͺ ൌ ͳ͸ͺ݉݉ "en passant" 2,5s 9 4s 15% 15s

‫ ݕ‬ൌ ͳǡͷǤͶͺ ൌ ͹ʹ݉݉ Castled 2,5s 9 4s 15% 15s

339
2018 4th International Conference on Green Technology and Sustainable Development (GTSD)

Applied Sciences, Engineering & Technology, 02-04 June 2016,


HCMUT, Vietnam
[4] Motoman MA1440, YASKAWA Europe GmbH datasheet.
[5] Franciscus C. A. Groen, A Chess-Playing Robot: Lab Course in Robot
Sensor Integration, IEEE Transactions on Instrumentation and
Measurement, Vol. 41, No: 6, Page 911-914, 1992.
[6] Emir Sokic, Simple Computer Vision System for Chess Playing Robot
Manipulator as a Project-based Learning Example, IEEE, Page 75-
79, 2008.

Figure 19. Experimental setup

IV. RESULT AND CONCLUSION


A. Results
After seven matches, the system works stable in taking
picture, image processing, choose best notation and move
the chess piece. However, if the light turns too dark or too
bright immediately, computer cannot detect the chess board
or the chess move. So, we have to train the SVM model in
this case and train many times in a day. Many experiments
have been carried out, hence the best choice is to set the light
in the zone, which we have already trained the SVM model.
B. Discussion and future study
The paper gives an overview of applying SVM algorithm
to obtain the chess move for an automatic chess-playing
robot. The system also experimented with human opponents
in real matches. Results can be improved by increasing the
number of nodes on the chessboard. The given depth can go
up to 10 at 5 seconds. In addition, the table and the
chessboard have to fix together because it is easily to take a
picture and also playing. However, if this one is used for
entertainment, we have to make a new one which can be

ACKNOWLEDGMENT
The author would like to thank his classmate at Ho Chi
Minh City University of Technology, especially Associate
Prof. Nguyen Duy Anh for his support.

REFERENCES
[1] N.D. Anh, L.T. Nhat, T. Van Phan Nhan (2015) “Design and control
automatic chess-playing robot arm”, Lecture Notes in Electrical
Engineering (Vol 371), 485-496
[2] Mustafa ARDA, Hilmi KUŞÇU, Emir ÖZTÜRK (2013) Obtain chess
moves with image processing. In: Proceeding of the Unitech Grabovo
international scientific conference.
[3] THANH NHAT LUONG, HOANG LIEN SON CHAU, TRI CONG
PHUNG, DUY ANH NGUYEN (2016) “Trajectory optimization for
chess-playing robot arm”, 5th World Conference on

340

You might also like