Sign in to follow this  
SHiDi

Having Problem with Java

Recommended Posts

Hi all. I got an assignment which I have to create a Pacman game. The only difference is that, only Ghost have to move while Pacman stays in the same position. I able to make it move, but now I'm having two problems: 1. Ghost keep moving even thou there's no lines there 2. The program keep producing more maps for each time the ghost moves. What I want to do is that: 1. The ghost should only move on the lines 2. Just the Ghost need to move, no need to produce more maps. Here's my codes: (filename aI.java) class aI { static int ghostY = 0, ghostX = 0, pacY = 6, pacX = 5; static boolean samePost; public static void move(int pacman[][]) { try { Thread.sleep(500); } catch(InterruptedException e) { e.printStackTrace(); } if (ghostY > pacY) ghostY--; else if (ghostY < pacY) ghostY++; else if (ghostX > pacX) ghostX--; else if (ghostX < pacX) ghostX++; }//end move() /*public static void move(int pacman[][]) { boolean willHitWall = false; try { Thread.sleep(500); } catch(InterruptedException e) { e.printStackTrace(); } if (ghostY == 5 && ghostX == -1) willHitWall = true; else if (ghostY == 6 && ghostX == -1) willHitWall = true; else if (ghostY > pacY) ghostY--; else if (ghostY < pacY) ghostY++; if ((ghostY == 0) && (ghostX == 1 || ghostX == 3 || ghostX == 5)) willHitWall = true; else if ((ghostY == 1) && (ghostX == 0 || ghostX == 1 || ghostX == 6)) willHitWall = true; else if ((ghostY == 2) && (ghostX == 3 || ghostX == 4)) willHitWall = true; else if ((ghostY == 3) && (ghostX == 0 || ghostX == 1 || ghostX == 4 || ghostX == 6)) willHitWall = true; else if ((ghostY == 4) && (ghostX == 0 || ghostX == 1 || ghostX == 2 || ghostX == 4 || ghostX == 6)) willHitWall = true; else if (ghostY == 5 && ghostX == 6) willHitWall = true; else if ((ghostY == 6) && (ghostX == -1 || ghostX == 1 || ghostX == 3 || ghostX == 5 || ghostX == 6)) willHitWall = true; else if ((ghostY == 7) && (ghostX == -1 || ghostX == 1 || ghostX == 3 || ghostX == 5 || ghostX == 6)) willHitWall = true; else if (ghostX < pacX) ghostX++; else if (ghostX > pacX) ghostX--; }//end move()*/ //building the map public static void map(int pacman[][]) { String output = ""; //declare and initialize variable output for (int row = 0; row < pacman.length; row++) { //creating rows for (int column = 0; column < pacman[row].length; column++) { //creating columns if (row == ghostY && column == ghostX) output += "O" + " "; //placing Ghost in its position else if (row == pacY && column == pacX) output += "X" + " "; //placing Pacman at its place //creating empty spaces for certain rows and columns else if ((row == 0) && (column == 2 || column == 4 || column == 5)) output += " " + " "; else if ((row == 1) && (column == 1 || column == 2 || column == 7)) output += " " + " "; else if ((row == 2) && (column == 4 || column == 5)) output += " " + " "; else if ((row == 3) && (column == 1 || column == 2 || column == 5 || column == 7)) output += " " + " "; else if ((row == 4) && (column == 1 || column == 2 || column == 3 || column == 5 || column == 7)) output += " " + " "; else if (row == 5 && column == 7) output += " " + " "; else if ((row == 6) && (column == 0 || column == 2 || column == 4 || column == 6 || column == 7)) output += " " + " "; else if ((row == 7) && (column == 0 || column == 2 || column == 4 || column == 6 || column == 7)) output += " " + " "; //end creating empty spaces else output += "." + " "; //placing "." at non empty rows and columns }//end for - column output += "\n"; //creating new row }//end for - row System.out.println(output); }//end method map() public static void main(String args[]) { System.out.println("Pacman and Ghost in Specific Position\n"); System.out.println("\nLegend:\t0 - Ghost\n\tX - Pacman"); int pacman[][] = new int [8][8]; //set number of rows and columns do { map(pacman); move(pacman); } while (ghostX != pacX || ghostY != pacY); map(pacman); System.out.println("Ghost has eat Pacman!"); }//end method main() }//end class As you can see I've tried two different methods on how the Ghost should move, but it still move outside the lines. Someone please help me. - Thanks -

Share this post


Link to post
Share on other sites
*cough*

Why not just do this:

public int SpeedX = 0;
public int SpeedY = 0;

...

if (ghostY > pacY) SpeedY = -1;
else if (ghostY < pacY) SpeedY = 1;
else if (ghostX > pacX) SpeedX = -1;
else if (ghostX < pacX) SpeedX = 1;
...

if(ghostX + SpeedX == //num) blah

If they hit the wall, don't move them, otherwise move them:

if(!willhitWall) {
ghostX += SpeedX;
ghostY += SpeedY;
}

Also, you really, really should store the map into an array, and loop the map to build/draw it. And then loop the map to check for walls, and whatever else.

i.e.

public int Map[10][10]; //A 10x10 map

Share this post


Link to post
Share on other sites
Quote:
Original post by xSKOTTIEx
if (ghostY > pacY) ghostY--;
else if (ghostY < pacY) ghostY++;
else if (ghostX > pacX) ghostX--;
else if (ghostX < pacX) ghostX++;


doesnt look like you are taking the lines into account here.


As I mentioned I made two methods for the ghost to move. That's one, and the other one is:

/*public static void move(int pacman[][]) {
boolean willHitWall = false;
try {
Thread.sleep(500);
}
catch(InterruptedException e) {
e.printStackTrace();
}
if (ghostY == 5 && ghostX == -1) willHitWall = true;
else if (ghostY == 6 && ghostX == -1) willHitWall = true;
else if (ghostY > pacY) ghostY--;
else if (ghostY < pacY) ghostY++;
if ((ghostY == 0) && (ghostX == 1 || ghostX == 3 || ghostX == 5)) willHitWall = true;
else if ((ghostY == 1) && (ghostX == 0 || ghostX == 1 || ghostX == 6)) willHitWall = true;
else if ((ghostY == 2) && (ghostX == 3 || ghostX == 4)) willHitWall = true;
else if ((ghostY == 3) && (ghostX == 0 || ghostX == 1 || ghostX == 4 || ghostX == 6)) willHitWall = true;
else if ((ghostY == 4) && (ghostX == 0 || ghostX == 1 || ghostX == 2 || ghostX == 4 || ghostX == 6)) willHitWall = true;
else if (ghostY == 5 && ghostX == 6) willHitWall = true;
else if ((ghostY == 6) && (ghostX == -1 || ghostX == 1 || ghostX == 3 || ghostX == 5 || ghostX == 6)) willHitWall = true;
else if ((ghostY == 7) && (ghostX == -1 || ghostX == 1 || ghostX == 3 || ghostX == 5 || ghostX == 6)) willHitWall = true;
else if (ghostX < pacX) ghostX++;
else if (ghostX > pacX) ghostX--;
}//end move()*/


as u can see there's a /* I made it into comments since it doesn't really works.

Share this post


Link to post
Share on other sites
Quote:
Original post by MetaCipher
if(ghostX + SpeedX == //num) blah

If they hit the wall, don't move them, otherwise move them:

if(!willhitWall) {
ghostX += SpeedX;
ghostY += SpeedY;
}


MetaCipher, I didn't really understand this part. Care to explain more?

Share this post


Link to post
Share on other sites
I'm having a new problem now. This time the Ghost won't hit the wall but it also unable to reach pacman. Do tell me where I did wrong. Here's the code:

class aI {

static int ghostY = 0, ghostX = 0, pacY = 6, pacX = 5;

//method use to check whether Ghost will hit wall or not (for rows)
public static boolean checkWallY() {
boolean checkWallY = false;
if (ghostY == 5 && ghostX == 0) checkWallY = true;
else checkWallY = false;
return checkWallY;
}//end checkWallX()

//method use to check whether Ghost will hit wall or not (for columns)
public static boolean checkWallX() {
boolean checkWallX = false;
int nextWall = ghostX+1;
if ((ghostY == 0) && (nextWall == 2 || nextWall == 4 || nextWall == 5)) checkWallX = true;
else if ((ghostY == 1) && (nextWall == 1 || nextWall == 2 || nextWall == 7)) checkWallX = true;
else if ((ghostY == 2) && (nextWall == 4 || nextWall == 5)) checkWallX = true;
else if ((ghostY == 3) && (nextWall == 1 || nextWall == 2 || nextWall == 5 || nextWall == 7)) checkWallX = true;
else if ((ghostY == 4) && (nextWall == 1 || nextWall == 2 || nextWall == 3 || nextWall == 5)) checkWallX = true;
else if (ghostY == 5 && nextWall == 7) checkWallX = true;
else if ((ghostY == 6) && (nextWall == 0 || nextWall == 2 || nextWall == 4 || nextWall == 6 || nextWall == 7)) checkWallX = true;
else if ((ghostY == 7) && (nextWall == 0 || nextWall == 2 || nextWall == 4 || nextWall == 6 || nextWall == 7)) checkWallX = true;
else checkWallX = false;
return checkWallX;
}//end checkWallY()

//Ghost moves to get Pacman
public static void move(int pacman[][]) {
boolean wallX = false, wallY = false;
try {
Thread.sleep(500);
}
catch(InterruptedException e) {
e.printStackTrace();
}
if (ghostY > pacY) {
wallY = checkWallY();
if (wallY == false) ghostY--;
else {
wallX = checkWallX();
if (wallX == false) ghostX--;
}
}
if (ghostY < pacY) {
wallY = checkWallY();
if (wallY == false) ghostY++;
else {
wallX = checkWallX();
if (wallX == false) ghostX++;
}
}
}//end move()

//building the map and placing Ghost and Pacman
public static void map(int pacman[][]) {
String output = ""; //declare and initialize variable output
for (int row = 0; row < pacman.length; row++) { //creating rows
for (int column = 0; column < pacman[row].length; column++) { //creating columns
if (row == ghostY && column == ghostX) output += "O" + " "; //placing Ghost in its position
else if (row == pacY && column == pacX) output += "X" + " "; //placing Pacman at its place
//creating empty spaces for certain rows and columns
else if ((row == 0) && (column == 2 || column == 4 || column == 5)) output += " " + " ";
else if ((row == 1) && (column == 1 || column == 2 || column == 7)) output += " " + " ";
else if ((row == 2) && (column == 4 || column == 5)) output += " " + " ";
else if ((row == 3) && (column == 1 || column == 2 || column == 5 || column == 7)) output += " " + " ";
else if ((row == 4) && (column == 1 || column == 2 || column == 3 || column == 5 || column == 7)) output += " " + " ";
else if (row == 5 && column == 7) output += " " + " ";
else if ((row == 6) && (column == 0 || column == 2 || column == 4 || column == 6 || column == 7)) output += " " + " ";
else if ((row == 7) && (column == 0 || column == 2 || column == 4 || column == 6 || column == 7)) output += " " + " ";
//end creating empty spaces
else output += "." + " "; //placing "." at non empty rows and columns
}//end for - column
output += "\n"; //creating new row
}//end for - row
System.out.println(output);
}//end method map()

public static void main(String args[]) {
System.out.println("Pacman and Ghost in Specific Position\n");
int pacman[][] = new int [8][8]; //set number of rows and columns
do {
map(pacman);
move(pacman);
System.out.println("\nLegend:\t0 - Ghost\n\tX - Pacman\n");
}
while (ghostX != pacX || ghostY != pacY);
map(pacman); //final position of the Ghost
System.out.println("Ghost has eat Pacman!");
}//end method main()

}//end class


Where's my mistake? Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by SHiDi
Quote:
Original post by MetaCipher
if(ghostX + SpeedX == //num) blah

If they hit the wall, don't move them, otherwise move them:

if(!willhitWall) {
ghostX += SpeedX;
ghostY += SpeedY;
}


MetaCipher, I didn't really understand this part. Care to explain more?


You check to see if that if Pacman's position, with his speed vars hits a wall. Then, if Pacman is not hitting a wall, move him, otherwise do nothing.

Share this post


Link to post
Share on other sites
uh thanks for explaining. I got it now. But the problem now is that, I think I got some errors in the calculation part. Cause the Ghost still move in a weird way.
It didn't hit the walls but instead it will skip it. Why is it? Can you tell me the mistake i made in my calculations. I've changed it several times but still there's no improvement.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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[i] == 1 )
draw wall bitmap
else if( map[i] == 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.

Share this post


Link to post
Share on other sites
@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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this