by Tdarcos » Sun Feb 24, 2013 12:35 pm
This example shuffles a deck and displays the results. Can be used to create any game that requires a deck of cards.
Code: Select all
!\---------------------------------------------------------------------------
!
! deal_cards.hug
! Demonstration of a method to create a shuffled deck
! Released by Viridian Development Corporation
!
! Paul Robinson 02/24/2013
! Last updated 00/00/0000
!
! Updates:
!
! 00/00/0000 -
!
!
---------------------------------------------------------------------------\!
#include "hugolib.h"
array usedcard[52] ! To make sure the deck has no repeats
array Card [52] ! Shuffled Deck
array cardname[13] ! Ace Through King
array suitname[4] ! H C D S
routine init
{
suitname[1] = "Hearts","Clubs","Diamonds","Spades"
cardname[1] = "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"
ShowGame
}
Routine Shuffle
{
local this_card
local Index
for (Index=1; Index<=52; Index++) ! Clear prior shuffle if any
usedcard[Index] = FALSE
for (Index=1; Index<=52; Index++)
{
this_card = random(52)
while usedcard[this_card]=true
{
this_card = random(52)
}
usedcard[this_card]=true
card[Index]=this_card
}
}
Routine Display_Card (Card_Number)
{
local Card_Rank
local Card_Suit
Card_rank = mod(card[Card_Number], 13 )
Card_suit = card[Card_Number] / 13 +1
if Card_rank = 0
{
Card_suit = Card_suit - 1
Card_rank = 13
}
print cardname[Card_rank]; " of ";suitname[Card_suit]
}
Routine Deal_Deck
{
local Index
for (Index=1; Index<=52; Index++)
{
print "Card["; number Index ; "]=" ; number card[index];" ";
Display_Card(Index)
}
}
routine main
{
}
Routine ShowGame
{
Shuffle
Deal_Deck
print "\n Press a key to deal again"
Pause
print " "
Shuffle
Deal_Deck
print "\n Press a key to end"
pause
quit
}
This example shuffles a deck and displays the results. Can be used to create any game that requires a deck of cards.
[code]
!\---------------------------------------------------------------------------
!
! deal_cards.hug
! Demonstration of a method to create a shuffled deck
! Released by Viridian Development Corporation
!
! Paul Robinson 02/24/2013
! Last updated 00/00/0000
!
! Updates:
!
! 00/00/0000 -
!
!
---------------------------------------------------------------------------\!
#include "hugolib.h"
array usedcard[52] ! To make sure the deck has no repeats
array Card [52] ! Shuffled Deck
array cardname[13] ! Ace Through King
array suitname[4] ! H C D S
routine init
{
suitname[1] = "Hearts","Clubs","Diamonds","Spades"
cardname[1] = "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"
ShowGame
}
Routine Shuffle
{
local this_card
local Index
for (Index=1; Index<=52; Index++) ! Clear prior shuffle if any
usedcard[Index] = FALSE
for (Index=1; Index<=52; Index++)
{
this_card = random(52)
while usedcard[this_card]=true
{
this_card = random(52)
}
usedcard[this_card]=true
card[Index]=this_card
}
}
Routine Display_Card (Card_Number)
{
local Card_Rank
local Card_Suit
Card_rank = mod(card[Card_Number], 13 )
Card_suit = card[Card_Number] / 13 +1
if Card_rank = 0
{
Card_suit = Card_suit - 1
Card_rank = 13
}
print cardname[Card_rank]; " of ";suitname[Card_suit]
}
Routine Deal_Deck
{
local Index
for (Index=1; Index<=52; Index++)
{
print "Card["; number Index ; "]=" ; number card[index];" ";
Display_Card(Index)
}
}
routine main
{
}
Routine ShowGame
{
Shuffle
Deal_Deck
print "\n Press a key to deal again"
Pause
print " "
Shuffle
Deal_Deck
print "\n Press a key to end"
pause
quit
}
[/code]