Results 1 to 11 of 11
  1. #1
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Question AI Programming help

    :confused:I would like some help with programming this game's artificial intelligance. It's a simple 8-bit style game where you move your tank around a labryinth. There's also an enemy tank that will try to hinder your progress. What I need help with is getting the enemy tank to move on it's own, or atleast the computer to move it.
    The enemy sprite is simply called enemy and the your character is called hero.

    XML Code:
    import java.awt.*;
    import java.awt.event.*;
    import  sun.audio.*;    //import the sun.audio package
    import  java.io.*;
    import java.util.ArrayList;
    import javax.swing.JOptionPane;
    
    public class SimpleDemo
    {
      
      private static ArrayList<Location> myBlockLocations = new ArrayList<Location>();
      
      public static void main(String[] args)
      {
        play();
      }
      
      public static boolean canMove(Location l)
      {
        for (int i = 0; i < myBlockLocations.size(); i++)
          if (myBlockLocations.get(i).equals(l))
          return false;
        return true;
      }
      
      public static void play()
      {
        //create display with each cell of size 64x64 pixels
         new AePlayWave("CallMe.wav").start();
        
        GridDisplay display = new GridDisplay(10,10);
        
        Grid<Sprite> grid = display.getSpriteGrid();
        
        display.setTitle("Simple Demo");
        
        display.getColorGrid().put(new Location(1, 5), new Color(255,255,255));
        
        //Create location for block
        myBlockLocations.add(new Location(0,0));
        myBlockLocations.add(new Location(0,1));
        myBlockLocations.add(new Location(0,2));
        myBlockLocations.add(new Location(0,3));
        myBlockLocations.add(new Location(0,4));
        myBlockLocations.add(new Location(0,5));
        myBlockLocations.add(new Location(0,6));
        myBlockLocations.add(new Location(0,7));
        myBlockLocations.add(new Location(0,9));
        myBlockLocations.add(new Location(1,0));
        myBlockLocations.add(new Location(1,4));
        myBlockLocations.add(new Location(1,9));
        myBlockLocations.add(new Location(2,0));
        myBlockLocations.add(new Location(2,1));
        myBlockLocations.add(new Location(2,4));
        myBlockLocations.add(new Location(2,5));
        myBlockLocations.add(new Location(2,6));
        myBlockLocations.add(new Location(2,9));
        myBlockLocations.add(new Location(2,2));
        myBlockLocations.add(new Location(3,9));                   
        myBlockLocations.add(new Location(3,0));
        myBlockLocations.add(new Location(3,2));
        myBlockLocations.add(new Location(4,2));
        myBlockLocations.add(new Location(4,4));
        myBlockLocations.add(new Location(4,5));
        myBlockLocations.add(new Location(4,6));
        myBlockLocations.add(new Location(5,0));
        myBlockLocations.add(new Location(5,9));
        myBlockLocations.add(new Location(6,0));
        myBlockLocations.add(new Location(6,3));
        myBlockLocations.add(new Location(6,4));
        myBlockLocations.add(new Location(6,5));
        myBlockLocations.add(new Location(6,7));
        myBlockLocations.add(new Location(6,9));
        myBlockLocations.add(new Location(7,0));
        myBlockLocations.add(new Location(7,1));
        myBlockLocations.add(new Location(7,2));
        myBlockLocations.add(new Location(7,3));
        myBlockLocations.add(new Location(7,7));
        myBlockLocations.add(new Location(7,9));
        myBlockLocations.add(new Location(8,0));
        myBlockLocations.add(new Location(8,5));
        myBlockLocations.add(new Location(8,7));
        myBlockLocations.add(new Location(8,9));
        myBlockLocations.add(new Location(9,0));
        myBlockLocations.add(new Location(9,1));
        myBlockLocations.add(new Location(9,2));
        myBlockLocations.add(new Location(9,3));
        myBlockLocations.add(new Location(9,4));
        myBlockLocations.add(new Location(9,5));
        myBlockLocations.add(new Location(9,6));
        myBlockLocations.add(new Location(9,7));
        myBlockLocations.add(new Location(9,9));
        
        
        for (int i = 0; i < myBlockLocations.size() ; i++)
          display.getColorGrid().put(myBlockLocations.get(i), Color.GREEN);
        
        
        //add object to grid, which will display using file named "hero.gif"
        Sprite hero = new Sprite();
        hero.setImageFileName("tankeast.gif");
        hero.putSelfInGrid(grid, new Location(4, 0));
        //add enemy
        Sprite enemy = new Sprite();
        enemy.setImageFileName("wolfeast.gif");
        enemy.putSelfInGrid(grid, new Location(1,1));
        while (true)
        {
        
     
    
    //tells the display to pause and redraw itself
          display.pause(100);
          
          int key = display.checkLastKeyPressed();
          
          Location loc = hero.getLocation();
          Location bad = enemy.getLocation();
          //test if right-arrow pressed
          
            
            
          if (key == KeyEvent.VK_RIGHT)
          {
            hero.setImageFileName("tankeast.gif"); 
            if (loc.getCol() + 1 == grid.getNumCols())
            {
              //teleport
              hero.setImageFileName("tankeast.gif");
              Location nextLocation = new Location(loc.getRow(), 0);
              if (canMove(nextLocation))       
                hero.moveTo(nextLocation);        
            }
            
            else
            {
              // normal move
              Location nextLocation = new Location(loc.getRow(), loc.getCol() + 1);
              if (canMove(nextLocation))
                hero.moveTo(nextLocation);
            }
          }
          
          if (key == KeyEvent.VK_LEFT)
          {
            hero.setImageFileName("tankwest.gif"); 
            if (loc.getCol() == 0)
            {
              //teleport
              hero.setImageFileName("tankwest.gif");
              Location nextLocation = new Location(loc.getRow(),9);
              
              if (canMove(nextLocation))       
                hero.moveTo(nextLocation); 
            }
            else
            {
              // normal move
              Location nextLocation = new Location(loc.getRow(), loc.getCol() - 1);
              if (canMove(nextLocation))
                hero.moveTo(nextLocation);
              
            }
          }
          
          
          
          if (key == KeyEvent.VK_UP)
          {
            hero.setImageFileName("tanknorth.gif"); 
            if (loc.getRow() == 0)
            {
              //teleport
              hero.setImageFileName("tanknorth.gif");
              Location nextLocation = new Location(9, loc.getCol());
              if (canMove(nextLocation))       
                hero.moveTo(nextLocation);        
            }
            else
            {
              // normal move
              Location nextLocation = new Location(loc.getRow() - 1, loc.getCol());
              if (canMove(nextLocation))
                hero.moveTo(nextLocation);
              
            }
          }
          
        
          
          
          
          if (key == KeyEvent.VK_DOWN)
          {
            hero.setImageFileName("tanksouth.gif"); 
            if (loc.getRow() == 9)
            {
              //teleport
              hero.setImageFileName("tanksouth.gif");
              Location nextLocation = new Location(0, loc.getCol());
              if (canMove(nextLocation))       
                hero.moveTo(nextLocation);      
            }
            else
            {
              // normal move
              Location nextLocation = new Location(loc.getRow() + 1, loc.getCol());
              if (canMove(nextLocation))
                hero.moveTo(nextLocation);
              
            }
            }
            // This is how you die
          
          
          
          
          
          if (loc.equals(bad))
          {
            JOptionPane.showMessageDialog(null, "Your next of kin will be informed of you death in the line of duty.");
            System.exit(0);
          }
          
          
          
        }
        
      }
    }
    :confused:

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    How advanced do you want the AI to be? Others may offer better advice, but it may be helpful to have the tank moving randomly for a simple ai and a smarter ai would be to find your current location and generate the quickest path and start moving down that path. The AI could update this as you move.

  3. #3
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    I was thinking like the second one, but as long as I could get the enemy moving on it's own at all, randomly or otherwise, I'll be happy.

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Making it random is easy: then you have to randomly choose N/W/S/E, and futher use the same logic as with your keys.
    I once used something like the tremaux maze algorithm (google->utube) to find a track, marking your trail from tank to hero as un/once/twice visited.
    It was rather easy, but I guess it is easier with a 2D array as datastructure.
    Maybe there are better algorithms though.

  5. #5
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks, I'll check it out.

    Once I'm done, it's gonna be called Tank Master, I'll list all you guys in the credits as devolopment help, if you don't mind. Since it's my first, I probably would just give it away. Or atleast this version. If it takes off, however, like I got alot of people downloading it, I'd go for sequal with extra levels or something.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Glad to have helped and glad you solved it.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Once you have the tank moving I'd suggest two AIs.
    One for the tank, which has the tanks location and the place the tank should move to. A simple pathing algorithm of some sort.

    The other would be the algorithm that decided where the tank should move. how that one works depends on the rules of your game. For example if the enemy tank needs to merely block the players one from getting to its destination, then the algorithm would look at where the players tank is, where it's going, select a path, then find a point on that path to send the tank.

    Anyway, just a thought.

    gamedev.net is a good resource for this sort of stuff, and some basic pathing code.

  8. #8
    Bianchi60's Avatar
    Bianchi60 is offline Member
    Join Date
    May 2011
    Posts
    11
    Rep Power
    0

    Default

    Thanks all of you for the help you have given. I see what to do now. The variable known as loc is the player's location. If I tell the enemy sprite, simply called enemy to automatically move to loc this should work. However, I also have to add the solid wall logic called my my method canMove() in which the computer constantly checks to see if the next space is avaliable. The enemy really dosn't have to use the teleport spots, so I won't worry about that for now. If my theory is correct, and the computer is as smart as I hope it is, the enemy tank will take the shortest route to chase me, as long as nothing is in it's way.

    Once again, thanks, and if I hit anymore speed-bumps, I'll know where to ask.
    BTW, once I get this game out, you're all going in the credits.

  9. #9
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I don't know which algorithm you're thinking of, but the algorithm I spoke of finds A route, not garanteed the shortest, and not garanteed the same route each time. But that should give your hero a chance! (Maybe calculate the route once every 5 moves or so of your hero, and then recalculate a new route to the new spot of the hero.)
    There are shortest-route algorithms, but then you should google (or think) some more.
    Last edited by Jodokus; 05-06-2011 at 11:51 PM.

  10. #10
    Join Date
    Apr 2011
    Posts
    10
    Rep Power
    0

    Default

    pac man was a game that had very minimal AI. You would think that the ghosts would follow you around the entire time but if you've ever played it they obviously don't. Truthfully only the red one follows you around the screen. the blue ghost tries to move to a point that is exactly thirty-two pixels in front of you. The Green ghost moves to a similar point like the blue ghost.Only the yellow ghost moves around randomly. This is how they achieved the very limited AI that turned out to be very effective. I would have multiple enemy tanks like this and try to achieve a similar effect such as this. But maybe not until harder levels.
    <Daniel>P.</Berry>

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by altosaxplayer88 View Post
    pac man was a game that had very minimal AI. You would think that the ghosts would follow you around the entire time but if you've ever played it they obviously don't. Truthfully only the red one follows you around the screen. the blue ghost tries to move to a point that is exactly thirty-two pixels in front of you. The Green ghost moves to a similar point like the blue ghost.Only the yellow ghost moves around randomly.
    I never noticed that; all I did was peacefully eating those pills and fruits and when I was completely surrounded by those ghosts I panicked and forgot to move my pacman. I wasn't very good at that game ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Programming? Where first?
    By Humbly in forum Forum Lobby
    Replies: 3
    Last Post: 05-14-2011, 12:06 AM
  2. GPS programming.
    By santa in forum New To Java
    Replies: 7
    Last Post: 02-06-2011, 10:21 AM
  3. GPS programming =)
    By santa in forum New To Java
    Replies: 6
    Last Post: 01-23-2011, 06:06 PM
  4. GUI Programming Help
    By sirwiggles in forum New To Java
    Replies: 4
    Last Post: 04-28-2009, 04:53 AM
  5. programming
    By abcdefg in forum New To Java
    Replies: 9
    Last Post: 03-10-2008, 10:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •