This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mackan90096's profile picture

[Error] [Java] paintutils:3: vm error: java.lang.ArrayIndexOutOfBoundsException: 256

Started by Mackan90096, 07 May 2013 - 04:31 AM
Mackan90096 #1
Posted 07 May 2013 - 06:31 AM
Hi! I'm trying to make a game.
I got this error:

paintutils:3: vm error:
java.lang.ArrayIndexOutOfBoundsException: 256

My lua code:

Spoiler


-- Variables --
debug = "Debug"
w, h = term.getSize()

-- Values --

limeName = "Lime gem: "
limeSell = 10
lblueName = "Light blue gem: "
lblueSell = 15
yellowName = "Yellow gem: "
yellowSell = 50
orangeName = "Orange gem: "
orangeSell = 100

-- Stats --
money = 0
playerName = "nil"
limeGems = 0
lblueGems = 0
yellowGems = 0
orangeGems = 0
-- End Stats --

-- End Variables --
-- Game --

function playGame()
-- Variables --
slc = 1
minX = 13
maxX = w-1
minY = 2
maxY = h-1
maxGem = 20
spawnedGems = 0
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
local function frame()
term.setBackgroundColor(colors.black)
term.clear()
paintutils.drawLine(12, 1, 12, h, colors.red)
paintutils.drawLine(12, 1, w, 1, colors.red)
paintutils.drawLine(12, h, w, h, colors.red)
paintutils.drawLine(w, 1, w, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(1, 1, 1, h, colors.red)
paintutils.drawLine(2, 1, 2, h, colors.red)
paintutils.drawLine(3, 1, 3, h, colors.red)
paintutils.drawLine(4, 1, 4, h, colors.red)
paintutils.drawLine(5, 1, 5, h, colors.red)
paintutils.drawLine(6, 1, 6, h, colors.red)
paintutils.drawLine(7, 1, 7, h, colors.red)
paintutils.drawLine(8, 1, 8, h, colors.red)
paintutils.drawLine(9, 1, 9, h, colors.red)
paintutils.drawLine(10, 1, 10, h, colors.red)
paintutils.drawLine(11, 1, 11, h, colors.red)
term.setBackgroundColor(colors.lime)
term.setTextColor(colors.black)
term.setCursorPos(5, h-4)
print(" Exit ")
end
function spawnGem()
if spawnedGems >= maxGem then
	playGame()
else
	 paintutils.drawPixel(newX, newY, color)
	 spawnedGems = spawnedGems + 1
	 gem()
end
end




function gem()
while true do
newX = math.random(minX, maxX)
newY = math.random(minY, maxY)
colorNum = math.random(1,4)
	if colorNum == 1 then
	color = colors.orange
	spawnGem()
elseif colorNum == 2 then
	color = colors.yellow
	spawnGem()
elseif colorNum == 3 then
	color = colors.lime
	spawnGem()
elseif colorNum == 4 then
	color = colors.lightBlue
	spawnGem()
end
end
end
frame()
gem()
end




function frame2()
slc = 0
term.setBackgroundColor(colors.white)
term.clear()
term.setCursorPos(math.floor(w-string.len("Logged in as: "..playerName))/2, 2)
print("Logged in as: "..playerName )
paintutils.drawPixel(1, 4, 32)
paintutils.drawPixel(1, 6, 8)
paintutils.drawPixel(1, 8, 16)
paintutils.drawPixel(1, 10, 2)
term.setBackgroundColor(1)
term.setCursorPos(3, 4)
print(limeName ..limeGems)
term.setCursorPos(3, 6)
print(lblueName ..lblueGems)
term.setCursorPos(3, 8)
print(yellowName ..yellowGems)
term.setCursorPos(3, 10)
print(orangeName ..orangeGems)
term.setBackgroundColor(32)
term.setCursorPos(10, 15)
print("Play")
term.setCursorPos(20, 15)
print("Sell Gems")
end


function game()
term.clear()
term.setBackgroundColor(1)
term.setTextColor(32768)
term.clear()
term.setCursorPos(math.floor(w-string.len("Name: "))/2, 2)
write("Name: ")
playerName = read()
if playerName == "" then
game()
else
frame2()
end
end



-- End Functions --

game()

-- User Interaction --

while true do
		event, key, x, y = os.pullEvent()
		if event == "mouse_click" then
		if slc == 0 then
		if  x >= 10 and x <= 18 and y == 15 and key == 1 then
		 playGame()
		elseif x >= 20 and x <= 30 and y == 15 and key == 1 then
		 sellGems()
		elseif slc == 1 then
			if x >= 5 and x <= 10 and y == h-4 and key == 1 then
				slc = 0
				frame2()
end
end
end
end			
end

Oh, I'm also using CC-emu

Whats wrong?
digpoe #2
Posted 08 May 2013 - 09:53 AM
What's CC-emu? I haven't heard of it, and it would be helpful for me to know so I can help you :)/>
theoriginalbit #3
Posted 08 May 2013 - 10:06 AM
I would say, at a quick glance, your problem is that you seem to be using recursion. Paintutils is not at fault.

So basically each time you call a function from within itself like so

local function a()
  -- do stuff
  a()
end
a()
the program's stack will eventually fill up until it cannot call anything anymore. you can also have cyclic recursion like so

local function a()
  -- do stuff
  b()
end
local function b()
  -- do stuff
  a()
end
b()
there is very few times is programming where we actually need to use recursion. Most times that people use it, it can be simply replaced with a loop. So this is your solution, make it use loops. example

local function a()
  -- do stuff
end
-- infinitely loop
while true do
  a()
end
or


local function a()
  -- do stuff
end
local function b()
  -- do stuff
  a()
end
while true do
  b()
end

Now there are other ways to solve this problem without using loops, but as they are a more 'advanced' topic I wont be detailing them, and as such I just suggest you modify your logical flow so that your program is using loops instead of recursion.

What's CC-emu? I haven't heard of it, and it would be helpful for me to know so I can help you :)/>
A recently abandoned project EDIT: idk how you have been around since May 2012 and haven't heard of it!
Edited on 08 May 2013 - 08:07 AM
digpoe #4
Posted 08 May 2013 - 10:08 AM
What's CC-emu? I haven't heard of it, and it would be helpful for me to know so I can help you :)/>
A recently abandoned project EDIT: idk how you have been around since May 2012 and haven't heard of it!

Ah, thanks. I joined but then didn't use the account that I created until a while ago xP