Java help - making a board game
Java help - making a board game
Hey
Ok for my project i need to make a board game. if anyone has code for the design of a board even if its just the making of the squares and allowing a "token" or beacon to move like snakes and ladders. Please help. Thanks
Ok for my project i need to make a board game. if anyone has code for the design of a board even if its just the making of the squares and allowing a "token" or beacon to move like snakes and ladders. Please help. Thanks
-
- Registered User
- Posts: 5
- Joined: 06 Dec 2006, 02:00
Okay, firstly I assume you're going to have a square board, let's just say 10x10 for now, but any size will do.
You will need a token_variable in which you store the token position (one for each token). This will simply be an integer between 0 and 99 (you could use a byte).
Now to get the actual board position, you would take your token_variable, and first divide it by 10 (discarding the remainder), this will give you your 'y' position on the board (were 0 is the first row), then take your token_variable and mod it by 10, this will give you your 'x' position (where 0 is the first column).
Eg:
token_variable = 15 -> 15/10 = 1, and 15%10 = 5, -> board position = [1,5]
token_variable = 23 -> 23/10 = 2, and 23%10 = 3, -> board_position = [2,3]
Note that this arrangements has the first position as [0,0].
Using this arrangement allows you to add and subtract arbitrary numbers to the position, and easily calculate the new position on the grid. (you can also mod the result by 100 if you need wrap-around).
Now for the visual representation, I would use a static image for the grid, and then use another image (or just draw a circle or something) for the token. then to get the top_x and top_y coordinates for placing the token_image, you could take the [x,y] calculated from the token_variable and multiply each by the width/height (in pixels) of each square. This obviously wouldn't be that simple, but I can't give you more specifics without seeing the exact arrangement. For example you may have a border around each square, and also the coordinate system for the image in java would be different eg, [0,0] top left, where as your board might be [0,0] bottom left. But by applying some basic algebra, you could figure this out.
Using this simple structure would allow you to move tokens around easily and in an arbitrary manner, which would be a good start for now.
Please note: I'm not particularly articulate, so if the above looks like one big mess to you, or is not what you're looking for, just let me know.
Alternatively, if you've made a start you can post the code here, and I ca help where you are stuck.
You will need a token_variable in which you store the token position (one for each token). This will simply be an integer between 0 and 99 (you could use a byte).
Now to get the actual board position, you would take your token_variable, and first divide it by 10 (discarding the remainder), this will give you your 'y' position on the board (were 0 is the first row), then take your token_variable and mod it by 10, this will give you your 'x' position (where 0 is the first column).
Eg:
token_variable = 15 -> 15/10 = 1, and 15%10 = 5, -> board position = [1,5]
token_variable = 23 -> 23/10 = 2, and 23%10 = 3, -> board_position = [2,3]
Note that this arrangements has the first position as [0,0].
Using this arrangement allows you to add and subtract arbitrary numbers to the position, and easily calculate the new position on the grid. (you can also mod the result by 100 if you need wrap-around).
Now for the visual representation, I would use a static image for the grid, and then use another image (or just draw a circle or something) for the token. then to get the top_x and top_y coordinates for placing the token_image, you could take the [x,y] calculated from the token_variable and multiply each by the width/height (in pixels) of each square. This obviously wouldn't be that simple, but I can't give you more specifics without seeing the exact arrangement. For example you may have a border around each square, and also the coordinate system for the image in java would be different eg, [0,0] top left, where as your board might be [0,0] bottom left. But by applying some basic algebra, you could figure this out.
Using this simple structure would allow you to move tokens around easily and in an arbitrary manner, which would be a good start for now.
Please note: I'm not particularly articulate, so if the above looks like one big mess to you, or is not what you're looking for, just let me know.
Alternatively, if you've made a start you can post the code here, and I ca help where you are stuck.
Ok i understand how you get your y and x values of where the token should be layed out but I dont get how you show your token in that position. i.e. Like is it an image over the image of the background image of the board game? Then would your token just be a picture file? (Ok it is but how would you do it just using setPosition?)
Do you have any examples like this on hand that you could perhaps e-mail me?
Do you have any examples like this on hand that you could perhaps e-mail me?
You could use a setPosition method on an image over the background image.
I have never actually done anything remotely graphic in netbeans so I'm not sure of the specifics, but I'm sure such generic functionality exists.
If I was on holiday, I'd gladly make an example, but unfortunately I'm very busy at the moment.
I have never actually done anything remotely graphic in netbeans so I'm not sure of the specifics, but I'm sure such generic functionality exists.
If I was on holiday, I'd gladly make an example, but unfortunately I'm very busy at the moment.
- rustypup
- Registered User
- Posts: 8872
- Joined: 13 Dec 2004, 02:00
- Location: nullus pixius demonica
- Contact:
check out the Canvas, Image and AlphaComposite classes...
Canvas supports double buffered rendering and is relatively lightweight enough to use in these instances; it does, however, require a certain level of know-how in the 2d rendering classes....
check out the J2D samples page...
spending some time thinking about the functional model before worrying about the gui would be a good idea... it doesn't help having wonderfully rendered graphics if the functional code is brittle.
<edit>
while we're at it, you also need to become familiar with Thread, Runnable and, more importantly, Timer and TimerTask... you'll need these to manage the various paint tasks...
</edit>
Canvas supports double buffered rendering and is relatively lightweight enough to use in these instances; it does, however, require a certain level of know-how in the 2d rendering classes....
check out the J2D samples page...
spending some time thinking about the functional model before worrying about the gui would be a good idea... it doesn't help having wonderfully rendered graphics if the functional code is brittle.
<edit>
while we're at it, you also need to become familiar with Thread, Runnable and, more importantly, Timer and TimerTask... you'll need these to manage the various paint tasks...
</edit>
Most people would sooner die than think; in fact, they do so - Bertrand Russel