Tic-tac-toe: Difference between revisions
Content deleted Content added
→{{header|J}}: shorten code; prefer separate explanation over comments |
m →{{header|J}}: place sample output before exposition |
||
Line 6,698: | Line 6,698: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
⚫ | 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. |
||
Output: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | 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: |
|||
⚫ | |||
enter a move (1–9) each turn; you're X's |
enter a move (1–9) each turn; you're X's |
||
1 2 3 |
1 2 3 |
||
Line 6,774: | Line 6,759: | ||
X wins |
X wins |
||
</pre> |
</pre> |
||
⚫ | 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. |
||
⚫ | |||
<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. |
||
=={{header|Java}}== |
=={{header|Java}}== |