Paste: a basic tic-tac-toe
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 12:48:40 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations
namespaces combinators.short-circuit math math.parser
splitting ;
IN: tic-tac-toe
SYMBOLS: X O ;
: set-nth! [ set-nth ] keep ;
: dup! dup clone ;
: draw dup [ . ] each ;
: create-grid 3 f <array> dup! dup! 3array ;
: valid-pos?
{ [ 0 >= ] [ 3 < ] } [ 1&& ] keep swapd 1&& and ;
: is-free? rot nth nth f = ;
: update-case
[ pick nth 4 npick -rot set-nth! ] keep rot set-nth! ;
: switch-player swap X = O X ? swap ;
: read-tuple
readln "," split [ string>number ] map
dup 0 swap nth 1 rot nth ;
: read-pos
4 4 [ 2dup valid-pos? ]
[ 2drop "Enter a position: " write read-tuple ] do until ;
: place
read-pos
[ 3dup is-free? ] [ 2drop "Occupied zone." print read-pos ]
until update-case ;
: is-complete
{ [ { X X X } = ] [ { O O O } = ] } 1|| ;
: get-column swap [ dupd nth ] map nip ;
: transpose 3 iota [ dupd get-column ] map ;
: get-diags dup
3 iota [ dup pick nth nth ] map swap
3 iota [ 2 over - swap pick nth nth ] map nip 2array ;
: game-over?
transpose get-diags append append [ is-complete ] any? ;
: game
X create-grid
[ dup game-over? ] [ draw switch-player place ] until
"Game over." print 2drop ;
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 15:09:04 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations
namespaces combinators.short-circuit math math.parser
splitting ;
IN: tic-tac-toe
SYMBOLS: X O ;
: set-nth! [ set-nth ] keep ;
: draw dup [ . ] each ;
: row 3 swap <array> ;
: create-grid 3 [ row ] replicate ;
: inside? { [ 0 >= ] [ 3 < ] } 1&& ;
: valid-pos? [ inside? ] both? ;
: is-free? rot nth nth f = ;
: update-case
[ pick nth 4 npick -rot set-nth! ] keep rot set-nth! ;
: switch-player swap X = O X ? swap ;
: read-tuple
readln "," split [ string>number ] map
0 over nth 1 rot nth ;
: read-pos
4 4 [ 2dup valid-pos? ]
[ 2drop "Enter a position: " write read-tuple ] do until ;
: place
read-pos
[ 3dup is-free? ] [ 2drop "Occupied zone." print read-pos ]
until update-case ;
: complete? { [ X row = ] [ O row = ] } 1|| ;
: diag 3 iota [ dup pick nth nth ] map nip ;
: diags [ diag ] [ reverse diag ] bi 2array ;
: game-over?
dup clone flip dup diags append append [ complete? ] any? ;
: game
X create-grid
[ dup game-over? ] [ draw switch-player place ] until
"Game over." print 2drop ;
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 15:25:44 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations
namespaces combinators.short-circuit math math.parser
splitting ;
IN: tic-tac-toe
SYMBOLS: X O ;
: at nth nth ;
: set-nth! [ set-nth ] keep ;
: draw dup [ . ] each ;
: row 3 swap <array> ;
: create-grid 3 [ f row ] replicate ;
: inside? { [ 0 >= ] [ 3 < ] } 1&& ;
: valid-pos? [ inside? ] both? ;
: is-free? rot at f = ;
: update-case
[ pick nth 4 npick -rot set-nth! ] keep rot set-nth! ;
: switch-player X = O X ? ;
: read-tuple
readln "," split [ string>number ] map
0 over nth 1 rot nth ;
: read-pos
4 4 [ 2dup valid-pos? ]
[ 2drop "Enter a position: " write read-tuple ] do until ;
: place
read-pos
[ 3dup is-free? ] [ 2drop "Occupied zone." print read-pos ]
until update-case ;
: complete? X row O row 2array member? ;
: diag 3 iota [ dup pick at ] map nip ;
: diags [ diag ] [ reverse diag ] bi 2array ;
: game-over?
dup clone flip dup diags append append [ complete? ] any? ;
: game
X create-grid
[ dup game-over? ] [ draw [ switch-player ] dip place ]
until "Game over." print 2drop ;
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 16:31:19 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations namespaces math
math.parser math.order splitting combinators combinators.short-circuit ;
IN: tic-tac-toe
SYMBOLS: X O ;
: at nth nth ;
: set-nth! [ set-nth ] keep ;
: draw dup [ . ] each ;
: row 3 swap <array> ;
: create-grid 3 [ f row ] replicate ;
: outside? [ 0 2 between? ] both? not ;
: free? rot at f = ;
: invalid-pos? {
{ [ 2dup outside? ] [ 3drop "Invalid pos." . t ] }
{ [ free? not ] [ "Occupied zone." . t ] }
[ f ]
} cond ;
: read-tuple readln "," split [ string>number ] map first2 ;
: ask "Enter a position: " write read-tuple ;
: read-pos [ ask 3dup invalid-pos? ] [ 2drop ] while ;
: update-case nth set-nth ;
: play 2dup read-pos rot update-case ;
: complete? X row O row 2array member? ;
: diag [ swap nth ] map-index ;
: diags [ diag ] [ reverse diag ] bi 2array ;
: game-over? dup flip dup diags append append [ complete? ] any? ;
: game
X create-grid
[ dup game-over? ] [ draw [ X = O X ? ] dip play ] until
"Game over." print 2drop ;
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 16:52:54 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations namespaces math
math.parser math.order splitting combinators combinators.short-circuit
sequences.deep ;
IN: tic-tac-toe
SYMBOLS: X O ;
: at nth nth ;
: draw dup [ . ] each ;
: row 3 swap <array> ;
: create-grid 3 [ f row ] replicate ;
: outside? [ 0 2 between? ] both? not ;
: taken? rot at f = ;
: invalid-pos? {
{ [ 2dup outside? ] [ 3drop "Invalid pos." . t ] }
{ [ taken? ] [ "Occupied zone." . t ] }
[ f ]
} cond ;
: read-tuple readln "," split [ string>number ] map first2 ;
: ask "Enter a position: " write read-tuple ;
: read-pos [ ask 3dup invalid-pos? ] [ 2drop ] while ;
: update-case nth set-nth ;
: play 2dup read-pos rot update-case ;
: diag [ swap nth ] map-index ;
: diags [ diag ] [ reverse diag ] bi 2array ;
: complete? X row O row 2array member? ;
: full? flatten f swap member? not ;
: game-over?
{ [ full? ] [ dup flip dup diags append append [ complete? ] any? ] } 1|| ;
: game
X create-grid
[ dup game-over? ] [ draw [ X = O X ? ] dip play ] until
"Game over." print 2drop ;
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 17:01:44 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations namespaces math
math.parser math.order splitting combinators combinators.short-circuit ;
IN: tic-tac-toe
SYMBOLS: X O ;
: at nth nth ;
: draw dup [ . ] each ;
: row 3 swap <array> ;
: create-grid 3 [ f row ] replicate ;
: outside? [ 0 2 between? ] both? not ;
: taken? rot at f = not ;
: invalid-pos? {
{ [ 2dup outside? ] [ 3drop "Invalid pos." . t ] }
{ [ taken? ] [ "Occupied zone." . t ] }
[ f ]
} cond ;
: read-tuple readln "," split [ string>number ] map first2 ;
: ask "Enter a position: " write read-tuple ;
: read-pos [ ask 3dup invalid-pos? ] [ 2drop ] while ;
: update-case nth set-nth ;
: play 2dup read-pos rot update-case ;
: diag [ swap nth ] map-index ;
: diags [ diag ] [ reverse diag ] bi 2array ;
: complete? X row O row 2array member? ;
: full? concat f swap member? not ;
: game-over?
{ [ full? ] [ dup flip dup diags append append [ complete? ] any? ] } 1|| ;
: game
X create-grid
[ dup game-over? ] [ draw [ X = O X ? ] dip play ] until
"Game over." print 2drop ;
Author: | rks |
Mode: | factor |
Date: | Sat, 25 Dec 2010 17:23:04 |
Plain Text |
USING: arrays sequences kernel io prettyprint generalizations namespaces math
math.parser math.order splitting combinators combinators.short-circuit ;
IN: tic-tac-toe
SYMBOLS: X O ;
: at nth nth ;
: draw dup [ . ] each ;
: row 3 swap <array> ;
: create-grid 3 [ f row ] replicate ;
: inside? drop [ 0 2 between? ] both? ;
: free? at f = ;
: check over [ drop ] [ . ] if ;
: valid?
{ [ inside? "Outside" check ] [ free? "Occupied" check ] } 3&& ;
: read-tuple readln "," split [ string>number ] map first2 ;
: ask "Enter a position: " write read-tuple ;
: read-pos [ ask rot 3dup valid? ] [ 2nip ] until ;
: update-case nth set-nth ;
: play 2dup read-pos update-case ;
: diag [ swap nth ] map-index ;
: diags [ diag ] [ reverse diag ] bi 2array ;
: complete? X row O row 2array member? ;
: full? concat f swap member? not ;
: 3-aligned? dup flip dup diags append append [ complete? ] any? ;
: game-over? { [ full? ] [ 3-aligned? ] } 1|| ;
: game
X O create-grid
[ dup game-over? ] [ draw swapd play ] until "Game over." print 3drop ;
New Annotation