-3

I created a simple method in my CardGames class that replicates a card game to play around with conditional statements. I call the method from a separate Player class because the player earns/loses points based on the card. I want the method to be able to change the player objects points variable.

What I want to have happen is when the playSimpleCardGame gets called by the Player object, the method changes the Player object's points.

But when I run it the points do not change. I've tried extending/implementing both classes (i.e. shooting in the dark). I also created an instance variable points in the CardGames class but then the Player object does not have points as a variable. What am I missing?

public class Player 
{
    private int points;

    public static void main(String[] args)
    {
        CardGames steve = new CardGames();
        System.out.println(steve.playSimpleCardGame("red"));
        System.out.println(steve.playSimpleCardGame("red"));
        System.out.println(steve.playSimpleCardGame("black"));
        System.out.println(steve.playSimpleCardGame("black"));
        System.out.println(steve.points);
    }

}

public class CardGames
{

    /*
     * Rules of this game: 
     * If you draw a red card, you get a point.  
     * If you draw a black card, you lose two points.
     */
    public int playSimpleCardGame(String color)
    {
        if (color.equalsIgnoreCase("red"))
            return points = points + 1;
        else 
            return points = points - 2;
    }
}

public class Player
{
    private int points;

    public Player(){
        points=0;
    }

    public static void main(String[] args)
    {
        CardGames game = new CardGames();
        Player steve = new Player();
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(steve.points);
    }

    public int getPoints() {
        return points;
    }

    public void addPoints(int p) {
        this.points = points + p;
    }

}

public class CardGames
{
    /*
     * Rules of this game: 
     * If you draw a red card, you get a point.  
     * If you draw a black card, you lose two points.
     */
    public int playSimpleCardGame(String color, Player player)
    {
        if (color.equalsIgnoreCase("red"))
        {
            player.addPoints(1);
            return player.getPoints();
        }
        else
        {
            player.addPoints(-2);
            return player.getPoints();
        }
    }
}
gunsnfloyd
  • 49
  • 4
  • You may have named two variables `points`, even on the same object, but they're not the same variable. – user2357112 Sep 11 '17 at 04:07
  • Your code would not compile, as there is no `points` in `CardGames`... Try moving the `points` from `Player` to `CardGames`... – Usagi Miyamoto Sep 11 '17 at 04:11
  • That makes the method run, but it does not give the Player object points, the points are still in CardGames. I would like to be able to call something line steve.points. – gunsnfloyd Sep 11 '17 at 04:17

1 Answers1

-1

Firstly, there is no need to extend CardGames class by the Player class. Secondly, even if you wish to do it, it will be a bad design. I won't go into the design part. The following code should answer your problem :

public class Player
{
    public Integer points;

    public Player(){
        points=0;
    }

    public static void main(String[] args)
    {
        CardGames game = new CardGames();
        Player steve = new Player();
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("red", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(game.playSimpleCardGame("black", steve));
        System.out.println(steve.points);
    }
}

public class CardGames
{
    /*
     * Rules of this game: 
     * If you draw a red card, you get a point.  
     * If you draw a black card, you lose two points.
     */
    public int playSimpleCardGame(String color, Player player)
    {
        if (color.equalsIgnoreCase("red"))
            return player.points = player.points + 1;
        else 
            return player.points = player.points - 2;
    }
}
Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
  • Thanks for the code. When I run it I get the message, "Cannot make a static reference to the non-static field points". Nothing is declared as static. Is the playSimpleCardGame making a static reference in its parameter? – gunsnfloyd Sep 11 '17 at 04:30
  • At which line you are getting this error @gunsnfloyd ? – Sahil Chhabra Sep 11 '17 at 04:37
  • I'm getting it at the System.out lines in Player. – gunsnfloyd Sep 11 '17 at 04:39
  • I'm getting the following message now: Exception in thread "main" java.lang.NullPointerException. Is it possible to use CardGames as an interface for player? The goal of this exercise is to show my students how objects can use other class's methods and have those methods affect objects. – gunsnfloyd Sep 11 '17 at 14:15
  • @gunsnfloyd It should surely work now. Sorry for the number of mistakes I made. – Sahil Chhabra Sep 11 '17 at 14:35
  • Ah ha! That does it and makes sense. You pass the player as an argument. As a follow up question, as a newbie, I've heard that having public instance variables is not the best idea. Is that false in this case? – gunsnfloyd Sep 11 '17 at 22:21
  • Thanks for working through this. Take a peek at the new code I added to the original code. I modified it to keep the Player variable private, but I'm wondering if making it private it is more cumbersome than it needs to be. – gunsnfloyd Sep 11 '17 at 22:44
  • Yes you should make it private, and add getter and setter functions to access it. I made it public because i was just focusing on your problem. – Sahil Chhabra Sep 12 '17 at 00:58