Hi all,
Definitely new to java (but have 13 years of VB/.NET experience). I wrote a little Monopoly app that asks for user input through the course of the game (such as 'How many players' before creating the game and then asking each user for their name before starting). It works wonderfully from the console but now I'm trying to give it a bit of a front-end.
I may not be taking the correct approach, but I'm working with JSP's and a copy of my original .java files. The problem I am having is that after posting the 'How many players' form and calling my createGame method, I can't figure out how to have my code ask the user(s) for their name instead of halting in the System.out.println(). Basically, I can't figure out how to get data to flow from .java to .jsp (I've got the .jsp to .java down, I believe).
So, to put it in more of a flow, I have this:
- index.html is the starting point and has a POST form asking for the number of players. The action goes to SetPlayers.jsp.
- in the SetPlayers.jsp, I read in the number of players from request.getParameter. I then initiate my main class and call newGame(numPlayers)
- newGame iterates over the numPlayers and creates a player for each one (addPlayer)
- addPlayer first asks the player for their name and token (such as 'Joe' and 'Car'). It then creates a new player and sets the player attributes as per the user input. This is repeated for the number of players. This is pretty much where I am stuck as I can't figure out how to pass the input back to the .jsp instead of sticking to the console and causing my .jsp to spin/wait.
And finally, here's the snippets of my code, as it might be more beneficial.
//Index.html <FORM METHOD="POST" ACTION="SetPlayers.jsp"> How many players will be playing? <INPUT TYPE="TEXT" NAME=numPlayers SIZE=5><BR> <P><INPUT TYPE="SUBMIT" VALUE="Create Game"> </FORM> //SetPlayers.jsp <%= request.getParameter("numPlayers") %> <% int numPlayers = Integer.parseInt(request.getParameter("numPlayers")); com.jlr.monopoly.TestApp monopoly = new com.jlr.monopoly.TestApp(); monopoly.setNumPlayers(numPlayers); monopoly.newGame(numPlayers); %> //TestApp.newGame public void newGame(int numPlayers) { Game game = new Game(); game.createGame(numPlayers); game.startGame(numPlayers); } //Game.createGame and Game.addPlayer public int createGame(int numPlayers) { Scanner input = new Scanner(System.in); int x = 1; playerArray = new Player[numPlayers]; while (x < numPlayers+1) { playerArray[x-1] = addPlayer(x); x++; } return numPlayers; } public Player addPlayer(int playerSeq) { Scanner input = new Scanner(System.in); String playerKey = "Player" + playerSeq; System.out.println(playerKey + ": Please enter your name"); String name = input.next(); System.out.println(playerKey + ": Please enter your token"); String token = input.next(); Player player = new Player(name, token, 0); return player; }
Any help to get me going in the right direction is greatly appreciated. I know my form isn't great, but I can always refactor as I go.
Thanks in advance!