2 Replies - 171 Views - Last Post: 28 November 2012 - 08:00 PM Rate Topic: -----

#1 hey92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 07-November 11

Implemented logic doesn't work?

Posted 28 November 2012 - 04:12 PM

Im doing a pacman game. In the beginning of the game the ghosts are in the center of the maze(home) and there is a door. When they are inside they can get out, if they are outside the home they cant get it. Im trying to implement that but right now my ghost just keep circling in the home.

I've omitted unnecessary methods.



public abstract class Ghost extends Character{
	
	
	private boolean doorIsWall=true;
	protected int count = 1;
	
	private Pacman pacman;
 
	public Ghost(int x, int y, Maze maze, int time, Pacman pacman, int frightTime) {
		...
		
	}
	 
	public void move() {
		target = calculateTarget();
		moveGhost(target);
	}

	public void moveGhost(Tiles target) {
		if(specialIntersections()){
			direction = direction; //keeps going in the same direction
		}
		else{
			int oppDir;
			if(direction == UP){
				oppDir = DOWN;
			}
			else if(direction == DOWN){
				oppDir = UP;
			}
			else if(direction == LEFT){
				oppDir = RIGHT;
			}
			else{
				oppDir=LEFT;
			}

			double minDist = 10000.0;
			Tiles potentialNext;

			for(int i=0; i<4; i++){
				if(i!=oppDir){
					potentialNext = maze.nextTile(getCurrentPos(), i);
					
						if(DoorIsWall()){
							moveWhenDoorIsWall(potentialNext, i,minDist);
						}
						
						if(!DoorIsWall()){
							moveWhenDoorIsNotWall(potentialNext, i,minDist);
						}
				}
			}
		}
		
		changeDirection();
		timer++;
		increment();
		
		
		x += xinc;
		y += yinc;
		tunnel();
		
	
	}
	
	//when ghost is outside the home, consider door as wall
	public void moveWhenDoorIsWall(Tiles potentialNext, int i, double minDist){
		
		if(!(potentialNext.wall()) && !(potentialNext.door())  && check(potentialNext)){
			if(calculateDistance(target, potentialNext) < minDist){
				minDist = calculateDistance(target, potentialNext);
				futureDirection = i;
				}
			}
		
	}
	
	//when ghost is inside the home, consider door open
	public void moveWhenDoorIsNotWall(Tiles potentialNext, int i,double minDist){
	
		if(!(potentialNext.wall())  && check(potentialNext)){
			if(calculateDistance(target, potentialNext) < minDist){
				minDist = calculateDistance(target, potentialNext);
				futureDirection = i;
				}
			}
		
		
	}
	
	//when ghost is inside the home returns !doorIsWall
	public boolean DoorIsWall(){
		//
		Tiles current = getCurrentPos();
		if(		   (current.getX()==9*20 && current.getY()==11*20)
				|| (current.getX()==10*20 && current.getY()==11*20)
				|| (current.getX()==11*20 && current.getY()==11*20)
				|| (current.getX()==12*20 && current.getY()==11*20)
				|| (current.getX()==9*20 && current.getY()==12*20)
				|| (current.getX()==10*20 && current.getY()==12*20)
				|| (current.getX()==11*20 && current.getY()==12*20)
				|| (current.getX()==12*20 && current.getY()==12*20)){
			doorIsWall= false;
			
			return doorIsWall;
		}else{	
			
		return doorIsWall;
		}
	}

}



Edited by Dogstopper:
Fix'd your code tags.

Is This A Good Question/Topic? 0
  • +

Replies To: Implemented logic doesn't work?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5438
  • View blog
  • Posts: 11,668
  • Joined: 02-June 10

Re: Implemented logic doesn't work?

Posted 28 November 2012 - 04:31 PM

Quote

[/CODE}

So close... Missed it by that much, chief.
Was This Post Helpful? 2
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Implemented logic doesn't work?

Posted 28 November 2012 - 08:00 PM

What horrible code... unless you only post the relevant part

current.getX()==10*20 && current.getY()==

getX() and getY() are method. You do not call method more than once for nothing (you never know how costly they are) call them only once and store the returned value in local variables

Not a really good idea to have a class Character which has the same name as a very basic Java data type... but if you want sonfusion and problems you are welcomed

And the question is ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1