gpt4 book ai didi

loops - 普通口齿不清 : recursive call from a loop

转载 作者:行者123 更新时间:2023-12-02 03:16:37 25 4
gpt4 key购买 nike

我不是高级 Lisper,我正在努力学习这门语言。所以,我正在尝试编写一个简单的纸牌游戏引擎。出价以下列方式实现:玩家被询问他们的出价。如果完整的玩家列表接受出价,则结束出价并存储数据。否则,所有必要的数据都将传递给递归调用。传输到下一次调用的数据如下:玩家、他们当前的账单、包含玩家是否活跃的信息的真假列表,以及游戏历史(所有付款)。

问题是函数没有从递归调用返回值。

初始输入数据为:

(defparameter *billings* (list 0 0 0 0 0 0 50 100))
(defparameter *players* '(Alice Bob Carol Eve Frank Oscar Trent Walter))
(defparameter *participants* (list t t t t t t t t))
(defparameter *history* (list))

代码(没有辅助函数的主体)如下所示:

(defun make-round(rel-players rel-billings rel-participants game-history)
(let((max-billing (maxval rel-billings))
(curr 0))
(loop
for billing in rel-billings
for player in rel-players
for participant in rel-participants
do (if(not participant) (setf game-history (add2->history 0 game-history (length rel-players)))
(let((bid (ask player billing max-billing)))
(cond ((not bid) (set-assoc-list-val player nil rel-players rel-participants)
(setf game-history (add2->history 0 game-history (length rel-players))) )
(t (setf curr (+ bid billing))
(set-assoc-list-val player curr rel-players rel-billings)
(setf game-history (add2->history bid game-history (length rel-players)))
(when (> curr max-billing)
(make-round (divide-players (next-player player rel-players) rel-players)
(divide-players-list (next-player player rel-players) rel-players rel-billings)
(divide-players-list (next-player player rel-players) rel-players rel-participants)
game-history)
))))))
(values rel-players rel-billings rel-participants game-history)))

我想我犯了一个愚蠢的错误,但实际上我做不到。请帮忙

input:
(make-round *players* *billings* *participants* *history*)

Player ALICE has paid 0 dollars but 100 is required. Player ALICE has to pay at least 100 dollars
100
Player BOB has paid 0 dollars but 100 is required. Player BOB has to pay at least 100 dollars
100
Player CAROL has paid 0 dollars but 100 is required. Player CAROL has to pay at least 100 dollars
100
Player EVE has paid 0 dollars but 100 is required. Player EVE has to pay at least 100 dollars
100
Player FRANK has paid 0 dollars but 100 is required. Player FRANK has to pay at least 100 dollars
100
Player OSCAR has paid 0 dollars but 100 is required. Player OSCAR has to pay at least 100 dollars
100
Player TRENT has paid 50 dollars but 100 is required. Player TRENT has to pay at least 50 dollars
150
Player WALTER has paid 100 dollars but 200 is required. Player WALTER has to pay at least 100 dollars
100
Player ALICE has paid 100 dollars but 200 is required. Player ALICE has to pay at least 100 dollars
100
Player BOB has paid 100 dollars but 200 is required. Player BOB has to pay at least 100 dollars
100
Player CAROL has paid 100 dollars but 200 is required. Player CAROL has to pay at least 100 dollars
100
Player EVE has paid 100 dollars but 200 is required. Player EVE has to pay at least 100 dollars
100
Player FRANK has paid 100 dollars but 200 is required. Player FRANK has to pay at least 100 dollars
100
Player OSCAR has paid 100 dollars but 200 is required. Player OSCAR has to pay at least 100 dollars
100
Player TRENT has paid 200 dollars but 200 is required. Player TRENT has to pay at least 0 dollars
0
output:
(ALICE BOB CAROL EVE FRANK OSCAR TRENT WALTER)
(100 100 100 100 100 100 200 100)
(T T T T T T T T)
((100 100 100 100 100 100 150))

最佳答案

查看对 MAKE-ROUND 的调用,并通过以下操作查看它返回值的位置:MAKE-ROUND -> WHEN -> COND -> LET -> IF 返回循环 DO - 丢弃值的关键字。

为了防止返回值被丢弃,您必须使用 RETURN-FROM 进行显式提前返回。 .

(defun make-round (rel-players rel-billings rel-participants game-history)
(let ((max-billing (maxval rel-billings))
(curr 0))
(loop
for billing in rel-billings
for player in rel-players
for participant in rel-participants
do (if (not participant)
(setf game-history (add2->history 0 game-history (length rel-players)))
(let ((bid (ask player billing max-billing)))
(cond ((not bid)
(set-assoc-list-val player nil rel-players rel-participants)
(setf game-history (add2->history 0 game-history (length rel-players))) )
(t
(setf curr (+ bid billing))
(set-assoc-list-val player curr rel-players rel-billings)
(setf game-history (add2->history bid game-history (length rel-players)))
(when (> curr max-billing)
(return-from make-round
(make-round (divide-players (next-player player rel-players) rel-players)
(divide-players-list (next-player player rel-players) rel-players rel-billings)
(divide-players-list (next-player player rel-players) rel-players rel-participants)
game-history))))))))
(values rel-players rel-billings rel-participants game-history)))

由于代码不可运行,我还没有检查逻辑是否真的有效,但这解决了不返回值的问题。我建议您在某个时候重构代码以提高可读性。

关于loops - 普通口齿不清 : recursive call from a loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36674208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com