Array is not filled

Monkey Forums/Monkey Beginners/Array is not filled

Hamrath(Posted 2015) [#1]
Hi,

I'm trying to write a card game and therefore create an array with the deck.

Here's the code so far:

Function CreateDeck:Int[] ()
	Local theDeck:Int[]

	Local curCard:Int = 0
	
	For Local suit:Int = 1 To suit <= 3
		For Local value:Int = 1 To value <= 13
			theDeck[curCard] = suit * value
			curCard += 1
			Print curCard
		Next
	Next

	cardsUsed = 0
		
	Return theDeck
End

Function Main:Int()
	deck = CreateDeck()
	Print deck[1]
End


Unfortunaly the deck is filled with 0's instead of suit * values, but I don't understand why. In my opinion everything looks fine.

Could you give me a hint?


skid(Posted 2015) [#2]
In monkey

For Local suit:Int = 1 To suit <= 3

is translated to

For Local suit:Int = 1 To 0

as (suit <= 3) -> False and then False->0.

What you want is

For Local suit:Int = 1 To 2

So you will need to fix both your loops,

The calculation suit * value also seems wrong for generating a unique ID, maybe suit*13+value?


Hamrath(Posted 2015) [#3]
Ah, silly me! Thanks! m(

suit * value is correct. suit is 1 to 4 (clubs, spades, hearts, diamond) and value is 1 to 13. With mod and div I can get each card.


skid(Posted 2015) [#4]
How do you tell the difference between value 1 of suit 2 and value 2 of suit 1 when they have the same card id???


Hamrath(Posted 2015) [#5]
I know. That's what I'm working on right now. ;) But I'll get to it. Switching from web development to game development is hard. :P


Gerry Quinn(Posted 2015) [#6]
You never actually created the deck:

deck = New Int[ 53] ' or whatever

You have only 3 suits!

Pro tip: computer stuff is generally simpler if you go 0..3 and 0..12 instead 1..4 and 1..13. But if you are doing the latter, you need to do it right!


Hamrath(Posted 2015) [#7]
Yeah, thanks. :)

To be honest, I do computer stuff since almost 30 years. I probably wasn't in the right mood yesterday.

Did start over and now it works as expected. Thanks again for the help.


Gerry Quinn(Posted 2015) [#8]
Heh, if you did Fortran, it's forgiven. I think arrays in that start with 1 !

But still, C-derivatives dominate now in that sense anyway....