1. Member Join Date
Sep 2010
Posts
6
Rep Power
0

## Newbie Game Experiment - looking for feedback/advice

I've just started learning some Java and it's my first programming language. I'd really appreciate input and assistance figuring out how to do various things.

but decided to put some of the knowledge to the test and also force myself to try some new things. Unfortunately this means I'm in unknown territory and don't know how to do everything I'm wanting to.

Game premise:

A simple top down 6x6 grid. Each square is either empty, occupied by an enemy, occupied by the Robot(player controlled), or is a wall object and can't be occupied.

I'm starting with the basics and trying to find out how to move up, down, left and right while checking first to see if the square is occupiable. To do this I need to figure out how to create the game board. My initial thought is to do this with each square of the grid having an x and y value to indicate vertical and horizontal squares as in chess. The Robot and the enemies would also have an x and y variable to show where they are at the time. Below is an example of the Robot class so far.

Questions for the experts:

1)Would an array[][] be useful for creating and/or referencing the game board or would you create 36 individual uniquely named objects to represent the spaces?

2)If the array[][], how do I use it to access the x and y positions of a square and find out if it's occupiable or not.

3)Also give any suggestions that might help me in case I'm going the wrong way about this.

Thanks,
-Justin

Java Code:
```public class Robot {

//Robot Variables
int xLoc;
int yLoc;
int health = 10;
int techLvl;

//Robot contructor
public Robot() {
this.techLvl = 1;
this.xLoc = 1;
this.yLoc = 1;
}

//Movement methods
public void moveUp(int y) {
//check if the square is occupied
if (checkValidLoc(this.xLoc,this.yLoc+1)) {
this.yLoc = this.yLoc + 1;
}
}
public void moveDown(int y) {
//check if the square is occupied
if (checkValidLoc(this.xLoc,this.yLoc-1)) {
this.yLoc = this.yLoc-1;
}
}
}```  Reply With Quote

2. ## Use the array[][]. Think about a chess game--are there really 64 separate allocated variables? Probably not. And a game world with thousands of grid spaces--definitely not. Arrays are the champions of data storage in large quantities.

As far as accessing the elements of the array, you know that arrays are 0-based, right? Just like a mathematical grid, x=0 y=0 is the same as the 0 index of the array and the 0 index of its arrays.

Getting the elements themselves is another matter. This link should help with that. If you have more questions feel free to ask!  Reply With Quote

3. Member Join Date
Sep 2010
Posts
6
Rep Power
0

## Okay so I'll have to read over that at least one more time, but so far I'm thinking I might need to have a 3 dimentional array? the first two [] to indicate an x and y position and the 3rd row of [] to represent various states of the squares.

Am I way off base or is that possible?  Reply With Quote

4. ##  Originally Posted by papium Okay so I'll have to read over that at least one more time, but so far I'm thinking I might need to have a 3 dimentional array? the first two [] to indicate an x and y position and the 3rd row of [] to represent various states of the squares.

Am I way off base or is that possible?
I wouldn't do that. I think better would be to use object-oriented techniques and to have a 2-D array of a Square class objects, and have Square objects that would know what state they are in, have methods for setting state and checking state, and have their behavior changed depending on their state. The state itself could be an enum.  Reply With Quote

5. Member Join Date
Sep 2010
Posts
6
Rep Power
0

## So with the array.. Would I create multiple arrays? one for each variable the space needs to track? Would I create an array of booleans

Java Code:
`boolean[][] occupied = new boolean;`
to represent if the space was occupied?

My original thought was that each space would be able to hold multiple variables using a single array but now I'm not so sure... Would a 3-dimensional boolean array be a good choice for this? The first two dimensions for the coordinates and the 3rd row of variables representing various states?

Java Code:
`Example: spaceVars = true;   ([x coord][y coord][variable])`
The first idea of a 2 dimensional boolean array seems easier probably.

Still trying to wrap my mind around it, -Justin
Last edited by papium; 09-21-2010 at 04:01 PM. Reason: change int to boolean  Reply With Quote

6. ## I wouldn't use an array of primitives (if you used booleans, you'd only be allowing for 2 states -- true or false) but rather a single array of smart objects, for instance a 2-D array of GridSquare objects where GridSquare has a private fields that hold the state of the square. This is what OOPs is all about. GridSquare could have a boolean wall field that if true, means it's a wall and can't be occupied, it could have Combatant field where Combatant is a parent class to your Enemy and Robot classes, that may be null (an empty square), or may hold an Enemy object or Robot object, and only if wall is false,... the possibilities here are mind-boggling.

Cheers!
Last edited by Fubarable; 09-21-2010 at 04:10 PM.  Reply With Quote

7. Member Join Date
Sep 2010
Posts
6
Rep Power
0

## I'm not sure I realized you could create an array of objects. I'll try to research how to do that.

I really appreciate the suggestion. Further pointers are welcome..  Reply With Quote

8. Member Join Date
Sep 2010
Posts
6
Rep Power
0

## Okay, I've created a 6x6 array and assigned a tile object to each member of the array with some default values. I then used a for loop to print out the array and make sure it worked. Then I experimented with creating a constructor that allows for arguments to be passed to it and thus create different tiles instead of all the same one. I haven't gotten this to work yet and am either getting errors, or nothing is happening at all, depending on what I try. I'm sure it must be a syntax error.

Is it a better idea to innitialize the array first with basic settings and then run a method (say it's called "level1()") which holds the blueprint for the map and calls other methods to change the variables?

Here are the two relevant classes.

<
Java Code:
```public class RobotGame {

/*
*
*/
public static void main(String[] args) {

// Variables
GameTile[][] tile;

// Innitialize array
tile = new GameTile;

for (int i=0; i<tile.length; i++) {
for (int j=0; j<tile[i].length; j++) {
if (i==1 && j==4) {tile[i][j] = new GameTile('w');}
tile[i][j] = new GameTile();
}
}

for (int i=0; i<tile.length; i++) {
for (int j=0; j<tile[i].length; j++) {
System.out.print("Damage Dealt: " + tile[i][j].damage + " , ");
System.out.print("Occupied?: " + tile[i][j].occupied + " , ");
System.out.println("Terrain Type: " + tile[i][j].terrain);
}
}

}

}```
And the tile class
Java Code:
```public class GameTile {

//Variables
int damage;
String terrain;
boolean occupied;

//Constructor for GameTile
public GameTile(char ter) {
if (ter == 'w') {
terrain = "water";
damage = 100;
occupied = false;}
else if (ter == 'd') {
terrain = "desert";}
else if (ter == 'g') {
terrain = "grass";}
else if (ter == 'b') {
terrain = "block";}
else {terrain = "testing"; }
}

public GameTile() {
damage = 0;
occupied = false;
terrain = "Grass";
}
}```  Reply With Quote

9. Member Join Date
Sep 2010
Posts
6
Rep Power
0

## Okay, I've switched a lot of stuff around. It's now a bit more compartmentalized and makes a bit more sense I think. Still struggling with figuring out how to create the game board with different terrain types and variable values.

My current idea is to create a temporary array of integers matching the size of the map array. The integers will each represent a terrain type and then a couple for loops will use that blueprint to assign the terrain values to the map array using methods for each terrain type.

Is there a more elegant way of storing and creating the various maps?

Here's an idea of what I'm trying:
Java Code:
```        //Change variable values in lvl.tiles array to reflect different terrains
private void createTerrain(int lvlNum, int width, int height) {
int w = width;
int h = height;
int[][] tMap = new int[w][h];

switch (lvlNum) {                         // Map for each level with 1=water, 2=grass, etc.
case 1: tMap=2;tMap=1;             //Each line is a horizontal line of the array
tMap=2;tMap=3;
break;
case 2:
}

for (int i = 0; i<tiles.length; i++) { 			//This will use a switch or if/else statements
for (int j=0; j<tiles[i].length; j++) {		//to change the variables in lvl.tiles, using the
//the above maps.
}
}

}```
Last edited by papium; 09-23-2010 at 03:40 AM.  Reply With Quote

help_me, newbie, project 