欢乐器的用法,男性解压神器大全

首页 > 经验 > 作者:YD1662022-10-28 09:07:01


这样我们就可以得到玩家AI手牌,其他玩家手牌(预测),地主三张底牌,三者角色关系,出牌顺序。核心代码如下:

# 坐标 self.MyHandCardsPos = (414, 804, 1041, 59) # AI玩家截图区域 self.LPlayedCardsPos = (530, 470, 380, 160) # 左侧玩家截图区域 self.RPlayedCardsPos = (1010, 470, 380, 160) # 右侧玩家截图区域 self.LandlordFlagPos = [(1320, 300, 110, 140), (320, 720, 110, 140), (500, 300, 110, 140)] # 地主标志截图区域(右-我-左) self.ThreeLandlordCardsPos = (817, 36, 287, 136) # 地主底牌截图区域,resize成349x168 def init_cards(self): # 玩家手牌 self.user_hand_cards_real = "" self.user_hand_cards_env = [] # 其他玩家出牌 self.other_played_cards_real = "" self.other_played_cards_env = [] # 其他玩家手牌(整副牌减去玩家手牌,后续再减掉历史出牌) self.other_hand_cards = [] # 三张底牌 self.three_landlord_cards_real = "" self.three_landlord_cards_env = [] # 玩家角色代码:0-地主上家, 1-地主, 2-地主下家 self.user_position_code = None self.user_position = "" # 开局时三个玩家的手牌 self.card_play_data_list = {} # 出牌顺序:0-玩家出牌, 1-玩家下家出牌, 2-玩家上家出牌 self.play_order = 0 self.env = None # 识别玩家手牌 self.user_hand_cards_real = self.find_my_cards(self.MyHandCardsPos) self.UserHandCards.setText(self.user_hand_cards_real) self.user_hand_cards_env = [RealCard2EnvCard[c] for c in list(self.user_hand_cards_real)] # 识别三张底牌 self.three_landlord_cards_real = self.find_three_landlord_cards(self.ThreeLandlordCardsPos) self.ThreeLandlordCards.setText("底牌:" self.three_landlord_cards_real) self.three_landlord_cards_env = [RealCard2EnvCard[c] for c in list(self.three_landlord_cards_real)] # 识别玩家的角色 self.user_position_code = self.find_landlord(self.LandlordFlagPos) if self.user_position_code is None: items = ("地主上家", "地主", "地主下家") item, okPressed = QInputDialog.getItem(self, "选择角色", "未识别到地主,请手动选择角色:", items, 0, False) if okPressed and item: self.user_position_code = items.index(item) else: return self.user_position = ['landlord_up', 'landlord', 'landlord_down'][self.user_position_code] for player in self.Players: player.setStyleSheet('background-color: rgba(255, 0, 0, 0);') self.Players[self.user_position_code].setStyleSheet('background-color: rgba(255, 0, 0, 0.1);') # 整副牌减去玩家手上的牌,就是其他人的手牌,再分配给另外两个角色(如何分配对AI判断没有影响) for i in set(AllEnvCard): self.other_hand_cards.extend([i] * (AllEnvCard.count(i) - self.user_hand_cards_env.count(i))) self.card_play_data_list.update({ 'three_landlord_cards': self.three_landlord_cards_env, ['landlord_up', 'landlord', 'landlord_down'][(self.user_position_code 0) % 3]: self.user_hand_cards_env, ['landlord_up', 'landlord', 'landlord_down'][(self.user_position_code 1) % 3]: self.other_hand_cards[0:17] if (self.user_position_code 1) % 3 != 1 else self.other_hand_cards[17:], ['landlord_up', 'landlord', 'landlord_down'][(self.user_position_code 2) % 3]: self.other_hand_cards[0:17] if (self.user_position_code 1) % 3 == 1 else self.other_hand_cards[17:] }) print(self.card_play_data_list) # 生成手牌结束,校验手牌数量 if len(self.card_play_data_list["three_landlord_cards"]) != 3: QMessageBox.critical(self, "底牌识别出错", "底牌必须是3张!", QMessageBox.Yes, QMessageBox.Yes) self.init_display() return if len(self.card_play_data_list["landlord_up"]) != 17 or \ len(self.card_play_data_list["landlord_down"]) != 17 or \ len(self.card_play_data_list["landlord"]) != 20: QMessageBox.critical(self, "手牌识别出错", "初始手牌数目有误", QMessageBox.Yes, QMessageBox.Yes) self.init_display() return # 得到出牌顺序 self.play_order = 0 if self.user_position == "landlord" else 1 if self.user_position == "landlord_up" else 2

效果如下:

欢乐器的用法,男性解压神器大全(5)

3. AI出牌方案输出

下面我们就需要用到DouZero开源的AI斗地主了。DouZero项目地址:https://github.com/kwai/DouZero。我们需要将该开源项目下载并导入项目中。

创建一个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 = [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()

到这里,整个AI斗地主出牌流程基本已经完成了。

欢乐器的用法,男性解压神器大全(6)

三、出牌器用法

按照上述过程,这款AI出牌器已经制作完成了。后面应该如何使用呢?如果不想研究源码,只想使用这款AI斗地主出牌器,验证下效果,该怎么配置环境运行这个AI出牌器呢?下面就开始介绍。

1. 环境配置

首先我们需要安装这些第三方库,配置相关环境,如下所示:

torch==1.9.0 GitPython==3.0.5 gitdb2==2.0.6 PyAutoGUI==0.9.50 PyQt5==5.13.0 PyQt5-sip==12.8.1 Pillow>=5.2.0 opencv-python rlcard2. 坐标调整确认

我们可以打开欢乐斗地主游戏界面,将游戏窗口模式下最大化运行,把AI出牌器程序窗口需要移至右下角,不能遮挡手牌、地主标志、底牌、历史出牌这些关键位置。

其次我们要确认屏幕截图获取的各个区域是否正确。如果有问题需要进行区域位置坐标调整。

# 坐标 self.MyHandCardsPos = (414, 804, 1041, 59) # 我的截图区域 self.LPlayedCardsPos = (530, 470, 380, 160) # 左边截图区域 self.RPlayedCardsPos = (1010, 470, 380, 160) # 右边截图区域 self.LandlordFlagPos = [(1320, 300, 110, 140), (320, 720, 110, 140), (500, 300, 110, 140)] # 地主标志截图区域(右-我-左) self.ThreeLandlordCardsPos = (817, 36, 287, 136) # 地主底牌截图区域,resize成349x168

欢乐器的用法,男性解压神器大全(7)

3. 运行测试

当所有环境配置完成,各区域坐标位置确认无误之后,下面我们就可以直接运行程序,测试效果啦~

首先我们运行AI出牌器程序,打开欢乐斗地主游戏界面,进入游戏。当玩家就位,手牌分发完毕,地主身份确认之后,我们就可以点击画面中开始按钮,让AI来帮助我们斗地主了。

下面可以一起来看看这款AI出牌器的实验效果喔,看看AI是如何带领农民打倒地主,取得胜利的!

若本篇内容对您有所帮助,请三连点赞再看转发支持一下。

上一页12末页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.