Commit 3d94d079 authored by Anna Maria Eilertsen's avatar Anna Maria Eilertsen
Browse files

added player name prompt and support

parent 82f05819
package inf101.v20.sem2.mnkgames.GUI; package inf101.v20.sem2.mnkgames.GUI;
import java.util.function.Supplier; import java.util.function.Supplier;
import inf101.v20.sem2.mnkgames.ConnectFour; import inf101.v20.sem2.mnkgames.ConnectFour;
import inf101.v20.sem2.mnkgames.MNKGame; import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.TicTacToe; import inf101.v20.sem2.mnkgames.TicTacToe;
public class GUIMain { public class GUIMain {
public static void main(String[] args) { public static void main(String[] args) {
MNKGameGUI.run(new GameSupplier()); MNKGameGUI.run(new GameSupplier());
// MNKGameGUI.run(() -> new TicTacToe());
} }
} }
class GameSupplier implements Supplier<MNKGame>{ class GameSupplier implements Supplier<MNKGame>{
@Override @Override
public MNKGame get() { public MNKGame get() {
return null; return null;
} }
public MNKGame getConnectFourGame() { public MNKGame getConnectFourGame() {
return new ConnectFour(); return new ConnectFour();
} }
public MNKGame getTicTacToeGame() { public MNKGame getTicTacToeGame() {
return new TicTacToe(); return new TicTacToe();
} }
} }
...@@ -14,6 +14,7 @@ import javax.swing.BorderFactory; ...@@ -14,6 +14,7 @@ import javax.swing.BorderFactory;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid; import inf101.v20.lab4.grid.Grid;
...@@ -36,12 +37,61 @@ s * Initializes a JFrame in which we place the game ...@@ -36,12 +37,61 @@ s * Initializes a JFrame in which we place the game
JFrame f = new JFrame("mnkGame"); JFrame f = new JFrame("mnkGame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MNKGameGUI ap = new MNKGameGUI(gameSupplier); MNKGameGUI ap = new MNKGameGUI(gameSupplier);
ap.collectPlayerInfo();
ap.initialize(); ap.initialize();
f.add("Center", ap); f.add("Center", ap);
f.pack(); f.pack();
f.setVisible(true); f.setVisible(true);
} }
public void collectPlayerInfo() {
JFrame frame = new JFrame();
multiplayer = promptMultiplayer(frame);
player1_screenName = promptPlayerName(frame, "Player 1");
if(multiplayer) {
player2_screenName = promptPlayerName(frame, "Player 2");
}
else {
player2_screenName = "AI player";
}
}
public 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;
}
private boolean promptMultiplayer(JFrame frame) {
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);
//If a string was returned, say so.
if ((s != null) && (s.length() > 0)) {
System.out.println("Received " + s);
}
return s.charAt(0)=='M';
}
// Fields // Fields
private MNKGame game; private MNKGame game;
private JButton playConnectFourButton; private JButton playConnectFourButton;
...@@ -49,6 +99,9 @@ s * Initializes a JFrame in which we place the game ...@@ -49,6 +99,9 @@ s * Initializes a JFrame in which we place the game
private JLabel statusMessage; private JLabel statusMessage;
private Grid<GamePanel> clickablePanels; private Grid<GamePanel> clickablePanels;
private GameSupplier gameSupplier; private GameSupplier gameSupplier;
private String player1_screenName;
private String player2_screenName;
private boolean multiplayer;
public MNKGameGUI(GameSupplier gameSupplier) { public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier; this.gameSupplier = gameSupplier;
...@@ -82,6 +135,7 @@ s * Initializes a JFrame in which we place the game ...@@ -82,6 +135,7 @@ s * Initializes a JFrame in which we place the game
private void updateBoardUI() { private void updateBoardUI() {
clickablePanels.forEach(item -> item.updateToGame()); clickablePanels.forEach(item -> item.updateToGame());
updateMessage();
MNKGameGUI.super.updateUI(); MNKGameGUI.super.updateUI();
if(game.hasWinner()) { if(game.hasWinner()) {
System.out.println("Winner!"); System.out.println("Winner!");
...@@ -116,6 +170,23 @@ s * Initializes a JFrame in which we place the game ...@@ -116,6 +170,23 @@ s * Initializes a JFrame in which we place the game
default: return Color.WHITE; default: return Color.WHITE;
} }
} }
/**
* Maps from Piece values to players
*
* @param pieceAt The piece to be drawn
* @return The player name
*/
private String getPlayerName(Piece pieceAt) {
if(pieceAt.equals(Piece.NONE))
return "No Player";
if(pieceAt.equals(MNKGame.getStartColor())) {
return player1_screenName;
}
else {
return player2_screenName;
}
}
@Override @Override
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
...@@ -213,7 +284,7 @@ s * Initializes a JFrame in which we place the game ...@@ -213,7 +284,7 @@ s * Initializes a JFrame in which we place the game
playConnectFourButton.setText("Connect Four"); playConnectFourButton.setText("Connect Four");
statusMessage = new JLabel(); statusMessage = new JLabel();
statusMessage.setText("Welcome to this game!"); statusMessage.setText("Welcome to this game! " + player1_screenName + " begins.");
JPanel buttons = new JPanel(); JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout()); buttons.setLayout(new BorderLayout());
...@@ -224,9 +295,9 @@ s * Initializes a JFrame in which we place the game ...@@ -224,9 +295,9 @@ s * Initializes a JFrame in which we place the game
private void updateMessage() { private void updateMessage() {
if (game.hasWinner()) if (game.hasWinner())
statusMessage.setText("The game is over!");// The winner is " + game.getWinner().name().toLowerCase()); statusMessage.setText("The game is over! The winner is " + getPlayerName(game.getWinner()));
else else
statusMessage.setText("Playing"); statusMessage.setText("Next move from " + getPlayerName(game.getCurrent()));
} }
} }
\ No newline at end of file
...@@ -277,4 +277,12 @@ public abstract class MNKGame { ...@@ -277,4 +277,12 @@ public abstract class MNKGame {
return r; return r;
} }
public static Piece getStartColor() {
return Piece.BLACK;
}
public Piece getCurrent() {
return currentPlayer;
}
} }
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