Deck of Cards

General FreeBASIC programming questions.
Post Reply
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

Deck of Cards

Post by NorbyDroid »

Not much here, but hopefully enough to be useful.

This includes two things:
1) Setup a Deck of Cards (Playing Cards, Uno Cards, or Skip-Bo Cards)
2) Shuffles the Deck

Thi is is a start for creating some card games.

When you run the code, it will tell you the Deck chosen, number of cards in that Deck, and a view of the Deck as it is fresh from the package. After pressing a key it will shuffle and display the shuffled Deck.

Enjoy.

Code: Select all

Declare Sub Riffle(iYag as Integer)

CLS

Randomize Timer

' Setup Initial Card Deck
ReDim Shared Deck(54, 2) as String

Sub LoadDeck(DeckName as String)
  DIm Card as Integer
  Dim Suit as Integer
  Dim Denom as Integer

  Color 11: Print " Deck Type: ";

  Card=1

  Select Case uCase$(DeckName)

    Case "PLAYING"
      Print "Playing Cards",

      For Suit=1 to 4
        For Denom=1 to 13
          Deck(Card, 1)=Mid$("A234567891JQK", Denom, 1)+Mid$("HDCS", Suit, 1)

          Card=Card+11
        Next
      Next

      ' Jokers Included
      Deck(53, 1)="BF"
      Deck(54, 1)="RF"

    Case "UNO"
      ' Reset Deck for Uno Cards
      ReDim Deck(108, 2) as String

      Print "Uno Cards",

      For Suit=1 to 4
        For Denom=1 to 25
          Deck(Card, 1)=Mid$("RGBY", Suit, 1)+_
                        Mid$("0112233445566778899DDRRSS", Denom, 1)
          Card=Card+1
        Next
      Next

      ' Wild 'Uno' Cards
      For Card=101 to 108
        Deck(Card, 1)="W"+Mid$("CCCCDDDD", Card-100, 1)
      Next

    Case "SKIP-BO"
      ' Reset Deck for Skip-Bo Cards
      ReDim Deck(162, 2) as String

      Print "Skip-Bo Cards",

      For Suit=1 to 12
        For Denom=1 to 12
          Deck(Card, 1)=Mid$(" 1 2 3 4 5 6 7 8 9101112", 2*(Denom-1)+1, 2)
          Card=Card+1
        Next
      Next

      ' Wild 'Skip=Bo' Cards
      For Denom=1 to 18
        Deck(Card, 1)="SB": Card=Card+1
      Next

  End Select

  Print "Number of cards:"; uBound(Deck)
End Sub

Sub ShowDeck
  Dim t1 as Integer
  Dim t2 as Integer

  Select Case uBound(Deck)
    Case  54: t2=13
    Case 108: t2=18
    Case 162: t2=12
  End Select

  For t1=1 to uBound(Deck)
    Print Deck(t1, 1);" ";
    If t1 MOD t2=0 then Print:Print " ";
  Next

  Print
End Sub

Sub Shuffle
  Dim cRand as Integer
  Dim tCard as Integer
  Dim tRand as Integer
  Dim Total as Integer

  Riffle 0

  tCard=1
  Total=uBound(Deck)

  While tCard<>Total+1
    cRand=Int(Total*Rnd)+1

    If Deck(cRand, 1)<>"" then
      Deck(tCard, 2)=Deck(cRand, 1)
      Deck(cRand, 1)="": tCard=tCard+1
    End If
  Wend

  For tCard=1 to Total
    Deck(tCard, 1)=Deck(tCard, 2): Deck(tCard, 2)=""
  Next

  Riffle 1
End Sub

Sub Riffle(iTag as Integer)
  Dim t as Integer
  Dim c1 as Integer
  Dim c2 as Integer

  c1=uBound(Deck): c2=c1\2

  For t=0 to c2-1
    If iTag=0 then
      Deck(2*t+1, 2)=Deck(c1-t, 1)
      Deck(2*t+2, 2)=Deck(c2-t, 1)
    Else
      Deck(2*t+2, 2)=Deck(c2-t, 1)
      Deck(2*t+1, 2)=Deck(c1-t, 1)
    End If
  Next

  For t=1 to c1
    Deck(t, 1)=Deck(t, 2): Deck(t, 2)=""
  Next
End Sub

' Decks: 'Playing' | 'Uno' | 'Skip-Bo'
LoadDeck "Uno"

Dim t as Integer

While Inkey$<>Chr$(27)
  For t=1 to 3
    Shuffle
  Next

  Locate 2, 1: Print " ";: ShowDeck
  Sleep 5
Wend
Last edited by NorbyDroid on May 03, 2022 0:45, edited 2 times in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Deck of Cards

Post by dodicat »

I got this running but after many corrections.
How did you compile your code with fb?
NorbyDroid
Posts: 70
Joined: May 21, 2016 22:55

Re: Deck of Cards

Post by NorbyDroid »

The code above is fully functional. Tested and created in DOS with FreeBasic Compiler 1.09.
Post Reply