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

2013/11/1

TicTacToe Game

MontclairState MontclairState

2013/11/1

#
Hello, I am trying to fix some bugs that are happening in my game and was wondering if there is anyway to only change the ball once; so you can't change it again if it was already changed by player1 or player2. ----------------------------The code for the Player is----------------------------------------------- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { enum PlayerMode {PLAYER1, PLAYER2 }; PlayerMode mode; public Player(){ mode = PlayerMode.PLAYER1; setImage("ant-with-food.png"); } /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. if (Greenfoot.mouseMoved(null)){ MouseInfo mouse = Greenfoot.getMouseInfo(); setLocation(mouse.getX(), mouse.getY()); } if (Greenfoot.mouseClicked(null)){ GameBall ball = (GameBall)getOneIntersectingObject(GameBall.class); if(ball!=null){ if (mode == PlayerMode.PLAYER1 ){ ball.setSteel(); setPlayer2(); } else if (mode == PlayerMode.PLAYER2){ ball.setGold(); setPlayer1(); } } } } public void setPlayer1(){ mode = PlayerMode.PLAYER1; setImage("ant-with-food.png"); } public void setPlayer2(){ mode = PlayerMode.PLAYER2; setImage("ant.png"); } } -----------------------------------AND THE CODE FOR THE GameBall----------------------------- import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class GameBall here. * * @author (your name) * @version (a version number or a date) */ public class GameBall extends Actor { enum BallState { UNCLICKED, GOLD, STEEL }; BallState state = BallState.UNCLICKED; GameBall(){ setImage("cell.jpg"); } /** * Act - do whatever the GameBall wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } public void setGold(){ setImage("gold-ball.png"); state=BallState.GOLD; } public void setSteel(){ setImage("steel-ball.png"); state = BallState.STEEL; } public void reset(){ setImage("cell.jpg"); state = BallState.UNCLICKED; } public boolean isUnclicked(){ return (state == BallState.UNCLICKED); } public boolean isGold(){ return (state==BallState.GOLD); } public boolean isSteel(){ return (state==BallState.STEEL); } public void setWinIfGold(){ if (state == BallState.GOLD){ setImage("ball.png"); } } public void setWinIfSteel(){ if ( state == BallState.STEEL){ setImage("ball.png"); } } }
davmac davmac

2013/11/1

#
Just check that the ball state is unclicked before setting it to either steel or gold. I.e. where you have:
 if(ball!=null){
... change it to also check the the ball is unclicked. (Please use 'code' tags around code in your posts).
MontclairState MontclairState

2013/11/1

#
Oh sorry about that but how do I have it check to see if ball is unclicked? This is the code where it works...
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Player  extends Actor
{
    enum PlayerMode {PLAYER1, PLAYER2 };
    PlayerMode mode;
    public Player(){
        mode = PlayerMode.PLAYER1;
        setImage("ant-with-food.png");
    }
        public void act() 
    {
        // Add your action code here.
        if (Greenfoot.mouseMoved(null)){
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
        if (Greenfoot.mouseClicked(null)){
            GameBall ball = (GameBall)getOneIntersectingObject(GameBall.class);
            if(ball!=null){
                if (mode == PlayerMode.PLAYER1 ){
                    ball.setSteel();
                    setPlayer2();
                }
                else if (mode == PlayerMode.PLAYER2){
                    ball.setGold();
                    setPlayer1();
                }
            }
        }
    }
        public void setPlayer1(){
        mode = PlayerMode.PLAYER1;
        setImage("ant-with-food.png");
    }
    public void setPlayer2(){
        mode = PlayerMode.PLAYER2;
        setImage("ant.png");
    }
}
I tried playing around with the code and came up with this but now the ball doesn't change at all.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Player extends Actor
{
    enum PlayerMode { PLAYER1, PLAYER2};
    PlayerMode mode;
    enum BallState { UNCLICKED, GOLD, STEEL };
    BallState state;
    public Player(){
        mode = PlayerMode.PLAYER1;
        setImage("ant-with-food.png");
    }   
     public void setPlayer1(){
        mode = PlayerMode.PLAYER1;
        setImage("ant-with-food.png");
    }
    public void setPlayer2(){
        mode = PlayerMode.PLAYER2;
        setImage("ant.png");
    }
    public void act() 
    {
        if(Greenfoot.mouseMoved(null)) {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            setLocation(mouse.getX(), mouse.getY());
        }
        if(Greenfoot.mouseClicked(null)) {
            GameBall ball = (GameBall) getOneIntersectingObject(GameBall.class);
            if(state == BallState.UNCLICKED){
                if (mode == PlayerMode.PLAYER1){
                    ball.setSteel();
                    setPlayer2();
                }
                else if (mode == PlayerMode.PLAYER2){
                    ball.setGold();
                    setPlayer1();
                }
            }
        }
    } 
}
MontclairState MontclairState

2013/11/1

#
Just re-posting the GameBall coding
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class GameBall extends Actor
{
    enum BallState { UNCLICKED, GOLD, STEEL };
    BallState state = BallState.UNCLICKED;
    public GameBall() {
        setImage("cell.jpg");
    }
    public void act() 
    {
         
    }
    public void setGold(){
        setImage("gold-ball.png");
        state = BallState.GOLD;
    }
    public void setSteel() {
        setImage("steel-ball.png");
        state = BallState.STEEL;
    }
    public void reset(){
        setImage("cell.jpg");
        state = BallState.UNCLICKED;
    }
    public boolean isUnclicked() {
        return (state == BallState.UNCLICKED);
    }
    public boolean isGold() {
        return (state == BallState.GOLD);
    }
    public boolean isSteel() {
        return (state == BallState.STEEL);
    }
    public void setWinIfGold(){
        if (state == BallState.GOLD){
            setImage("ball.png");
        }
    }
    public void setWinIfSteel(){
        if (state == BallState.STEEL){
            setImage("ball.png");
        }
    }
}
Am I able to use the booleans I set up in the GameBall code in the Player code?
danpost danpost

2013/11/1

#
At line 29 in the Player class, you need to make sure, first, that there is a ball where clicked ('ball' cannot be 'null') and, if there was a ball object clicked on, that the state of the ball is 'unclicked' ('ball.state' must be 'BallState.UNCLICKED').
MontclairState MontclairState

2013/11/1

#
Would you be able to give me an example? Cause I'm not sure how to tell it to say ball != null and for ball == BallState.UNCLICKED. Sometimes it says something about static something and I can't do it.
MontclairState MontclairState

2013/11/1

#
I tried changing it to this but still doesn't work.
 if(Greenfoot.mouseClicked(null)) {
            GameBall ball = (GameBall) getOneIntersectingObject(GameBall.class);
            if(ball != null & state == BallState.UNCLICKED){
                if (mode == PlayerMode.PLAYER1){
                    ball.setSteel();
                    setPlayer2();
                }
                else if (mode == PlayerMode.PLAYER2){
                    ball.setGold();
                    setPlayer1();
                }
            }
        }
MontclairState MontclairState

2013/11/1

#
if(Greenfoot.mouseClicked(null)) {
            GameBall ball = (GameBall) getOneIntersectingObject(GameBall.class);
            if(ball != null & ball == GameBall.isUnclicked()){
                if (mode == PlayerMode.PLAYER1){
                    ball.setSteel();
                    setPlayer2();
                }
                else if (mode == PlayerMode.PLAYER2){
                    ball.setGold();
                    setPlayer1();
                }
            }
        }
If I try this it says "non-static method isUnclicked() cannot be referenced from a static context". Was trying to emulate my game check to see if someone won which is this code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Board here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Board extends World
{
    GameBall cell_1, cell_2, cell_3, cell_4, cell_5, cell_6, cell_7, cell_8, cell_9;
    Player player;
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(3, 3, 60); 
        setPaintOrder(Player.class, GameBall.class);
        setBackground("cell.jpg");
        
        player = new Player();
        addObject (player, 0, 0);
        
        cell_1 = new GameBall();
        addObject(cell_1, 0, 0);
        
        cell_2 = new GameBall();
        addObject(cell_2, 1, 0);
        
        cell_3 = new GameBall();
        addObject(cell_3, 2, 0);
        
        cell_4 = new GameBall();
        addObject(cell_4, 0, 1);
        
        cell_5 = new GameBall();
        addObject(cell_5, 1, 1);
        
        cell_6 = new GameBall();
        addObject(cell_6, 2, 1);
        
        cell_7 = new GameBall();
        addObject(cell_7, 0, 2);
        
        cell_8 = new GameBall();
        addObject(cell_8, 1, 2);
        
        cell_9 = new GameBall();
        addObject(cell_9, 2, 2);
    }
    public void act(){
                    if (
                    (cell_1.isGold() && cell_2.isGold() && cell_3.isGold())
            ){
                cell_1.setWinIfGold(); cell_2.setWinIfGold(); cell_3.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
                if (
                    (cell_4.isGold() && cell_5.isGold() && cell_6.isGold())
            ){
                cell_4.setWinIfGold(); cell_5.setWinIfGold(); cell_6.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
                if (
                    (cell_7.isGold() && cell_8.isGold() && cell_9.isGold())
            ){
                cell_7.setWinIfGold(); cell_8.setWinIfGold(); cell_9.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
        
                if (
                    (cell_1.isGold() && cell_4.isGold() && cell_7.isGold())
            ){
                cell_1.setWinIfGold(); cell_4.setWinIfGold(); cell_7.setWinIfGold();
                Greenfoot.stop();
            }
        
                if (
                    (cell_2.isGold() && cell_5.isGold() && cell_8.isGold())
            ){
                cell_2.setWinIfGold(); cell_5.setWinIfGold();cell_8.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
        
                if (
                    (cell_3.isGold() && cell_6.isGold() && cell_9.isGold())
            ){
                cell_3.setWinIfGold();cell_6.setWinIfGold();cell_9.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
        
                if (
                   (cell_1.isGold() && cell_5.isGold() && cell_9.isGold())
                    ){
                cell_1.setWinIfGold(); cell_5.setWinIfGold();cell_9.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
        
      
                if (
                    (cell_3.isGold() && cell_5.isGold() && cell_7.isGold())
            ){
                cell_3.setWinIfGold(); cell_5.setWinIfGold(); cell_7.setWinIfGold();
                player.setPlayer2();
                Greenfoot.stop();
            }
            
           
                if (
                    (cell_1.isSteel() && cell_2.isSteel() && cell_3.isSteel())
            ){
                cell_1.setWinIfSteel(); cell_2.setWinIfSteel(); cell_3.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
                if (
                    (cell_4.isSteel() && cell_5.isSteel() && cell_6.isSteel())
            ){
                cell_4.setWinIfSteel(); cell_5.setWinIfSteel(); cell_6.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
                if (
                    (cell_7.isSteel() && cell_8.isSteel() && cell_9.isSteel())
            ){
                cell_7.setWinIfSteel(); cell_8.setWinIfSteel(); cell_9.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
        
                if (
                    (cell_1.isSteel() && cell_4.isSteel() && cell_7.isSteel())
            ){
                cell_1.setWinIfSteel(); cell_4.setWinIfSteel(); cell_7.setWinIfSteel();
                Greenfoot.stop();
            }
        
                if (
                    (cell_2.isSteel() && cell_5.isSteel() && cell_8.isSteel())
            ){
                cell_2.setWinIfSteel(); cell_5.setWinIfSteel();cell_8.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
        
                if (
                    (cell_3.isSteel() && cell_6.isSteel() && cell_9.isSteel())
            ){
                cell_3.setWinIfSteel();cell_6.setWinIfSteel();cell_9.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
        
                if (
                   (cell_1.isSteel() && cell_5.isSteel() && cell_9.isSteel())
                    ){
                cell_1.setWinIfSteel(); cell_5.setWinIfSteel();cell_9.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
        
      
                if (
                    (cell_3.isSteel() && cell_5.isSteel() && cell_7.isSteel())
            ){
                cell_3.setWinIfSteel(); cell_5.setWinIfSteel(); cell_7.setWinIfSteel();
                player.setPlayer1();
                Greenfoot.stop();
            }
        }
    }
danpost danpost

2013/11/1

#
I think it would have to coded as follows:
if (ball != null && ball.state == ball.BallState.UNCLICKED)
MontclairState MontclairState

2013/11/2

#
Thank you so much Danpost :D just had to change the ball in "ball.BallState.UNCLICKED" to "GameBall.BallState.UNCLICKED" but that solved the problem.
danpost danpost

2013/11/2

#
MontclairState wrote...
Thank you so much Danpost :D just had to change the ball in "ball.BallState.UNCLICKED" to "GameBall.BallState.UNCLICKED" but that solved the problem.
That makes sense, since BallState is a class of Enum type.
davmac davmac

2013/11/2

#
I think you could simplify it to:
    if (ball != null && ball.isUnclicked())  
MontclairState MontclairState

2013/11/2

#
You are right davmac that works as well. Thanks you both for your input :D
You need to login to post a reply.