Re: Help With My Game of Black Jack

new topic     » goto parent     » topic index » view thread      » older message » newer message

David Roach wrote:

> For instance pulling random cards
> (I am only using cards 2-9 and jack,
> queen, king, and an ace , four of each
> of course, just to keep it simple), making
> it where the computer will not [use] any one
> card more than one time.


I'll assume the cards are set up like this:

   -- WARNING! UNTESTED CODE!

   -- create a deck of cards
   sequence deckOfCards

   -- create the deck in order
   deckOfCards = {}
   for i = 1 to 52 do
      deckOfCards = i
   end for


One option is to shuffle the deck:

   -- WARNING! UNTESTED CODE!

   function shuffle( sequence deck )

      -- shuffle a deck of cards (integer) by swapping them

      integer tmp, card1, card2

      -- shuffle them
      for i = 1 to length( deck ) do

         -- pick cards to swap
         card1 = rand( length( deck ) )
         card2 = rand( length( deck ) )

         -- swap the cards
         tmp = deck[card1]
         deck[card1] = deck[card2]
         deck[card2] = tmp

      end for

      return deck

   end function


This gives you a list of cards that are shuffled. You can deal them
sequentially.

   -- WARNING! UNTESTED CODE!

   -- shuffle the deck
   deckOfCards = shuffle( deckOfCards )

   -- deal sequentially
   for i = 1 to length( deckOfCards ) do
      -- display the card
      printf( 1, "card #%d is %d\n", {i, deckOfCards[i]} )
   end for


Another option is to remove a card at random from the deck, until the deck
is empty:

   -- WARNING! UNTESTED CODE!

   function removeNth( integer index, sequence s )

      -- return the nth item, and the sequence with the nth item removed

      return { s[index], s[1..index-1] & s[index+1..length(index)] }

   end function


   function removeRandom( sequence s )

      -- remove an item from the sequence at random
      return removeNth( rand(length( s )), s )

   end function

For example:

   -- WARNING! UNTESTED CODE!

   integer card
   sequence tmp

   for i = 1 to length( deckOfCards ) do

      -- draw a card at random
      tmp = removeRandom( deckOfCards )
      card = tmp[1]
      deckOfCards = tmp[2]

      -- display the selection
      printf( 1, "card #%d is %d\n", {i, card } )

   end for

Hope this helps!


[ SOAPBOX ]

In both versions, it would be nice if Euphoria supported the syntax:

   { var list } = expr

For example, the swap could have been written as:

   { deck[card1], deck[card2] } = { deck[card2], deck[card1] }

Drawing a card would be:

   { card, deckOfCards } = removeRandom( deckOfCards )

-- David Cuny

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu