Jump to content
  • Advertisement
Sign in to follow this  
SHiDi

Having Problem with Java

This topic is 4946 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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.

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
Guest Anonymous Poster
Put your code in "less than"source"greater than" tags.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!