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:
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 model.Referee;
|
||||
import model.comPlayer.Player;
|
||||
|
||||
public class MainFrame extends JFrame {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static void main(String[] args) {
|
||||
new UserChooserFrame();
|
||||
new UserChooserFrame(ArgParser.parse(args));
|
||||
}
|
||||
|
||||
private BoardPanel bp;
|
||||
@@ -23,10 +24,10 @@ public class MainFrame extends JFrame {
|
||||
private TileSelectionPanel tp;
|
||||
ScorePanel sp;
|
||||
|
||||
public MainFrame(String name) {
|
||||
super("CS8803 Project 4");
|
||||
public MainFrame(String name, Player comPlayer) {
|
||||
super("CS8803 Project 4 : " + name + " vs. " + comPlayer);
|
||||
|
||||
referee = new Referee(this, name);
|
||||
referee = new Referee(this, name, comPlayer);
|
||||
|
||||
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.JLabel;
|
||||
|
||||
import model.comPlayer.Player;
|
||||
import model.playerModel.PlayerModel;
|
||||
|
||||
public class UserChooserFrame extends JFrame {
|
||||
|
||||
public static final String PLAYER_LIST_FILE = "players.dat";
|
||||
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 final JButton playButton = new JButton("Play!");
|
||||
private final JComboBox<String> userNameBox = new JComboBox<String>();
|
||||
private ArrayList<String> users;
|
||||
|
||||
public UserChooserFrame() {
|
||||
private final Player comPlayer;
|
||||
|
||||
public UserChooserFrame(ParsedArgs parsedArgs) {
|
||||
this.comPlayer = parsedArgs.getComPlayer();
|
||||
initLayout();
|
||||
initActions();
|
||||
|
||||
@@ -75,35 +78,36 @@ public class UserChooserFrame extends JFrame {
|
||||
for (int i = 0; i < users.size(); i++) {
|
||||
userNameBox.addItem(users.get(i));
|
||||
}
|
||||
|
||||
playButton.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
String name = ((String) userNameBox.getSelectedItem());
|
||||
name = name == null ? name : name.trim().toLowerCase();
|
||||
|
||||
if (name != null && name.compareTo("") != 0) {
|
||||
UserChooserFrame.this.setVisible(false);
|
||||
|
||||
boolean found = false;
|
||||
for (int i = 0; !found && i < users.size(); i++) {
|
||||
if (name.compareTo(users.get(i)) == 0) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
users.add(name);
|
||||
}
|
||||
|
||||
saveUserList();
|
||||
new MainFrame(name);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ActionListener psActionListener = new PlayerSelectActionListener(this);
|
||||
|
||||
userNameBox.addActionListener(psActionListener);
|
||||
playButton.addActionListener(psActionListener);
|
||||
}
|
||||
|
||||
void launchSelectedPlayer() {
|
||||
String name = ((String) userNameBox.getSelectedItem());
|
||||
name = name == null ? name : name.trim().toLowerCase();
|
||||
|
||||
if (name != null && name.compareTo("") != 0) {
|
||||
UserChooserFrame.this.setVisible(false);
|
||||
|
||||
boolean found = false;
|
||||
for (int i = 0; !found && i < users.size(); i++) {
|
||||
if (name.compareTo(users.get(i)) == 0) {
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
users.add(name);
|
||||
}
|
||||
|
||||
saveUserList();
|
||||
new MainFrame(name, comPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
GridBagLayout gbl = new GridBagLayout();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user