Tic-tac-toe: Difference between revisions

Content added Content deleted
(Added Forth entry)
(→‎{{header|Tailspin}}: update to stricter typing)
Line 13,794: Line 13,794:
<lang tailspin>
<lang tailspin>
processor Tic-Tac-Toe
processor Tic-Tac-Toe
@: [1..9 -> (position:$) -> $::value];
@: [position´1..position´9];


source isWonOrDone
source isWonOrDone
Line 13,801: Line 13,801:
$@Tic-Tac-Toe([1,5,9]) -> #, $@Tic-Tac-Toe([3,5,7]) -> #
$@Tic-Tac-Toe([1,5,9]) -> #, $@Tic-Tac-Toe([3,5,7]) -> #
] -> \(
] -> \(
when <=[]?($@Tic-Tac-Toe <~[<1..9>]>)> do 'draw' !
when <=[]?($@Tic-Tac-Toe <~[<position>]>)> do 'draw' !
when <~=[]> do $(1) !
when <~=[]> do $(1) !
\) !
\) !
when <[<=$(first)::raw>+ VOID]> do '$(first); wins!'!
when <[<mark>+ VOID]?($ <[<=$(first)>+ VOID]>)> do '$(first); wins!'!
end isWonOrDone
end isWonOrDone


source validMoves
source validMoves
$@Tic-Tac-Toe -> \[i](<1..9> $ !\) !
$@Tic-Tac-Toe -> \[i](<position> $ !\) !
end validMoves
end validMoves


Line 13,829: Line 13,829:
source play
source play
def board: $Tic-Tac-Toe;
def board: $Tic-Tac-Toe;
@: 'X';
@: mark´'X';
templates getMove
templates getMove
[] -> #
[] -> #
Line 13,837: Line 13,837:
[{mark: $@play, position: $IN::readline -> toInt} -> board::move] -> #
[{mark: $@play, position: $IN::readline -> toInt} -> board::move] -> #
otherwise
otherwise
$(1) !
$(1) !
end getMove
end getMove


Line 13,843: Line 13,843:
when <{}> do
when <{}> do
'$.mark; played $.position;$#10;' -> !OUT::write
'$.mark; played $.position;$#10;' -> !OUT::write
@: $@ -> \(<='X'> 'O'! <='O'> 'X' !\);
@: $@ -> \(<=mark´'X'> mark´'O'! <=mark´'O'> mark´'X' !\);
[$board::isWonOrDone] -> \(
[$board::isWonOrDone] -> \(
when <=[]> do $getMove!
when <=[]> do $getMove!