Tic-tac-toe: Difference between revisions

1,950 bytes removed ,  4 days ago
→‎J: Clearer code. My prior code was too terse.
(→‎J: Clearer code. My prior code was too terse.)
Tags: Mobile edit Mobile web edit
(6 intermediate revisions by the same user not shown)
Line 6,686:
 
=={{header|J}}==
To subsequent j poster: replacing this entry is fine by me.
<syntaxhighlight lang="j">
Until=: {{[F.(u[_2:Z:v)}} NB. apply u until v is true
Note 'ttt adjudicates or plays'
'`ermsg turn board'=: echo@'no'`{.`}. NB. get turn/board from state vector
'`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 ]
show=: [[:echo@''@echo (,' '&,)/"1@('.XO'{~3 3$board) [echo@'')
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)
ttt=: outcome@(show@move Until (won+.full))@(10{._1)@prompt
NB. use: ttt 0 (arg is ignored)
</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.
 
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.
use: markers ttt characters
 
Note that <nowiki>U=:{{u^:(-.@:v)^:_}}</nowiki> would've quit upon receiving invalid input, as this would leave the argument unchanged.
characters represent the cell names.
<pre>
]M=: 0 1 _1{~?3 3$3
0 _1 _1
_1 1 1
_1 1 0
M{'.XO'
.OO
OXX
OX.
</pre>
Sample play:
<pre> ttt 0
enter a move (1–9) each turn; you're X's
1 2 3
4 5 6
7 8 9
 
. O .
markers is a length 3 character vector of the
. . .
characters to use for first and second player
. . .
followed by the opponent's mark.
'XOX' means X plays 1st, O is the other mark,
and the first strategy plays 1st.
'XOO' means X plays 1st, O is the other mark,
and the second strategy moves first.
 
1
The game is set up for the computer as the
first strategy (random), and human as second.
 
X O .
A standard use:
. . .
'XOX'ttt'abcdefghijkl'
. . .
 
Example game reformatted w/ emacs artist-mode
to fit your display:
 
X O .
'#-#'ttt'wersdfxcv'
. . .
w│e│r w│e│r .... -│e│r . -│e│#
. . O
─┼─┼─ . ─┼─┼─ .. ─┼─┼─ .. ─┼─┼─
s│d│f . s│#│f .. s│#│f .. -│#│f
─┼─┼─ .. ─┼─┼─ . ─┼─┼─ ... ─┼─┼─
x│c│v .. -│c│v . -│c│# .. -│c│#
d .. v .. r . VICTORY
w│e│r .. w│e│r .. -│e│# .
─┼─┼─ ... ─┼─┼─ .. ─┼─┼─ .
s│#│f ... s│#│f .. s│#│f .
─┼─┼─ .. ─┼─┼─ ... ─┼─┼─ ...
x│c│v -│c│# -│c│#
-->cell for -? -->cell for -? -->cell for -?
x w s
)
 
5
while=: conjunction def 'u ^: v ^:_' NB. j assumes while is a verb and needs to know while is a conjunction.
 
X O .
ttt=: outcome@:((display@:move) while undecided)@:display@:prepare
. X .
. . O
 
blindfolded_variant=: outcome@:display@:(move while undecided)@:display@:prepare
 
X O .
outcome=: {&(>;:'kitty VICTORY')@:won NB. (outcome does not pass along the state)
. X .
move=: post locate
. O O
undecided=: won nor full
prepare=: , board@:] NB. form the state vector
 
7
Note 'locate'
is a monadic verb. y is the state vector.
returns the character of the chosen cell.
Generally:
locate=: second_strategy`first_strategy@.(first = mark)
Simplified:
locate=: human_locate NB. human versus human
)
locate=: human_locate`computer_locate@.(first = mark)
 
X O .
display=: show [: (1 1,:5 5)&(];.0)@:": [: <"0 fold
. X .
X O O
 
computer_locate=: [: show@{. board -. marks NB. strategy: first available
computer_locate=: [: show@({~ ?@:#) board -. marks NB. strategy: random
 
X O .
human_locate=: monad define
O X state=. y
X O O
m=. mark state
b=. board state
cells=. b -. marks state
show '-->cell for ' , m , '?'
whilst. cell -.@:e. cells do. cell =. {. (1!:1]1) , m end.
)
 
9
post=: 2&A.@:(3&{.)@:[ prepare mark@:[`((i.~ board)~)`(board@:[)}
no
 
X O .
mark=: {. NB. mark of the current player from state
O X .
marks=: 2&{. NB. extract both markers from state
X O O
board=: _9&{. NB. extract board from state
first=: 2&{ NB. extract first player from state
 
3
show=: [ smoutput
 
X O X
full=: 2 = #@:~.
O X .
won=: test@:fold
X O O
fold=: 3 3 $ board
test=: [: any [: all [: infix_pairs_agree |:@:lines
 
X wins</pre>
lines=: , diagonal , diagonal@:|. , |:
diagonal=: (<0 1)&|:
all=: *./
any=: +./
nor=: 8 b.
infix_pairs_agree=: 2&(=/\)
</syntaxhighlight>
 
=={{header|Java}}==
13

edits