斗地主老是输?一起用Python做个AI出牌器!

# 创建一个代表玩家的 AI

ai_players = [0, 0]

ai_players[0] = self.user_position

ai_players[1] = DeepAgent(self.user_position, self.card_play_model_path_dict[self.user_position])

# 初始化游戏环境

self.env = GameEnv(ai_players)

# 游戏开始

self.start()

def start(self):

     self.env.card_play_init(self.card_play_data_list)

     print(" 开始出牌 \n")

     while not self.env.game_over:

         # 玩家出牌时就通过智能体获取 action ,否则通过识别获取其他玩家出牌

         if self.play_order == 0:

             self.PredictedCard.setText("...")

             action_message = self.env.step(self.user_position)

             # 更新界面

             self.UserHandCards.setText(" 手牌: " + str(''.join(

                 [EnvCard2RealCard[c] for c in self.env.info_sets[self.user_position].player_hand_cards]))[::-1])

             self.PredictedCard.setText(action_message["action"] if action_message["action"] else " 不出 ")

             self.WinRate.setText(" 胜率: " + action_message["win_rate"])

             print("\n 手牌: ", str(''.join(

                     [EnvCard2RealCard[c] for c in self.env.info_sets[self.user_position].player_hand_cards])))

             print(" 出牌: ", action_message["action"] if action_message["action"] else " 不出 ", " , 胜率: ",

                   action_message["win_rate"])

             while self.have_white(self.RPlayedCardsPos) == 1 or \

                     pyautogui.locateOnScreen('pics/pass.png',

                                              region=self.RPlayedCardsPos,

                                              confidence=self.LandlordFlagConfidence):

                 print(" 等待玩家出牌 ")

                 self.counter.restart()

                 while self.counter.elapsed() < 100:

                     QtWidgets.QApplication.processEvents(QEventLoop.AllEvents, 50)

             self.play_order = 1

         elif self.play_order == 1:

             self.RPlayedCard.setText("...")

             pass_flag = None

             while self.have_white(self.RPlayedCardsPos) == 0 and \

                     not pyautogui.locateOnScreen('pics/pass.png',

                                                  region=self.RPlayedCardsPos,

confidence=self.LandlordFlagConfidence):

                 print(" 等待下家出牌 ")

                 self.counter.restart()

                 while self.counter.elapsed() < 500:

                     QtWidgets.QApplication.processEvents(QEventLoop.AllEvents, 50)

             self.counter.restart()

             while self.counter.elapsed() < 500:

                 QtWidgets.QApplication.processEvents(QEventLoop.AllEvents, 50)

             # 不出

             pass_flag = pyautogui.locateOnScreen('pics/pass.png',

                                                  region=self.RPlayedCardsPos,

confidence=self.LandlordFlagConfidence)

             # 未找到 " 不出 "

             if pass_flag is None:

                 # 识别下家出牌

                 self.other_played_cards_real = self.find_other_cards(self.RPlayedCardsPos)

             # 找到 " 不出 "

             else:

                 self.other_played_cards_real = ""

             print("\n 下家出牌: ", self.other_played_cards_real)

             self.other_played_cards_env = [RealCard2EnvCard[c] for c in list(self.other_played_cards_real)]

             self.env.step(self.user_position, self.other_played_cards_env)

             # 更新界面

             self.RPlayedCard.setText(self.other_played_cards_real if self.other_played_cards_real else " 不出 ")

             self.play_order = 2

         elif self.play_order == 2:

             self.LPlayedCard.setText("...")

             while self.have_white(self.LPlayedCardsPos) == 0 and \

                     not pyautogui.locateOnScreen('pics/pass.png',

                                                 region=self.LPlayedCardsPos,

confidence=self.LandlordFlagConfidence):

                 print(" 等待上家出牌 ")

                 self.counter.restart()

                 while self.counter.elapsed() < 500:

                     QtWidgets.QApplication.processEvents(QEventLoop.AllEvents, 50)

             self.counter.restart()

             while self.counter.elapsed() < 500:

                 QtWidgets.QApplication.processEvents(QEventLoop.AllEvents, 50)

             # 不出

             pass_flag = pyautogui.locateOnScreen('pics/pass.png',

                                                  region=self.LPlayedCardsPos,

confidence=self.LandlordFlagConfidence)

             # 未找到 " 不出 "

             if pass_flag is None:

                 # 识别上家出牌

                 self.other_played_cards_real = self.find_other_cards(self.LPlayedCardsPos)

             # 找到 " 不出 "

             else:

                 self.other_played_cards_real = ""

             print("\n 上家出牌: ", self.other_played_cards_real)

             self.other_played_cards_env外汇跟单gendan5.com = [RealCard2EnvCard[c] for c in list(self.other_played_cards_real)]

             self.env.step(self.user_position, self.other_played_cards_env)

             self.play_order = 0

             # 更新界面

             self.LPlayedCard.setText(self.other_played_cards_real if self.other_played_cards_real else " 不出 ")

         else:

             pass

         self.counter.restart()

         while self.counter.elapsed() < 100:

             QtWidgets.QApplication.processEvents(QEventLoop.AllEvents, 50)

     print("{} 胜,本局结束 !\n".format(" 农民 " if self.env.winner == "farmer" else " 地主 "))

     QMessageBox.information(self, " 本局结束 ", "{} 胜! ".format(" 农民 " if self.env.winner == "farmer" else " 地主 "),

                             QMessageBox.Yes, QMessageBox.Yes)

     self.env.reset()

     self.init_display()


请使用浏览器的分享功能分享到微信等