Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use graphics to realize AI Gobang in Python Game Development

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "Python game development how to use graphics to achieve AI Gobang". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Effect display

Source code import sysimport cfgfrom 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 Interface' 'class gameStartUI (QWidget): def _ _ init__ (self, parent=None, * * kwargs): super (gameStartUI, self). _ _ init__ (parent) self.setFixedSize Self.setWindowTitle ('Gobang-WeChat account: ilove-python') self.setWindowIcon (QIcon (cfg.ICON_FILEPATH)) # background picture palette = QPalette () palette.setBrush (self.backgroundRole () QBrush (QPixmap (cfg.BACKGROUND_IMAGEPATHS.get ('bg_start') self.setPalette (palette) # Button #-Man-Machine Battle self.ai_button = PushButton (cfg.BUTTON_IMAGEPATHS.get (' ai'), self) self.ai_button.move Self.ai_button.show () self.ai_button.click_signal.connect (self.playWithAI) #-- online fighting self.online_button = PushButton (cfg.BUTTON_IMAGEPATHS.get ('online'), self) self.online_button.move Self.online_button.show () self.online_button.click_signal.connect (self.playOnline)''Man-Machine War' 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 ()''online fighting' '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_ ()) development tool

Python version: 3.6.4

Related modules:

Graphics module.

Environment building

Just install Python and add it to the environment variable.

Note:

The graphics module is provided in the relevant files, which is a py file that can be placed directly in the current path or in the site-packages folder under the python installation folder.

Brief introduction of principle

For a game such as Gobang AI, it is natural to let the computer try all the possible situations and find the best one. Here are two questions:

(1) how to try all possible situations

(2) how to quantitatively judge the advantages and disadvantages of a certain falling point.

For the first question, it is actually the so-called game tree search, and for the second question, it is actually the so-called selection evaluation function. The selection of evaluation function directly determines the pros and cons of AI algorithm, and its form is also changeable. It can be said that each evaluation function is a player, and each player naturally has different views and countermeasures for different chess types, of course, their chess strength is therefore different.

But the game tree search is relatively fixed, and its core idea is to let the computer consider all the possible situations after the N steps in the current situation, in which the odd step (because it is now the turn of the AI) is to make the AI side score the highest, and the even step is to make the AI side score the lowest (because the opponent is also human, you can also choose the best strategy).

Of course, the amount of computation of this kind of search is very large, so pruning is needed to reduce the amount of computation.

Where A stands for AI side and P for human side. The AI side searches for the maximum value and the human side searches for the minimum value. Therefore, the final result of the A1 downward search of Layer3 is the A2 downward search of 4PowerLayer3, first search the P3 of Layer4, and get a score of 6, considering that the P1 of Layer2 searches for the smaller values of A1 and A2 of Layer3, while the A2 of Layer3 searches the P3 of Layer4, its value must be greater than the A1 of Layer3, so there is no need to search, 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.

"Python game development how to use graphics to achieve AI Gobang" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report