Examples of 2d array in games

I have been looking (and without any luck so far) of examples of java games that use 2d arrays.Does anyone know where or can get source code for java games so that I can see how it all works together.
[214 byte] By [melkiora] at [2007-10-2 17:48:18]
# 1
Do a google and lookup almost any chess or checkers game.
morgalra at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 2
I have found a good website for java games ...... http://www.codebeach.com/index.asp?tabID=1&categoryID=4&subcategoryID=11&Page=4it allows you to play the games and then look at the source code :)
melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 3

This is what I have come up with for the 2d array........ im not sure if it will work as I have not finished yet. But what do you think to this!?!?

Source[][] grid = new Source[20][20];

public void makeGrid(Source[][] theGrid)

{

for(int i = 0; i < 20; i++)

{

for(int j = 0; j < 20; j++)

{

grid[i][j] = theGrid[i][j];

}

}

}

Also I would appreciate a little help if it is possible? im a bit lost with this project and I would appreciate it if someone would look at all my code and give me adivce (im looking for guidence here not "do my homework") :)

But a little help in which direction I should go in etc

melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 4

> > Source[][] grid = new Source[20][20];

>

> public void makeGrid(Source[][] theGrid)

> {

>for(int i = 0; i < 20; i++)

>{

>for(int j = 0; j < 20; j++)

>{

>grid[i][j] = theGrid[i][j];

>}

>}

> }

>

In my way of thinking this would be more of a copyGrid type of method, but maybe I'm just nit-picking, since your grid has already been created and you require an already existing grid to be sent as an argument to load your local grid from.

morgalra at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 5
No no! nit picking is good........I do want my code to work afterall :PWould it be possible for me to email you my project so far and tell me what you think?
melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 6

How large is it?

Could you post it here, so I can look at it during lunch? (We are not allowed to use private e-mail accounts at work--at least not at this client).

Essentially after putting in 11+ hours a day coding (I'm in a coding phase of development right now), I'm not real eager to even look at the computer in my off time.

morgalra at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 7
if I were to zip it up it would be 13.5kbI don't really want to post all the code here as people will be able to copy it.But if you are too busy and don't want to look at it........thats ok no problem
melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 8

If you want to look at a full scale game google my project "Legends of Galloway". It is over 2 megs all done in java. It should be about 10 thousands line long.

It has a proper RPG engine, also done in java by myself, that you can use freely. I call it an RPG engine but you could really make anything with it. I've seen someone make a platform game from it.

But I was just wondering why you really need a 2D array for your game. It seams like an odd requirment. It might be wiser to encapsulate all of your 2d array into another class to make your code more modular. It tends to make life easier so you don't have to be passing around everywhere.

BTW, you should really think about making your source GPL, or open in some other way. Mine is basically BSD so you can take it and sell it for all I care. But it helps the comunity to share your source.

Good luck.

Phibreda at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 9

I will make my code open source! but not yet........the college where I go to, they see that putting up your code or letting anyone else see it will be seen as plagarism.......so do not really want to put up ALL my code and let a possible other pupil pf the college look at my code and submit it as their own work.

melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 10

ok lets break down the problems one by one!! I need help connecting my 2d array to my panel.

This game will eventualy have blocks moving across the screen....

something along the lines of the type of games like the ones where you have to avoid the on coming rocks in a space shoot 'em up.

here is the code to make the 2d array (we were told that the best option was to use a 2d array, unless anyone knows of a better way to have the blocks to move around)

public class Grid {

private int [][] grid = new int[20][20]; // 20 is just a place holder

// will change later(not sure what to yet)

public void makeGrid(int[][] theGrid)

{

for(int i = 0; i < 20; i++)

{

for(int j= 0; j < 20; j++)

{

grid[i][j] = theGrid[i][j];

}

}

}

}

And here is the code that makes the panel

public class GamePanel extends JPanel{

private Grid gridArea;

Shape square = new Shape(); // this is the class which makes a square

//that will become the player and the//objects coming towards the player

GamePanel()

{

this.setBackground(Color.GREEN);

this.setForeground(Color.BLACK);

this.setPreferredSize(new Dimension(600,400)); // maybe change later

}

/*

* Makes a new grid

*/

public void loadGrid()

{

int [][] grid = new int[20][20];

gridArea = new Grid();

gridArea.makeGrid(grid);

}

}

Thats all I have for that part so far. I have done the panels, buttons, a few listeners ect etc but how do I connect that array to the panel?

melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 11
Ok... yea, if you want, I can beta test this code, since I allready have a almost complete RPG game, and I need a code like this for my game. I can also mention you in my code if you let me test it.
Dark_Rulera at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 12
Thanks! but there isn't much for you to test at the moment :(But could you pop your source code here so I can see how you made it all work?
melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 13

Well, I'm trying to create a graphical interface for my game for school. Now, I need to know how to impliment it so it would work for my program, if even possible. When I try to compile the code you have, It says "Superclass JPanel of class GamePanel not found"... it might be something you have imported that I don't have... How do you get JPanel to work? or, is there another way of creating the grid panel without using the JPanel superclass?

Dark_Rulera at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 14

Well Dark_ruler I don't really want to put my code here as you and I might be doing the same project and if we both have the same code then we will both be in trouble :(

But........ I have found one way (not the way I want it to be) was to make a 2D array of JLabel and add it into the constructor of my panel, there are a few examples around here for making a 2D array, so give the search thingy a go and play around with the JLabel........... what I really want to do is to make a Rectangle(square) object and make a 2D array from that!! but it doesn't seem to work

public Shape(int x, int y, int width, int height)

{

x = 1;

y = 1;

width = 1;

height = 1; // 1 is used as an example!

}

This is wrong somehow, but from the api it seemed like the best option, but I can't get it to work.

Good luck with it and keep me informed on how you get on with it =D

Cheers Melior

melkiora at 2007-7-13 19:06:18 > top of Java-index,Other Topics,Java Game Development...
# 15

Ok, yea, us doing the same project is highly unlikely, since i'm in high school. i'm doing this to basically to teach myself java. I found some things on 2d arrays and I'm writing that code now, it's just I dont know how to get the squares to show up properly. I'm getting rid of the JPanel superclass because thats just WAY to complicated for me... What I hope for is that I could have a 10 by 10 grid with a red square as the main person and blue suqares for monsters (which are not programed yet). Could I do a GridLayout and have the buttons change colors? I dont know, I'll tinker around with the code I have to see if I cant get that to work...

Dark_Rulera at 2007-7-20 23:22:43 > top of Java-index,Other Topics,Java Game Development...