Skip to content

Commit b4cbb3b

Browse files
committed
实现双ai游戏
1 parent 0b2c54b commit b4cbb3b

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

-1.8 KB
Binary file not shown.

douzero/evaluation/simulation.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -316,11 +316,11 @@ def init(data):
316316

317317
if env.acting_player_position not in list(
318318
env.players.keys()
319-
): # 如果下一位不是AI
319+
): # 玩家是地主
320320
res_data = {"pid": pid, "game_over": res_game_over}
321321
res_action = "receive"
322322

323-
else: # 下一位是AI
323+
else: # AI是地主
324324
cards_pd, confidence_pd = env.step(data) # ai出牌
325325
cards_po = confidence_po = None
326326
if env.game_over:
@@ -405,36 +405,54 @@ def next(data): # 收到他人出牌
405405
error = Exception(f"非此玩家回合,当前为{acting_player}的回合")
406406
raise error
407407

408-
cards, ___ = env.step(data)
408+
env.step(data) # 上报玩家出牌
409409
if env.game_over:
410-
# print("{}win, game over!\n".format("farmer" if env.winner == "farmer" else "landlord"))
411410
res_game_over = True
412411
env = None
413412
return {"action": "receive", "data": {}}
414413

415414
else:
416415
if (
417-
env.acting_player_position == list(env.players.keys())[0]
416+
env.acting_player_position in list(env.players.keys())
418417
): # 如果下一位是AI,则直接获取出牌
419-
cards, confidence = env.step(data)
418+
cards_pd, confidence_pd = env.step(data) # ai出牌
419+
cards_po = confidence_po = None
420420
if env.game_over:
421421
# print("{}win, game over!\n".format("farmer" if env.winner == "farmer" else "landlord"))
422422
res_game_over = True
423423
env = None
424+
else:
425+
if env.acting_player_position in list(
426+
env.players.keys()
427+
): # 第二位出牌的还是ai,也就是双ai时
428+
cards_po, confidence_po = env.step(data)
429+
if env.game_over:
430+
res_game_over = True
431+
env = None
432+
else: # 单ai时
433+
pass
434+
435+
res_action = "play"
424436
res_data = {
425437
"pid": pid,
426-
"cards": cards,
427-
"confidence": confidence,
428438
"game_over": res_game_over,
439+
"play": [
440+
{
441+
"cards": cards_pd,
442+
"confidence": confidence_pd,
443+
},
444+
{
445+
"cards": cards_po,
446+
"confidence": confidence_po,
447+
},
448+
],
429449
}
430-
res_action = "play"
431450
else:
432451
res_data = {"pid": pid, "game_over": res_game_over}
433452
res_action = "receive"
434453

435454
env_list[pid] = env
436455
res_status = "ok"
437-
# print(f"result:{result}")
438456
except Exception as err:
439457
res_action = "step"
440458
res_data = {"pid": pid}

0 commit comments

Comments
 (0)