How to resolve the algorithm Playing cards step by step in the K programming language

Published on 12 May 2024 09:40 PM
#K

How to resolve the algorithm Playing cards step by step in the K programming language

Table of Contents

Problem Statement

Create a data structure and the associated methods to define and manipulate a deck of   playing cards. The deck should contain 52 unique cards. The methods must include the ability to: Each card must have a pip value and a suit value which constitute the unique value of the card. Related tasks:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Playing cards step by step in the K programming language

Source code in the k programming language

   v:"A23456789TJQK" / values
   s:"SHCD"          / suites

   / create a new deck
   newdeck:{deck::,/s,'\:v}
   newdeck();


   show:{`0:$,/-3$$deck}

   show()
SA S2 S3 S4 S5 S6 S7 S8 S9 ST SJ SQ SK HA H2 H3 H4 H5 H6 H7 H8 H9 HT HJ HQ HK CA C2 C3 C4 C5 C6 C7 C8 C9 CT CJ CQ CK DA D2 D3 D4 D5 D6 D7 D8 D9 DT DJ DQ DK


   shuffle:{deck::(-#deck)?deck}

   shuffle();show()
S8 CA D5 D2 SJ D6 DJ H7 S4 S9 SQ SK S5 D8 C4 HT DA H3 S6 S2 DT HA C2 C5 D9 ST C7 DK S3 HQ D7 DQ C8 D3 SA CJ CQ CT H4 H2 CK H9 H5 C3 C6 H6 D4 HJ C9 S7 HK H8


   deal1:{|((#deck)-x)_|deck}
   deal:{c:deal1[x];deck::(deck _dvl c);c}

   deal[5]
("S8"
 "CA"
 "D5"
 "D2"
 "SJ")

   #deck / 5 cards are removed
47


   {deal@5}'!3
(("D6"
  "DJ"
  "H7"
  "S4"
  "S9")
 ("SQ"
  "SK"
  "S5"
  "D8"
  "C4")
 ("HT"
  "DA"
  "H3"
  "S6"
  "S2"))


  #deck
32
  show()
DT HA C2 C5 D9 ST C7 DK S3 HQ D7 DQ C8 D3 SA CJ CQ CT H4 H2 CK H9 H5 C3 C6 H6 D4 HJ C9 S7 HK H8


  

You may also check:How to resolve the algorithm Test a function step by step in the Ruby programming language
You may also check:How to resolve the algorithm Loops/Increment loop index within loop body step by step in the Raku programming language
You may also check:How to resolve the algorithm A+B step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Address of a variable step by step in the Component Pascal programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Maple programming language