【发布时间】:2019-05-03 15:59:02
【问题描述】:
我正在编写一个 SML Yahtzee/Poker 程序,它接收一个列表作为格式的输入
yahtzee(1,3,3,4,3)
其中每个数字是掷骰子的结果,然后打印相应的结果,在这种情况下为
(three-of-a-kind, 14)
最后一个数字是在这种情况下作为输入接收的所有 5 个骰子的总和。
我目前使用字符串并使用计数变量计算每个数字的出现次数,因为掷骰子有 6 种可能的结果 - 1、2、3、4、5 或 6。
我按升序对计数进行排序,以便我的新列表的最后一个数字为我提供一些关于可能是什么“手”的信息,如下所示:
yahtzee - 这是 5 种,“价值”50 分,所以回报是 (yahtzee, 50)
大直 - 5 数字序列,价值 40 点所以返回 (large-straight, 40)
小直 - 4 数字序列,30 点所以 (small-straight, 30)
满屋 - 3 种和另一种,25 分所以(full-house, 25)
四种——价值与所有 5 个骰子的总和一样多,所以(four-of-a-kind, sum)
三样 - 与所有 5 个骰子的总和一样多,所以(three-of-a-kind, sum)
机会 - (chance, sum)
因为我也不关心出现 3 次的数字,例如,因为无论如何它都会是三种。通过检查最后一个数字,我可以很好地了解正在发生的事情。
对于满堂彩,如果我检查最后一个数字之前的数字等于 2,我知道这是满堂彩,因为最后一个将是 3。重要的是要注意,如果总和满堂彩的点数大于 25,但是,那手牌应该是三点,因为它产生的点数更多。
我还没有实现所有案例,所以下面的代码是我目前拥有的,最后一个函数是尝试测试我是否朝着正确的方向前进,但将重命名为yahtzee(L)稍后它可以接收输入。
fun counter (nil, count1:int, count2:int, count3:int, count4:int, count5:int, count6:int) = ListMergeSort.sort (fn (x,y) => x > y) [count1, count2, count3, count4, count5, count6]
| counter (6::t, count1, count2, count3, count4, count5, count6) = counter (t, count1, count2, count3, count4, count5, count6+1)
| counter (5::t, count1, count2, count3, count4, count5, count6) = counter (t, count1, count2, count3, count4, count5+1, count6)
| counter (4::t, count1, count2, count3, count4, count5, count6) = counter (t, count1, count2, count3, count4+1, count5, count6)
| counter (3::t, count1, count2, count3, count4, count5, count6) = counter (t, count1, count2, count3+1, count4, count5, count6)
| counter (2::t, count1, count2, count3, count4, count5, count6) = counter (t, count1, count2+1, count3, count4, count5, count6)
| counter (1::t, count1, count2, count3, count4, count5, count6) = counter (t, count1+1, count2, count3, count4, count5, count6)
| counter (h::t, count1, count2, count3, count4, count5, count6) = ListMergeSort.sort (fn (x,y) => x > y) [count1, count2, count3, count4, count5, count6];
fun sum (nil) = 0
| sum (h::t) = h + sum(t)
fun anyOfAKind (nil) = 0
| anyOfAKind (L) = List.last(L)
fun fullHouse (nil) = 0
| fullHouse (L) = List.nth(L, 3)
fun testdice (nil) = []
| testdice (L) =
let
val listSum = sum(L)
val count = counter(L,0,0,0,0,0,0)
in
if fullHouse(count) = 2 then
if listSum <= 25 then print "(fullhouse, 25)"
else print "(threeofakind, " ^ Int.toString (sum) ^ ")"
end;
如果我删除最后一个函数,则构建文件没有问题,但是,当最后一个函数到位时,我会收到错误消息
Error: syntax error found at END
我的第一个问题是,为什么我会收到此错误消息?
我想我需要退货但不是?
我的另一个问题是我的思维过程是否看起来不错,如果没有任何建议,我们将不胜感激。
谢谢!
【问题讨论】:
-
您收到语法错误,因为您的
ifs 之一缺少其else子句。 -
另一个问题是
testdice的一个子句产生一个列表,而另一个产生unit。