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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Playing cards step by step in the Arturo 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 Arturo programming language

Source code in the arturo programming language

ranks: 1..13
suits: [`♣`, `♦`, `♥`, `♠`]

define :card [rank, suit][
    init: [
        ensure -> 
            and? -> contains? ranks this\rank
                 -> contains? suits this\suit
    ]

    print: [
        R: ø
        case [this\rank=]
            when? [1] -> R: `A`
            when? [11]-> R: `J`
            when? [12]-> R: `Q`
            when? [13]-> R: `K`
            else -> R: this\rank
        ~{|R||this\suit|}
    ]
]

define :deck [][
    init: [
        this\cards: []
        loop ranks 'rank ->
            loop suits 'suit ->
                this\cards: this\cards ++ to :card @[rank, suit]
    ]
]

shuffleUp: function [this :deck][
    this\cards: shuffle this\cards
]

deal: function [this :deck, cnt :integer][
    if cnt > size this\cards ->
        panic "Not enough cards in deck"

    cards: []
    do.times: cnt [
        dealt: sample this\cards
        'cards ++ dealt
        this\cards: remove this\cards dealt
    ]
    return cards
]

; create a new deck
Deck: to :deck []

; and shuffle it
shuffleUp Deck

; deal 5 cards
print deal Deck 5


  

You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the Lua programming language
You may also check:How to resolve the algorithm Set of real numbers step by step in the Rust programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Raku programming language
You may also check:How to resolve the algorithm Cumulative standard deviation step by step in the FOCAL programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Perl programming language