-1

So my project is to make a small poker game that will ask users their name, how many players, and your bets.

However before all this, we need to ask the player if they would like to play a NEW game or a SAVED game. The saved game can be hard-coded.

I created a class1 game, and for the saved game file, I created class2. I want class2 to replace the values of class1 but I am not sure how to do this.

The issue is the values of CLASS1 are NOT being replaced by the values of CLASS2 when we call CLASS2. How can I get the variables from CLASS1 to update to CLASS2?

My First Class:

public class NoC2_Musick extends savedGame
{
    public static void main(String[] args)
    {
        savedGame obj = new savedGame();

        Scanner input = new Scanner(System.in);
        Scanner inputString = new Scanner(System.in);
        System.out.println("Is this a NEW Game or a PREVIOUS game?");
        System.out.println("How many players will join this game?");
        int totalPlayers = input.nextInt();
        String[] players = new String[totalPlayers];

        obj.oldsavedgame();
        System.out.println("What is your name and club?");

        for (int i = 0; i < totalPlayers; i++) {
            players[i] = inputString.nextLine();
        }

        System.out.println("These are the Players: ");
        for (int i = 0; i < totalPlayers; i++) {
            System.out.println(players[i]);
        }

        System.out.println("Place your Bet.");
        int bet = input.nextInt();
    }
}

My Second Class:

public class savedGame
{
    public int oldsavedgame()
    {
        int totalPlayers = 3;
        String[] players;
        players = new String[3];
        players[0] = "Sofia";
        players[1] = "Shawn";
        players[2] = "Tomi";
        int bet = 100;
        System.out.println("Test");
        return totalPlayers;
    }

    public static void main(String[] args)
    {
    }
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Shawn
  • 1,175
  • 2
  • 11
  • 20
  • Possible duplicate of [Accessing variables in other classes (Java)](http://stackoverflow.com/questions/2861915/accessing-variables-in-other-classes-java) – Timothy Truckle Dec 21 '16 at 21:07

1 Answers1

0

I think you are missing some key things here. I do not think you should create a new class for storing the old info.

Just write a method that loads the data into instance variables.

Also, you are not reading the answer to the question: is this a new game / previous.

I would do something along these lines (note: It is not complete, but it should get you headed in the right direction, as far as loading a previous game):

public class NoC2_Musick {
   int totalPlayers = 0;
   ArrayList<String> players = new ArrayList<String>();
   int bet = 0;

private void LoadSavedGame() {
    totalPlayers = 3;
    players.add("Sofia");
    players.add("Shawn");
    players.add("Tomi");
    bet = 100;
}

public NoC2_Musick() {
    Scanner input = new Scanner(System.in);
    Scanner inputString = new Scanner(System.in);
    System.out.println("Is this a NEW Game? (y/n)");
    char newGame = input.next().charAt(0);

    if(newGame == 'y')
    {
        System.out.println("How many players will join this game?");
        totalPlayers = input.nextInt();

        for (int i = 0; i < totalPlayers; i++) {
            System.out.println("What is your name and club?");
            players.add(inputString.nextLine());
        }
    }
    else {
        LoadSavedGame();
    }

    System.out.println("These are the Players: ");
    for (int i = 0; i < totalPlayers; i++) {
        System.out.println(players.get(i));
    }

    System.out.println("The current bet is: " + bet);

    System.out.println("Place your Bet.");
    int bet = input.nextInt();
}

public static void main(String[] args) {
    new NoC2_Musick();
}

}

bradleyfitz
  • 686
  • 4
  • 8