21 game: Difference between revisions

m
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 <tt>''</tt> types quickly, hence often used to trigger niladic verbs. g21 does not superfluously penalize final sums exceeding 21.
<lang J>
g21=: summarysummarize@play@setup ::('21g21: error termination'"_)
 
 
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 'HouseIt chose {}.' format~ choice Fetch)@(choice Alter~ cb)
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 '21g21: early termination'"_))^:(3 = choice Fetch)
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
summarysummarize=: ' won' ,~ (];.2_2 'houseit you ') {~ turn Fetch
 
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
HouseIt chose 21.
sum: 31
your choice?
q
3
21g21: early termination
sum: 3
sum: 666
House chose 2.
you won
 
g21''
On your turn enter 1 2 or 3, other entries exit
your choice?
sum: 2
It chose 3.
sum: 5
your choice?
sum: 76
HouseIt chose 23.
sum: 9
your choice?
2
sum: 1011
HouseIt chose 32.
sum: 13
your choice?
3
sum: 16
HouseIt chose 1.
sum: 17
your choice?
1
sum: 18
HouseIt chose 3.
sum: 21
houseit won
g21''
On your turn enter 1 2 or 3, other entries exit
your choice?
x
21: early termination
sum: 666
you won
</pre>
 
Anonymous user