Tic-tac-toe: Difference between revisions

Content added Content deleted
(→‎J: Clearer code. My prior code was too terse.)
Tags: Mobile edit Mobile web edit
(→‎{{header|J}}: shorten code; prefer separate explanation over comments)
Line 6,687: Line 6,687:
=={{header|J}}==
=={{header|J}}==
<syntaxhighlight lang="j">
<syntaxhighlight lang="j">
'`turn board open full'=: {.`}.`(0=[{board@])`(0-.@e.board)
Until=: {{[F.(u[_2:Z:v)}} NB. apply u until v is true
'`cpu_move position'=: (?@#{])@([:I.0=board)`(cpu_move`you_move@.(_1 1 i.turn))
'`ermsg turn board'=: echo@'no'`{.`}. NB. get turn/board from state vector
you_move=: $:@echo@'invalid'^:(-.@e.i.@9)@:<:@".@(1!:1@1)
'`open full'=: (0=[{board@])`(0-.@e.board)
move=: position (][echo@'already taken!')`(-@turn@] , turn@]`[`(board@])})@.open ]
'`cpu_move you_move'=: (?@#{])@([:I.0=board)`(<:@".@(1!:1@1))
show=: [ [: echo@''@echo (,' '&,)/"1@('.XO'{~3 3$board) [ echo@''
get_position=: cpu_move`you_move@.(_1 1 i.turn)
move=: get_position (][errmsg)`(-@turn@] , turn@]`[`(board@])})@.open ]
won=: [:+./ (3=[:|+/)"1@(], |:, (<@i.@2|:]),: <@i.@2|:|.)@(3 3$board)
show=: [[:echo@''@echo (,' '&,)/"1@('.XO'{~3 3$board) [echo@'')
prompt=: echo@:>:@i.@3 3 [ echo@'enter a move (1–9) each turn; you''re X''s'
won=: [:+./ (3=[:|+/)"1@(], |:, (<@i.@2|:]),: <@i.@2|:|.)@(3 3$board)
prompt=: echo@:>:@i.@3 3@echo@'enter a move (1–9) each turn; you''re X''s'
outcome=: (' wins'echo@,~'.XO'{~-@turn)`(echo@'tie')@.(1:i.~won,full)
outcome=: (' wins'echo@,~'.XO'{~-@turn)`(echo@'tie')@.(1:i.~won,full)
ttt=: outcome@(show@move Until (won+.full))@(10{._1)@prompt
ttt=: outcome@([F.(show@move[_2:Z:won+.full))@(10{._1)@prompt

NB. use: ttt 0 (arg is ignored)
</syntaxhighlight>
</syntaxhighlight>
The board is represented by a vector of -1's, 0’s and 1’s, which index into the character vector when it’s time to display the board.
This version is stateless, purely functional, and tacit. CPU selects a random open position. The <code>turn</code> and <code>board</code> verbs extract the turn/board from the state vector. The board is represented by a vector of -1's, 0’s and 1’s, which index into the character vector '.XO' when it’s time to display the board.


Encoding data with numbers this way allows the convenience of e.g. being able to simply check whether the absolute value of the sum across a given line is 3, indicating a win.
Encoding data with numbers this way allows the convenience of e.g. being able to simply check whether the absolute value of the sum across a given line is 3, indicating a win.


Note that <nowiki>U=:{{u^:(-.@:v)^:_}}</nowiki> would've quit upon receiving invalid input, as this would leave the argument unchanged.
<pre>
<pre>
]M=: 0 1 _1{~?3 3$3
]M=: 0 1 _1{~?3 3$3
Line 6,715: Line 6,712:
OX.
OX.
</pre>
</pre>

The verb <code>open</code> tests whether the selected position is available on the board. Fold (<code>F.</code>) is used because <code>Until=:<tt>{{</tt>u^:(-.@:v)^:_<tt>}}</tt></code> would've quit upon receiving invalid input, as the argument would be unchanged between that iteration and the next.

Sample play:
Sample play:
<pre> ttt 0
<pre> ttt 0
Line 6,760: Line 6,760:


9
9
already taken!
no


X O .
X O .
Line 6,772: Line 6,772:
X O O
X O O


X wins</pre>
X wins
</pre>


=={{header|Java}}==
=={{header|Java}}==