Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Try again...

Try again...

Scheduled Pinned Locked Moved The Weird and The Wonderful
designalgorithms
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 0 Offline
    0 Offline
    0bx
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • 0 0bx

      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.

      L Offline
      L Offline
      Lutoslaw
      wrote on last edited by
      #2

      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

      0 1 Reply Last reply
      0
      • L Lutoslaw

        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

        0 Offline
        0 Offline
        0bx
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups