Ok, in this assignment I have to write the code for a game that involves picking up and putting down stones in the right combination in order to unlock the treasure chest.
We've been advised that it only needs 4 classes i.e Player, Game, TreasureChest and Stones
My problem comes when I try to get the Game class to run a toString method that when it's finished should return something like this "Jack(0) #(1) @(2) %(3) $(4) Treasure/6\(5)"
This is what my code looks like for the Game class so far:
import java.util.Scanner; public class Game { private int maxMoves; private int combination; private String name; private TreasureChest chest; private Player player; public Game() { Game game1 = new Game(); Scanner keyboard = new Scanner(System.in); System.out.print("Enter combination for the treasure chest (5-10): "); int combination = keyboard.nextInt(); keyboard.nextLine(); chest = new TreasureChest(combination); System.out.print("Enter maximum allowed moves: "); int maxMoves = keyboard.nextInt(); keyboard.nextLine(); System.out.print("Enter player name: "); String name = keyboard.next(); keyboard.nextLine(); game1.toString(); } public String toString() { return name + ""; } }
I still need to add the other information that the toString method is supposed to return, but I can't get it to return even the name. When I try to initialise the Game class it is supposed to ask for the combination, max moves and player name. Instead I get the error "java.lang.StackOverflowError:null"
Any pointers?