-2

I am trying to code a game of Bridge in APL the part I'm stuck on is when printing hands I want to return 4 separate hands to be scored of all random cards and I want to sort the cards by suit first and then within the suits sort by Ace to two I am able to accomplish each one of these sorts individually

 HAND←DEAL;CHAR;DECK

 DECK←MAKE_DECK

 CHAR←DECK[;13?52]

 ⍝HAND←CHAR[;'SHDC23456789TJQKA'”³CHAR]

 HAND←CHAR[;'CDHS'”³CHAR] 

sorts by suit but not the numbers in the suit and I also dont know how to make it deal 4 hands instead of just one hand of 13 cards

example output:

 Q982AQT93T934 AKJ657428J987 K865AKJ54KQT3 T743JQ762A652
 SSSSHHHHHDDDC SSSSSHHHDCCCC HHHHDDDDDCCCC SSSSHDDDDCCCC
MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Hello & welcome to SO :-) I edited your post and tried to use the correct APL-Symbols, but was not sure about the last line - which symbols were you usinf inside the brackets? (A legible question might be easier to discuss...;-)) – MBaas Mar 08 '16 at 20:52
  • the last line is supposed to be 'CDHS' downarrow with line through it and then circle with line diagonally through it – user3226133 Mar 09 '16 at 02:25
  • 1
    Ok, thanks. Did you mean these: ⍒⍉ ? Now, can you pls. also post MAKE_DECK? BTW, which APL are you working with and what material are you using to learn the language? – MBaas Mar 09 '16 at 06:08

2 Answers2

1

(I wasn't sure what you did in MAKE_DECK, maybe there is some special stuff going on? Otherwise there is no reason to make a dedicated function for that. Also, as a non-native english-speaker, I'm not sure about the naming of cards etc. - pls. fix as appropriate...)

DECK←(52⍴'AKQJ0987654'),[.5]13/'SHDC'

I am then dealing the cards and just create an array of indices into DECK;

HANDS←4 13⍴52?52

These need to be sorted row-wise. (I'm doing it using "classic" style which will hopefully work in all APL-Dialects...):

  x←0
  x←x+1 ⋄ HANDS[x;]←HANDS[x;⍋HANDS[x;]] ⋄  →(x<4)/⎕LC

(Pls. note that this line will only work within a function, not when you execute it directly in the interpreter - there is no ⎕LC he could → to...) So, what's the hand of player 1?

DECK[;HANDS[1;]]
JAQJ08740Q4K7
SSHHHHHHDDCCC

Hope you can put together a nice function from that ;-)

MBaas
  • 7,248
  • 6
  • 44
  • 61
  • Great - could you then pls. tick the checkmark next to the reply to indicate that the question is answered? – MBaas Mar 12 '16 at 09:22
  • Oh, and also you might be interested to look at this: http://www.dyalog.com/mastering-dyalog-apl.htm (This refers to Dyalog APL, the best APL around IMHO. They offer free licenses for personal/educational use AND have a student-competition going on atm!) – MBaas Mar 12 '16 at 09:24
  • One last thing That would help a lot I have an array a 2x52 array with numbers on top suits on the bottom and I want to turn it into a 2x56 array with a space every 13 characters to show the difference between the 4 hands is there a simple way to do this Thank you! – user3226133 Mar 13 '16 at 23:35
  • This does that: `(' ',DECK)[;1+,HANDS,0]` – MBaas Mar 14 '16 at 06:29
0

Building on the preceding answer, we can eliminate the loop with a bit of arithmetic. Output spacing is accomplished with expand :

      ⎕←deck←2 52⍴(52⍴'AKQJ098765432'),13/'SHDC'
AKQJ098765432AKQJ098765432AKQJ098765432AKQJ098765432
SSSSSSSSSSSSSHHHHHHHHHHHHHDDDDDDDDDDDDDCCCCCCCCCCCCC

      ⎕←hand←52?52
41 45 42 18 33 15 7 44 21 16 25 11 49 10 40 6 1 19 43 52 46 9 24 17 8 38 39 34 28 23 20 50 29 37 2 36 26 13 31 51 4 48 32
  5 27 22 14 3 30 47 35 12

Unsorted

      (55⍴14↑13⍴1)\deck[;hand]
K9Q08K807Q345 5A9A9J2864J73 27K584Q4K5220 3J690A6AQJ763
CCCHDHSCHHHSC SCSSHCCCSHHSD DDDHHCDDSDHSD CSCDSDHHSDCDS

Sorted

      (55⍴14↑13⍴1)\deck[;hand[⍋hand+13/1000×⍳4]]
84KQ0738KQ095 A9765J943AJ82 K2852KQ075424 QJ03A6AJ96763
SSHHHHHDCCCCC SSSSSHHHDCCCC SSHHHDDDDDDDC SSSSHHDDDDCCC