NB.  Since J has no declarations I didn't plan ahead.

Note 0

Sample:

   fish load'rosetta.ijs'
You're dealt a three, 2 fives, a seven, an eight, a ten, a king, 2 aces.
You hold a three, 2 fives, a seven, an eight, a ten, a king, 2 aces.
Request [2-9tjqka]?
t
You hold a three, 2 fives, a seven, an eight, 2 tens, a king, 2 aces.
Request [2-9tjqka]?

...

You caught a three, a five, an eight, a ten, an ace.
Fine computer said "Go, fish."
You drew a queen.
You told computer to "go fish" when asked for a four.
You hold 2 threes, 3 fives, a seven, 2 eights, 2 tens, a queen, a king,
3 aces.
Request [2-9tjqka]?

...

You hold 4 threes, 4 fives, 4 sevens, 4 eights, 4 tens, 4 queens, 4
kings, 4 aces.
You caught a JOKER, a queen.
Fine computer said "Go, fish."
+-----------+-----+
|ShallowBlue|Human|
+-----------+-----+
|5          |8    |
+-----------+-----+
)   



rules =: 0 : 0
Each player is dealt nine cards to start with.

On their turn, a player asks  their opponent for a given rank (such as
threes or kings).   A player must already have at least  one card of a
given rank to ask for more.

If the opponent  has any cards of the named rank,  they must hand over
all such cards, and the requester can ask again.

If the opponent has no cards  of the named rank, the requester draws a
card and ends their turn.

A book  is a  collection of every  card of  a given rank.   Whenever a
player completes a book, they may remove it from their hand.

If at any  time a player's hand is empty, they  may immediately draw a
new card, so long as any new cards remain in the deck.

The game ends  when every book is complete.  The  player with the most
books wins.
)
instructions =: 0 : 0
Go Fish!
Request a card using a single character: 23456789tjqka
)
write =: (": 1!:2 2:) :[:
read =: 3 :'1!:1]1'

coclass 'Cards'                     NB. define methods in Cards locale
 create =: 3 : 'cards =: y'         NB. cards i.54
 destroy =: codestroy
 push =: 3 : 0                       NB. push 3 2 8
  cards =: y , cards                 NB. returning new cards is useful
  y
 )
 shuffle =: 3 : 'cards =: ({~ ?...@#)cards' NB. shuffle a:
 pop =: 3 : 0                             NB. pop 4
  y =. y<.#cards
  rv =. y {. cards
  cards =: y }. cards
  rv
 )
 give =: 3 : 0                          NB. give 8  NB. give rank
  rv =. y#~+/y=cards
  cards =: cards-.y
  rv
 )
 count =: 3 :'#cards'
 worms =: 3 : '1 2 3 of_a_kind'
 no_worms =: 3 : '0 = +/worms a:'   NB. useful for hand
 clear =: 3 :'cards=:i.0'
 empty =: 3 :'0=count a:'               NB. useful for deck
 rank_count =: 3 : '([: <: [: #/.~ (i.14) , ]) cards' NB. rank_count max
 of_a_kind =: 1 : 'I.u e.~rank_count a:' NB. 1 2 3 of_a_kind
 books =: 3 :'4 of_a_kind'
 draw =: 3 : 'push pop__y 1'

 card_names=:;:'JOKER ace deuce three four five six seven eight nine ten
jack queen king'
 plural =: ;:'S s s s s s es s s s s s s s'
 ia =: ' ',~&.>([: ; (<;._1 ' a an') {~ 'aeiou' e.~ {.)&.>card_names
  NB. card_names ,&> plural   -->  ...sixes sevens...
  NB. ia ,&> card_names   -->  ...an ace a deuce...

 show =: 3 : 0 NB. show'X'->'X a deuce, a five, a seven, 2 tens..., 2
aces'
  o =. (0,1,~2+i.12)                   NB. order
  counts =. rank_count a:              NB. 0 2 1 0 0 1 0 1 0 0 2 1 0 1
  scount =. (1=counts)}ia,:~_2<\' ',~":counts NB. │0 │2 │a │0 │0 │a
│0...
  suffix =. (1=counts)}plural,:a:             NB.
│S│s││s│s││es││s│s│s││s││
  names =. card_names ,&.> suffix  NB. │JOKERS│aces│deuce│threes│fours
  2(1!:2)~y,' ','.',~_2}.;(,&', ')&.>(0<o{counts)#(o{scount) ,&.>
o{names
 )
cocurrent 'base'

deal =: 3 : 0
 deck =: 'Cards'conew~>:13|i.52
 shuffle__deck a:
 sb =: 'Cards'conew~pop__deck y         NB. shallow blue
 hh =: 'Cards'conew~pop__deck y         NB. human hand
)

shallow_blue =: 3 : 0
 NB. AI: fill an empty hand only when it's time to guess.
 NB.     The penultimate rule gives choice.
 draw__sb^:(no_worms__sb *. -...@empty__deck)^:_ deck
 clear__void''
 if.no_worms__sb''do.0 return.end.
 c =. push__sb give__hh push__void ({~ ?...@#) worms__sb''
 fill^:(no_worms__hh *. -...@empty__deck)^:_ deck NB. human may refill
hand
 c
)

NB.   10 1 -: translate"0'tA'
translate =: 14 | {...@i.@('0a23456789tjqk0A23456789TJQK'&e.)

fill =: 3 : 0
 if. no_worms__hh'' do.
  if. +./'yY'e.{.read write'Draw a card into your empty hand [yn]?' do.
   draw__hh deck
  end.
 end.
 show__hh'Now have'
)

kasparov =: 3 : 0                       NB. human turn
 show__hh'You hold'
 fill^:(no_worms__hh *. -...@empty__deck)^:_ deck
 if. no_worms__hh'' do. 0 return. end.  NB. chose against drawing
 worms =. worms__hh''
 whilst. (0&= +. -...@e.&worms)request do.
  request =. translate {. read write'Request [2-9tjqka]?'
 end.
 push__hh give__sb request
 NB. AI: if computer hand were emptied here it could draw a card.
)

fish =: 3 : 0
 deal 9
 show__hh 'You''re dealt'
 transfer =: ''conew'Cards'
 void =: ''conew'Cards'
 take =. 2 : '0 < +/push__transfer [email protected] y'
 turn =. ?2
 while. (13 > #...@books__sb + #...@books__hh)0 do.
  while. (shallow_blue`kasparov take turn)0 do. 0 end.
  if. -.empty__transfer'' do.
   show__transfer>turn{'Your fine computer took from you';'You caught'
   clear__transfer''
  end.
  if.turn do.
   write'Fine computer said "Go, fish."'
  else.
   show__void'You told computer to "go fish" when asked for'
  end.
  if. -.empty__deck'' do.
   push__transfer draw__sb`[email protected] deck
   show__transfer^:turn'You drew'
   clear__transfer''
  end.
  turn =. -.turn
 end.
 ('ShallowBlue';'Human'),:#&.>(books__sb ; books__hh)0
)


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to