Java Turn Based Game mechanics

danhodge

In Runtime
Messages
324
Location
UK
Okay, so I am still getting in to Java, but I am at the stage where I am sick of using IF statements for everything, and I need another way of doing something (because I can't really do it with IF).

What I want to do, is have a map with squares (already done that bit) that are each 50 pixels, and when someone clicks on one of the squares, the character gets taken to that square.

I did manage this with making an IF statement for every single square, and have 'SquareX1' and 'SquareY1' as the variables that made up each square. But I want big maps (I am planning on a large scale TBS), and I don't want any future coding to have to be added to the 100+ IF statements.

So, my question is: Is there a way to make this work, without the use of IF statements? Like, something which would know if you clicked the square at 350px by 450px, and move the character there, without it having a variable preassigned to that specific square?

Thanks a lot, and I have been rewatching all the tutorials I could to find a way, and all I could think of was using a While Loop, or an Array - both of which would only make it take a bit less time.
Danny
 
off the top of my head, i think i would try dynamically building a multi dimensional array for each board. then, use the mouseclick to determine which square was clicked. then pass in the x, y, and character to save.

the problem with that is there's alot of unused space in the array.

so it may be better to rebuild the screen on each click. have a list of selected squares and their character value.


and that makes me think it maybe better to setup a class design where you create new square objects with a value attribute and onclick event. this is prolly best but i've never done it before.
 
off the top of my head, i think i would try dynamically building a multi dimensional array for each board. then, use the mouseclick to determine which square was clicked. then pass in the x, y, and character to save.

the problem with that is there's alot of unused space in the array.

so it may be better to rebuild the screen on each click. have a list of selected squares and their character value.


and that makes me think it maybe better to setup a class design where you create new square objects with a value attribute and onclick event. this is prolly best but i've never done it before.

Instead of an array, what about a list since it's Java?
 
I haven't heard of lists before. I'm looking into them now.

All I have is TheNewBoston videos - I really need a textbook or something for Java.
 
Back
Top Bottom