Array elements and making sure only 1 exists?

BlitzMax Forums/BlitzMax Beginners Area/Array elements and making sure only 1 exists?

Amon(Posted 2007) [#1]
Ok Here goes.

This draws the corresponding image to screen dependant on what it stored in the array.

Function DrawFactoryMap()
	For Local yiter:Int = 0 To MapHeight - 1
		For Local xiter:Int = 0 To MapWidth - 1
			If FactoryMap[xiter,yiter] = 0
				DrawImage BlackTile,xiter*32,yiter*32,0
			End If
			If FactoryMap[xiter,yiter] = 1
				For Local r:RedTile = EachIn RedTileList
					DrawImage r.image, xiter*32, yiter*32,0
				Next
			End If
			If FactoryMap[xiter , yiter] = 50
				DrawImage g, xiter*32, yiter*32, 0
				GCount = 1
			EndIf
		Next
	Next
End Function


How do i check if Just one entry in to the array is missing?

For instance, if I loop through the array and do this:

if Map[xiter,yiter] <> blah
print Yay

it will print yay anyway because it loops through the array and finds elements not equal to blah.
I want to check if it's been overwritten and if it has been over written to set a global var to 1


Grey Alien(Posted 2007) [#2]
overwritten with what? If it's an array of integers, there will always be an array slot there with an integer value in that you can test. If it's an array of objects (e.g. TTile) then initally it will be full of null pointers until you create a tile and attach it to each array slot. Then you could check to see if an array slot hasn't got a type attached to it yet because it would have a value of null. e.g. If Map[x,y] = Null then print yay


H&K(Posted 2007) [#3]
Well...,

You could strore what you think each element is supposed to bt pointing to, in another array. But then how would you know if that array had changed.

I dont think youve asked the question you ment to ask.


Grey Alien(Posted 2007) [#4]
yeah I'm a teeny bit confused :-)


SculptureOfSoul(Posted 2007) [#5]
If all you are trying to do is find out if "Blah" isn't in the array (which is what I gathered from reading your post) just loop through the array trying to find blah and return true if you find it, otherwise if the loop terminates, return false


Function TestLoop( blah:int )
for xiter = 0 to mapwidth
 for yiter = 0 to mapheight
   if map[xiter,yiter] = blah
      return true
   endif
 next
next
return false   'wasn't found in the loop so return false
EndFunction


alternately you can return false if it's found and true if it isn't if you want. It doesn't really matter, it just changes the test from being "is blah in the array" (if so return true) to "is blah not in the array" (if so return true)

So the above code is the "is blah in the array" test.


Amon(Posted 2007) [#6]
Yeah, I have code that detects if blah is in the array but not if blah has been removed from the array.

I place a certain tile in my array and I only want for there to be one in the array. If there is one then I don't want anymore being put in. This I can do. But, if blah is removed from the array, looping through the array to test if blah doesn't exist would always return true.

If I looped through the array and checked if xiter,yiter <> blah, it would be true because every element in the array except for one does not have blah in it. Because jst one element doesn't have blah it would throw that blah didn't exist. :/

I hope that makes sense. :)


SculptureOfSoul(Posted 2007) [#7]
Yeah, I have code that detects if blah is in the array but not if blah has been removed from the array.



Your main mistake is in using the "<>" operator in your tests.

See, if you have code that will return TRUE if blah is in the array, you use that same piece of code to test if blah isn't in the array. If the function returns false, then blah isn't in the array.

So just call your "IsBlahInTheArrayFunction" and test its result for false to handle when blah isn't in the array.


Amon(Posted 2007) [#8]
Well, I test if Blah is in the array by drawing an image.

I have this.

for local xiter = 0 to mapwidth
for local yiter = 0 to mapheight
if map[xiter,yiter] = blah
drawimage mytile, xiter*32,yiter*32,0
endif
next
next

I have a tile graphic that is used to place in the map array which will corrspond to the Players start position when the game loads.

I have a temp variable called count that counts how many blahs are in the array. As soon as a user places the player tile in the array 'count' becomes 1 and stops the user from being able to place anymore. Now if they overite that tile with a different tile I want to detect that the player tile (blah) is gone and for count to become 0.

Thanks for your help by the way. It's appreciated :)


SculptureOfSoul(Posted 2007) [#9]
Okay, I see what you are trying to do. What you could do is - whenever a player places a tile, test to see if that tile is of type "blah" and if it is, decrease Count to zero.

Now, whenever they want to place a start tile you can just check Count and see if it is zero or not. I'd still let them attempt to place a start tile if one already exists though. Why? Well, it would prevent them from having to find (and delete) the old tile first. I'd run a test whenever they attempt to lay a start tile, and if count = 1 then you could pop up a menu box that says something like "A start point already exists at (X:32, Y:57). Would you like to replace that with the tile you are attempting to place?" and if they select yes, then you let them place it and delete the old one, and if not do nothing.


SculptureOfSoul(Posted 2007) [#10]
Eek, I wasn't real clear with that. What I meant with my first sentence is, whenever a player places a tile, check the array at that spot and see if the tile already in the array at that spot is of type "blah" and if so, then decrease count to zero.

So lets say you have a 50x50 array, and the user already placed a start tile at 25,25.

Now, they select a "grass tile" and place it at 5,10. So your game checks the array at [5,10] and sees that it is not a start tile, and so it doesn't touch count. Now, if the user tries to place that grass tile at [25,25] you check the map array and see that that spot currently IS the start tile, and so you decrease your count to zero.


Grey Alien(Posted 2007) [#11]
Yeah or just had a completely separate flag called STartTilePlaced and don't even bother searching the array.