Tic-tac-toe: Difference between revisions
→J: Clearer code. My prior code was too terse.
(→J: Attempt to correct bad templating) Tags: Mobile edit Mobile web edit |
(→J: Clearer code. My prior code was too terse.) Tags: Mobile edit Mobile web edit |
||
Line 6,686:
=={{header|J}}==
'`
'`open full'=: (0=[{board@])`(0-.@e.board)
'`cpu_move you_move'=: (?@#{])@([:I.0=board)`(<:@".@(1!:1@1))
get_position=: cpu_move`you_move@.(_1 1 i.turn)
move=: get_position (][errmsg)`(-@turn@] , turn@]`[`(board@])})@.open ]
w=: [:+./ (3=[:|+/)"1@(], |:,(<@i.@2|:]),: <@i.@2|:|.)@(3 3$b) NB. 3 in a row (win)?▼
ttt=: o@(d@m U (w+.f))@(10{._1)@pr▼
outcome=: (' wins'echo@,~'.XO'{~-@turn)`(echo@'tie')@.(1:i.~won,full)
NB. use: ttt 0 (arg is ignored)
</syntaxhighlight>
The
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.
Line 6,704 ⟶ 6,706:
Note that <nowiki>U=:{{u^:(-.@:v)^:_}}</nowiki> would've quit upon receiving invalid input, as this would leave the argument unchanged.
<pre>
]M=: 0 1 _1{~?3 3$3
0
M{'.XO'
.OO
|