鬥地主老是輸?一起用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()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2869228/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Python鬥地主Python
- 用Python破解鬥地主殘局Python
- 模擬鬥地主
- Python 三人鬥地主程式碼Python
- 自己實現鬥地主引擎
- Java鬥地主專案碎片Java
- 練習4. 鬥地主遊戲遊戲
- Android鬥地主遊戲原始碼Android遊戲原始碼
- 破解鬥地主1。0 (932字)
- 上班划水神器:一個可以在控制檯玩鬥地主的專案!
- 鬥地主AI演算法——第五章の總值計算AI演算法
- Java寫的鬥地主遊戲原始碼Java遊戲原始碼
- JAVA集合練習:鬥地主發牌Java
- Golang多執行緒簡單鬥地主Golang執行緒
- 請教如何破解鬥地主 (269字)
- 使用Java實現簡單的鬥地主案例Java
- Map實現鬥地主發牌有序版二
- 聯眾鬥地主計牌器V2.2註冊演算法分析演算法
- 遊戲《鬥遊鬥地主》被撤銷出版物號,為今年首款遊戲
- 集合框架-模擬鬥地主洗牌和發牌案例框架
- 8、用java的ArrayList集合完成模擬鬥地主案例Java
- 500 行 Python 程式碼做一個英文解析器Python
- 用鬥地主的例項學會使用java Collections工具類Java
- “歡喜鬥地主”等44款App存違規行為APP
- 1.cocos2d-x鬥地主實現-發牌
- 用 Python 做一個 H5 遊戲機器人PythonH5遊戲機器人
- 一定要用Photoshop?no!動手用Python做一個顏色提取器! ⛵Python
- 位元組跳動入股《芒果鬥地主》開發商林子互娛
- 做一個程式碼閱讀器
- 鬥地主4.0註冊演算法,序號產生器在OCG論壇 (22千字)演算法
- 動手做個 AI 機器人,幫我回訊息!AI機器人
- 一款“鬥地主”遊戲靠位元組跳動的流量空降免費榜TOP1遊戲
- 開心鬥地主1.7S4非法註冊破解 (512字)
- 賽博鬥地主——使用大語言模型扮演Agent智慧體玩牌類遊戲。模型智慧體遊戲
- 用canvas做一個簡易取色器Canvas
- golang 實現鬥地主棋牌遊戲伺服器,GitHub 上發現的一個專案,作者在積極維護,希望大家能幫幫忙一起完善這個專案Golang遊戲伺服器Github
- 用Python做一個翻譯軟體Python
- 如何使用python做一個聊天小程式?Python