Commit 436324af authored by mva021's avatar mva021
Browse files

Moved promtPlayerName() to the HumanPlayer class

parent a06ed362
......@@ -21,6 +21,7 @@ import inf101.v20.lab4.grid.Grid;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
import inf101.v20.sem2.twoPlayerGame.player.HumanPlayer;
/**
*
......@@ -253,53 +254,25 @@ s * Initializes a JFrame in which we place the game
* Helper method to collect player info
*/
private void collectPlayerInfo() {
JFrame frame = new JFrame();
boolean multiplayer = promptMultiplayer(frame);
game.setHumanPlayer1(promptPlayerName(frame, "Player 1"));
boolean multiplayer = promptMultiplayer();
game.setHumanPlayer1(HumanPlayer.promptPlayerName("Player 1"));
if(multiplayer) {
game.setHumanPlayer2(promptPlayerName(frame, "Player 2"));
game.setHumanPlayer2(HumanPlayer.promptPlayerName("Player 2"));
}
else {
game.setAIPlayer2();
}
}
/**
* Helper method that prompts for player name
* @param player the name of the player that will be used in the prompt dialog
*/
private String promptPlayerName(JFrame frame, String player) {
String name = (String)JOptionPane.showInputDialog(
frame,
"Welcome " + player + ":\n"
+ "What is your name?",
"MKGame StartUp",
JOptionPane.PLAIN_MESSAGE,
null,
null,
null);
//If a string was returned, say so.
if ((name != null) && (name.length() > 0)) {
System.out.println("Received " + name);
}
return name;
}
/**
* Helper method that prompts for multiplayer or not
* @return true if multiplayer is selected, false otherwise
*/
private boolean promptMultiplayer(JFrame frame) {
private boolean promptMultiplayer() {
Object[] possibilities = {"Multiplayer", "Single Player (against AI)"};
String s = (String)JOptionPane.showInputDialog(
frame,
"Welcome:\n"
+ "Select one or two players",
"MKGame StartUp",
JOptionPane.PLAIN_MESSAGE,
null,
possibilities,
null);
String message = "Welcome:\n" + "Select one or two players";
String s = (String)JOptionPane.showInputDialog(null,message,"MKGame StartUp",
JOptionPane.PLAIN_MESSAGE,null,possibilities,null);
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
......
package inf101.v20.sem2.twoPlayerGame.player;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import inf101.v20.sem2.mnkgames.MNKGame;
public class HumanPlayer extends AbstractPlayer{
......@@ -7,4 +10,20 @@ public class HumanPlayer extends AbstractPlayer{
public HumanPlayer(String name, MNKGame.Piece color) {
super(name, color);
}
/**
* Helper method that prompts for player name
* @param player the name of the player that will be used in the prompt dialog
*/
public static String promptPlayerName(String player) {
String message = "Welcome " + player + ":\n"+ "What is your name?";
String name = JOptionPane.showInputDialog(message,"MKGame StartUp");
//If a string was returned, say so.
if ((name != null) && (name.length() > 0)) {
System.out.println("Received " + name);
}else {
System.err.println("No name entered");
}
return name;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment