Java help - making a board game

Get help on programming - C++, Java, Delphi, etc.
Post Reply
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Java help - making a board game

Post by Worship »

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
Moses
Registered User
Posts: 2545
Joined: 21 Jul 2004, 02:00
Location: Location:
Contact:

Post by Moses »

:?
sv_roadkill
Registered User
Posts: 5
Joined: 06 Dec 2006, 02:00

Post by sv_roadkill »

Why don't you create a jpeg image of the board and set it as the background of the window. Does anyone know if you can place images over images in java? i.e. for the place marker
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Because then how am i supposed to show a specific video or thing when a token lies on a square?
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

*Sorry about double post* Ah yes i understand now sv, u mean like use an integer to record your position on the board. Ah thats not half bad.
Moses
Registered User
Posts: 2545
Joined: 21 Jul 2004, 02:00
Location: Location:
Contact:

Post by Moses »

Am going out now, but when I get back I can help you out.. I've done a few board games over the years. Just post some more details about the game so long.
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Ok my project is a bit like snakes and ladders except when you land on a square a windows pops up asking you a trivia question and this window will hopefully contain a video, audio or picture that will be the basis of the question. Thanks for the forthcoming help
Moses
Registered User
Posts: 2545
Joined: 21 Jul 2004, 02:00
Location: Location:
Contact:

Post by Moses »

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.
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

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?
Moses
Registered User
Posts: 2545
Joined: 21 Jul 2004, 02:00
Location: Location:
Contact:

Post by Moses »

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.
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Oh i thought you had examples on you but thanks it been a lot of help. Just what IDE do u use?
Moses
Registered User
Posts: 2545
Joined: 21 Jul 2004, 02:00
Location: Location:
Contact:

Post by Moses »

I've got sample code I wrote in Delphi, but that was 7 years ago. I almost never use an IDE, but when I have to I use Eclipse.
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

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. :wink:

<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
Post Reply