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;
import java.util.function.Supplier;
import inf101.v20.sem2.mnkgames.ConnectFour;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.TicTacToe;
public class GUIMain {
public static void main(String[] args) {
MNKGameGUI.run(new GameSupplier());
// MNKGameGUI.run(() -> new TicTacToe());
MNKGameGUI.run(new GameSupplier());
}
}
class GameSupplier implements Supplier<MNKGame>{
@Override
public MNKGame get() {
return null;
}
public MNKGame getConnectFourGame() {
return new ConnectFour();
}
public MNKGame getTicTacToeGame() {
return new TicTacToe();
}
}
......@@ -14,6 +14,7 @@ import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;
......@@ -36,12 +37,61 @@ s * Initializes a JFrame in which we place the game
JFrame f = new JFrame("mnkGame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MNKGameGUI ap = new MNKGameGUI(gameSupplier);
ap.collectPlayerInfo();
ap.initialize();
f.add("Center", ap);
f.pack();
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
private MNKGame game;
private JButton playConnectFourButton;
......@@ -49,6 +99,9 @@ s * Initializes a JFrame in which we place the game
private JLabel statusMessage;
private Grid<GamePanel> clickablePanels;
private GameSupplier gameSupplier;
private String player1_screenName;
private String player2_screenName;
private boolean multiplayer;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
......@@ -82,6 +135,7 @@ s * Initializes a JFrame in which we place the game
private void updateBoardUI() {
clickablePanels.forEach(item -> item.updateToGame());
updateMessage();
MNKGameGUI.super.updateUI();
if(game.hasWinner()) {
System.out.println("Winner!");
......@@ -116,6 +170,23 @@ s * Initializes a JFrame in which we place the game
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
public Dimension getPreferredSize() {
......@@ -213,7 +284,7 @@ s * Initializes a JFrame in which we place the game
playConnectFourButton.setText("Connect Four");
statusMessage = new JLabel();
statusMessage.setText("Welcome to this game!");
statusMessage.setText("Welcome to this game! " + player1_screenName + " begins.");
JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());
......@@ -224,9 +295,9 @@ s * Initializes a JFrame in which we place the game
private void updateMessage() {
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
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 {
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