I get an array index out of bounds.... on line 29 (where it looks at the array given its position) and the value after says: 816, the length of the array.

All of those if statements at the top are to make sure I don't get an array exception (for instance, by it checking the squares above and around 0,0) . pWidth and pHeight are the width and height of pCell (the array of Booleans 816 entries long). What do you see wrong? anything?

Array indices always start at 0. So if you have any array with length of 'n', the indices are from 0 to n - 1. An array with size of 816 means that the last entry is 815. Perhaps you're trying to access a value that is either above 815 or less than 0.

I think your algorithm gets in trouble when you check if x = pWidth and y = pHeight. If your pixel array is of width pWidth and height pHeight, then you probably want to make your leftside check if (x == pWidth-1) and your bottomside check if (y == pHeight-1).

There's a couple more changes that will be needed as a result of this change, but I think you will see them.

The problem is that if you have a columns that has pHeight cells the minimum y would be 0 and the maximum y would be pHeight-1. Same goes for x and pWidth. So if you set y2 to pHeight you set it to the first non-existent row after the last one, hence the AIOOBE.

I think your algorithm gets in trouble when you check if x = pWidth and y = pHeight. If your pixel array is of width pWidth and height pHeight, then you probably want to make your leftside check if (x == pWidth-1) and your bottomside check if (y == pHeight-1).

There's a couple more changes that will be needed as a result of this change, but I think you will see them.

right that makes sense (so how do I check the last one!? I added an if statement so it basically skips the last one, but I really don't want it to do that. So what is this fix that I was supposed to find obvious?)

also... I don't want the check to count the cell(x,y), only the cells AROUND (x,y). So to stop that I added some logic to the check part.

now to me this says that when the cell is true AND (x does not equal the middle cell's x AND y does not equal the middle cell's y) ... so every time except when the co-ordinates are directly ON the middle cell. But instead whenever there is a true cell on the same X as the middle or the same Y it returns false, only when there are true cells in the corners does it work (I made it to the game of life rules (myself) if you must know)

Is that better? I'm not sure. But at least the edge case tests are all grouped together. And the "qualifying tests" are separated from the counting test. The "qualifying" tests could also then be further consolidated.

I get an array index out of bounds.... on line 29 (where it looks at the array given its position) and the value after says: 816, the length of the array.

All of those if statements at the top are to make sure I don't get an array exception (for instance, by it checking the squares above and around 0,0) . pWidth and pHeight are the width and height of pCell (the array of Booleans 816 entries long). What do you see wrong? anything?

that's an idea, I'm sure I would get the last value thing eventually, but since I've never done this before its not obvious. I don't really know how to make eclipse track variables in debug mode, I find them sometimes when the variables are part of the line that gives the error, but it would be really helpful if I could know what all the variables are. Is that possible?I tried this condition loop before and it was even longer, if statements everywhere, this is my shortest one ill try some of what you said.

Is that better? I'm not sure. But at least the edge case tests are all grouped together. And the "qualifying tests" are separated from the counting test. The "qualifying" tests could also then be further consolidated.

Just some ideas...a lot of what determines what code is easiest to read is in the eye of the beholder and the context. The function isWithinP() is also easy to Unit Test.

yeah, organising it did fix the silly AIOOB but it still cant detect when there is a live cell on the same X or Y as the middle, it still only picks up the corners. The logic looks perfect (this is following both mine and yours). If its inside(j, i) AND i does not equal y AND j does not equall X, whats wrong with that!? It skips the line entirely if i = y OR j = x, instead of AND.

You are reminding me of a course I took in college, decades ago, on logic for circuit design. I recall there were interesting ways "not and" could be set up to be the equivalent of "or"; or "not or" used for "and". We had assignments where we had to use just "nand" or just "nor" gate IC's (with negations allowed as well).

The best way to find the correct answer might be to just work out the "truth table" by hand.

With a debugger you should be able to make sense of even the most obtuse code. Stick a break point on the Exception in question, and investigate the state of all the relevant variables at the point the error occurs.

That said, if you can't understand your own code, what chance do others have?

Always endeavor to write code that'll be understandable to others, and yourself 6 months down the line!

well, thank you for the help I guess, I fixed the logic problem by simply saying if the cell I'm checking is true, take away one from the number of surrounding cells. And now, I have my very own Game of life!

With a debugger you should be able to make sense of even the most obtuse code. Stick a break point on the Exception in question, and investigate the state of all the relevant variables at the point the error occurs.

That said, if you can't understand your own code, what chance do others have?

Always endeavor to write code that'll be understandable to others, and yourself 6 months down the line!

Try telling that to the people who make examples in the official oracle documentation. If I could understand what they wrote, I'm sure I'd come to forums less often

With a debugger you should be able to make sense of even the most obtuse code. Stick a break point on the Exception in question, and investigate the state of all the relevant variables at the point the error occurs.

That said, if you can't understand your own code, what chance do others have?

Always endeavor to write code that'll be understandable to others, and yourself 6 months down the line!

if that was true, then people wouldn't use the help forum, as there would be no point. sometimes people make mistakes and can't notice them themselves, so they need us (the community ) to help them. Besides, we're all friends here, and more then willing to help. :3

I agree with you, TrinityGamer, but it is also true that some people will not even try to solve the problem themselves before asking for help, so it is always good to remind people that the power is in their own hands.

well, thank you for the help I guess, I fixed the logic problem by simply saying if the cell I'm checking is true, take away one from the number of surrounding cells. And now, I have my very own Game of life!

With a debugger you should be able to make sense of even the most obtuse code. Stick a break point on the Exception in question, and investigate the state of all the relevant variables at the point the error occurs.

That said, if you can't understand your own code, what chance do others have?

Always endeavor to write code that'll be understandable to others, and yourself 6 months down the line!

Try telling that to the people who make examples in the official oracle documentation. If I could understand what they wrote, I'm sure I'd come to forums less often

Be grateful there are examples. If you start using a library like libGDX you're going to find a lot less and more inconsistent examples, if they exist at all. Oracle has done a pretty good job with their documentation and tutorials, but in the end it's up to you to implement whatever you need. If the Oracle examples confuse you, use Google and find another one, not that complicated.

With a debugger you should be able to make sense of even the most obtuse code. Stick a break point on the Exception in question, and investigate the state of all the relevant variables at the point the error occurs.

That said, if you can't understand your own code, what chance do others have?

Always endeavor to write code that'll be understandable to others, and yourself 6 months down the line!

if that was true, then people wouldn't use the help forum, as there would be no point. sometimes people make mistakes and can't notice them themselves, so they need us (the community ) to help them. Besides, we're all friends here, and more then willing to help. :3

I wasn't meaning to chastise the OP for asking a question he should have been able to answer himself; I was suggestion the course of action I'd take to resolving his problem*.

*Assuming I had the time & motivation to download his code, paste it into Eclipse, run it to the point it crashes, and analyse how what it's doing differs from what it's supposed to be doing.

Be grateful there are examples. If you start using a library like libGDX you're going to find a lot less and more inconsistent examples, if they exist at all. Oracle has done a pretty good job with their documentation and tutorials, but in the end it's up to you to implement whatever you need. If the Oracle examples confuse you, use Google and find another one, not that complicated.

No doubt it IS actually useful in more advanced stages, but for a beginner like me? I could go on youtube tutorials, and that is what I've done up until now, but I don't want to follow a tutorial f you know what I mean.

I have a key listener, listening for: enter, shift and control. The window IS the active window upon startup, but before the keyListener can actually pick up anything I have to click the canvas (inside the borders).

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org