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

2017/1/24

Score

1
2
puddle148 puddle148

2017/1/24

#
How do I code for score if (score 1 > score 2) score 1 wins. how do I go about this?
danpost danpost

2017/1/24

#
Not enough info. What classes and fields are all involved with them (score 1 and 2) and where are you trying to access them from?
puddle148 puddle148

2017/1/24

#
I am making a card game and have the player score of cards and dealer score of cards. I am able to get another card for myself but want the dealer to do it automatically if under 17. Also want to say if player score greater than dealer score player gets "money".MyWorld class player class and card class.
danpost danpost

2017/1/24

#
danpost wrote...
Not enough info. What classes and fields are all involved with them (score 1 and 2) and where are you trying to access them from?
What I meant was that what was needed was the code of the entire class(es) where the score fields are located and the act method of your MyWorld class (including all methods call from the act method).
puddle148 puddle148

2017/1/24

#
while (dealerScore <17)
        {
            int l = Greenfoot.getRandomNumber(4);
            int p = Greenfoot.getRandomNumber(13);
            addObject(Crd[l][p], 470+i*120, 150);  
            i++;
        }
 public static Counter dealerScore = new Counter();
if (MyWorld.playerScore = 21)
        {
            MyWorld.playerScore.setValue(MyWorld.playerScore.getValue() + 1);
        }
        if (MyWorld.playerScore == MyWorld.dealerScore )
        {
            MyWorld.playerScore.setValue(MyWorld.playerScore.getValue() + 1);
        }
        if (MyWorld.playerScore >> MyWorld.dealerScore && MyWorld.dealerScore <21)
        {
            MyWorld.playerScore.setValue(MyWorld.playerScore.getValue() + 1);
        }

    }    
 for (int i = 0; i <= 1; i++)
        {
            int l = Greenfoot.getRandomNumber(4);
            int p = Greenfoot.getRandomNumber(13);
            addObject(Crd[l][p], 230+i*120, 430);
            Score += cv;
            playerScore.setValue(playerScore.getValue() + cv);
        }
public int cardValue;
int cv
cardValue = cv;
danpost danpost

2017/1/24

#
There are multiple problems with the code given. Starting at the top, using a 'while' loop for the dealer to draw cards will have the dealers hand instantly at a "over" or "hold" state (all cards will be added at one time without any time in between). Next, having the 'dealerScore' Counter object in a 'static' field is questionable. Continuing on, the same can be said about the 'playerScore' Counter object. Moving on, the 'for' loop is inconsequential as looping one time has everything executed once which will happen without the loop. Now, your last line 6 refers to a 'cv' field which should be the value of the card added into the world on the previous line; however, 'cv' is not set to the value of that card. Will need to see your Counter class code to proceed to fix your issue.
puddle148 puddle148

2017/1/24

#
 if (dealerScore <17)
        {
            int l = Greenfoot.getRandomNumber(4);
            int p = Greenfoot.getRandomNumber(13);
            addObject(Crd[l][p], 470+u*120, 150);  
            u++;
        }
I changed them from static also.
int[] cardvalues = {2,3,4,5,6,7,8,9,10,10,10,10,11};
  public Counter dealerScore = new Counter();
    public Counter dealerMoney = new Counter();
    public Counter playerMoney = new Counter();
    public Counter playerScore = new Counter();
This is all i have for counters nothing in the class.
danpost danpost

2017/1/24

#
That is good that you changed them from static. Changing the 'while' to an 'if' is not enough, however, as now only one extra card could possibly be added to the dealers hand (you can leave it as a 'while', for now; and deal with timing later). I see you are not allowing the value '1' for an ace. I guess that would make things easier in the long run; though, I would think you would want the project to emulate the way 21 is played in real life. You still have not included the code within your Counter class. Please post the entire class code -- not just bits and pieces of it as you have been doing above.
puddle148 puddle148

2017/1/24

#
/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
puddle148 puddle148

2017/1/24

#
i havenot done anytihing with this class this is how it imported. I would also like to heve the option to do 1 or 11 for the ace but didnt know how to approach it with the score and all.
danpost danpost

2017/1/24

#
puddle148 wrote...
i havenot done anytihing with this class this is how it imported. I would also like to heve the option to do 1 or 11 for the ace but didnt know how to approach it with the score and all.
That is okay. At line 83, you will see a 'getValue' method which you can use on 'playerScore' and 'dealerScore' so you can compare their values with each other.
puddle148 puddle148

2017/1/24

#
I don't understand how I would go about doing that.
danpost danpost

2017/1/24

#
As far as the '1' and '11' for the ace, I think you can pretty much ignore it as far as the score itself being possibly two different values. When a player or the dealer draws a card, just take the higher score that is less than or equal to 21, if possible. You will be checking for over 21 at those particular times anyways. You may have to count number of aces in each hand.
danpost danpost

2017/1/24

#
puddle148 wrote...
I don't understand how I would go about doing that.
if (playerScore.getValue() > dealerScore.getValue())
puddle148 puddle148

2017/1/24

#
danpost whenever i take the static context off of the counters the PlayerScore and dealerScore get errors saying non static cant be referenced from static context.
There are more replies on the next page.
1
2