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

2014/12/12

Addition game help

Yukine Yukine

2014/12/12

#
I'm trying to make a game where when I press the play button two random numbers will generate and the user has to put in the correct number. If they get the answer correct the image (it's a box) turns green and the counter goes up one point. If they get the answer wrong the counter is cleared and the box is clear again but the numbers are still the same.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;

/**
 * Write a description of class bathWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bathWorld extends World
{
    String num = ""+ Greenfoot.getRandomNumber(10);
    String num2 = ""+ Greenfoot.getRandomNumber(10);
    /**
     * Constructor for objects of class bathWorld.
     * 
     */
    public bathWorld()
    {    
        //string num = ""+ Greenfoot.getRandomNumber(10);
        //showText(num,100,100);
        super(600, 400, 1); 
        showText(num,100,100);
        showText(num2,200,200);
        

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        alli alli = new alli();
        addObject(alli, 276, 356);
    }
 
that's my world code and here's what i have for my play button
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.swing.JOptionPane;

/**
 * Write a description of class alli here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class alli extends Actor
{
    int num1 = Greenfoot.getRandomNumber(10);
    int num2 = Greenfoot.getRandomNumber(10);
    int sum = num1 + num2;
    /**
     * Act - do whatever the alli 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))
        {
            JOptionPane.showInputDialog("sum");
        }
    }    
}
I when I click on alli nothing happens but the numbers are still randomly generated. I'm also a bit confused on the counter and how the image will know when the answer is wrong or right.
danpost danpost

2014/12/12

#
When the user presses enter after entering a value in the JOptionPane inputDialog box, the String entered is returned. You can hold it in a field and use it to compare with the real answer:
String guess = JOptionPane.showInputDialog("sum");
You may have to (1) make sure that something was entered (2) make sure that something was strictly numbers and then (3) compare it to the real answer.
Yukine Yukine

2014/12/16

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.*;
import java.lang. *;
import java.lang.Object;
import javax.swing.JOptionPane;

/**
 * Write a description of class SpaceWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
    playButton playButton = new playButton();
    Box Box = new Box();
    Box Box2 = new Box();
    answerbox answerbox = new answerbox();
    public int i = Greenfoot.getRandomNumber(10);
    public int h = Greenfoot.getRandomNumber(10);
    public int totalValue = i + h;

    public String inputStr;
    public int buttonNbr;
    public int RealAnswer;
    public int YourAnswer = 4;
    counter counter = new counter();
    public int Answer;
    public int Score = 0;

    public void act()
    {
        if(Greenfoot.mouseClicked(answerbox))
        {
            inputStr = JOptionPane.showInputDialog( "place your answer here");
            String msg = "YourAnswer" + inputStr + "\nRealAnswer:" + totalValue 
                + "\nPress OK or Enter key to exist.";
            JOptionPane.showMessageDialog(null, msg);
            Answer = Integer.parseInt(inputStr);
        }
        if(Greenfoot.mouseClicked(playButton))
        {
            i = Greenfoot.getRandomNumber(10);
            h = Greenfoot.getRandomNumber(10);
            Box.setImage(new GreenfootImage("" +i, 100, Color.WHITE, Color.BLACK));
            Box2.setImage(new GreenfootImage("" +h, 100, Color.WHITE, Color.BLACK));
            totalValue = i + h;
        }
        if(totalValue == Answer)
        {
            Score ++;
            showText("" + Score, 100, 100);
            totalValue = 0;
        }
        if(Score == 5)
        {
            Score =-1;
        }
    }

    /**
     * Constructor for objects of class SpaceWorld.
     * 
     */
    public SpaceWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        addObject(answerbox, 543, 231);
        
        equals equals = new equals();
        addObject(equals, 466, 239);

        addObject(Box, 100, 200);
        
        additionSign additionsign = new additionSign();
        addObject(additionsign, 300, 250);
        
        addObject(Box2, 379, 238);
        
        addObject(playButton, 297, 361);
       
        counter counter = new counter();
        addObject(counter, 85, 52);
    }
}
How do I make it where the answerbox turns green if it's correct and the Score resets itself if the user puts in the wrong number?
danpost danpost

2014/12/16

#
You will need to post the code to your answerBox class. You will need to be more specific as to what you mean by 'the Score resets itself'. What exactly is involved with that?
Yukine Yukine

2014/12/16

#
The Score is the counter and when you get an answer correct the score will go up by one. However if the user get the answer incorrect it's supposed to restart the counter, but right now the counter will stay the same if the user get the wrong answer. I know the code has to be along the lines of If the total value does not equal the answer the score stays the same but i just don't know how to put that into code
danpost danpost

2014/12/16

#
If the score is to stay the same, then don't put anything in code. The score will not change unless you tell it to. Line 52 above will only increment score if the condition on line 50 is true (a correct answer was given). You do not have to do anything else to keep the score the same on a wrong answer.
Yukine Yukine

2014/12/16

#
sorry i didnt mean stays the same, i meant that the score = 0 if the input is wrong
if(totalValue <= 5 && totalValue != Answer)
        {
            Score = 0;
        }
i put this in the world class but when i put that or if i i put in the if condition: totalValue != Answer then the score does not equal zero if you mess up and then the counter will only go up to one.
danpost danpost

2014/12/16

#
So, you want to user to answer 5 in a row correctly to win. I was wondering if that might be the case; but you kept saying that the score would stay the same. I would think if you changed line 55 above to this:
} else Score = 0;
that it would do what you wanted.
Yukine Yukine

2014/12/16

#
no ): the counter will just stay at one and still won't change to zero (thank you for all the help so far too btw!!)
if(totalValue == Answer)
        {
            Score ++;
            showText("" + Score, 100, 100);
            totalValue = 0;
        }
        else if("YourAnswer" != "RealAnswer")
        {
            Score = 0;
        }
i also tried that but it didn't work.
danpost danpost

2014/12/16

#
Oh, we still have to show the updated value. Try this:
if (totalValue == Answer)
{
    Score++;
    totalValue = 0;
} else Score == 0;
showText(""+Score, 100, 100);
Yukine Yukine

2014/12/16

#
No. When I do that the Score is just zero. When i take away the else statement though the counter will count as normal.
danpost danpost

2014/12/16

#
Move lines 50 through 55 to right after line 40 (inside the 'if (Greenfoot.mouseClicked(answerbox))' block) You only want to check this when an answer is given.
You need to login to post a reply.