Tic-tac-toe: Difference between revisions
→J: Remove trivial exposition.
m (→{{header|J}}: place sample output before exposition) |
(→J: Remove trivial exposition.) Tags: Mobile edit Mobile web edit |
||
Line 6,696:
outcome=: (' wins'echo@,~'.XO'{~-@turn)`(echo@'tie')@.(1:i.~won,full)
ttt=: outcome@([F.(show@move[_2:Z:won+.full))@(10{._1)@prompt
</syntaxhighlight>
Output:
<pre> ttt 0
enter a move (1–9) each turn; you're X's
1 2 3
Line 6,757 ⟶ 6,755:
X O O
X wins</pre>
This is a stateless, purely functional, tacit approach. CPU selects a random open position. The <code>turn</code> and <code>board</code> verbs extract the turn/board from the vector which represents the game state. 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.
=={{header|Java}}==
|