1. Help With My Game of Black Jack
- Posted by David Roach <roachd_76 at YAHOO.COM> Nov 30, 1999
- 598 views
I am making a game of black jack. It's my first euphoria program and I need a little guidance. I've only been doing this for a realy short period of time (2 weeks). I am using Dave Cunny's win32lib and I have figured out how to open windows make menu bars, display text, and make butons and assign actions to the buttons. My game is very simple. Visual black jack.The dealer gets two cards to start and the player gets two cards to start. I have only two buttons "Hit Me" and "Stay". What I need help with is setting up the procedures that make the game work. 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 any one card more than one time. Pretty much any thing beyond simple window stuff I get lost. I can do it in QBasic but I like this programming too much and I want to use it. I just nees some guidance in the right direction. Please help.
2. Re: Help With My Game of Black Jack
- Posted by Irv Mullins <irv at ELLIJAY.COM> Nov 30, 1999
- 574 views
----- Original Message ----- From: David Roach <roachd_76 at YAHOO.COM> To: <EUPHORIA at LISTSERV.MUOHIO.EDU> Sent: Tuesday, November 30, 1999 1:22 PM Subject: Help With My Game of Black Jack > I am making a game of black jack. It's my first euphoria program and I need > a little guidance. I've only been doing this for a realy short period of > time (2 weeks). I am using Dave Cunny's win32lib and I have figured out how > to open windows make menu bars, display text, and make butons and assign > actions to the buttons. My game is very simple. Visual black jack.The > dealer gets two cards to start and the player gets two cards to start. I > have only two buttons "Hit Me" and "Stay". What I need help with is setting > up the procedures that make the game work. 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 any one card more than one time. Pretty much any thing beyond > simple window stuff I get lost. I can do it in QBasic but I like this > programming too much and I want to use it. I just nees some guidance in the > right direction. Please help. Hi: One way is to set up a list of cards in the deck, and then remove each card as it is drawn (so it can't be drawn again) -- put this part in (General) constant hearts = 1, diamonds = 2, clubs = 3, spades = 4 constant suit = {"Hearts","Diamonds","Clubs","Spades"} sequence deck deck = { {"Ace",1},{"Ace",2},{"Ace",3},{"Ace",4},{2,1},{2,2},{2,3},{2,4}, {3,1},.....{"Queen",4},{"King",1},{"King",2},{"King",3},{"King",4} } --put this part in the draw-a-card onClick button response: object newcard integer x -- the second number of each pair denotes the suit. -- each time you draw a card, use rand(length(deck)) to pick from what's left. x = rand(length(deck)) -- randomly select a card newcard = deck[x] --save the card deck = deck[1..x-1] & deck[x+1..length(deck)] -- remove that card from the deck printf(1,"You have drawn the %d of %s",newcard) Try this in text mode - there may be bugs. HTH Irv
3. Re: Help With My Game of Black Jack
- Posted by "Cuny, David at DSS" <David.Cuny at DSS.CA.GOV> Nov 30, 1999
- 570 views
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
4. Re: Help With My Game of Black Jack
- Posted by Brian Broker <bkb at CNW.COM> Nov 30, 1999
- 557 views
Hi David, I might be able to help you. I have a good start on a wrapper/library for cards32.dll, a windows library for drawing cards. It has been designed for win32lib and works well but is not thoroughly documented. The library shuffles cards and remembers where they are placed in your window so that you can easily select them. Perhaps with your help I could polish it up and get it released to the Eu archives. I don't have access to it at the moment but I can get back to you by tomorrow if you're interested. -- Brian
5. Re: Help With My Game of Black Jack
- Posted by Jiri Babor <J.Babor at GNS.CRI.NZ> Dec 01, 1999
- 633 views
David, Just a couple of minor points. Firstly, you did not properly initialize your deck. Secondly, in your shuffle, you call rand() twice per iteration. One is enough, it is an expensive operation. This is roughly what I have been using for the last 30 years: -- WARNING! TESTED CODE! sequence pack -- initialize pack pack = {} for i=1 to 52 do pack &= i end for procedure shuffle() integer dummy, r for i = 1 to 52 do r = rand(52) dummy = pack[r] pack[r] = pack[i] pack[i] = dummy end for end procedure Btw, for very obscure theoretical reasons, this is *not* the best possible shuffle, but it is still far better than what you can get by hand. jiri
6. Re: Help With My Game of Black Jack
- Posted by Everett Williams <rett at GVTC.COM> Nov 30, 1999
- 620 views
- Last edited Dec 01, 1999
>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. > > Lets try this once more from a mathematicians viewpoint: sequence cards,shuffled,newdeck atom card card = 1 cards = repeat(0,52) shuffled = cards newdeck = cards for c = 1 to 52 do cards[c] = c end for newdeck = cards -- now we have a permanent new deck for c = 52 to 1 by -1 do -- shuffle the cards card = rand(c) -- select a card shuffled[c] = cards[card] -- put it face down cards = cards[1..(card - 1)] & cards[(card + 1)..c] -- put deck back together end for -- do it again print(1, shuffled) -- write out the shuffled deck from the first card to -- the last This can easily be made into a procedure and cards can be refreshed from shuffled and "shuffled" again or cards can be refreshed from newdeck and a completely different random sequence generated. The difference between this and a true shuffle algorithm is that the deck must be completely shuffled before it is valid. At any point other than the exit from the loop, shuffled is not valid. Obviously, you might want to put into cards a sequence of an entirely different nature. It should still work. Everett L.(Rett) Williams rett at gvtc.com
7. Re: Help With My Game of Black Jack
- Posted by "Cuny, David at DSS" <David.Cuny at DSS.CA.GOV> Nov 30, 1999
- 641 views
Jiri Babor wrote: > Firstly, you did not properly initialize your deck. Ooops... > Secondly, in your shuffle, you call rand() twice > per iteration. One is enough, it is an expensive operation. Errrr... > -- WARNING! TESTED CODE! Thanks! -- David Cuny