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

cleanup: added AI interface, reordered methods

parent e5af9e87
package inf101.v20.sem2.mnkgames.AI;
import inf101.v20.sem2.mnkgames.MNKGame;
public interface AIPlayer {
public void makeMove(MNKGame game);
}
......@@ -2,7 +2,7 @@ package inf101.v20.sem2.mnkgames.AI;
import inf101.v20.sem2.mnkgames.MNKGame;
public class SimpleStatelessAIPlayer {
public class SimpleStatelessAIPlayer implements AIPlayer{
public SimpleStatelessAIPlayer() {
}
......
......@@ -20,6 +20,7 @@ import javax.swing.JPanel;
import inf101.v20.lab4.grid.Grid;
import inf101.v20.sem2.mnkgames.MNKGame;
import inf101.v20.sem2.mnkgames.MNKGame.Piece;
import inf101.v20.sem2.mnkgames.AI.AIPlayer;
import inf101.v20.sem2.mnkgames.AI.SimpleStatelessAIPlayer;
/**
......@@ -45,54 +46,6 @@ s * Initializes a JFrame in which we place the game
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;
......@@ -103,10 +56,11 @@ s * Initializes a JFrame in which we place the game
private String player1_screenName;
private String player2_screenName;
private boolean multiplayer;
private SimpleStatelessAIPlayer ai;
private AIPlayer ai;
public MNKGameGUI(GameSupplier gameSupplier) {
this.gameSupplier = gameSupplier;
//initialize to default values
this.game = gameSupplier.getTicTacToeGame();
this.ai = new SimpleStatelessAIPlayer();
}
......@@ -315,4 +269,52 @@ s * Initializes a JFrame in which we place the game
statusMessage.setText("Next move from " + getPlayerName(game.getCurrent()));
}
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';
}
}
\ No newline at end of file
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