In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use python to achieve intelligent Gobang game, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Introduction
The hottest and most interesting Python games are played with one click!
The game recommended today is Gobang Mini Game-man-machine or online!
Do you remember? The Gobang that you played with your deskmate in high school?
If you play chess like this, I'm afraid you'll be beaten! I suspect he hung up and found evidence.
Text
First of all, prepare the necessary pictures and other materials as follows:
Okay! Go straight to the code-set up the game start interface:
Class gameStartUI (QWidget): def _ init__ (self, parent=None, * * kwargs): super (gameStartUI, self). _ init__ (parent) self.setFixedSize (760,650) self.setWindowTitle ('Gobang Game') 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 battles' 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_ ())
The effect is as follows:
One is the man-machine game set by Gobang rules:
Import pygamefrom.. misc import * from PyQt5 import QtCorefrom PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from itertools import productfrom. AiGobang import aiGobang 'Man-Machine War' class playWithAIUI (QWidget): back_signal = pyqtSignal () exit_signal = pyqtSignal () send_back_signal = False def _ init__ (self, cfg, parent=None, * * kwargs): super (playWithAIUI) Self). _ init__ (parent) self.cfg = cfg self.setFixedSize (760,650) self.setWindowTitle ('Gobang Man-Machine Battle') self.setWindowIcon (QIcon (cfg.ICON_FILEPATH)) # background picture palette = QPalette () palette.setBrush (self.backgroundRole () QBrush (QPixmap (cfg.BACKGROUND_IMAGEPATHS.get ('bg_game') self.setPalette (palette) # button self.home_button = PushButton (cfg.BUTTON_IMAGEPATHS.get (' home'), self) self.home_button.click_signal.connect (self.goHome) self.home_button.move (680,10) self.startgame_button = PushButton (cfg.BUTTON_IMAGEPATHS.get ('startgame') Self) self.startgame_button.click_signal.connect (self.startgame) self.startgame_button.move (640,240) self.regret_button = PushButton (cfg.BUTTON_IMAGEPATHS.get ('regret'), self) self.regret_button.click_signal.connect (self.regret) self.regret_button.move (640,310) self.givein_button = PushButton (cfg.BUTTON_IMAGEPATHS.get (' givein')) Self) self.givein_button.click_signal.connect (self.givein) self.givein_button.move Self.chessman_sign = QLabel (self) sign = QPixmap (cfg.CHESSMAN_IMAGEPATHS.get ('sign')) self.chessman_sign.setPixmap (sign) self.chessman_sign.setFixedSize (sign.size ()) self.chessman_sign.show () self.chessman_sign.hide () # chessboard (1919 matrix) Self.chessboard = [[None for i in range (19)] for _ in range (19)] # History (remorse) self.history_record = [] # whether in the game self.is_gaming = True # winner self.winner = None self.winner_info_label = None # Color assignment and currently Whose turn is it to fall self.player_color = 'white' self.ai_color =' black' self.whoseround = self.player_color # instantiate ai self.ai_player = aiGobang (self.ai_color Self.player_color) # drop sound load pygame.mixer.init () self.drop_sound = pygame.mixer.Sound (cfg.SOUNDS_PATHS.get ('drop'))' left mouse button click event-player round''def mousePressEvent (self Event): if (event.buttons ()! = QtCore.Qt.LeftButton) or (self.winner is not None) or (self.whoseround! = self.player_color) or (not self.is_gaming): return # guarantee to respond only within the range of the chessboard if event.x () > = 50 and event.x () = 50 and event.y () alpha: if depth = = self .search _ depth: self.next_point = next_step if value > = beta: return beta alpha = value return alpha''is the game over 'def _ _ isgameover (self Oneslist): for I, j in product (range (19), range (19)): if I
< 15 and (i, j) in oneslist and (i+1, j) in oneslist and (i+2, j) in oneslist and (i+3, j) in oneslist and (i+4, j) in oneslist: return True elif j < 15 and (i, j) in oneslist and (i, j+1) in oneslist and (i, j+2) in oneslist and (i, j+3) in oneslist and (i, j+4) in oneslist: return True elif i < 15 and j < 15 and (i, j) in oneslist and (i+1, j+1) in oneslist and (i+2, j+2) in oneslist and (i+3, j+3) in oneslist and (i+4, j+4) in oneslist: return True elif i >3 and j
< 15 and (i, j) in oneslist and (i-1, j+1) in oneslist and (i-2, j+2) in oneslist and (i-3, j+3) in oneslist and (i-4, j+4) in oneslist: return True return False '''重新排列未落子位置''' def __rearrange(self, blank_list): last_step = self.aiplayer_list[-1] for item in blank_list: for i, j in product(range(-1, 2), range(-1, 2)): if i == 0 and j == 0: continue next_step = (last_step[0]+i, last_step[1]+j) if next_step in blank_list: blank_list.remove(next_step) blank_list.insert(0, next_step) return blank_list '''是否存在近邻''' def __hasNeighbor(self, next_step): for i, j in product(range(-1, 2), range(-1, 2)): if i == 0 and j == 0: continue if (next_step[0]+i, next_step[1]+j) in self.aiplayer_list: return True return False '''计算得分''' def __calcScore(self, i, j, x_direction, y_direction, list1, list2, all_scores): add_score = 0 max_score = (0, None) for each in all_scores: for item in each[1]: if i == item[0] and j == item[1] and x_direction == each[2][0] and y_direction == each[2][1]: return 0, all_scores for noffset in range(-5, 1): position = [] for poffset in range(6): x, y = i + (poffset + noffset) * x_direction, j + (poffset + noffset) * y_direction if (x, y) in list2: position.append(2) elif (x, y) in list1: position.append(1) else: position.append(0) shape_len5 = tuple(position[0: -1]) shape_len6 = tuple(position) for score, shape in self.score_model: if shape_len5 == shape or shape_len6 == shape: if score >Max_score [0]: max_score = (score, (I + (0 + noffset) * x_direction, j + (0 + noffset) * y_direction), (I + (1 + noffset) * x_direction, j + (1 + noffset) * y_direction) (I + (2 + noffset) * x_direction, j + (2 + noffset) * y_direction), (I + (3 + noffset) * x_direction, j + (3 + noffset) * y_direction) (I + (4 + noffset) * x_direction, j + (4 + noffset) * y_direction)), (x_direction Y_direction)) if max_score [1] is not None: for each in all_scores: for p1 in each [1]: for p2 in max_score [1]: if p1 = = p2 and max_score [0] > 10 and each [0] > 10: add_score + = Max_score [0] + each [0] all_scores.append (max_score) return add_score+max_score [0] All_scores''Evaluation function' 'def _ _ evaluation (self Is_ai_round): if is_ai_round: list1 = self.ai_list list2 = self.player_list else: list2 = self.ai_list list1 = self.player_list active_all_scores = [] active_score = 0 for item in list1: score, active_all_scores = self.__calcScore (item [0], item [1] 0,1, list1, list2, active_all_scores) active_score + = score score, active_all_scores = self.__calcScore (item [0], item [1], 1, 0, list1, list2, active_all_scores) active_score + = score score, active_all_scores = self.__calcScore (item [0], item [1], 1, 1, list1, list2 Active_all_scores) active_score + = score score, active_all_scores = self.__calcScore (item [0], item [1],-1,1, list1, list2, active_all_scores) active_score + = score passive_all_scores = [] passive_score = 0 for item in list2: score, passive_all_scores = self.__calcScore (item [0], item [1]) 0,1, list2, list1, passive_all_scores) passive_score + = score score, passive_all_scores = self.__calcScore (item [0], item [1], 1, 0, list2, list1, passive_all_scores) passive_score + = score score, passive_all_scores = self.__calcScore (item [0], item [1], 1, 1, list2, list1 Passive_all_scores) passive_score + = score score, passive_all_scores = self.__calcScore (item [0], item [1],-1,1, list2, list1, passive_all_scores) passive_score + = score total_score = active_score-passive_score * 0.1return total_score on how to use python to implement intelligent Gobang games is here. I hope the above content can help you to a certain extent, if you still have a lot of doubts to be solved, you can follow the industry information channel to learn more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.