Card games...

Blitz3D Forums/Blitz3D Beginners Area/Card games...

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's a yugioh program me and my son are working on. Each player has their own deck but it illustrates using arrays in types to track which cards are where.

When you have arrays pointing to types you can use the Null value to set that slot to empty, so to move a card from a player's hand into play-
p\inplay[j]=c
p\hand[i]=Null

where p is the current player, i is the card from their hand they are playing and j is the playarea which in yugioh is an array of 5 cards in front of the player.

The method of shuffle swaps two random cards in the deck multiple times. Also note the use of the command SeedRnd MilliSecs() to make sure you get a random deal every time you run the game.


; yugioh.bb
; by simon and harley

Type card
	Field	name$
	Field	attack
	Field	defence
	Field	effect$
	Field	stars
	Field	place
End Type

Type player
	Field	name$
	Field	decksize
	Field	deck.card[256]
	Field	hand.card[7]
	Field   inplay.card[5]
	Field	graveyard.card[256]
	Field	lifepoints
End Type

Function createplayer.player(name$)
; read in deck
	p.player=New player
	p\name=name
	Read size
	p\decksize=size
	For i=1 To size	
		c.card=New card
		Read c\name$
		Read c\attack
		Read c\defence
		Read c\effect$
		Read c\stars		
		p\deck[i]=c
	Next
	p\lifepoints=8000
	Return p
End Function

Function ShuffleDeck(p.player)
	For i=1 To 50
		a=Rnd(p\decksize)
		b=Rnd(p\decksize)
; swap 2 cards in the deck		
		c.card=p\deck[a]
		p\deck[a]=p\deck[b]
		p\deck[b]=c
	Next
End Function

Function DealCards(p.player)
	For i=1 To 5
		p\hand[i]=p\deck[p\decksize]
		p\decksize=p\decksize-1
	Next
End Function

Function PlayTurn(p.player,q.player)
	Print ""
	Print p\name+"'s Turn"
; deal card
	For i=1 To 7
		If p\hand[i]=Null
			p\hand[i]=p\deck[p\decksize]
			p\decksize=p\decksize-1
			Exit
		EndIf
	Next
	summons=7
; command loop
	While True
; print out commands
		Print ""
		For i=1 To summons
			If p\hand[i]<>Null
				Print "    [S"+i+"] Summon "+p\hand[i]\name
			EndIf
		Next
		attacks=0		
		For i=1 To 5	
			If p\inplay[i]<>Null
				For j=1 To 5
					If q\inplay[j]<>Null
						Print "    [A"+i+""+j+"] Attack "+q\inplay[j]\name+" with "+ p\inplay[i]\name
						attacks=attacks+1
					EndIf
				Next
			EndIf
		Next
		If (summons+attacks=0) Return
		Print ""
; input command
		a$=Input("("+p\lifepoints+") >")
		Print ""
; finish turn
		If a$="" Then Return
; quit game?
		If a$="q" Then End
; summon monster ?
		If (summons>0 And Left$(a$,1))="S" Or (Left$(a$,1))="s"
			i=Mid$(a$,2)
			c.card=p\hand[i]
			If c<>Null
				Print p\name+" summons "+c\name
				For j=1 To 5
					If p\inplay[j]=Null
						p\inplay[j]=c
						p\hand[i]=Null
						Exit
					EndIf
				Next
			EndIf
		EndIf
; attack monster ?
		If (Left$(a$,1))="A" Or (Left$(a$,1))="a"
			i=Mid$(a$,2,1)
			j=Mid$(a$,3,1)
			If (i<6 And j<6)
				c.card=p\inplay[i]
				cc.card=q\inplay[j]
				If c<>Null And cc<>Null			
					If (c\attack>cc\defence)
						pts=c\attack-cc\defence
						Print c\name+" wins! "+cc\name+" dies, "+q\name+" looses "+pts+" lifepoints."
						q\lifepoints=q\lifepoints-pts	
						q\inplay[j]=Null
					Else
						pts=cc\defence-c\attack
						Print c\name+" dies "+p\name+" looses, "+pts+" lifepoints,"	;destroy monster
						p\lifepoints=p\lifepoints-pts
						p\inplay[i]=Null
					EndIf
				EndIf
			EndIf
		EndIf
		
		summons=0
		
	Wend
	
End Function


; main program
; to have different deck for each player change the Restore points

SeedRnd MilliSecs() 

a$=Input("Player 1 name-")
Restore HarleyDeck
p1.player=CreatePlayer(a$)

a$=Input("Player 2 name-")
Restore HarleyDeck
p2.player=CreatePlayer(a$)

ShuffleDeck(p1)
ShuffleDeck(p2)
DealCards(p1)
DealCards(p2)

.gameloop
	PlayTurn(p1,p2)
	PlayTurn(p2,p1)
	Goto gameloop

;PlayTurn(deck1)
;	Print c\name+" "+Left$("*************",c\stars)
;MouseWait

Print "Game Over Dudes"
Input "Hit Return To Quit"

End

	
.HarleyDeck
Data 21
Data "LordOfD",1200,1100,"No Dragon Effects",4
Data "LordOfD",1200,1100,"No Dragon Effects",4
Data "LordOfD",1200,1100,"No Dragon Effects",4
Data "Blue-Eyes",3000,2500,"No Effect",8
Data "Blue-Eyes",3000,2500,"No Effect",8
Data "Blue-Eyes",3000,2500,"No Effect",8
Data "Rude Kaiser",1800,1600,"No Effect",5
Data "Swordstalker",2000,1600,"No Effect",6
Data "Battle Ox",1700,1000,"No Effect",5
Data "Battle Ox",1700,1000,"No Effect",5
Data "Hane-Hane",450,500,"FLIP:1 monster back to owners hand",2
Data "Dark Assailant",1200,1200,"No Effect",4
Data "Skull Red Bird",1550,1200,"No Effect",4
Data "La Ginn The Mgical Genie Of The Lamp",1800,1000,"No Effect",4
Data "Master & Expert",1200,1000,"No Effect",4
Data "Orge Of The Black Shadow",1200,1400,"No Effect",4
Data "Ryu-Kishin",1000,500,"No Effect",3
Data "Hitotsu-Me Giant",1200,1000,"No Effect",4
Data "Trap Master",500,1100,"No Effect",3
Data "Rouge Doll",1600,1000,"No Effect",4
Data "Ray & Temprature",1000,1000,"No Effect",3