memory access violation

Blitz3D Forums/Blitz3D Programming/memory access violation

Taking advice I changed my game pieces into sprites... The game works much faster (but still slow) and after about 12 - 17 moves I get a MEMORY ACCESS VIOLATION on renderworld.

I think it is because I am not cleaning the sprites off the screen correctly. Maybe.

; PLONK a simple 3 in a row game of color
; Ralph Dunn
Graphics3D 800,600,16,2
SetBuffer BackBuffer()

SeedRnd MilliSecs()
player=CreatePivot()
camera=CreateCamera(player)
light=CreateLight()
RotateEntity light,45,45,0
AmbientLight 48,48,48    ; was 32,32,32

PositionEntity camera,0,83,-29 ; 0,53,-69
RotateEntity camera,75,0,0 ; was 35

Dim board(9,9)
Dim plunk(1)
Dim boardx(9)
Dim boardz(9)

boardx(0)=-45
boardx(1)=-35
boardx(2)=-25
boardx(3)=-15
boardx(4)=-5
boardx(5)=5
boardx(6)=15
boardx(7)=25
boardx(8)=35
boardx(9)=45

boardz(0)=45
boardz(1)=35
boardz(2)=25
boardz(3)=15
boardz(4)=5
boardz(5)=-5
boardz(6)=-15
boardz(7)=-25
boardz(8)=-35
boardz(9)=-45

Global xx
Global zz
Global row
Global col
Global punk
Global g$
Global gong
Global score
; #######################################  Load Mesh Objects
gameboard=LoadMesh("board.b3d")
UpdateNormals gameboard
ScaleEntity gameboard,5.5,5.5,5.5
EntityShininess gameboard,1

Global empty=LoadMesh("square.b3d")
EntityPickMode empty,2
HideEntity empty

Global zap=LoadSound("zap0.wav")
Global kazap=LoadSound("kazap.wav")
Global happy=LoadSound("happy.wav")

;Global bomb=LoadMesh("bomb.b3d")
;EntityShininess bomb,1
;HideEntity bomb

Global piece1=LoadSprite("tile1.png",2)
ScaleSprite piece1,4,4
Global piece2=LoadSprite("tile2.png",2)
ScaleSprite piece2,4,4
Global piece3=LoadSprite("tile3.png",4)
ScaleSprite piece3,4,4
Global piece4=LoadSprite("tile4.png",4)
ScaleSprite piece4,4,4
Global piece5=LoadSprite("tile5.png",4)
ScaleSprite piece5,4,4
Global piece6=LoadSprite("tile6.png",4)
ScaleSprite piece6,4,4
Global piece7=LoadSprite("tile7.png",4)
ScaleSprite piece7,4,4
Global piece8=LoadSprite("tile8.png",4)
ScaleSprite piece8,4,4
Global piece9=LoadSprite("tile9.png",4)
ScaleSprite piece9,4,4
Global piece10=LoadSprite("tile10.png",4)
ScaleSprite piece10,4,4

; ######################################  Plunk Tiles
For xx=0 To 9
	For zz=0 To 9
		e=CopyEntity (empty)
		PositionEntity e,boardx(xx),1,boardz(zz) 
	Next
Next

; ############################################## Start of Game Loop
ShowPointer 

setup_row()
check_three()
check_zero()
put_row()

While Not KeyDown(1)
Cls
x=MouseX()
y=MouseY()

e=CameraPick(camera,x,y)
	If e<>entity
		If entity Then EntityColor entity,255,255,255 
		entity=e
	EndIf
	
If KeyHit(50)>=1 Then musicflag=1-musicflag

If MouseHit(1) = True Then
	hat=CameraPick(camera,x,y)
	If hat=0 Then Goto outme
		Select empty 
			Case empty
			xx=EntityX(entity)
			zz=EntityZ(entity) 
			Default 
			pp=0
			
		End Select
		
.outme
get_rowcol()
check_three()
EndIf

If entity
	EntityColor entity,54,80,252
; 73,253,146 lime 254,216,38 gold 254,80,232 magenta 54,80,252
EndIf



check_zero()
put_row()

UpdateWorld

RenderWorld

Text 0,10,"X: "+xx
Text 0,20,"Z: "+zz
Text 0,30,"ROW: "+row
Text 0,40,"COL: "+col

Text 0,60,""+g$
Text 0,80,"SCORE: "+score

Flip

Wend

End

Function setup_row()

For t=0 To 9
	For tt=0 To 9
		w=Rand(9)+1
	board(t,tt)=w
	Next
Next

End Function

Function put_row()
Cls
; this is going to be BIG
For row = 0 To 9
	For col=0 To 9
		q=board(row,col)
	Select q
	Case 1
		red=CopyEntity (piece1)
		PositionEntity red,boardx(row),4,boardz(col)
	Case 2
		blue=CopyEntity (piece2)
		PositionEntity blue,boardx(row),4,boardz(col)
	Case 3
		violet=CopyEntity (piece3)
		PositionEntity violet,boardx(row),4,boardz(col)
	Case 4
		yellow=CopyEntity (piece4)
		PositionEntity yellow,boardx(row),4,boardz(col)
	Case 5
		redviolet=CopyEntity (piece5)
		PositionEntity redviolet,boardx(row),4,boardz(col)
	Case 6
		orange=CopyEntity (piece6)
		PositionEntity orange,boardx(row),4,boardz(col)
	Case 7
		bluviolet=CopyEntity (piece7)
		PositionEntity bluviolet,boardx(row),4,boardz(col)
	Case 8
		green=CopyEntity (piece8)
		PositionEntity green,boardx(row),4,boardz(col)
	Case 9
		redorange=CopyEntity (piece9)
		PositionEntity redorange,boardx(row),4,boardz(col)
	Case 10
		yellogreen=CopyEntity (piece10)
		PositionEntity yellogreen,boardx(row),4,boardz(col)
	End Select
Next

Next

End Function

Function get_rowcol()

For r=0 To 9
	For c=0 To 9
		g=zz
			gg=xx
				h=boardz(c)
				i=boardx(r)
		If h=g Then col=c
		If i=gg Then row=r
	Next
Next
	
board(row,col)=gong

End Function

Function check_three()
; Left and RIGHT
For flirt=0 To 9
	For fart=0 To 7
		a=board(flirt,fart)
		b=board(flirt,fart+1)
		c=board(flirt,fart+2)
			If a = b Then
				If b = c Then
				board(flirt,fart)=0
				board(flirt,fart+1)=0
				board(flirt,fart+2)=0
				; maybe some points and a sound
				score = score+15
				PlaySound kazap
				EndIf			
			EndIf
	Next		
Next
; up and down
For flirt=0 To 7
	For fart=0 To 9
		a=board(flirt,fart)
		b=board(flirt+1,fart)
		c=board(flirt+2,fart)
			If a = b Then
				If b = c Then
				board(flirt,fart)=0
				board(flirt+1,fart)=0
				board(flirt+1,fart)=0
				; maybe some points and a sound
				score = score+25
				PlaySound happy
				EndIf	
			EndIf
	Next		
Next
; make new color
gong=Rand(9)+1
	Select gong
	Case 1
	g$="white"
	Case 2
	g$="lite-blu"
	Case 3
	g$="black"
	Case 4
	g$="lite-green"
	Case 5
	g$="blue"
	Case 6
	g$="red"
	Case 7
	g$="rainbow"
	Case 8
	g$="brown"
	Case 9
	g$="purple"
	Case 10
	g$="pink"
	End Select

End Function

Function check_zero()
ClearWorld(entities)
For gaggle = 0 To 9
	For giggle=0 To 9
		a=board(gaggle,giggle)
			If a = 0 Then
				kolor=Rand(9)+1
				board(gaggle,giggle)=kolor
			EndIf
	Next
Next
End Function
What happens is the game tells you what color piece you get to put in a square. You put it. If you make 3 in a row L-R or U-D then you get points.

That is all I am trying to do for now. I have to understand what I did wrong before I get too fancy.

HELP!!! :]
-RZ

The parameter in the Rand() comand is the highest possible value the Rand function will return. You add 1 to Rand(9) giving you numbers in the range of 2 - 10 and you are using the result to access an array that has been dimentioned with elements 0 - 9. Replace the 9 with 8 in your Rand statements should fix your problem.

Oops, actually you should replace Rand(9)+1 with Rand(0,9) By replacing 8 with 9 like in my example will give you numbers in the range of 2-9 (unless of course that's what you want). Using Rand(0,9) will give you a number from 0 to 9.