Deal Cards

This is a discussion / support forum for the Hugo programming language by Kent Tessman. Hugo is a powerful programming language for making text games / interactive fiction with multimedia support.

Hugo download links: https://www.generalcoffee.com/hugo
Roody Yogurt's Hugo Blog: https://notdeadhugo.blogspot.com
The Hugor interpreter by RealNC: http://ifwiki.org/index.php/Hugor

Moderators: Ice Cream Jonsey, joltcountry

User avatar
Tdarcos
Posts: 9333
Joined: Fri May 16, 2008 9:25 am
Location: Arlington, Virginia
Contact:

Deal Cards

Post by Tdarcos »

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 &#40;Index=1; Index<=52; Index++&#41; ! Clear prior shuffle if any
     usedcard&#91;Index&#93; = FALSE
   
   for &#40;Index=1; Index<=52; Index++&#41;
   &#123;
		this_card = random&#40;52&#41;
		while usedcard&#91;this_card&#93;=true         
		&#123;
      	  this_card = random&#40;52&#41;
	    &#125;
		usedcard&#91;this_card&#93;=true
		card&#91;Index&#93;=this_card 
   &#125;
&#125;
 
Routine Display_Card &#40;Card_Number&#41; 
&#123;
 local Card_Rank
 local Card_Suit
 
	Card_rank = mod&#40;card&#91;Card_Number&#93;, 13 &#41; 
	Card_suit = card&#91;Card_Number&#93; / 13 +1
	if Card_rank = 0 
	&#123;
		Card_suit = Card_suit - 1
		Card_rank = 13
	&#125;

 	print cardname&#91;Card_rank&#93;; " of ";suitname&#91;Card_suit&#93;
&#125;

 
Routine Deal_Deck
&#123;
local Index
 
   for &#40;Index=1; Index<=52; Index++&#41;
   &#123;
      print "Card&#91;"; number Index ; "&#93;=" ; number card&#91;index&#93;;"  ";
	  Display_Card&#40;Index&#41;
   &#125;
&#125;


routine main
&#123;

&#125;



Routine ShowGame
&#123;   
   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
&#125;

Alan Francis wrote a book containing everything men understand about women. It consisted of 100 blank pages.