Added configurable computer player - use 'com=random', 'com=minimax' etc as cmd-line args.
Default is AlphaBeta. Updated ActionListener for UserChooserFrame so that selecting from the list (or typing a new name and pressing RETURN) also triggers the action. Added the player and computer algorithm names to the title bar e.g. "Bob vs. Alpha-Beta".
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
import model.Board.TileColor;
|
import model.Board.TileColor;
|
||||||
import model.comPlayer.AlphaBetaComPlayer;
|
|
||||||
import model.comPlayer.HumanPlayer;
|
import model.comPlayer.HumanPlayer;
|
||||||
import model.comPlayer.Player;
|
import model.comPlayer.Player;
|
||||||
import model.playerModel.PlayerModel;
|
import model.playerModel.PlayerModel;
|
||||||
@@ -14,7 +13,6 @@ import view.MainFrame;
|
|||||||
import view.ScorePanel;
|
import view.ScorePanel;
|
||||||
|
|
||||||
public class Referee implements Runnable {
|
public class Referee implements Runnable {
|
||||||
|
|
||||||
public static final String COM_TURN = "Waiting for the computer's move.";
|
public static final String COM_TURN = "Waiting for the computer's move.";
|
||||||
public static final String GAME_OVER = "Game over!";
|
public static final String GAME_OVER = "Game over!";
|
||||||
public static final Logger LOGGER = Logger.getLogger(Referee.class
|
public static final Logger LOGGER = Logger.getLogger(Referee.class
|
||||||
@@ -30,7 +28,7 @@ public class Referee implements Runnable {
|
|||||||
private final MainFrame mf;
|
private final MainFrame mf;
|
||||||
private PlayerModel playerModel = null;
|
private PlayerModel playerModel = null;
|
||||||
|
|
||||||
public Referee(MainFrame mnFrm, String player) {
|
public Referee(MainFrame mnFrm, String player, Player computerPlayer) {
|
||||||
if (PlayerModel.exists(player)) {
|
if (PlayerModel.exists(player)) {
|
||||||
PlayerModel.getPlayerPath(player);
|
PlayerModel.getPlayerPath(player);
|
||||||
playerModel = PlayerModel.load(PlayerModel.getPlayerPath(player));
|
playerModel = PlayerModel.load(PlayerModel.getPlayerPath(player));
|
||||||
@@ -41,6 +39,7 @@ public class Referee implements Runnable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mf = mnFrm;
|
mf = mnFrm;
|
||||||
|
this.computerPlayer = computerPlayer;
|
||||||
initGame();
|
initGame();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +143,6 @@ public class Referee implements Runnable {
|
|||||||
|
|
||||||
private void initGame() {
|
private void initGame() {
|
||||||
board = new Board();
|
board = new Board();
|
||||||
computerPlayer = new AlphaBetaComPlayer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void play() {
|
private void play() {
|
||||||
|
|||||||
@@ -22,4 +22,9 @@ public class AlphaBetaComPlayer implements Player {
|
|||||||
public boolean isReady() {
|
public boolean isReady() {
|
||||||
return true; // always ready to play a random valid move
|
return true; // always ready to play a random valid move
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Alpha-Beta ComPlayer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,10 @@ import model.comPlayer.generator.MoveGenerator;
|
|||||||
public class MinimaxComPlayer implements Player{
|
public class MinimaxComPlayer implements Player{
|
||||||
private MoveGenerator moveGenerator = new MinimaxMoveGenerator();
|
private MoveGenerator moveGenerator = new MinimaxMoveGenerator();
|
||||||
|
|
||||||
|
public MinimaxComPlayer() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Move getMove(Board board) {
|
public Move getMove(Board board) {
|
||||||
return moveGenerator.genMove(board, false);
|
return moveGenerator.genMove(board, false);
|
||||||
@@ -22,4 +26,9 @@ public class MinimaxComPlayer implements Player{
|
|||||||
public boolean isReady() {
|
public boolean isReady() {
|
||||||
return true; // always ready to play a random valid move
|
return true; // always ready to play a random valid move
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Minimax ComPlayer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,4 +54,9 @@ public class MonteCarloComPlayer implements Player {
|
|||||||
public boolean isReady() {
|
public boolean isReady() {
|
||||||
return true; // always ready to play a random valid move
|
return true; // always ready to play a random valid move
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Monte Carlo ComPlayer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,4 +50,9 @@ public class RandomComPlayer implements Player {
|
|||||||
public boolean isReady() {
|
public boolean isReady() {
|
||||||
return true; // always ready to play a random valid move
|
return true; // always ready to play a random valid move
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Random ComPlayer";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/view/ArgParser.java
Normal file
17
src/view/ArgParser.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package view;
|
||||||
|
|
||||||
|
public class ArgParser {
|
||||||
|
public static ParsedArgs parse(String[] cmdLineArgs) {
|
||||||
|
ParsedArgs parsedArgs = new ParsedArgs();
|
||||||
|
for (int i = 0; i < cmdLineArgs.length; i++) {
|
||||||
|
if (cmdLineArgs[i].toUpperCase().startsWith("COM=")) {
|
||||||
|
String comPlayer = cmdLineArgs[i].split("=")[1];
|
||||||
|
parsedArgs.setComPlayer(comPlayer);
|
||||||
|
System.out.println("ComPlayer set to: " + comPlayer);
|
||||||
|
} else {
|
||||||
|
System.out.println("Ignoring unrecognized argument: " + cmdLineArgs[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return parsedArgs;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,13 +8,14 @@ import javax.swing.JFrame;
|
|||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
import model.Referee;
|
import model.Referee;
|
||||||
|
import model.comPlayer.Player;
|
||||||
|
|
||||||
public class MainFrame extends JFrame {
|
public class MainFrame extends JFrame {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new UserChooserFrame();
|
new UserChooserFrame(ArgParser.parse(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
private BoardPanel bp;
|
private BoardPanel bp;
|
||||||
@@ -23,10 +24,10 @@ public class MainFrame extends JFrame {
|
|||||||
private TileSelectionPanel tp;
|
private TileSelectionPanel tp;
|
||||||
ScorePanel sp;
|
ScorePanel sp;
|
||||||
|
|
||||||
public MainFrame(String name) {
|
public MainFrame(String name, Player comPlayer) {
|
||||||
super("CS8803 Project 4");
|
super("CS8803 Project 4 : " + name + " vs. " + comPlayer);
|
||||||
|
|
||||||
referee = new Referee(this, name);
|
referee = new Referee(this, name, comPlayer);
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
|
|||||||
32
src/view/ParsedArgs.java
Normal file
32
src/view/ParsedArgs.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package view;
|
||||||
|
|
||||||
|
import model.comPlayer.AlphaBetaComPlayer;
|
||||||
|
import model.comPlayer.MinimaxComPlayer;
|
||||||
|
import model.comPlayer.Player;
|
||||||
|
import model.comPlayer.RandomComPlayer;
|
||||||
|
|
||||||
|
public class ParsedArgs {
|
||||||
|
public static final String COM_RANDOM = "RANDOM";
|
||||||
|
public static final String COM_MINIMAX = "MINIMAX";
|
||||||
|
public static final String COM_ALPHABETA = "ALPHABETA";
|
||||||
|
public static final String COM_DEFAULT = COM_ALPHABETA;
|
||||||
|
|
||||||
|
private String comPlayer = COM_DEFAULT;
|
||||||
|
|
||||||
|
public Player getComPlayer() {
|
||||||
|
if (COM_RANDOM.equalsIgnoreCase(comPlayer)) {
|
||||||
|
return new RandomComPlayer();
|
||||||
|
} else if (COM_MINIMAX.equalsIgnoreCase(comPlayer)) {
|
||||||
|
return new MinimaxComPlayer();
|
||||||
|
} else if (COM_ALPHABETA.equalsIgnoreCase(comPlayer)) {
|
||||||
|
return new AlphaBetaComPlayer();
|
||||||
|
} else {
|
||||||
|
System.out.println("Unrecognized comPlayer '" + comPlayer +"', using default: " + COM_DEFAULT);
|
||||||
|
return new AlphaBetaComPlayer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setComPlayer(String comPlayer) {
|
||||||
|
this.comPlayer = comPlayer;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/view/PlayerSelectActionListener.java
Normal file
18
src/view/PlayerSelectActionListener.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package view;
|
||||||
|
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
|
||||||
|
|
||||||
|
public class PlayerSelectActionListener implements ActionListener {
|
||||||
|
private UserChooserFrame ucFrame;
|
||||||
|
|
||||||
|
public PlayerSelectActionListener(UserChooserFrame ucFrame) {
|
||||||
|
this.ucFrame = ucFrame;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent arg0) {
|
||||||
|
ucFrame.launchSelectedPlayer();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -19,20 +19,23 @@ import javax.swing.JComboBox;
|
|||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
|
||||||
|
import model.comPlayer.Player;
|
||||||
import model.playerModel.PlayerModel;
|
import model.playerModel.PlayerModel;
|
||||||
|
|
||||||
public class UserChooserFrame extends JFrame {
|
public class UserChooserFrame extends JFrame {
|
||||||
|
|
||||||
public static final String PLAYER_LIST_FILE = "players.dat";
|
public static final String PLAYER_LIST_FILE = "players.dat";
|
||||||
public static final JLabel RULES_TEXT = new JLabel(
|
public static final JLabel RULES_TEXT = new JLabel(
|
||||||
"Here should go some rules text. Lorem ipsum and blah blah blah.");
|
"Here should go some rules text. Lorem ipsum dolor sit amet, consectetur adipiscing elit...");
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final JButton playButton = new JButton("Play!");
|
private final JButton playButton = new JButton("Play!");
|
||||||
private final JComboBox<String> userNameBox = new JComboBox<String>();
|
private final JComboBox<String> userNameBox = new JComboBox<String>();
|
||||||
private ArrayList<String> users;
|
private ArrayList<String> users;
|
||||||
|
private final Player comPlayer;
|
||||||
|
|
||||||
public UserChooserFrame() {
|
public UserChooserFrame(ParsedArgs parsedArgs) {
|
||||||
|
this.comPlayer = parsedArgs.getComPlayer();
|
||||||
initLayout();
|
initLayout();
|
||||||
initActions();
|
initActions();
|
||||||
|
|
||||||
@@ -76,32 +79,33 @@ public class UserChooserFrame extends JFrame {
|
|||||||
userNameBox.addItem(users.get(i));
|
userNameBox.addItem(users.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
playButton.addActionListener(new ActionListener() {
|
ActionListener psActionListener = new PlayerSelectActionListener(this);
|
||||||
|
|
||||||
@Override
|
userNameBox.addActionListener(psActionListener);
|
||||||
public void actionPerformed(ActionEvent arg0) {
|
playButton.addActionListener(psActionListener);
|
||||||
String name = ((String) userNameBox.getSelectedItem());
|
}
|
||||||
name = name == null ? name : name.trim().toLowerCase();
|
|
||||||
|
|
||||||
if (name != null && name.compareTo("") != 0) {
|
void launchSelectedPlayer() {
|
||||||
UserChooserFrame.this.setVisible(false);
|
String name = ((String) userNameBox.getSelectedItem());
|
||||||
|
name = name == null ? name : name.trim().toLowerCase();
|
||||||
|
|
||||||
boolean found = false;
|
if (name != null && name.compareTo("") != 0) {
|
||||||
for (int i = 0; !found && i < users.size(); i++) {
|
UserChooserFrame.this.setVisible(false);
|
||||||
if (name.compareTo(users.get(i)) == 0) {
|
|
||||||
found = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!found) {
|
boolean found = false;
|
||||||
users.add(name);
|
for (int i = 0; !found && i < users.size(); i++) {
|
||||||
}
|
if (name.compareTo(users.get(i)) == 0) {
|
||||||
|
found = true;
|
||||||
saveUserList();
|
|
||||||
new MainFrame(name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
if (!found) {
|
||||||
|
users.add(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
saveUserList();
|
||||||
|
new MainFrame(name, comPlayer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initLayout() {
|
private void initLayout() {
|
||||||
|
|||||||
Reference in New Issue
Block a user