This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kronos
Kronos wrote ...

2017/5/8

Programming A.i. for Pong

1
2
3
Kronos Kronos

2017/5/8

#
I need help with fixing my ai. At the moment, the ai works even if the playerRedControlled boolean is false. I don't know what is wrong. If there is an error, it is a logic error as there is no syntax or runtime error showing up.
boolean playerRedControlled = false;
   // int w = Greenfoot.getRandomNumber(3);
   /**
    * Act - do whatever the Red wants to do. This method is called whenever
    * the 'Act' or 'Run' button gets pressed in the environment.
    */
   public void act() 
   {
       checkKeypress(); // This statement calls the checkKeyPress method.
   }
   
   public void checkKeypress() // This method controls the movement of the Red player.
   {
      Actor pongball = getWorld().getObjects(PongBall.class).get(0);
      Paddles paddles = new Paddles();
      if(pongball.getX() > 615)
       {
         if(getY() > pongball.getY())
         {
             if(Greenfoot.getRandomNumber(5) != 0)
             {
                 setLocation(getX(),getY() - 10); 
             }
             else if(Greenfoot.getRandomNumber(5) == 0)
             {
                 setLocation(getX(),getY() - 5); 
             }
         }
         else if(getY() < pongball.getY())
         {
             if(Greenfoot.getRandomNumber(5) != 0)
             {
                 setLocation(getX(),getY() + 10); 
             }
             else if(Greenfoot.getRandomNumber(5) == 0)
             {
                 setLocation(getX(),getY() + 5); 
             }
         }
      
         if(playerRedControlled = true)
         { 
             if (Greenfoot.isKeyDown("up")) // Pressing the up arrow key will move the red paddle up.
             {       
                 setLocation(getX(),getY() - 10);
             }
             if (Greenfoot.isKeyDown("down")) // Pressing the down arrow key will move the red paddle down. 
             {
                 setLocation(getX(),getY() + 10);
             }
         }
         else if(playerRedControlled = false)
         {
             paddles.act();
         }
         BorderCheck();
      }
   }
danpost danpost

2017/5/8

#
Remove line 15 and replace line 54 with lines 20 through 39 (not with a copy of those lines; but, with those lines -- cut/paste).
Kronos Kronos

2017/5/8

#
I did it. Now the ai isn't working.
danpost danpost

2017/5/8

#
Kronos wrote...
I did it. Now the ai isn't working.
Did you try it with the Boolean both true and false? (it may be just reversed from what you expected).
Kronos Kronos

2017/5/8

#
I did try it with both. No difference.
danpost danpost

2017/5/8

#
Kronos wrote...
I did try it with both. No difference.
Sorrry, I misunderstood what lines 20 through 39 were doing. Go back to the code you supplied above. Then, change line 41 to this:
if(playerRedControlled == true)
and change line 52 to this:
if(playerRedControlled == false)
That will be a better starting point.
Kronos Kronos

2017/5/10

#
Now I can't move the player. Could the problem be due to line 1 of the code? (Presetting the boolean variable playerRedControlled to false).
Yehuda Yehuda

2017/5/10

#
The key pressing code is not running since the value of the boolean is false, not true which is what's in the 'if' statement. You have an 'if' statement checking for the value of a boolean. Only the if which is true will run, so you can't have the AI and key presses both work at the same time (that's the reason that you made the boolean I assume). If you want to have multiple instances of this class, with some being AI and some not, then there should probably be a parameter in the constructor to specify whether it should be controlled by AI.
Kronos Kronos

2017/5/10

#
What do you mean about the value of the boolean is false? Are you talking about line 1? I changed it say the value is true, and the player control worked, but no ai. If line 1 is true, paddle is player controlled If line 1 is false, paddle is ai controlled.
danpost danpost

2017/5/10

#
When you create a paddle, if it is to be player controlled, you need to change the value of the field on line 1. To create a player controlled red paddle (from the world class):
Red paddle = new Red();
paddle.playerRedControlled = true;;
addObject(paddle, // etc.
Yehuda Yehuda

2017/5/10

#
Kronos wrote...
If line 1 is true, paddle is player controlled If line 1 is false, paddle is ai controlled.
This shouldn't be new news to anyone, because that is what lines 41 and 52 are made to do. So you have to set the boolean to be what you want the paddle to do.
danpost wrote...
When you create a paddle, if it is to be player controlled, you need to change the value of the field on line 1. To create a player controlled red paddle (from the world class):
Red paddle = new Red();
paddle.playerRedControlled = true;
addObject(paddle, // etc.
Kronos Kronos

2017/5/11

#
The paddle is now both player and ai controlled. Even with the code you gave, the ai won't turn off. Here is the new code.
boolean playerRedControlled;
   /**
    * Act - do whatever the Red wants to do. This method is called whenever
    * the 'Act' or 'Run' button gets pressed in the environment.
    */
   public void act() 
   {
       checkKeypress(); // This statement calls the checkKeyPress method.
   }
   
   public void checkKeypress() // This method controls the movement of the Red player.
   {
        Actor pongball = getWorld().getObjects(PongBall.class).get(0);
        if(pongball.getX() > 615)
        {
             // if(getY() > pongball.getY())
             // {
                 // if(Greenfoot.getRandomNumber(5) != 0)
                 // {
                     // setLocation(getX(),getY() - 10); 
                 // }
                 // else if(Greenfoot.getRandomNumber(5) == 0)
                 // {
                     // setLocation(getX(),getY() - 5); 
                 // }
             // }
             // else if(getY() < pongball.getY())
             // {
                 // if(Greenfoot.getRandomNumber(5) != 0)
                 // {
                     // setLocation(getX(),getY() + 10); 
                 // }
                 // else if(Greenfoot.getRandomNumber(5) == 0)
                 // {
                     // setLocation(getX(),getY() + 5); 
                 // }
                // }
      
             if(playerRedControlled == true)
             {                
                 if (Greenfoot.isKeyDown("up")) // Pressing the up arrow key will move the red paddle up.
                 {       
                     setLocation(getX(),getY() - 10);
                     System.out.println("UP");
                 }
                 else if (Greenfoot.isKeyDown("down")) // Pressing the down arrow key will move the red paddle down. 
                 {
                     setLocation(getX(),getY() + 10);
                     System.out.println("Down");
                 }
             }
             else if(playerRedControlled == false)
             {
                 if(getY() > pongball.getY())
                 {
                     if(Greenfoot.getRandomNumber(5) != 0)
                     {
                         setLocation(getX(),getY() - 10); 
                     }
                     else if(Greenfoot.getRandomNumber(5) == 0)
                     {
                         setLocation(getX(),getY() - 5); 
                     }
                 }
                 else if(getY() < pongball.getY())
                 {
                     if(Greenfoot.getRandomNumber(5) != 0)
                     {
                          setLocation(getX(),getY() + 10); 
                     }
                     else if(Greenfoot.getRandomNumber(5) == 0)
                     {
                          setLocation(getX(),getY() + 5); 
                     }
                 }
             }
             BorderCheck();      // This statement calls the BorderCheck method
        }
    }
danpost danpost

2017/5/11

#
Please show what you have in your world class.
Kronos Kronos

2017/5/12

#
Here is my world code. Ignore lines 65 to 155
GreenfootSound sound1 = new GreenfootSound("Sound_1.mp3"); 
    GreenfootSound sound2 = new GreenfootSound("Sound_2.mp3");
    GreenfootSound sound3 = new GreenfootSound("Sound_3.mp3");
    GreenfootSound sound4 = new GreenfootSound("Sound_4.wav");  
    PongBall pongBall = new PongBall();    
    BlueScore blueScore = new BlueScore();
    RedScore redScore = new RedScore();
    GameOverScreen gameOver = new GameOverScreen();   
    int blueNumber = 0; // This statement sets the initial score for Blue, to 0.
    int redNumber = 0; // This statement sets the initial score for Red, to 0.
    int g = 0;
    int directionPicker;
    int red;
    int blue;
    /**
     * Constructor for objects of class PongCourt.
     */
    public PongCourt()
    {    
        super(1080, 600, 1); // Create a new world with 1080x600 cells with a cell size of 1x1 pixels.
        addObject(new TopWall(),540,0); // Create a Top Wall for the ball to ricohet off.
        addObject(new BottomWall(),540,600);  // Create a Bottom Wall for the ball to ricohet off.
        
        // if(blue == 0)
        // {
            // Blue bluePaddle = new Blue();
            // bluePaddle.playerBlueControlled = false;
            // addObject(bluePaddle,5,getHeight()/2); // Create a new Blue player in a certain spot in the world. 
        // }
        // else if(blue == 1)
        // {
            // Blue bluePaddle = new Blue();
            // bluePaddle.playerBlueControlled = true;
            // addObject(bluePaddle,5,getHeight()/2); // Create a new Blue player in a certain spot in the world. 
        // }
        
        if(red == 0)
        {
            Red redPaddle = new Red();
            redPaddle.playerRedControlled = false;
            addObject(redPaddle,1075,getHeight()/2); // Create a new Red player in a certain spot in the world. 
        }
        else if(red == 1)
        {
            Red redPaddle = new Red();
            redPaddle.playerRedControlled = true;
            addObject(redPaddle,1075,getHeight()/2); // Create a new Red player in a certain spot in the world. 
        }
        
        // Red redPaddle = new Red();
        // redPaddle.playerRedControlled = true;
        // addObject(redPaddle,1075,getHeight()/2); // Create a new Red player in a certain spot in the world.    
        
        
        addObject(new GameOverScreen(),getWidth()/2, getHeight()/2); // Create a Halfway line to indicate each player's half. 
                                                                     //Create a gameOver object in the world as an anchor point for the GameOver screen.                                                                 
        addObject(new LeftGoal(),0,300); // Create a Goal that Blue has to defend, against Red attacking.
        addObject(new RightGoal(),1080,300); // Create a Goal that Red has to defend, against Blue attacking.            
        addObject(blueScore,getWidth()/2-75,60); // Create a Scoreboard to keep track of Blue's score.
        addObject(redScore,getWidth()/2+75,60); // Create a Scoreboard to keep track of Red's score.       
        NewBall(); // Call the NewBall method to create a new ball after a goal is scored.        
        Greenfoot.setSpeed(100); // Set the speed of the Greenfoot scenario.
    }
          
    public String NumberToFilenameBlue(int number) // A method that will turn the number of goals scored by Blue into a string to build a filename
    {
       return ("Score_" + String.valueOf(number) + "blue.png"); // Turn the number of goals scored into a phrase that will call an image.
    }   
   
    public String NumberToFilenameRed(int number) // A method that will turn the number of goals scored by Red into a string to build a filename
    {
       return ("Score_" + String.valueOf(number) + "red.png"); // Turn the number of goals scored into a phrase that will call an image.
    }       
   
    public void DisplayScoreBlue(int number, Actor score) // A method that will display the number of goals scored by Blue as an image
    {
       String filename = NumberToFilenameBlue(number); // Display an image of the score through either the blueScore or redScore actors.
       score.setImage(filename);                   // Set the image for the score to match the number of goals scored for each player.
    }
   
    public void DisplayScoreRed(int number, Actor score)  // A method that will display the number of goals scored by Red as an image
    {
       String filename = NumberToFilenameRed(number); // Display an image of the score through either the blueScore or redScore actors.
       score.setImage(filename);                   // Set the image for the score to match the number of goals scored for each player.
    }
   
    public void IncrementBlueScore() // This method increases the the score for Blue when the ball goes in the Right Goal.
    {
       blueNumber++;                               // Increments the variable blueNumber
       DisplayScoreBlue(blueNumber, blueScore);    // Displays the score for Blue
       if (blueNumber >= 9) //insert maximum score here
       {
            GreenfootImage winner = new GreenfootImage("GameOverBlueWins.png");         // This method attaches GameOverBlueWins.png to the variable winner.
            gameOver.setImage(winner);                                                  // This method sets the image variable winner to the variable gameOver.
            addObject(new BlueWins(),getWidth()/2, getHeight()/2);                      // Creates an object of class BlueWins in the middle of the screen          
            GameOver();                                                                 // This statement calls the GameOver method
       }
       else 
       {
            NewBall();                                              // This statment calls the NewBall method
            g++;                                                    // Increments the vairable g
            directionPicker = Greenfoot.getRandomNumber(3);         // Assigns a random number to the variable directionPicker
       }
    }
   
    public void IncrementRedScore() // This method increases the the score for Red when the ball goes in the Left Goal.
    {
       redNumber++;                                 // Increments the variable redNumber
       DisplayScoreRed(redNumber, redScore);        // Displays the score for Red
       if (redNumber >= 9) //insert maximum score here
       {
            GreenfootImage winner = new GreenfootImage("GameOverRedWins.png");  // This method attaches GameOverRedWins.png to the variable winner.
            gameOver.setImage(winner);                                          // This method sets the image variable winner to the variable gameOver.
            addObject(new RedWins(),getWidth()/2, getHeight()/2);               // Creates an object of class RedWins in the middle of the screen
            GameOver();                                                         // This statement calls the GameOver method
       }
       else
       {
           NewBall();                                              // This statment calls the NewBall method
           g++;                                                    // Increments the variable g
           directionPicker = Greenfoot.getRandomNumber(3);         // Assigns a random number to the variable directionPicker
       }
    }
   
    public void NewBall() // This method creates a new ball in the center of the world when the ball enters a goal.
    {
       pongBall = new PongBall(); 
       if(g % 2 == 0)                                               // This if statement changes the vSpeed of the ball depending on the value of the variable g
       {
           pongBall.vSpeed = Greenfoot.getRandomNumber(10) + 5;
       }
       addObject(pongBall,getWidth()/2, getHeight()/2);             // Creates an object of class PongBall in the middle of the screen                    
    }
   
    public void GameOver() // This method stops the Greenfoot scenario.
    {
       if(sound1.isPlaying())                   // This statement checks to see if sound1 is playing
       {
           sound1.stop();                       // If sound1 is playing, then it will stop
       }
       else if(sound2.isPlaying())              // This statement checks to see if sound2 is playing
       {
           sound2.stop();                       // If sound2 is playing, then it will stop
       }
       else if(sound3.isPlaying())              // This statement checks to see if sound3 is playing
       {
           sound3.stop();                       // If sound3 is playing, then it will stop
       }
       else if(sound4.isPlaying())              // This statement checks to see if sound4 is playing
       {
           sound4.stop();                       // If sound4 is playing, then it will stop
       }
       Greenfoot.playSound("GAME_OVER.wav");    // Plays the sound file Game_Over.wav
       Greenfoot.stop();                        // Stops the greenfoot scenario
    }
danpost danpost

2017/5/12

#
I do not see how you can possibly have a paddle that is both player and ai controlled at the same time. I can see how the red paddle would always be ai controlled (without ever being player controlled) because the 'red' field is always zero when you create a PongCourt world.
There are more replies on the next page.
1
2
3