Try again...
-
I was about to delete this, but might as well post it here first. I'm making pac-man. The Enemy is the "ghost", Hero is "pacman". When the value of the map[x,y] = 1 it means there's a wall. Worst thing is that the ghost is still going through walls. Lesson for today: don't be lazy and design a decent recursive algorithm :^)
if (Math.Abs(Enemy.X - Hero.X) > Math.Abs(Enemy.Y - Hero.Y))
{
if ((Enemy.X - Hero.X) > 0)
{
if ((map[Enemy.X - 1,Enemy.Y]) != 1)
{
Enemy.X -= 1;
}
else
{
if ((Enemy.Y - Hero.Y) > 0)
{
Enemy.Y -= 1;
}
else Enemy.Y += 1;
}
}
else
{
if ((map[Enemy.X + 1,Enemy.Y] != 1))
{
Enemy.X += 1;
}
else
{
if ((Enemy.Y - Hero.Y) > 0)
{
Enemy.Y -= 1;
}
else Enemy.Y += 1;
}
}} else { if ((Enemy.Y - Hero.Y) > 0) { if ((Enemy.Y - 1) != 1) { Enemy.Y -= 1; } else { if ((Enemy.X - Hero.X) > 0) { Enemy.X -= 1; } else Enemy.X += 1; } } else { if ((Enemy.Y + 1 != 1)) { Enemy.Y += 1; } else { if ((Enemy.X - Hero.X) > 0) { Enemy.X -= 1; } else Enemy.X += 1; } }
Giraffes are not real.
-
I was about to delete this, but might as well post it here first. I'm making pac-man. The Enemy is the "ghost", Hero is "pacman". When the value of the map[x,y] = 1 it means there's a wall. Worst thing is that the ghost is still going through walls. Lesson for today: don't be lazy and design a decent recursive algorithm :^)
if (Math.Abs(Enemy.X - Hero.X) > Math.Abs(Enemy.Y - Hero.Y))
{
if ((Enemy.X - Hero.X) > 0)
{
if ((map[Enemy.X - 1,Enemy.Y]) != 1)
{
Enemy.X -= 1;
}
else
{
if ((Enemy.Y - Hero.Y) > 0)
{
Enemy.Y -= 1;
}
else Enemy.Y += 1;
}
}
else
{
if ((map[Enemy.X + 1,Enemy.Y] != 1))
{
Enemy.X += 1;
}
else
{
if ((Enemy.Y - Hero.Y) > 0)
{
Enemy.Y -= 1;
}
else Enemy.Y += 1;
}
}} else { if ((Enemy.Y - Hero.Y) > 0) { if ((Enemy.Y - 1) != 1) { Enemy.Y -= 1; } else { if ((Enemy.X - Hero.X) > 0) { Enemy.X -= 1; } else Enemy.X += 1; } } else { if ((Enemy.Y + 1 != 1)) { Enemy.Y += 1; } else { if ((Enemy.X - Hero.X) > 0) { Enemy.X -= 1; } else Enemy.X += 1; } }
Giraffes are not real.
Probility of making a typo in this kind of code is equal to 1. Looking forward to see an updated algorithm in a "Clever Code" forum ;) I'd try making a 3x3 stochastic matrix which probabilities would depend on 1) player's location (let's say 0.9 xor 0.1), 2) validness of a move through wall (0 xor not-0)
Greetings - Jacek
modified on Monday, July 4, 2011 8:56 AM
-
Probility of making a typo in this kind of code is equal to 1. Looking forward to see an updated algorithm in a "Clever Code" forum ;) I'd try making a 3x3 stochastic matrix which probabilities would depend on 1) player's location (let's say 0.9 xor 0.1), 2) validness of a move through wall (0 xor not-0)
Greetings - Jacek
modified on Monday, July 4, 2011 8:56 AM
Thanks, for the advice... it almost makes too much sense. Why didn't I think of that? I'm never going to put my own code in 'clever code', seriously if I ever write something clever it'll be because I stole the idea. ;P Right now I got what it supposed to be doing, it's exactly the same idea but it has a better structure. I use one "if block" to gather information where the walls are, put the result in a byte and run it through a "select" block where every "case" evaluates the direction of the player. It's more readable and a lot easier (as in, not impossible) to comment on. I'm getting more ambitious now and I'd like to try some ideas for more complex behavior, based on your suggestion. It also needs to be able to run through any given maze, right now I'm 'cheating' because the navigation is tailored to the only maze in the game. The project started as a break from the boring database stuff at the job training center, so I didn't have much time to work on it. The rest of the game is only about 200 lines of code and it didn't took long to write, but to get decent enemies working proves to be quite a challenge.
Giraffes are not real.