Results 1 to 8 of 8
Like Tree2Likes
  • 2 Post By pbrockway2

Thread: How to make AI dumber

  1. #1
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default How to make AI dumber

    Well I'm programming a Pong clone and I'm wondering how to make the enemy paddle "dumber" or "more human" if you prefer it that way

    Fairly simple code:

    Java Code:
    public void AI()
    	{
    		if (middlepaddle-ycoordinateball<-5)
    		{
    			y2=y2+5;
    		}
    		else if (middlepaddle-ycoordinateball>5)
    		{
    			y2=y2-5;
    		}
    	}
    The Ai checks if the middlepoint of the ball (y-coordinate) is higher or lower then the middle of the paddle. (with a fault of 5) and in response adjusts its position with 5 pixels.

    At this moment the AI is unbeatable.

    I have tried changing the pixels it adjusts itself to lower values. But that makes the aI not convincing, it makes it beatable though but I want a convincing aI.

    Thnx in advance,

    Reskaillev

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Maybe you can add some randomness to the y2 coordinate? e.g.

    Java Code:
    public void AI()
    	{
    		if (middlepaddle-ycoordinateball<-5)
    		{
    			y2=y2+r.nextInt(15);
    		}
    		else if (middlepaddle-ycoordinateball>5)
    		{
    			y2=y2-r.nextInt(15);
    		}
    	}
    ... where variable r is an instance of the Random class; you can play a bit with how big the random numbers should be to avoid turning your AI into a complete idiot ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Also you could check to see if the ball is on the AI's side of the court. If it is, then track it towards the ball. If its not, you could invoke a random movement on its side. Kind of like how players some times move their paddle while their waiting for the ball to come back.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Thnx a lot for those answers!

    It's much appreciated

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    No, no, no. I'm sorry, but I think that you're missing the boat. The key to making your AI dumber is to create and call the proper methods. For example:

    Java Code:
    MyAI ai = new MyAI();
    ai.setName("Jos");
    while (true) {
      ai.consumeGrolsch();
    }
    This never fails for my apps, and will work for yours, guaranteed!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    No! As written ai will never get to be used because of the infinite loop.

    You shouldn't be calling consumeGrolsch() on the EDT anyway, the results are never pretty. And you MUST read the docs and catch the exceptions that this method is declared to throw. It is sufficient to respond to IntoxificationException by sending some random string to System.err. But you cannot suppress the OutOfGrolschException: use BeverageUtilities.replenishStock(), that's what it is there for.

    There is no need to be overcareful about synchronising variables in MyAI that might be accessed by consumeGrolsch() from other threads as it is well known that after a few method calls the "synchronised" keyword no longer has any effect. Your code should note that the method is not thread safe, but that you don't really care.
    Fubarable and Dark like this.

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

    Default

    I feel so flattered ...

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Reskaillev is offline Member
    Join Date
    Jul 2011
    Posts
    53
    Rep Power
    0

    Default

    Ok thnx guys, I made it dumber using the methods dark and jos gave me.

    The consumeGrolsch method was too smart for this....:o

Similar Threads

  1. how to make .exe
    By vibaviattigala in forum New To Java
    Replies: 4
    Last Post: 07-22-2011, 10:07 PM
  2. how do i make a jar to an exe?
    By jack13580 in forum New To Java
    Replies: 23
    Last Post: 12-19-2010, 04:15 AM
  3. Trying to make a bot
    By ighor10 in forum New To Java
    Replies: 4
    Last Post: 04-10-2010, 03:29 AM
  4. how can i make bufferedreader
    By chyeeqi in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 05:24 PM
  5. Make it just A, Help me please
    By yuuchan in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 02:09 PM

Posting Permissions

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