鬥地主老是輸?一起用Python做個AI出牌器!

專注的阿熊發表於2022-03-10

# 建立一個代表玩家的 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()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2869228/,如需轉載,請註明出處,否則將追究法律責任。

相關文章