- Implemented multiple users, including a selection dialog and automatic preference saving and loading.
- Integrated the ANN with the game. The network now predicts a user move, completely ignores it, and trains itself on the players actual move. This integration also included implementing two new functions. The first translates a board state to a boolean array to correspond with input nodes. The second translates a move to a boolean array to correspond with output nodes.
This commit is contained in:
@@ -20,28 +20,6 @@ public class HighScoreDialog extends JDialog {
|
||||
*/
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame x = new JFrame();
|
||||
x.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
x.setVisible(true);
|
||||
|
||||
PlayerModel pm = new PlayerModel();
|
||||
pm.logGame(100);
|
||||
pm.logGame(90);
|
||||
pm.logGame(80);
|
||||
pm.logGame(70);
|
||||
pm.logGame(60);
|
||||
pm.logGame(50);
|
||||
pm.logGame(40);
|
||||
pm.logGame(30);
|
||||
pm.logGame(20);
|
||||
pm.logGame(10);
|
||||
pm.logGame(98);
|
||||
pm.logGame(105);
|
||||
|
||||
new HighScoreDialog(x, pm);
|
||||
}
|
||||
|
||||
public HighScoreDialog(JFrame owner, PlayerModel pm) {
|
||||
super(owner, "Game over!", true);
|
||||
|
||||
|
||||
@@ -14,8 +14,7 @@ public class MainFrame extends JFrame {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static void main(String[] args) {
|
||||
MainFrame mainFrame = new MainFrame();
|
||||
mainFrame.playGame();
|
||||
new UserChooserFrame();
|
||||
}
|
||||
|
||||
private BoardPanel bp;
|
||||
@@ -24,9 +23,10 @@ public class MainFrame extends JFrame {
|
||||
private TileSelectionPanel tp;
|
||||
ScorePanel sp;
|
||||
|
||||
public MainFrame() {
|
||||
public MainFrame(String name) {
|
||||
super("CS8803 Project 4");
|
||||
referee = new Referee(this);
|
||||
|
||||
referee = new Referee(this, name);
|
||||
|
||||
init();
|
||||
|
||||
@@ -35,6 +35,7 @@ public class MainFrame extends JFrame {
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
|
||||
playGame();
|
||||
}
|
||||
|
||||
public ScorePanel getScorePanel() {
|
||||
|
||||
164
src/view/UserChooserFrame.java
Normal file
164
src/view/UserChooserFrame.java
Normal file
@@ -0,0 +1,164 @@
|
||||
package view;
|
||||
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
|
||||
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.");
|
||||
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() {
|
||||
initLayout();
|
||||
initActions();
|
||||
|
||||
pack();
|
||||
setResizable(false);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null);
|
||||
setVisible(true);
|
||||
}
|
||||
|
||||
private String getUserListPath() {
|
||||
return PlayerModel.DATA_FOLDER + PLAYER_LIST_FILE;
|
||||
}
|
||||
|
||||
private ArrayList<String> getUsers() {
|
||||
FileInputStream fin = null;
|
||||
ObjectInputStream oin = null;
|
||||
|
||||
try {
|
||||
fin = new FileInputStream(getUserListPath());
|
||||
oin = new ObjectInputStream(fin);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
ArrayList<String> list = (ArrayList<String>) oin.readObject();
|
||||
|
||||
oin.close();
|
||||
return list;
|
||||
|
||||
} catch (Exception e) {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
}
|
||||
|
||||
private void initActions() {
|
||||
userNameBox.setEditable(true);
|
||||
|
||||
users = getUsers();
|
||||
Collections.sort(users);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void initLayout() {
|
||||
GridBagLayout gbl = new GridBagLayout();
|
||||
|
||||
GridBagConstraints con = new GridBagConstraints();
|
||||
con.fill = GridBagConstraints.BOTH;
|
||||
con.gridheight = 1;
|
||||
con.gridwidth = 5;
|
||||
con.gridx = 0;
|
||||
con.gridy = 0;
|
||||
con.insets = new Insets(20, 20, 5, 20);
|
||||
con.weightx = 1;
|
||||
con.weighty = 1;
|
||||
gbl.setConstraints(RULES_TEXT, con);
|
||||
|
||||
con = new GridBagConstraints();
|
||||
con.fill = GridBagConstraints.BOTH;
|
||||
con.gridheight = 1;
|
||||
con.gridwidth = 5;
|
||||
con.gridx = 0;
|
||||
con.gridy = 1;
|
||||
con.insets = new Insets(5, 20, 5, 20);
|
||||
con.weightx = 1;
|
||||
con.weighty = 1;
|
||||
gbl.setConstraints(userNameBox, con);
|
||||
|
||||
con = new GridBagConstraints();
|
||||
con.fill = GridBagConstraints.BOTH;
|
||||
con.gridheight = 1;
|
||||
con.gridwidth = 1;
|
||||
con.gridx = 2;
|
||||
con.gridy = 2;
|
||||
con.insets = new Insets(5, 20, 20, 20);
|
||||
con.weightx = 1;
|
||||
con.weighty = 1;
|
||||
gbl.setConstraints(playButton, con);
|
||||
|
||||
setLayout(gbl);
|
||||
add(RULES_TEXT);
|
||||
add(userNameBox);
|
||||
add(playButton);
|
||||
}
|
||||
|
||||
private void saveUserList() {
|
||||
FileOutputStream fout = null;
|
||||
ObjectOutputStream oout = null;
|
||||
String path = getUserListPath();
|
||||
try {
|
||||
(new File(PlayerModel.DATA_FOLDER)).mkdirs();
|
||||
|
||||
fout = new FileOutputStream(path);
|
||||
oout = new ObjectOutputStream(fout);
|
||||
oout.writeObject(users);
|
||||
oout.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user