Need help about TList

BlitzMax Forums/BlitzMax Programming/Need help about TList

MacSven(Posted 2011) [#1]
I am creating an Card game. The Cards are stored in a random order in a TList like this:


Global card_list:TList=New TList

Type card
	Field cardrow:Int
	Field cardx:Int
	Field cardy:Int
	Field cardid:Int
	Field cardsuit:String
	Field cardvalue:Int
	Field cardside:Int
	Field cardmove:Int
EndType



The Drawing sequence is this:


		i=1
		For cardpos:card=EachIn card_list
			If cardpos.cardsuit="KREUZ" Then
				If cardpos.cardside=0 And cardpos.cardmove=0 Then
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,2
				Else
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,-1+(cardpos.cardvalue*4)
				EndIf
			EndIf
			If cardpos.cardsuit="KARO" And cardpos.cardmove=0 Then
				If cardpos.cardside=0 Then
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,2
				Else
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,(cardpos.cardvalue*4)
				EndIf
			EndIf
			If cardpos.cardsuit="HERZ" And cardpos.cardmove=0 Then
				If cardpos.cardside=0 Then
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,2
				Else
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,1+(cardpos.cardvalue*4)
				EndIf
			EndIf
			If cardpos.cardsuit="PIK" And cardpos.cardmove=0 Then
				If cardpos.cardside=0 Then
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,2
				Else
					DrawImage cardimage,cardpos.cardx,cardpos.cardy,2+(cardpos.cardvalue*4)
				EndIf
			EndIf

			EndIf
			i:+1
		Next


I have now the problem that the card is drawn from the first to the last card like the TList.
Is it possible to remove a single card in the deck at a position and insert it at the last position in the TList,
so the card will drawn at last and i can see it.


Midimaster(Posted 2011) [#2]
(if you are german, you could ask this on the german forum www.blitzforum.de too, so it is more easy to describe...)

there are a lot of ways to do this:

A. ARRAY

there is no need of using a list, you could also use an array with the cards. here you can change positions and swap cards easy.

Cards:Card[100]
Cards[0] = new Card
Cards[0].cardx = ....
....

Function Swap()
locCard:Card = Card[34]
Cards[34] = Card[46]
Card[46] = locCard


B. SWAP IN TLIST

you can add another new empty Card, copy the desired card on it, remove the desired card and add the copy to the list:
Type A
    Field nr%
End Type

Global L:TList = New TList

For i%=1 To 10 
	loc:A = New A
	loc.Nr = i
	ListAddLast l, loc
Next

For loc:A =EachIn L
   Print loc.nr
Next

Local locloc:A=New a
For loc:A =EachIn L
	If loc.nr=5
		locloc = loc
		ListRemove L, loc
	EndIf
Next
ListAddLast L, locloc

For loc:A =EachIn L
   Print loc.nr
Next



C. MAPS

You could use TMap instead of TList. Have a look on any TMap Tutorial

Linked Lists

You could use TLink in combination with TList. Have a look on any TLink Tutorial

Last edited 2011


MacSven(Posted 2011) [#3]
Thanx for your Help. (Yes i am a german) but this is a good part to refresh my english) and you have helped my.


Czar Flavius(Posted 2011) [#4]
You don't need to put "card" in the fields. Use x, y, suit instead of cardx, cardy, cardsuit. I just think it will be more readable.

Make the suits constants. Const hearts:String = "Hearts", spades:String = "Spades" etc. and then you can use If cardpos.suit = hearts


Jesse(Posted 2011) [#5]
most often than none card games don't suffer from speed but if speed becomes and issue use intgers for constants instead. Such as:
Const HEARTS:int = 1
Const SPADES:int = 2

kind of like enumerators in c