[TI ASM] Problem with shuffling

Got questions? Got answers? Go here for both.

Moderator: MaxCoderz Staff

User avatar
kv83
Maxcoderz Staff
Posts: 2735
Joined: Wed 15 Dec, 2004 7:26 pm
Location: The Hague, Netherlands
Contact:

[TI ASM] Problem with shuffling

Post by kv83 »

I was trying to make the routine we spoke of in the General Algorithme thread. Now, when compiling, I get a strange error. Sometimes it crashes, other times it shuffle the cards, but suddenly two of the same cards appear.

Anyone a idea? Here is the code:

Code: Select all

;==========================================================
; Shuffle Routine
; ---------------
; Shuffles hl. Use 255 as end marker.
;==========================================================
lblShuffle:
	ld hl,CardsPlayer
lblShuffleLoop:	
	;--   Copy current card position to de   --
	push hl
	pop de
	;------------------------------------------
	;--Search random position to shuffle with--
	ld b,5
	call ionrandom
	ld b,$00
	ld c,a
	add hl,bc
	;------------------------------------------	
	;--   Shuffle values of both positions   --
	ld b,(hl)
	ld a,(de)
	ld c,a
	ld a,b
	ld (de),a
	ld (hl),c
	;------------------------------------------
	;--         Go to next position          --	
	push de
	pop hl
	inc hl
	ld a,(hl)
	cp 255
	ret z
	jr lblShuffleLoop
Thanks in advance
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Hhhmmm... strange, you could probably get rid of that last push/pop with an 'ex de,hl' but either way, thats not the problem. Are you sure its this particular routines fault?

I like how nice you code is layed out though! :).
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
Duck
Sir Posts-A-Lot
Posts: 231
Joined: Sat 18 Dec, 2004 3:38 am

Post by Duck »

tr1p1ea wrote:Hhhmmm... strange, you could probably get rid of that last push/pop with an 'ex de,hl'
Nope, that will echange the values, whereas you want to keep the value in hl.
But this is faster then push/pop:

Code: Select all

   ld d,h
   ld e,l
And, (this is causing the failure I think), remove the push/pop in the end:

Code: Select all

   push de      ;<
   pop hl      ;< should be removed
because HL is the position in the list you want to keep, and DE is just temporary.
coelurus
Calc Wizard
Posts: 585
Joined: Sun 19 Dec, 2004 9:02 pm
Location: Sweden
Contact:

Post by coelurus »

Let's see if I remember those things... :)

Are you certain this is where things crash? How do you generate cards?

Other than that, it looks fine. The "push de \ pop hl" lines should be ok, because it seems that you use "hl" to both step through all cards one by one and to calculate a new random location. "de" is a temp container alright and must be put into hl in the end of the loop, if I'm not totally off track :p
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

255 gets shuffled with the other cards, have your first byte be the length and loop that many times. :)
also ex de,hl at the end would be faster and smaller, but then agian your probably just going for a working version first. Personally I don't like Ionrandom it's alittle slow for my taste.

And almost foregot to mention that you are also overwriting things after the end mark. My suggestion is to have de move through each card and have hl contain a random offset within the cards length. then just add hl,de and swap the contents.
CoBB
MCF Legend
Posts: 1601
Joined: Mon 20 Dec, 2004 8:45 am
Location: Budapest, Absurdistan
Contact:

Post by CoBB »

Exchanging (hl) and (de) can be optimised a bit, too:

Code: Select all

   ld b,(hl) 
   ld a,(de)
   ex de,hl 
   ld (hl),b 
   ld (de),a
This also takes care of loading DE into HL at the same time.
Duck
Sir Posts-A-Lot
Posts: 231
Joined: Sat 18 Dec, 2004 3:38 am

Post by Duck »

Well i also thought of that, but I forgot that DE would be the original value of HL so that's the one you'll want to return to HL anyway.
User avatar
kv83
Maxcoderz Staff
Posts: 2735
Joined: Wed 15 Dec, 2004 7:26 pm
Location: The Hague, Netherlands
Contact:

Post by kv83 »

Hmmm... I am not overwriting anything, and don't shuffle with the end mark, why? Because of this:

Code: Select all

ld b,5
There are not more than 5 cards in the array. (Though you were particular right, cause I needed an additional 'dec a' after the ionrandom). Speed isn't really an issue in a card game, if you ask me btw.

It's still not working, and the reason that I think that this routine is giving the problems is because if I don't use it (unshuffled cards) it just works fine...

The cards are not really generated, just loaded into an array located in saferam1.

Thanks for the optimization btw, but it's still broken :cry:
Image
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Only 5 cards? Why not just use djnz? As i think its still possible for it to shuffle the marker because if you are at the 5th card and ionRandom gives you 1 then it will shuffle with the marker. Then when it increases the marker wont be there anymore and it will also keep going until it finds a 255 in memory ... which could cause crashes. Or did you say this wasnt possible?
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
kv83
Maxcoderz Staff
Posts: 2735
Joined: Wed 15 Dec, 2004 7:26 pm
Location: The Hague, Netherlands
Contact:

Post by kv83 »

oh darn!!!!! you are right, how could I be so stupid... it should be 5+the hl value at the beginning, not the one I increase every time! tr1p, you are a genius... thanks to everyone else also btw :roll:

Edit: It works now :D
Image
Kozak
Maxcoderz Staff
Posts: 791
Joined: Fri 17 Dec, 2004 5:33 pm
Location: On the dark side of the moon.
Contact:

Post by Kozak »

kv83 wrote:tr1p, you are a genius...
|| + && you are a du.....
"They say that sea was created by a man named Maarten Zwartbol, a long time ago...." - Duck, an old Corbin version
User avatar
tr1p1ea
Maxcoderz Staff
Posts: 4141
Joined: Thu 16 Dec, 2004 10:06 pm
Location: I cant seem to get out of this cryogenic chamber!
Contact:

Post by tr1p1ea »

Im pretty sure Jim e is the one who picked it. Glad to see its working :).
"My world is Black & White. But if I blink fast enough, I see it in Grayscale."
Image
Image
User avatar
kv83
Maxcoderz Staff
Posts: 2735
Joined: Wed 15 Dec, 2004 7:26 pm
Location: The Hague, Netherlands
Contact:

Post by kv83 »

tr1p1ea wrote:Im pretty sure Jim e is the one who picked it. Glad to see its working :).
I know, I know... :roll: many thanks to him aswell, but you are the one who triggered it by me :P
Image
CalcKing
Regular Member
Posts: 147
Joined: Sat 18 Dec, 2004 3:24 am
Contact:

Post by CalcKing »

If you want to decrease the size even more, you can replace

Code: Select all

   cp 255
   ret z 
with

Code: Select all

   inc a
   ret z
Optimizing code is fun... :D
User avatar
Jim e
Calc King
Posts: 2457
Joined: Sun 26 Dec, 2004 5:27 am
Location: SXIOPO = Infinite lives for both players
Contact:

Post by Jim e »

I know, I know... many thanks to him aswell, but you are the one who triggered it by me
Yeah, I guess I need to work on my explaining skills, I didn't even site code, just told you what to try. Well the point is it works now, But I'm curious, what kind of Card game(If a game at all) only has 5 cards.

Just as a side note, I honestly have to say debugging and optimisation is my favorite part of coding, not kidding, anyone else with me on this or am I just too nerdy.
Post Reply