preface

Using Python+ Graphics module to achieve AI backgammon. Let’s have a good time

Results show

The source code

import sys
import cfg

from modules.misc.Buttons import *
from modules.ai.playWithAI import *
from modules.online.playOnline import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

"Game start screen"
class gameStartUI(QWidget) :
	def __init__(self, parent=None, **kwargs) :
		super(gameStartUI, self).__init__(parent)
		self.setFixedSize(760.650)
		self.setWindowTitle('Gobang - wechat: ilove-python')
		self.setWindowIcon(QIcon(cfg.ICON_FILEPATH))
		# Background image
		palette = QPalette()
		palette.setBrush(self.backgroundRole(), QBrush(QPixmap(cfg.BACKGROUND_IMAGEPATHS.get('bg_start'))))
		self.setPalette(palette)
		# button
		# -- Man versus machine
		self.ai_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('ai'), self)
		self.ai_button.move(250.200)
		self.ai_button.show()
		self.ai_button.click_signal.connect(self.playWithAI)
		# -- Play online
		self.online_button = PushButton(cfg.BUTTON_IMAGEPATHS.get('online'), self)
		self.online_button.move(250.350)
		self.online_button.show()
		self.online_button.click_signal.connect(self.playOnline)
	"Man versus machine"
	def playWithAI(self) :
		self.close()
		self.gaming_ui = playWithAIUI(cfg)
		self.gaming_ui.exit_signal.connect(lambda: sys.exit())
		self.gaming_ui.back_signal.connect(self.show)
		self.gaming_ui.show()
	"Go Online."
	def playOnline(self) :
		self.close()
		self.gaming_ui = playOnlineUI(cfg, self)
		self.gaming_ui.show()


'''run'''
if __name__ == '__main__':
	app = QApplication(sys.argv)
	handle = gameStartUI()
	font = QFont()
	font.setPointSize(12)
	handle.setFont(font)
	handle.show()
	sys.exit(app.exec_())
Copy the code

The development tools

Python version: 3.6.4

Related modules:

Graphics module.

Environment set up

Install Python and add it to the environment variables.

Note: The graphics module is provided in the related file, which is a py file. It can be placed directly in the current path or in the Site-Packages folder under the Python installation folder.

Introduction of the principle

For a game AI like backgammon, the natural idea is to ask the computer to run through all the possible scenarios and find the optimal spot. There are two questions:

(1) How to try all possible situations once;

(2) how to quantitatively judge the merits and demerits of a landing point.

For the first problem, it’s what’s called a game tree search, and for the second problem, it’s what’s called a choice evaluation function. The selection of evaluation function directly determines the merits of AI algorithm, and its form is also ever-changing. It can be said that each evaluation function is a player, and each player has a different view and response to different types of moves, of course, their ability is also different.

But the game tree search is relatively fixed, its core idea is to let the computer after considering the current situation all possible N steps, including columns (for now it is up to the AI) to make AI maximum score, even step to make AI the minimum score (because the opponent is human, also can choose the optimal strategy).

Of course, such searches are extremely computationally intensive, so pruning is needed to reduce the amount of computation. For example:

Where A represents the AI side and P represents the human side. The AI side searches for the maximum and the human side searches for the minimum. Therefore, the final result of A1 downward search of Layer3 is 4, A2 downward search of Layer3 searches P3 of Layer4 first, and the score obtained is 6. Considering that A1 and A2 of Layer3 are smaller values in P1 downward search of Layer2, while A2 of Layer3 searches P3 of Layer4, The value is already greater than A1 of Layer3, so there is no need to search down, so the path 3 from Layer3 to Layer4 can be cut off.

The essence of the above search strategy is:

Minimax algorithm + Alpha-beta pruning algorithm.

Once you understand the above principles, you can write your own code to implement them. Of course, I made some simplifications in the implementation process, but the core idea is the same.