所以我正在尝试使用touples(一种学习练习)为erlang编写tic-tac-toe。我总是希望Board变量是九个元素的元组。我已经获得了胜利的匹配,但是与猫的比赛的匹配证明是困难的。我想做{_,_,_,_,_,_,_,_,_}
- > “猫的游戏!”; ,但是当我只希望它匹配x OR y时,它会匹配其他元素,如[]
或""
,但没有别的。例如:
{(x||y),(x||y),(x||y),(x||y),(x||y),(x||y),(x||y),(x||y),(x||y)} -> "Cat's game!"
本质上是我想要做的,但我不知道如何使用Erlang元组做到这一点,并且它证明很难谷歌。
-module(d2bonus).
-export([status/1]).
status(Board) ->
case Board of
{x,x,x,_,_,_,_,_,_} -> "X Wins!";
{o,o,o,_,_,_,_,_,_} -> "O Wins!";
{_,_,_,x,x,x,_,_,_} -> "X Wins!";
{_,_,_,o,o,o,_,_,_} -> "O Wins!";
{_,_,_,_,_,_,x,x,x} -> "X Wins!";
{_,_,_,_,_,_,o,o,o} -> "O Wins!";
{x,_,_,x,_,_,x,_,_} -> "X Wins!";
{o,_,_,o,_,_,o,_,_} -> "O Wins!";
{_,x,_,_,x,_,_,x,_} -> "X Wins!";
{_,o,_,_,o,_,_,o,_} -> "O Wins!";
{_,_,x,_,_,x,_,_,x} -> "X Wins!";
{_,_,o,_,_,o,_,_,o} -> "O Wins!";
{x,_,_,_,x,_,_,_,x} -> "X Wins!";
{o,_,_,_,o,_,_,_,o} -> "O Wins!";
{_,_,x,_,x,_,x,_,_} -> "X Wins!";
{_,_,o,_,o,_,o,_,_} -> "O Wins!";
({P1,P2,P3,P4,P5,P6,P7,P8,P9}) when (P1 =:= x orelse P1 =:= y), (P2 =:= x orelse P2 =:= y), (P3 =:= x orelse P3 =:= y), (P4 =:= x orelse P4 =:= y), (P5 =:= x orelse P5 =:= y), (P6 =:= x orelse P6 =:= y), (P7 =:= x orelse P7 =:= y), (P8 =:= x orelse P8 =:= y), (P9 =:= x orelse P9 =:= y) -> "Cat's Game!";
_ -> "No winner yet"
end
.
答案 0 :(得分:3)
如果你想要严格,你可以这样做:
f({P1,P2,...}) when (X1 =:= x orelse X1 =:= y), (P2 =:= x orelse P2 =:= y), ... ->
"Cat's game!".
我会做类似的事情:
f(Board) when is_tuple(Board), size(Board) =:= 9 ->
true = lists:all(fun(x) -> true;
(y) -> true;
(_) -> false
end,
tuple_to_list(Board)),
"Cat's Game".
但这主要是因为我对长卷的支票的容忍度很低。
答案 1 :(得分:1)
您可以在输入值进入元组之前验证输入值,例如:
make_move(Pos, Z, StateTuple) when Pos >= 1, Pos =< 9, (Z =:= x orelse Z =:= y) ->
erlang:setelement(Pos, StateTuple, Z).
答案 2 :(得分:1)
结合更多
is_cats_game1(Board) when is_tuple(Board), size(Board) =:= 9 ->
[] =:= [Z || Z <- tuple_to_list(Board), Z =/= x andalso Z =/= y].
-define(is_yx(E), (E =:= x orelse E =:= y)).
is_cats_game2({Z1,Z2,Z3,Z4,Z5,Z6,Z7,Z8,Z9} = __Board)
when ?is_yx(Z1)
andalso ?is_yx(Z2)
andalso ?is_yx(Z3)
andalso ?is_yx(Z4)
andalso ?is_yx(Z5)
andalso ?is_yx(Z6)
andalso ?is_yx(Z7)
andalso ?is_yx(Z8)
andalso ?is_yx(Z9) ->
true;
is_cats_game2(_) -> false.
答案 3 :(得分:0)
- 模(d2bonus)。 -export([状态/ 1])。
状态(板) - &GT; 案件委员会 {x,x,x,,,_,,,_} - &gt; “X胜利!”; {o,o,o,,,_,,,_} - &gt; “O Wins!”; {,,_,x,x,x,,,_} - &gt; “X胜利!”; {,,_,o,o,o,,,_} - &gt; “O Wins!”; {,,_,,,_,x,x,x} - &gt; “X胜利!”; {,,_,,,_,o,o,o} - &gt; “O Wins!”; {x,,,x,,,x,,} - &gt; “X胜利!”; {o,,,o,,,o,,} - &gt; “O Wins!”; {,x,,_,x,,,x,} - &gt; “X胜利!”; {,o,,,o,,,o,} - &gt; “O Wins!”; {,_,x,,,x,,,x} - &gt; “X胜利!”; {,,o,,,o,,,o} - &gt; “O Wins!”; {x,,,_,x,,,_,x} - &gt; “X胜利!”; {o,,,_,o,,,_,o} - &gt; “O Wins!”; {,,x,,x,,x,,} - &gt; “X胜利!”; {,,o,,o,,o,,} - &gt; “O Wins!”; {P1,P2,P3,P4,P5,P6,P7,P8,P9}当(P1 =:= x orelse P1 =:= o),(P2 =:= x orelse P2 =:= o),(P3 =:= x orelse P3 =:= o),(P4 =:= x orelse P4 =:= o),(P5 =:= x orelse P5 =:= o),(P6 =:= x orelse P6 =: =(o),(P7 =:= x orelse P7 =:= o),(P8 =:= x orelse P8 =:= o),(P9 =:= x orelse P9 =:= o) - &gt; “猫的游戏!”; _ - &gt; “还没有胜利者” 结束