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:
Woody Folsom
2012-04-29 10:18:15 -04:00
parent 15ed56134e
commit c53ff75ea4
10 changed files with 132 additions and 38 deletions

View File

@@ -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();