Having Problem with Java

Started by
14 comments, last by Frank Henry 19 years, 4 months ago
It skips it? What do you mean? Like does it pass over the wall and continues on?
Advertisement
Hey

I've got to say this. If I was your teacher and I saw this I would not give you very high marks. Why? Because you aren't making your code effecient and adaptable.

For startes. Don't use all those if else statements. Declare an array, somehting like: Because you don't want to make it to complex.

int[] map = {0,0,0,0,0,0             0,1,1,1,1,0             0,1,0,0,1,0};


etc.

Then declare constants like

final WALL = 1;final GROUND = 0;


Then all you have to do is run through a loop of map and check the next position where either the ghost or PacMan is going to move. If that position is equal to WALL, then you change direction or do whatever you want.


The ability to succeed is the ability to adapt
Quote:Original post by MetaCipher
It skips it? What do you mean? Like does it pass over the wall and continues on?


yes... it continue running on other lines....
sTRiVe fOr PeRFeCTioN..:: http://shidi.cjb.net ::..
Quote:Original post by Mr_Ridd
Hey

I've got to say this. If I was your teacher and I saw this I would not give you very high marks. Why? Because you aren't making your code effecient and adaptable.

For startes. Don't use all those if else statements. Declare an array, somehting like: Because you don't want to make it to complex.

int[] map = {0,0,0,0,0,0             0,1,1,1,1,0             0,1,0,0,1,0};


etc.

Then declare constants like

final WALL = 1;final GROUND = 0;


Then all you have to do is run through a loop of map and check the next position where either the ghost or PacMan is going to move. If that position is equal to WALL, then you change direction or do whatever you want.


I saw some other students used this method. But then what will the output be? Since I run the program in Dos mode, so is it only be consists of 1 and 0? that is 1 for wall and 0 for ground?
sTRiVe fOr PeRFeCTioN..:: http://shidi.cjb.net ::..
See well, that array is just an index indicator, telling you where you are in the map. If you were doing a graphics app you would run through a loop and do somthing like this:

for(int i = 0; i < map.length; i++){   if( map == 1 )      draw wall bitmap   else if( map == 0 )      draw ground bitmap}


You must apply the same priciple except, depending on how you do it, if you are only using text or perhaps some graphics, you draw a rectangle or a funny character instead of a bitmap.

Just bare in mind that the code to work out where you are in the map and where you are going in the map could get a bit complex. I wrote a sought of 2D game like this a while ago and it work like a machine.

If you're stuck on the code just give me a shout and I'll help you out.

The ability to succeed is the ability to adapt
@SHiDi:
Do as Mr_Ridd suggests and make your world as an array.
Why? Not only is it easyer to expand but also it looks just like your world if you look at it. How you draw it is not of concern atm.

Take a page of graph paper.
Draw a box the size your world should be. This is your world.
Outside each corner draw the coords for the corner (where x and y are the dimensions respectively :
0/0 x/0

0/y x/y

Go through each box and if there is a Wall mark that field with a 1.

This is EXACTLY how your array will be created. Now you have something to look at while you figure out how to write the AI.
Then you can simply draw a ghost into the world and then think your way through it.

Imagine this is your world:

111111111
100000001 0 = empty
101111101 1 = wall
1G0000001 G = ghost
111111111

Give your ghost a direction and let it test to see if the field in that direction is vacant (i.e. 0) if it is go to that field
else choose a different direction and retry

rinse repeat

how you draw it is secondary and not important atm.

This topic is closed to new replies.

Advertisement