Tic-tac-toe: Difference between revisions

Content added Content deleted
(→‎J: Made the output readable; coded the game to common expectations (e.g., display X’s/O’s) to avoid confusion; removed unnecessary naming of trivial functions.)
Tags: Mobile edit Mobile web edit
Line 6,686: Line 6,686:


=={{header|J}}==
=={{header|J}}==
To subsequent j poster: replacing this entry is fine by me.
<syntaxhighlight lang="j">
<syntaxhighlight lang="j">
Until=: {{[F.(u[_2:Z:v)}} NB. apply u until v is true
Note 'ttt adjudicates or plays'
'Marks State'=:'.OX' ; 10{.1
'`errmsg turn board=: echo@'no'`{.`}. NB. get turn/board from state vector
'`available full'=: (0=[{board@])`(0-.@e.board)
you_move=: $:@errmsg^:(-.@e.i.@9)@:<:@".@(1!:1@1)
cpu_move=: (?@#{])@([:I.0=board)
get_position=: (cpu_move`you_move@.(1 _1 i.turn))
move=: get_position (][errmsg)`(-@turn@] , turn@]`[`(board@])})@.available ]
show=: [[:echo@''@echo ([,' ',])/"1@(Marks{~3 3$board)[echo@''
won=: ([:+./[:(3=[:|+/)"1],|:,(<@i.@2|:]),:<@i.@2|:|.)@(3 3$board)
outcome=: (' wins'echo@,~Marks{~-@t)`(echo@'tie')@.(1:i.~won,full)
prompt=: echo@'you''re X''s. enter a move (1–9) each turn'
ttt=: outcome@(show@move Until (won+.full))@State@prompt


use: markers ttt characters
NB. use: ttt 0 (arg is ignored)
</syntaxhighlight>
Sample play:


ttt 0
characters represent the cell names.
you're X's. enter a move (1–9) each turn'


. 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
state=. y
O X .
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
lines=: , diagonal , diagonal@:|. , |:
diagonal=: (<0 1)&|:
all=: *./
any=: +./
nor=: 8 b.
infix_pairs_agree=: 2&(=/\)
</syntaxhighlight>


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