Movement problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Movement problem

  1. #1
    Join Date
    Jan 2004
    Posts
    22

    Movement problem

    I'm trying to make Pacman. Thus far, I have a yellow circle being controlled by the keys that eats white circles. My problem is with the ghosts. I began with an ambitious movement function.

    Code:
    	void ProcessMove2(int change, int squareSize, boolean upClear, boolean downClear, boolean leftClear, boolean rightClear) {
    		Random randNum = new Random();
    		if (direction=="u") smallY-=change;
    		else if (direction=="d") smallY+=change;
    		else if (direction=="l") smallX-=change;
    		else if (direction=="r") smallY+=change;
    		if (smallY>=squareSize) { smallY=0; loc.y++; }
    		else if (smallY<=-squareSize) { smallY=0; loc.y--; }
    		else if (smallX>=squareSize) { smallX=0; loc.x++; }
    		else if (smallX<=-squareSize) { smallX=0; loc.x--; }
    		if (smallX==0 && smallY==0) { 					//Location has changed
    			if (direction=="u") downClear=false;	//Don't double back unless necessary
    			if (direction=="d") upClear=false;
    			if (direction=="r") leftClear=false;
    			if (direction=="l") rightClear=false;
    			if (upClear || downClear || leftClear || rightClear ) { //No need to turn around
    				int dirNum;
    				do {
    					dirNum=(int) Math.round(randNum.nextDouble()*4);
    					if (dirNum==0 && upClear) break;
    					else if (dirNum==1 && downClear) break;
    					else if (dirNum==2 && leftClear) break;
    					else if (dirNum==3 && rightClear) break;
    				} while (true);
    				switch (dirNum) {
    					case 0: direction="u"; break;
    					case 1: direction="d"; break;
    					case 2: direction="l"; break;
    					case 3: direction="r"; break;
    				}
    			} else { 		//Turn around
    				if (direction=="u") direction="d";
    				else if (direction=="d") direction="u";
    				else if (direction=="l") direction="r";				
    				else if (direction=="r") direction="l";
    			}
    		}
    	}
    It compiler, but it didn't work. The first part did work. Based on the direction, it moved. The turning did not in any way. My goal was to start with four booleans indicating whether up, down, left, and right were clear and then pick a random one of the true ones. Before picking from the trues, I removed the one opposite of the direction its facing to avoid uneccesary doubling back.

    Can anyone see some logic error there?

    Anyway, I decided to go for something simpler, at least initially.

    Code:
    	void ProcessMove(int change, int squareSize, int[][] map) {
    		Random randNum = new Random();
    		
    		if (direction=="u") smallY-=change;
    		else if (direction=="d") smallY+=change;
    		else if (direction=="l") smallX-=change;
    		else if (direction=="r") smallY+=change;
    		if (smallY>=squareSize) { smallY=0; loc.y++; }
    		else if (smallY<=-squareSize) { smallY=0; loc.y--; }
    		else if (smallX>=squareSize) { smallX=0; loc.x++; }
    		else if (smallX<=-squareSize) { smallX=0; loc.x--; }
    		if (smallX==0 && smallY==0) { 
    				int dirNum;
    				dirNum=(int) Math.round(randNum.nextDouble()*4);
    	
    				switch (dirNum) {
    					case 0: direction="u"; break;
    					case 1: direction="d"; break;
    					case 2: direction="l"; break;
    					case 3: direction="r"; break;
    				}
    				if (loc.x==1) direction="r";
    				if (loc.x==8) direction="l";
    				if (loc.y==1) direction="d";
    				if (loc.y==8) direction="u";
    		}
    	}
    It hopefully would just avoid walls. It doesn't. It gets stuck on the bottom two rows, and then just goes up and down again and again.

    I'll attach my whole code if it can help. Thanks in advance to anyone who helps.
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    it might be something as simple as you use of strings like this:

    if(direction=="u") ...

    you cannot compare strings in this way, if direction is even a string.. strings must be compared using String.equals(String)

    however.. using strings as a container for the direction is definitely not sensible. use an int instead, its directly comparable, useful for array indexes etc..

    your processmove logic should perhaps be a part of the ghost Sprite.. it should know where it is within the mave, and where it is going. when told to move, it will look around and asses what walls are near, and where it can go.. with some high probablity it will continue in the direction it is going, with a lesser probablity it will change direction, with a even lesser probaility it will reverse back on itself:

    double d = Math.random();
    if(d < 0.05) //5%
    double back
    else if(d<0.4)
    change direction (if possible)
    else
    continue as you are!

    if th ghost os south of pacman, it should be more likely that it will change its direction to north, etc, but that comes later.. do basics first..
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Jan 2004
    Posts
    22
    Thanks for the help. I fixed it after a while.

    Out of curiousity, what is the difference between == and .equals() ?

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    == compared memory addresses, .equals() compares string contents...
    it does occur, occasionally, that:

    String string1 = "hello";
    String string2 = "hello";

    then string1 == string2 may well return true, but only because the jvm or the compiler (probably the compiler, as it is very unreliable for dynamically created strings..) has noticed that they are the same, so it has pointed them to the same memeory address (strings cannot be changed once created so it is safe to do so)

    it will not always be the case:

    String string1 = "hello";
    char[] c = new char[]{'h', 'e', 'l', 'o'};
    String string2 = c[0]+c[1]+c[2]+c[2]+c[3];

    string1 == srting2 will now likely be false; they will be at different memory addresses

    in short: always use .equals()
    never use == on strings

    dont use strings for what you ahve used here; they are too big, and inefficient..have an array of ints, booleans, whatever.. not strings
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center