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