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

2014/12/6

Question!

inkpenKingpin inkpenKingpin

2014/12/6

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List; // allows creation of lists using Java API

/**
 * Write a description of class Pinball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pinball extends Actor
{
    public static final int EAST = 0;
    public static final int NORTH = 1;
    public static final int WEST = 2;
    public static final int SOUTH = 3;
    
    private int direction;
    private int lives;
    private int score;
    private int pinballId;

    public Pinball(int initialDirection, int initialLives, int id)
    {
        pinballId = id;
        setDirection(initialDirection);
        setRotation(0);
        lives = initialLives;
        score = 0;
    }
    
    /**
     * Act - do whatever the Pinball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        updateLives(1);
        
        if(facingWall())
        {
            int wallCost = getWallCost();
            updateLives(wallCost);
            if(lives <= 0)
            {
                removeBall();
                return;
            }
            turnRight();
        }
        else if (facingBumper())
        {
            int bumperCost = getBumperCost();
            updateLives(bumperCost);
            if(lives <= 0)
            {
                removeBall();
                return;
            }
            int bumperValue = getBumperValue();
            updateScore(bumperValue);
            turnRight();
        }
        else
        {
            move();
            updateLives(1);
            if(lives <= 0)
            {
                removeBall();
                return;
            }
        }  
    }
    
    /**
     * Updates current lives of pinballs.
     */
    private void updateLives(int cost)
    {
        lives = lives - cost;
    }
    
    /**
     * Updates score for pinballs.
     */
    private void updateScore(int value)
    {
        score = score + value;
    }
    
    /**
     * Removes ball from world after it has lost all its lives.
     */
    private void removeBall()
    {
        PBWorld pinworld = (PBWorld)getWorld();
        pinworld.totalScore(score);
        reportValue();
        getWorld().removeObject(this);
    }
    
    /**
     * Reports values for pinballs that have lost all their lives in a popup window.
     */
    private void reportValue()
    {
        System.out.println(score);
    }
    
    /**
     * Moves the object according to parameters. 
     * If facing Wall or Bumper, the Pinball does not move.
     */
    public void move()
    {
        if(facingWall() || facingBumper())
        {
            return;
        }
        
        switch(direction)
        {
            case SOUTH :
                setLocation(getX(), getY() + 1);
                break;
            case EAST :
                setLocation(getX() + 1, getY());
                break;
            case NORTH :
                setLocation(getX(), getY() - 1);
                break;
            case WEST :
                setLocation(getX() - 1, getY());
                break;
        }
    }
    
    /**
     * Determines if facing a wall.
     */
    public boolean facingWall()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        switch(direction)
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        
        // test for wall
        List wall = getWorld().getObjectsAt(x, y, Wall.class);
        if (wall.size() > 0)
        {
            return true;
        }
        return false;
    }
    
    /**
     * Returns cost of hitting a wall.
     */
    private int getWallCost()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        switch(direction)
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        
        List wall = getWorld().getObjectsAt(x, y, Wall.class);
        if(wall.size() > 0)
        {
            Wall wallObj = (Wall)wall.get(0);
            return wallObj.getCost();
        }
        return 0;
    }
    
    /**
     * Determines if facing a bumper.
     */
    public boolean facingBumper()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        switch(direction)
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        
        // test for bumper
        List bumper = getWorld().getObjectsAt(x, y, Bumper.class);
        if(bumper.size() > 0)
        {
            return true;
        }
        return false;
        
    }
    
    /**
     * Returns cost of hitting bumper.
     */
    private int getBumperCost()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        switch(direction)
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        
        List bumper = getWorld().getObjectsAt(x, y, Bumper.class);
        if(bumper.size() > 0)
        {
            Bumper bumperObj = (Bumper)bumper.get(0);
            return bumperObj.getCost();
        }
        return 0;
    }
    
    /**
     * Returns value of the bumper.
     */
    private int getBumperValue()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        
        switch(direction)
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        
        List bumper = getWorld().getObjectsAt(x, y, Bumper.class);
        if(bumper.size() > 0)
        {
            Bumper bumperObj = (Bumper)bumper.get(0);
            bumperObj.getValue();
        }
        return 0;
    }
    
    /**
     * Turns to the left.
     */
    public void turnLeft()
    {
        switch(direction)
        {
            case SOUTH :
                setDirection(EAST);
                setImage("gold-ball3.png");
                setRotation(90);
                break;
            case EAST :
                setDirection(NORTH);
                setImage("Gold-ball2.png");
                setRotation(0);
                break;
            case NORTH :
                setDirection(WEST);
                setImage("gold-ball5.png");
                setRotation(270);
                break;
            case WEST :
                setDirection(SOUTH);
                setImage("gold-ball4.png");
                setRotation(180);
                break;
        }
    }
    
    /**
     * Turns to the right.
     */
    public void turnRight()
    {
        turnLeft();
        turnLeft();
        turnLeft();
    }
    
    public void setDirection(int direction)
    {
        setRotation(1);
    }
}
My question is, in the getWallCost, getBumperValue, and getBumperCost methods, there is a couple methods (getCost, getValue) that need to be called for those methods to compile properly. I missed the class where this part was explained. Each Bumper has a cost and a point value (both 1), and the walls around the world have a cost (0). Does the getCost method need to be in the Bumper and Wall classes? I know that the getValue method has to be in the Bumper class. Any help would be appreciated.
danpost danpost

2014/12/6

#
It would not hurt to have a 'get' method for each of those fields in each of the classes. But, if a method is not being called, then the method is not necessary. Obviously, your Bumper class needs both methods (getValue and getCost); and, as coded, your Wall class will need one of them (getCost). But, if all the walls have cost zero, then neither the cost nor the getCost method are needed in the Wall class, as there is no need to check for its cost.
inkpenKingpin inkpenKingpin

2014/12/6

#
I made a method for each of them like you suggested "bCost" wCost" and "bValue" each as integers, bCost and bValue returned 1, and wCost returned 0. I'll cut out a piece of the code for an example.
public class Wall extends Actor
{   
    private int wallCost = 0;
    
    public int wCost()
    {
        return wallCost;
    }

^ Wall class
public class Bumper extends Actor
{
    public int bumperCost = 1;
    public int bumperValue = 1;
    
    public int bCost()
    {
        return bumperCost;
    }
    
    public int bValue()
    {
        return bumperValue;
    }
^Bumper Then I replaced the getCost method with the methods I created, and I got the desired output for my program. Thank you danpost!
You need to login to post a reply.