Anonymous user
21 game: Difference between revisions
m
→{{header|J}}
m (→{{header|J}}) |
m (→{{header|J}}) |
||
Line 2,749:
putStrLn $ (snd names) ++ " has won the game"</lang>
=={{header|J}}==
The main definition <tt>g21</tt> starts the game. J doesn't support niladic verbs. Although the <tt>y<tt> argument has no effect, it must be given. The empty literal vector
<lang J>
g21=:
read=: 1!:1@:1:▼
write=: 1!:2 4: NB. unused▼
format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])▼
NB. non-verb must be defined before use, otherwise are assumed verbs.
Until=: 2 :'u^:(0-:v)^:_'
t=: 'score turn choice'
(t)=: i. # ;: t
empty erase't'
Fetch=: &{
Alter=: }
play=: move Until done▼
done=: 21 <: score Fetch
setup=: ([ [: echo 'On your turn enter 1 2 or 3, other entries exit'"_)@((3 :'?2') turn Alter ])@0 0 0"_ NB. choose first player randomly▼
summary=: 'won' ,~ (];.2 'house you ') {~ turn Fetch▼
▲play=: move Until done
move=: [: update you`it@.(turn Fetch)
Line 2,783 ⟶ 2,772:
swap=: turn Alter~ ([: -. turn Fetch)
it=: ([ [: echo '
cb=: (1:`r`3:`2:)@.(4 | score Fetch) NB. computer brain
r=: 3 :'>:?3'
Line 2,790 ⟶ 2,779:
prompt=: [ ([: echo 'your choice?'"_)
acquire=: choice Alter~ ('123' i. 0 { ' ' ,~ read)
check=: (choice Alter~ (665 - score Fetch))@([ ([: echo '
qio1=: choice Alter~ ([: >: choice Fetch)
▲setup=: ([ [: echo 'On your turn enter 1 2 or 3, other entries exit'"_)@((3 :'?2') turn Alter ])@0 0 0"_ NB. choose first player randomly
▲read=: 1!:1@:1:
▲write=: 1!:2 4: NB. unused
▲format=: ''&$: :([: ; (a: , [: ":&.> [) ,. '{}' ([ (E. <@}.;._1 ]) ,) ])
</lang>
<pre>
g21''
On your turn enter 1 2 or 3, other entries exit
your choice?
q
▲sum: 3
sum: 666▼
▲House chose 2.
g21''▼
On your turn enter 1 2 or 3, other entries exit▼
your choice?▼
2▼
sum: 2
It chose 3.
sum: 5
your choice?
1▼
▲2
sum:
sum: 9
your choice?
2
▲1
sum:
sum: 13
your choice?
3
sum: 16
sum: 17
your choice?
1
sum: 18
sum: 21
▲ g21''
▲On your turn enter 1 2 or 3, other entries exit
▲your choice?
▲21: early termination
▲sum: 666
▲you won
</pre>
|