Drawing Cards?

Blitz3D Forums/Blitz3D Beginners Area/Drawing Cards?

Fuller(Posted 2007) [#1]
i'm making a simple card game and am having trouble "drawing" or picking cards from a deck.

here's my attempt:
For x=1 To drawnum 
temp(x)=Rand(1,drawnum)
Next 

For c.card=Each card
	If c\num=temp(x) And c\where$="" Then 
		c\where$="hand"
		c\x=500+nx     ;offset hand value
	EndIf 
Next 


this doesn't seem to work as it will draw multiple card of the same kind.
Please Help!


Fuller(Posted 2007) [#2]
For x=1 To drawnum 
temp(x)=Rand(1,drawnum)
For xx=1 To drawnum
If temp(x)=temp(xx) And x<>xx Then drawnum=drawnum+1
Next 
Next 

For x=1 To drawnum
For c.card=Each card
	If c\num=temp(x) And c\where$="" Then 
		c\where$="hand"
		c\x=500+nx     ;offset hand value
	EndIf 
Next 
Next 


ok, this is selecting the first two of three cards i told it to draw but not the third one. Whats wrong?


Fuller(Posted 2007) [#3]
hmm.. i kinda rooted the problem down to this:
i need a function that return a different number between a [to] and [from] variable that is much like the rand() function.

Seems simple but i cant get it.


Yahfree(Posted 2007) [#4]
hmm, i'll try to whip up a small example of drawing random numbers


Yahfree(Posted 2007) [#5]
heres a small example:

Type Card
	Field name_number%
	Field name$
	Field number%
End Type

Global maxnumber

Graphics 500,300
SeedRnd MilliSecs()

Print "Press R to redraw!"
maxnumber%=Input("Whats the max amount of cards to be drawn?")
Print ""

ReDraw()

i=0

While Not KeyHit(1)

If KeyHit(19)
	ReDraw()
End If

Wend
End
	
	
	
Function ReDraw()

Delete Each Card

For i=0 To maxnumber-1

	DrawnCard.Card = New Card
	DrawnCard\name_number = Rand(1,4)
	
	Select DrawnCard\name_number
	
		Case 1: DrawnCard\name = "Spades"
		Case 2: DrawnCard\name = "Hearts"
		Case 3: DrawnCard\name = "Dimonds"
		Case 4: DrawnCard\name = "Clubs"
	
	End Select
	DrawnCard\number = Rand(1,10)
	
Next

Print ""
i=1
	For Hand.Card = Each Card
		Print "Card "+i+": " + Hand\number + " Of " + Hand\name
		i=i+1
	Next

End Function



hope it helps


Fuller(Posted 2007) [#6]
thats helpful but i'm getting duplicates:

3 of clubs
...
3 of clubs



I've got an idea for a card game that's like Fluxx (if you ever heard of it) and uses a special deck of cards which has no dups.

thanks for the help


Yahfree(Posted 2007) [#7]
yeah, it doesnt count dupes, you'd have to set up a system:

1) create a card
2) check that card agenst all of the cards
3) if it finds one card, its fine, because that 1 would be the card just drawn.
4) if it find more then 1, delete the card, and redraw it.

heres an example:

Type Card
	Field name_number%
	Field name$
	Field number%
End Type

Global maxnumber

Graphics 500,300
SeedRnd MilliSecs()

Print "Press R to redraw!"
maxnumber%=Input("Whats the max amount of cards to be drawn?")
Print ""

ReDraw()

i=0

While Not KeyHit(1)

If KeyHit(19)
	ReDraw()
End If

Wend
End
	
	
	
Function ReDraw()

Delete Each Card


For i=0 To maxnumber-1
.redo

;sets matchs up to count matchs
matchs=0

    
	DrawnCard.Card = New Card
	DrawnCard\name_number = Rand(1,4)
	
	Select DrawnCard\name_number
	
		Case 1: DrawnCard\name = "Spades"
		Case 2: DrawnCard\name = "Hearts"
		Case 3: DrawnCard\name = "Dimonds"
		Case 4: DrawnCard\name = "Clubs"
	
	End Select
	DrawnCard\number = Rand(1,10)
	
	
	For check.Card = Each Card
	
		If DrawnCard\name$ = check\name$ And DrawnCard\number = check\number matchs=matchs+1
			
	Next
	
	If matchs > 1 
		Delete DrawnCard
		Goto redo
	End If
Next

Print "" 
	i=1
	For Hand.Card = Each Card
		Print "Card "+i+": " + Hand\number + " Of " + Hand\name
		i=i+1
	Next

End Function



Fuller(Posted 2007) [#8]
oh, yeah! I forgot you could locally put a goto in a function! That was what i was trying to work around! Thanks!


TomToad(Posted 2007) [#9]
I actually prefer to make an array of cards and scramble it. Then just draw off the top like you would any deck.


Edit: And no Goto in sight :)


Rob Farley(Posted 2007) [#10]
Why not just create your pack of cards and have a flag if it's been drawn or not...

So

Type TCard
Field Value
Field Suit$
Field HeldBy$
End Type

Then when picking a new card just check if it's been drawn already, if it has just move onto the next one until you get a card that isn't drawn. This way you have your deck of cards and you know which player is holding it, or if it's on the table, in the dealers hands, up someone's sleeve etc.

Or go down the array route, I'm pretty sure I wrote a card game a while back and used arrays.


Fuller(Posted 2007) [#11]
thats kinda what i've been doing but i need to draw new cards every turn out of the deck and be able to draw cards out of the discard pile randomly for this card game.

anyways this is kinda working:
Function DrawHand(drawnum)

Local x=1

.Draw
drawrand=Rand(1,total)
Print drawrand
For c.card=Each card
If c\num=drawrand And c\where$="" Then 
	c\where$="hand"
	c\x=x*100
	c\y=GraphicsHeight()-100
	DebugLog  "Card "+x+" Found"
	If x<drawnum Then x=x+1:Else If x=drawnum Then Return  
Else 
Goto Draw
EndIf
Next 


End Function 


...but it results in finding one card and then gets stuck in an endless loop


Yahfree(Posted 2007) [#12]
Would it be ok if you posted all of your code? or at least more, its hard for me to make out the problem with the code listed in your latest post.


Rob Farley(Posted 2007) [#13]
OK... A bit of work and I get something like this:

This creates a deck of cards, each card can be given to a 'pile'. A pile could be the main deck, a player, the table, whatever you want, you can have any number of piles, it doesn't matter.

You can actually create any number of decks of cards, however, there's no id for which deck you're working with so that might be something worth adding if you need to distinguish between multiple decks.

Each pile can be drawn from, by default it draws from pile 0, simply pass which pile you want to draw from. ie DrawNextCard(5) will take the next card off 5.

You can pass a card to any pile by using the function GiveCardTo(card, pile)

Example code of how to use it after all the functions.

Not sure if this is 100% as not really tested it. So it probably needs tweaking.

It needs a couple more functions
MovePileTo(PileID) - Moves all cards from one pile to another
ResetPile(PileID) - Resets the next card (used after a shuffle etc)





Rob Farley(Posted 2007) [#14]
Don't you just hate demonstrating you have too much free time on your hands?




Yahfree(Posted 2007) [#15]
thats neat, i'm a bit on the bored side aswell, i'll try to make some nifty images in photoshop :D


Fuller(Posted 2007) [#16]
here's my code:


those examples look useful, i'll go and see if i can get this baby running.


Rob Farley(Posted 2007) [#17]
With my code if you wanted to deal 5 cards to 3 players for example:

Current.Card = New Card
CreateDeck(0)
shufflePile(0)

For Deal = 1 To 5
For Player = 1 To 3
	current = DrawNextcard(Current.Card,0)
	GiveCardTo(Current,Player)
Next
Next



Yahfree(Posted 2007) [#18]


heres a 1 of hearts i made in photoshop, took 10 minutes :D

Edit, i think the texture is a bit to visible.. :\


LineOf7s(Posted 2007) [#19]
Nice card...

...but when was the last time you saw a standard deck of cards with a "1" in it?


Yahfree(Posted 2007) [#20]
As far as card images go i think this is a good find
(5 minutes of google/photoshop):


load as an animated image strip and use like so:

AutoMidHandle True

image = LoadAnimImage("Media/Cards/classic-playing-cards.png",73,98,0,52)
MaskImage image,255,0,255
cur_image=0
ClsColor 0,100,0
Repeat

Cls

DrawImage image,GraphicsHeight()/2,GraphicsWidth()/2,cur_image
cur_image=cur_image+1

Delay 1000
Until cur_image >51


WaitKey



Rob Farley(Posted 2007) [#21]
I thought I'd have a go at making them look a little better...




Yahfree(Posted 2007) [#22]
holy crap, that looks nice.

Edit:

:D




Fuller(Posted 2007) [#23]
actually yahfree, i like the first one with the texture that shows. It reminds me of the plastic-coated playing card decks.


Yahfree(Posted 2007) [#24]
if you like the texture fill i used maybe you'll like this:




Fuller(Posted 2007) [#25]
hmm... the texture actually is a little too much isn't it?

Doesn't really matter to me as i can't use them :)


Blitzplotter(Posted 2007) [#26]
not so sure fuller, I quite like Yahfree's textures. Go s back to the old addage 'beauty is in the eye of the beholder chap'. I don't have B3d on this computer, but I'm firing up my main beast (B3D capable) to check out these posts. It's great to have a reason to play with protean again.....

regards, BP.