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/12

#
I don't understand why the red field is always zero. This code should change it.
**
     * Act - do whatever the RedPick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))               // This statement checks to see if this object has been clicked on by the mouse
        {
            PongCourt pongCourt = new PongCourt();
            Greenfoot.setWorld(pongCourt);        // This statement sets the world to PongCourt
            pongCourt.red = 1;                          // This statement assigns the variable red the value of 1, which is true
            pongCourt.blue = 1;                          // This statement assigns the variable blue the value of 0, which is false
            // redPaddle.playerRedControlled = true;             // This statement assigns the variable playerRedControlled the value of true
            // blue.playerBlueControlled = false;          // This statement assigns the variable playerBlueControlled the value of false
        }
    }    
danpost danpost

2017/5/13

#
I think line 12 should set pongCount.blue to '0' instead of one.
Kronos Kronos

2017/5/13

#
You are right. But I still don't understand why the red field is not being changed. And the red paddle is entirely ai controlled. I was wrong when i said it was player controlled.
danpost danpost

2017/5/13

#
Kronos wrote...
I still don't understand why the red field is not being changed. And the red paddle is entirely ai controlled. I was wrong when i said it was player controlled.
It is because you create the world, AND the paddles, before you change the value of the field.
Kronos Kronos

2017/5/13

#
How do i fix it?
danpost danpost

2017/5/13

#
I was thinking a static field or two in the world where the RedPick object is located in could be used to store the pick. Then, the PongCourt world can use its value to create the proper paddles:
// in pick world
public static int red;
public static int blue;

// in PongCourt constructor before any paddle code
red = PickWorld.red;
blue = PickWorld.blue;
Replace 'PickWorld' with the appropriate class name. Lines 11 and 12 in the RedPick class would then be:
PickWorld.red = 1;
PickWorld.blue = 0;
Those two lines should also be moved up to before where you create a PongWorld object.
Kronos Kronos

2017/5/13

#
danpost wrote...
I was thinking a static field or two in the world where the RedPick object is located in could be used to store the pick. Then, the PongCourt world can use its value to create the proper paddles:
// in pick world
public static int red;
public static int blue;

// in PongCourt constructor before any paddle code
red = PickWorld.red;
blue = PickWorld.blue;
Replace 'PickWorld' with the appropriate class name. Lines 11 and 12 in the RedPick class would then be:
PickWorld.red = 1;
PickWorld.blue = 0;
Those two lines should also be moved up to before where you create a PongWorld object.
Thank you so much, it now works. However, i tried doing the same thing in my blue class and it has no ai control. Blue Paddle class code.
boolean playerBlueControlled;
    /**
     * Act - do whatever the Blue 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.
    }    
    
    /**
     * Check whether a control key on the keyboard has been pressed.
     * If it has, react accordingly.
     */
    public void checkKeypress()
    {
        Actor pongball = getWorld().getObjects(PongBall.class).get(0);
        if(pongball.getX() < 490)
        {
            if(playerBlueControlled = true)
            {
                if (Greenfoot.isKeyDown("w")) // Pressing the w key will move the blue paddle up.
                {       
                    setLocation(getX(),getY() - 10);
                }
                if (Greenfoot.isKeyDown("s")) // Pressing the s key will move the blue paddle down.
                {
                    setLocation(getX(),getY() + 10);
                }
            }
        }
        else if(playerBlueControlled = false)
        {
             if(getY() > pongball.getY())
             {
                 if(Greenfoot.getRandomNumber(5) != 0)
                 {
                     setLocation(getX(),getY() - 10); 
                 }
                 else 
                 {
                     setLocation(getX(),getY() - 5); 
                 }
             }
             else if(getY() < pongball.getY())
             {
                 if(Greenfoot.getRandomNumber(5) != 0)
                 {
                     setLocation(getX(),getY() + 10); 
                 }
                 else 
                 {
                     setLocation(getX(),getY() + 5); 
                 }
             }
        }
        BorderCheck();    // This statement calls the BorderCheck method
    }
Red Paddle class 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() > 590)
        {
             if(playerRedControlled == true)
             {                
                 if (Greenfoot.isKeyDown("up")) // Pressing the up arrow key will move the red paddle up.
                 {       
                     setLocation(getX(),getY() - 10);
                 }
                 else if (Greenfoot.isKeyDown("down")) // Pressing the down arrow key will move the red paddle down. 
                 {
                     setLocation(getX(),getY() + 10);
                 }
             }
             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/13

#
It is not the Red and Blue class codes that are screwing things up. Need to see pick world codes and the pick classes.
Kronos Kronos

2017/5/13

#
Here they are. PlayerPick:
public static int red;
    public static int blue;
     /**
     * Constructor for objects of class PlayerPick.
     * The screen where the user will decide which paddle they want to use.
     */
    public PlayerPick() 
    {    
        super(1080, 600, 1); // Create a new world with 1100x600 cells with a cell size of 1x1 pixels.
        BackButton backbutton = new BackButton(); 
        addObject(backbutton,150,560); //Creates a object of class BackButton
        BlueRedPlayerPick bluepick = new BluePick();
        addObject(bluepick,420,530); //Creates a object of class BluePick
        BlueRedPlayerPick redpick = new RedPick();
        addObject(redpick,675,530); //Creates a object of class RedPick
        Start start2 = new Start2();
        addObject(start2,975,560); //Creates a object of class Start2
    }
RedPick:
/**
     * Act - do whatever the RedPick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))               // This statement checks to see if this object has been clicked on by the mouse
        {
            PlayerPick playerPick = new PlayerPick();
            playerPick.red = 1;                          // This statement assigns the variable red the value of 1, which is true
            playerPick.blue = 0;                         // This statement assigns the variable blue the value of 0, which is false
            PongCourt pongCourt = new PongCourt();
            Greenfoot.setWorld(pongCourt);               // This statement sets the world to PongCourt
        }
    }    
BluePick:
/**
     * Act - do whatever the BluePick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))               // This statement checks to see if this object has been clicked on by the mouse
        {
            PlayerPick playerPick = new PlayerPick();
            playerPick.red = 0;                          // This statement assigns the variable red the value of 0, which is false
            playerPick.blue = 1;                         // This statement assigns the variable blue the value of 1, which is true
            PongCourt pongCourt = new PongCourt();
            Greenfoot.setWorld(pongCourt);               // This statement sets the world to PongCourt
        }
    }    
danpost danpost

2017/5/13

#
Remove line 9 from both the RedPick and BluePick classes.
Kronos Kronos

2017/5/15

#
It causes a syntax error.
 'cannot find symbol - variable playerPick'
and changing line 9 to say PlayerPick instead of playerPick, does not change anything. The blue paddle still has no ai control.
danpost danpost

2017/5/15

#
Sorry. remove line 9 and change 'playerPick' on lines 10 and 11 to 'PlayerPick'.
Kronos Kronos

2017/5/15

#
Kronos wrote...
It causes a syntax error.
 'cannot find symbol - variable playerPick'
Changing line 9 to say PlayerPick instead of playerPick, does not change anything. The blue paddle still has no ai control.
danpost wrote...
Sorry. remove line 9 and change 'playerPick' on lines 10 and 11 to 'PlayerPick'.
I already tried that.
danpost danpost

2017/5/15

#
Kronos wrote...
I already tried that.
Please show the code you tried so that we can check to see if you had properly made the proposed changes. That is the act method of the RedPick and BluePick classes.
Kronos Kronos

2017/5/15

#
RedPick:
/**
     * Act - do whatever the RedPick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))               // This statement checks to see if this object has been clicked on by the mouse
        {
            //PlayerPick playerPick = new PlayerPick();
            PlayerPick.red = 1;                          // This statement assigns the variable red the value of 1, which is true
            PlayerPick.blue = 0;                         // This statement assigns the variable blue the value of 0, which is false
            PongCourt pongCourt = new PongCourt();
            Greenfoot.setWorld(pongCourt);               // This statement sets the world to PongCourt
        }
    }    
BluePick:
/**
     * Act - do whatever the BluePick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this))               // This statement checks to see if this object has been clicked on by the mouse
        {
            //PlayerPick playerPick = new PlayerPick();
            PlayerPick.red = 0;                          // This statement assigns the variable red the value of 0, which is false
            PlayerPick.blue = 1;                         // This statement assigns the variable blue the value of 1, which is true
            PongCourt pongCourt = new PongCourt();
            Greenfoot.setWorld(pongCourt);               // This statement sets the world to PongCourt
        }
    }    
There are more replies on the next page.
1
2
3