鬥地主老是輸?一起用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
- 自己實現鬥地主引擎
- 上班划水神器:一個可以在控制檯玩鬥地主的專案!
- 鬥地主AI演算法——第五章の總值計算AI演算法
- Golang多執行緒簡單鬥地主Golang執行緒
- Java寫的鬥地主遊戲原始碼Java遊戲原始碼
- 趁老王不在,和隔壁鄰居鬥鬥地主,比比大小
- 使用Java實現簡單的鬥地主案例Java
- Map實現鬥地主發牌有序版二
- python做一個人機對戰五子棋,AI版本PythonAI
- 用 Python 做一個 H5 遊戲機器人PythonH5遊戲機器人
- 遊戲《鬥遊鬥地主》被撤銷出版物號,為今年首款遊戲
- 一定要用Photoshop?no!動手用Python做一個顏色提取器! ⛵Python
- 8、用java的ArrayList集合完成模擬鬥地主案例Java
- 歡樂鬥地主x喜茶:天生一對小歡喜,打牌喝茶在一起
- P2540 [NOIP2015 提高組] 鬥地主 加強版
- “歡喜鬥地主”等44款App存違規行為APP
- 用鬥地主的例項學會使用java Collections工具類Java
- 做一個程式碼閱讀器
- 如何使用python做一個聊天小程式?Python
- 用Python做一個三階拼圖。Python
- 用Python做一個翻譯軟體Python
- 位元組跳動入股《芒果鬥地主》開發商林子互娛
- 動手做個 AI 機器人,幫我回訊息!AI機器人
- python輸入一個列表求平均值Python
- golang 實現鬥地主棋牌遊戲伺服器,GitHub 上發現的一個專案,作者在積極維護,希望大家能幫幫忙一起完善這個專案Golang遊戲伺服器Github
- 新書《Go 語言程式設計之旅:一起用 Go 做專案》出版啦!新書Go程式設計
- 用Python基於Google Bard做一個互動式的聊天機器人PythonGo機器人
- 一款“鬥地主”遊戲靠位元組跳動的流量空降免費榜TOP1遊戲
- jquery連線html頁面做一個彈出框輸入jQueryHTML
- 使用QT做一個簡易計算器QT
- 給大家分享一個 python 做的搜尋引擎Python
- 用Python做一個簡單的翻譯工具Python
- 初探python之做一個簡單小爬蟲Python爬蟲