init commit
BIN
App.jar
Normal file
BIN
GameRunner$1.class
Normal file
BIN
GameRunner.class
Normal file
15
GameRunner.java
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GameRunner {
|
||||||
|
public static int player1Won = 0, player2Won = 0;
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
SwingUtilities.invokeLater(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
new GridDialog();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
GridDialog$1$1$1.class
Normal file
BIN
GridDialog$1$1$2.class
Normal file
BIN
GridDialog$1$1$3.class
Normal file
BIN
GridDialog$1$1$4.class
Normal file
BIN
GridDialog$1$1$5.class
Normal file
BIN
GridDialog$1$1$6.class
Normal file
BIN
GridDialog$1$1$7.class
Normal file
BIN
GridDialog$1$1.class
Normal file
BIN
GridDialog$1$2.class
Normal file
BIN
GridDialog$1$3.class
Normal file
BIN
GridDialog$1$4.class
Normal file
BIN
GridDialog$1$5.class
Normal file
BIN
GridDialog$1.class
Normal file
BIN
GridDialog$2.class
Normal file
BIN
GridDialog.class
Normal file
243
GridDialog.java
Normal file
|
|
@ -0,0 +1,243 @@
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
public class GridDialog {
|
||||||
|
private JDialog dialog = new JDialog();
|
||||||
|
|
||||||
|
private JTextField name1 = new JTextField("Player 1"), name2 = new JTextField("Player 2");
|
||||||
|
private SpinnerModel cards = new SpinnerNumberModel(12, 6, 100, 2);
|
||||||
|
private JSpinner numCards = new JSpinner(cards);
|
||||||
|
|
||||||
|
public GridDialog(String str1, String str2, int cards) {
|
||||||
|
name1.setText(str1);
|
||||||
|
name2.setText(str2);
|
||||||
|
numCards.setValue(cards);
|
||||||
|
createDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
public GridDialog() {
|
||||||
|
/*try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}*/
|
||||||
|
createDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createDialog() {
|
||||||
|
dialog.setLayout(new GridBagLayout());
|
||||||
|
|
||||||
|
SpinnerModel players = new SpinnerNumberModel(2, 1, 4, 1);
|
||||||
|
JSpinner numPlayers = new JSpinner(players);
|
||||||
|
|
||||||
|
GridBagConstraints mainGBC = new GridBagConstraints();
|
||||||
|
|
||||||
|
mainGBC.gridy = 0;
|
||||||
|
mainGBC.gridwidth = 2;
|
||||||
|
mainGBC.fill = GridBagConstraints.BOTH;
|
||||||
|
mainGBC.insets = new Insets(2, 5, 2, 5);
|
||||||
|
JLabel numPlayerTitle = new JLabel("Player Picker", JLabel.CENTER);
|
||||||
|
numPlayerTitle.setFont(new Font(numPlayerTitle.getFont().getName(), Font.BOLD, numPlayerTitle.getFont().getSize() + 2));
|
||||||
|
dialog.add(numPlayerTitle, mainGBC);
|
||||||
|
|
||||||
|
mainGBC.gridy = 1;
|
||||||
|
mainGBC.gridwidth = 1;
|
||||||
|
dialog.add(new JLabel("# of Players:"), mainGBC);
|
||||||
|
dialog.add(numPlayers, mainGBC);
|
||||||
|
|
||||||
|
mainGBC.gridy = 2;
|
||||||
|
JButton playersOk = new JButton("Ok");
|
||||||
|
playersOk.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
int players = (Integer)numPlayers.getValue();
|
||||||
|
dialog.getContentPane().removeAll();
|
||||||
|
|
||||||
|
GridBagConstraints c = new GridBagConstraints();
|
||||||
|
c.fill = GridBagConstraints.BOTH;
|
||||||
|
c.gridy = 0;
|
||||||
|
c.gridwidth = 2;
|
||||||
|
c.weightx = 1;
|
||||||
|
c.weighty = 1;
|
||||||
|
c.insets = new Insets(2, 5, 10, 5);
|
||||||
|
JLabel title = new JLabel("Board Size Picker", JLabel.CENTER);
|
||||||
|
title.setFont(new Font(title.getFont().getName(), Font.BOLD, title.getFont().getSize() + 2));
|
||||||
|
dialog.add(title, c);
|
||||||
|
|
||||||
|
c.gridy = 1;
|
||||||
|
c.gridwidth = 1;
|
||||||
|
c.insets = new Insets(2, 5, 2, 5);
|
||||||
|
dialog.add(new JLabel("Name 1:", JLabel.RIGHT), c);
|
||||||
|
dialog.add(name1, c);
|
||||||
|
|
||||||
|
if (players > 1) {
|
||||||
|
c.gridy = 2;
|
||||||
|
dialog.add(new JLabel("Name 2:", JLabel.RIGHT), c);
|
||||||
|
dialog.add(name2, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
c.gridy = players + 1;
|
||||||
|
dialog.add(new JLabel("# of Cards:", JLabel.RIGHT), c);
|
||||||
|
dialog.add(numCards, c);
|
||||||
|
|
||||||
|
c.gridy = players + 2;
|
||||||
|
JButton okButton = new JButton("Ok");
|
||||||
|
okButton.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
dialog.getContentPane().removeAll();
|
||||||
|
|
||||||
|
GridBagConstraints gbc = new GridBagConstraints();
|
||||||
|
|
||||||
|
gbc.gridy = 0;
|
||||||
|
gbc.gridwidth = 2;
|
||||||
|
gbc.fill = GridBagConstraints.BOTH;
|
||||||
|
gbc.insets = new Insets(2, 5, 2, 5);
|
||||||
|
JLabel playerTitle = new JLabel("Game Mode Picker", JLabel.CENTER);
|
||||||
|
playerTitle.setFont(new Font(playerTitle.getFont().getName(), Font.BOLD, playerTitle.getFont().getSize() + 2));
|
||||||
|
dialog.add(playerTitle, gbc);
|
||||||
|
|
||||||
|
if (players > 1) {
|
||||||
|
gbc.gridy = 1;
|
||||||
|
JButton localMultiplayerButton = new JButton("Local Multiplayer");
|
||||||
|
localMultiplayerButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
new Main(name1.getText(), name2.getText(), factor((Integer)numCards.getValue())[0], factor((Integer)numCards.getValue())[1], players);
|
||||||
|
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(localMultiplayerButton, gbc);
|
||||||
|
|
||||||
|
gbc.gridy = 2;
|
||||||
|
JButton LANMultiplayerButton = new JButton("LAN Multiplayer");
|
||||||
|
LANMultiplayerButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
new Main(name1.getText(), name2.getText(), factor((Integer)numCards.getValue())[0], factor((Integer)numCards.getValue())[1], players);
|
||||||
|
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(LANMultiplayerButton, gbc);
|
||||||
|
|
||||||
|
gbc.gridy = 3;
|
||||||
|
JButton onlineMultiplayerButton = new JButton("Online Multiplayer");
|
||||||
|
onlineMultiplayerButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(onlineMultiplayerButton, gbc);
|
||||||
|
} else {
|
||||||
|
gbc.gridy = 1;
|
||||||
|
JButton singleplayerButton = new JButton("Singleplayer");
|
||||||
|
singleplayerButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
new Main(name1.getText(), name2.getText(), factor((Integer)numCards.getValue())[0], factor((Integer)numCards.getValue())[1], players);
|
||||||
|
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(singleplayerButton, gbc);
|
||||||
|
|
||||||
|
gbc.gridy = 2;
|
||||||
|
JButton comButton = new JButton("Player v. COM");
|
||||||
|
comButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
|
||||||
|
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(comButton, gbc);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (players > 1) gbc.gridy = 4;
|
||||||
|
else gbc.gridy = 3;
|
||||||
|
gbc.gridwidth = 1;
|
||||||
|
gbc.insets = new Insets(5, 5, 2, 5);
|
||||||
|
JButton backPlayerButton = new JButton("Back");
|
||||||
|
backPlayerButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
dialog.dispatchEvent(new WindowEvent(dialog, WindowEvent.WINDOW_CLOSING));
|
||||||
|
new GridDialog(name1.getText(), name2.getText(), factor((Integer)numCards.getValue())[0] * factor((Integer)numCards.getValue())[1]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(backPlayerButton, gbc);
|
||||||
|
|
||||||
|
JButton cancelPlayerButton = new JButton("Cancel");
|
||||||
|
cancelPlayerButton.addActionListener(new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
dialog.dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(cancelPlayerButton, gbc);
|
||||||
|
|
||||||
|
dialog.revalidate();
|
||||||
|
dialog.repaint();
|
||||||
|
dialog.pack();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(okButton, c);
|
||||||
|
|
||||||
|
c.gridx = 1;
|
||||||
|
JButton cancelButton = new JButton("Cancel");
|
||||||
|
cancelButton.addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
dialog.dispose();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
dialog.add(cancelButton, c);
|
||||||
|
|
||||||
|
dialog.revalidate();
|
||||||
|
dialog.repaint();
|
||||||
|
dialog.pack();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dialog.add(playersOk, mainGBC);
|
||||||
|
dialog.add(new JButton("Cancel"), mainGBC);
|
||||||
|
|
||||||
|
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||||
|
dialog.pack();
|
||||||
|
dialog.setVisible(true);
|
||||||
|
|
||||||
|
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
dialog.setLocation(dim.width/2-dialog.getSize().width/2, dim.height/2-dialog.getSize().height/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[] factor(int num) {
|
||||||
|
double fac1 = 1, fac2;
|
||||||
|
double lowest = num;
|
||||||
|
int ans[] = {0, 0};
|
||||||
|
|
||||||
|
while(fac1 <= Math.sqrt(num)) {
|
||||||
|
fac2 = num/fac1;
|
||||||
|
|
||||||
|
if (trunc(fac2) == fac2) {
|
||||||
|
if ((fac1 + fac2) < lowest) {
|
||||||
|
lowest = fac1 + fac2;
|
||||||
|
ans[0] = (int)fac1;
|
||||||
|
ans[1] = (int)fac2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fac1++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double trunc(double d) {
|
||||||
|
return ((long)(d * 1)) / 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Main$1.class
Normal file
BIN
Main$2.class
Normal file
BIN
Main$3.class
Normal file
BIN
Main$4.class
Normal file
BIN
Main.class
Normal file
400
Main.java
Normal file
|
|
@ -0,0 +1,400 @@
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.io.File;
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.border.EtchedBorder;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
int rows, cols;
|
||||||
|
String name1, name2;
|
||||||
|
|
||||||
|
JFrame frame = new JFrame("Memory");
|
||||||
|
JPanel infoPanel = new JPanel();
|
||||||
|
JPanel panel = new JPanel();
|
||||||
|
|
||||||
|
Random rand = new Random();
|
||||||
|
|
||||||
|
String labelNames[] = {"Car", "Elephant", "Phone", "Tree", "Person", "Computer", "Toy", "Paper", "Book", "Desk", "Bed", "Glasses", "Keyboard", "Ring", "Fan", "Sock", "Sign", "Eraser",
|
||||||
|
"Pencil", "Bow", "Cat", "Boat", "Fork", "Spoon", "Door", "Clock", "Bottle", "Purse", "Brush", "Camera", "Money", "Bread", "Screw", "Mirror", "Cork", "Sponge",
|
||||||
|
"Banana", "Bowl", "Tomato", "Key", "Nail", "Hammer", "Chain", "Table", "Towel", "Cord", "Thread", "Chalk", "Baby", "Candy"};
|
||||||
|
|
||||||
|
JPanel panels[][];
|
||||||
|
JButton btns[][];
|
||||||
|
JLabel labels[][];
|
||||||
|
JLabel cardFronts[][];
|
||||||
|
|
||||||
|
Timer gameTimer;
|
||||||
|
JLabel timerLabel;
|
||||||
|
long lastTickTime;
|
||||||
|
|
||||||
|
JLabel player1Games = new JLabel("" + GameRunner.player1Won), player2Games = new JLabel("" + GameRunner.player2Won);
|
||||||
|
|
||||||
|
JLabel player1 = new JLabel(), player2 = new JLabel();
|
||||||
|
JLabel player1Turn, player2Turn;
|
||||||
|
int player1Score = 0, player2Score = 0;
|
||||||
|
int turn = 1;
|
||||||
|
|
||||||
|
JButton selected[] = {new JButton(), new JButton()};
|
||||||
|
int picked = 0;
|
||||||
|
|
||||||
|
Timer timer = new Timer(2000, new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent evt) {
|
||||||
|
checkMatch();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
public Main(String name1, String name2, int rows, int cols, int players) {
|
||||||
|
/*try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
timerLabel = new JLabel(String.format("%04d:%02d:%02d.%03d", 0, 0, 0, 0));
|
||||||
|
|
||||||
|
gameTimer = new Timer(100, new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
long runningTime = System.currentTimeMillis() - lastTickTime;
|
||||||
|
Duration duration = Duration.ofMillis(runningTime);
|
||||||
|
long hours = duration.toHours();
|
||||||
|
duration = duration.minusHours(hours);
|
||||||
|
long minutes = duration.toMinutes();
|
||||||
|
duration = duration.minusMinutes(minutes);
|
||||||
|
long millis = duration.toMillis();
|
||||||
|
long seconds = millis / 1000;
|
||||||
|
timerLabel.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
player1Turn = new JLabel(new ImageIcon(ImageIO.read(new File("icons\\dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
player2Turn = new JLabel(new ImageIcon());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.name1 = name1;
|
||||||
|
this.name2 = name2;
|
||||||
|
|
||||||
|
player1.setText(name1 + ": 0");
|
||||||
|
player2.setText(name2 + ": 0");
|
||||||
|
|
||||||
|
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
screenSize = new Dimension((int)((((screenSize.getHeight()*4)/5)/rows)*2)/3, (int)((screenSize.getHeight()*4)/5)/rows);
|
||||||
|
|
||||||
|
if (Toolkit.getDefaultToolkit().getScreenSize().getWidth() < ((cols * screenSize.getWidth()) + ((cols + 1) * 5))) {
|
||||||
|
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
screenSize = new Dimension((int)((screenSize.getWidth()*5)/6)/cols, (int)((((screenSize.getWidth()*5)/6)/cols)*3)/2);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fontSize;
|
||||||
|
int largestWord = 0;
|
||||||
|
JLabel testFont = new JLabel();
|
||||||
|
|
||||||
|
for (int i = 0; i < (rows*cols)/2; i++) {
|
||||||
|
if (testFont.getFontMetrics(testFont.getFont()).stringWidth(labelNames[i]) > largestWord) largestWord = testFont.getFontMetrics(testFont.getFont()).stringWidth(labelNames[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fontSize = (int)(12 * ((double)screenSize.getWidth()/(double)largestWord));
|
||||||
|
fontSize = Math.min(fontSize, (int)screenSize.getHeight());
|
||||||
|
|
||||||
|
Font useFont = new Font(testFont.getFont().getName(), Font.PLAIN, fontSize);
|
||||||
|
|
||||||
|
this.rows = rows;
|
||||||
|
this.cols = cols;
|
||||||
|
|
||||||
|
panels = new JPanel[rows][cols];
|
||||||
|
btns = new JButton[rows][cols];
|
||||||
|
labels = new JLabel[rows][cols];
|
||||||
|
cardFronts = new JLabel[rows][cols];
|
||||||
|
|
||||||
|
for (int i = 0; i < panels.length; i++) {
|
||||||
|
for (int j = 0; j < panels[0].length; j++) {
|
||||||
|
panels[i][j] = new JPanel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < btns.length; i++) {
|
||||||
|
for (int j = 0; j < btns[0].length; j++) {
|
||||||
|
btns[i][j] = new JButton();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < labels.length; i++) {
|
||||||
|
for (int j = 0; j < labels[0].length; j++) {
|
||||||
|
labels[i][j] = new JLabel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < cardFronts.length; i++) {
|
||||||
|
for (int j = 0; j < cardFronts[0].length; j++) {
|
||||||
|
cardFronts[i][j] = new JLabel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timer.setRepeats(false);
|
||||||
|
|
||||||
|
player1Games.setMinimumSize(new Dimension(25, 32));
|
||||||
|
player1Games.setPreferredSize(new Dimension(25, 32));
|
||||||
|
player1Games.setMaximumSize(new Dimension(25, 32));
|
||||||
|
player2Games.setMinimumSize(new Dimension(25, 32));
|
||||||
|
player2Games.setPreferredSize(new Dimension(25, 32));
|
||||||
|
player2Games.setMaximumSize(new Dimension(25, 32));
|
||||||
|
player1Games.setFont(new Font(player1Games.getFont().getName(), Font.BOLD, player1Games.getFont().getSize() + 30));
|
||||||
|
player2Games.setFont(new Font(player2Games.getFont().getName(), Font.BOLD, player2Games.getFont().getSize() + 30));
|
||||||
|
|
||||||
|
player1Turn.setMinimumSize(new Dimension(15, 15));
|
||||||
|
player1Turn.setPreferredSize(new Dimension(15, 15));
|
||||||
|
player1Turn.setMaximumSize(new Dimension(15, 15));
|
||||||
|
player2Turn.setMinimumSize(new Dimension(15, 15));
|
||||||
|
player2Turn.setPreferredSize(new Dimension(15, 15));
|
||||||
|
player2Turn.setMaximumSize(new Dimension(15, 15));
|
||||||
|
|
||||||
|
player1.setBorder(BorderFactory.createEmptyBorder(0, 7, 0, 10));
|
||||||
|
player2.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 7));
|
||||||
|
player1.setFont(new Font(player1.getFont().getName(), Font.BOLD, player1.getFont().getSize() + 2));
|
||||||
|
player2.setFont(new Font(player2.getFont().getName(), Font.BOLD, player2.getFont().getSize() + 2));
|
||||||
|
|
||||||
|
infoPanel.setLayout(new GridBagLayout());
|
||||||
|
GridBagConstraints infoGBC = new GridBagConstraints();
|
||||||
|
|
||||||
|
infoGBC.gridheight = 2;
|
||||||
|
infoGBC.insets = new Insets(0, 5, 0, 5);
|
||||||
|
|
||||||
|
infoPanel.add(player1Games,infoGBC);
|
||||||
|
|
||||||
|
infoGBC.gridheight = 1;
|
||||||
|
infoGBC.insets = new Insets(0, 0, 0, 0);
|
||||||
|
|
||||||
|
infoPanel.add(player1Turn, infoGBC);
|
||||||
|
infoPanel.add(player1, infoGBC);
|
||||||
|
infoPanel.add(player2, infoGBC);
|
||||||
|
infoPanel.add(player2Turn, infoGBC);
|
||||||
|
|
||||||
|
infoGBC.gridheight = 2;
|
||||||
|
infoGBC.insets = new Insets(0, 5, 0, 5);
|
||||||
|
|
||||||
|
infoPanel.add(player2Games, infoGBC);
|
||||||
|
|
||||||
|
infoGBC.gridy = 1;
|
||||||
|
infoGBC.gridx = 2;
|
||||||
|
infoGBC.gridwidth = 2;
|
||||||
|
infoGBC.insets = new Insets(0, 0, 0, 0);
|
||||||
|
|
||||||
|
infoPanel.add(timerLabel, infoGBC);
|
||||||
|
infoPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "Scores"));
|
||||||
|
|
||||||
|
panel.setLayout(new GridLayout(rows, cols, 5, 5));
|
||||||
|
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
||||||
|
|
||||||
|
int row;
|
||||||
|
int column;
|
||||||
|
|
||||||
|
for (int i = 0; i < (rows*cols)/2; i++) {
|
||||||
|
for (int j = 0; j < 2; j++) {
|
||||||
|
do {
|
||||||
|
row = rand.nextInt(rows);
|
||||||
|
column = rand.nextInt(cols);
|
||||||
|
} while (!labels[row][column].getText().equals(""));
|
||||||
|
labels[row][column].setText(labelNames[i]);
|
||||||
|
|
||||||
|
try {
|
||||||
|
labels[row][column].setIcon(new ImageIcon(ImageIO.read(new File("icons\\" + labelNames[i] + ".png")).getScaledInstance((int)screenSize.getWidth() - 10, (int)screenSize.getWidth() - 10, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
|
for (int j = 0; j < cols; j++) {
|
||||||
|
panels[i][j].setLayout(new OverlayLayout(panels[i][j]));
|
||||||
|
|
||||||
|
btns[i][j].setBorder(BorderFactory.createEmptyBorder());
|
||||||
|
btns[i][j].setContentAreaFilled(false);
|
||||||
|
try {
|
||||||
|
btns[i][j].setIcon(new ImageIcon(ImageIO.read(new File("card.png")).getScaledInstance((int)screenSize.getWidth(), (int)screenSize.getHeight(), java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
cardFronts[i][j].setIcon(new ImageIcon(ImageIO.read(new File("cardFront.png")).getScaledInstance((int)screenSize.getWidth(), (int)screenSize.getHeight(), java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
btns[i][j].addActionListener(new ActionListener() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
selected[picked] = (JButton) e.getSource();
|
||||||
|
removeBtn(selected[picked]);
|
||||||
|
picked++;
|
||||||
|
if (picked == 2) {
|
||||||
|
int row0, column0;
|
||||||
|
int row1, column1;
|
||||||
|
|
||||||
|
row0 = findIndexRow(selected[0], rows, cols);
|
||||||
|
row1 = findIndexRow(selected[1], rows, cols);
|
||||||
|
|
||||||
|
column0 = findIndexColumn(selected[0], rows, cols);
|
||||||
|
column1 = findIndexColumn(selected[1], rows, cols);
|
||||||
|
|
||||||
|
if (!labels[row0][column0].getText().equals(labels[row1][column1].getText())) {
|
||||||
|
timer.start();
|
||||||
|
} else {
|
||||||
|
checkMatch();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
panels[i][j].add(btns[i][j]);
|
||||||
|
|
||||||
|
labels[i][j].setFont(useFont);
|
||||||
|
labels[i][j].setPreferredSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight()));
|
||||||
|
labels[i][j].setMaximumSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight()));
|
||||||
|
labels[i][j].setMinimumSize(new Dimension((int)screenSize.getWidth(), (int)screenSize.getHeight()));
|
||||||
|
labels[i][j].setHorizontalAlignment(JLabel.CENTER);
|
||||||
|
labels[i][j].setHorizontalTextPosition(JLabel.CENTER);
|
||||||
|
labels[i][j].setVerticalTextPosition(JLabel.TOP);
|
||||||
|
panels[i][j].add(labels[i][j]);
|
||||||
|
panels[i][j].add(cardFronts[i][j]);
|
||||||
|
panel.add(panels[i][j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
frame.setLayout(new GridBagLayout());
|
||||||
|
GridBagConstraints c = new GridBagConstraints();
|
||||||
|
c.gridy = 0;
|
||||||
|
|
||||||
|
frame.add(infoPanel, c);
|
||||||
|
c.gridy = 1;
|
||||||
|
|
||||||
|
frame.add(panel, c);
|
||||||
|
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
frame.pack();
|
||||||
|
frame.setVisible(true);
|
||||||
|
|
||||||
|
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
||||||
|
frame.setLocation(dim.width/2-frame.getSize().width/2, dim.height/2-frame.getSize().height/2);
|
||||||
|
|
||||||
|
lastTickTime = System.currentTimeMillis();
|
||||||
|
gameTimer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkMatch() {
|
||||||
|
int row0, column0;
|
||||||
|
int row1, column1;
|
||||||
|
|
||||||
|
row0 = findIndexRow(selected[0], rows, cols);
|
||||||
|
row1 = findIndexRow(selected[1], rows, cols);
|
||||||
|
|
||||||
|
column0 = findIndexColumn(selected[0], rows, cols);
|
||||||
|
column1 = findIndexColumn(selected[1], rows, cols);
|
||||||
|
|
||||||
|
if (!labels[row0][column0].getText().equals(labels[row1][column1].getText())) {
|
||||||
|
selected[0].setVisible(true);
|
||||||
|
selected[1].setVisible(true);
|
||||||
|
if (turn == 1) turn++;
|
||||||
|
else turn--;
|
||||||
|
} else {
|
||||||
|
if (turn == 1) {
|
||||||
|
player1Score++;
|
||||||
|
player1.setText(name1 + ": " + player1Score);
|
||||||
|
} else {
|
||||||
|
player2Score++;
|
||||||
|
player2.setText(name2 + ": " + player2Score);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
picked = 0;
|
||||||
|
|
||||||
|
if (turn == 1) {
|
||||||
|
try {
|
||||||
|
player1Turn.setIcon(new ImageIcon(ImageIO.read(new File("icons\\dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
player2Turn.setIcon(new ImageIcon());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
player2Turn.setIcon(new ImageIcon(ImageIO.read(new File("icons\\dot.png")).getScaledInstance(15, 15, java.awt.Image.SCALE_SMOOTH)));
|
||||||
|
player1Turn.setIcon(new ImageIcon());
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((player1Score + player2Score == (rows*cols)/2) || (player1Score > ((rows*cols)/2)/2) || (player2Score > ((rows*cols)/2)/2)) {
|
||||||
|
String winner;
|
||||||
|
|
||||||
|
gameTimer.stop();
|
||||||
|
if (player1Score > player2Score) {
|
||||||
|
winner = name1 + " wins!";
|
||||||
|
GameRunner.player1Won++;
|
||||||
|
player1Games.setText("" + GameRunner.player1Won);
|
||||||
|
} else if (player2Score > player1Score) {
|
||||||
|
winner = name2 + " wins!";
|
||||||
|
GameRunner.player2Won++;
|
||||||
|
player2Games.setText("" + GameRunner.player2Won);
|
||||||
|
} else {
|
||||||
|
winner = "It's a tie!";
|
||||||
|
}
|
||||||
|
|
||||||
|
Object[] options = {"New Game", "Cancel"};
|
||||||
|
if (JOptionPane.showOptionDialog(frame, winner, winner, JOptionPane.PLAIN_MESSAGE, JOptionPane.QUESTION_MESSAGE, null, options, options[0]) == 0) {
|
||||||
|
new GridDialog(name1, name2, rows*cols);
|
||||||
|
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
|
||||||
|
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeBtn(JButton b) {
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
|
for (int j = 0; j < cols; j++) {
|
||||||
|
Component components[] = panels[i][j].getComponents();
|
||||||
|
if (contains(components, b)) {
|
||||||
|
btns[i][j].setVisible(false);
|
||||||
|
panels[i][j].revalidate();
|
||||||
|
panels[i][j].repaint();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean contains(Component components[], JButton b) {
|
||||||
|
for (Component c: components) {
|
||||||
|
if (c == b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int findIndexRow(JButton b, int r, int c) {
|
||||||
|
for (int i = 0; i < r; i++) {
|
||||||
|
for (int j = 0; j < c; j++) {
|
||||||
|
if (btns[i][j] == b) return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int findIndexColumn(JButton b, int r, int c) {
|
||||||
|
for (int i = 0; i < r; i++) {
|
||||||
|
for (int j = 0; j < c; j++) {
|
||||||
|
if (btns[i][j] == b) return j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
card.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
cardFront.png
Normal file
|
After Width: | Height: | Size: 465 B |
4
compilerRunner.bat
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
javac *.java
|
||||||
|
IF EXIST App.jar DEL /F App.jar
|
||||||
|
jar cfm App.jar manifest.txt *.class
|
||||||
|
java -jar App.jar
|
||||||
6
compilerRunnerSchool.bat
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
"C:\Program Files\Java\jdk1.8.0_172\bin\javac" *.java
|
||||||
|
IF EXIST App.jar DEL /F App.jar
|
||||||
|
"C:\Program Files\Java\jdk1.8.0_172\bin\jar" cfm App.jar manifest.txt *.class
|
||||||
|
echo off
|
||||||
|
cls
|
||||||
|
"C:\Program Files\Java\jdk1.8.0_172\bin\java" -jar App.jar
|
||||||
BIN
icons/baby.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
icons/banana.png
Normal file
|
After Width: | Height: | Size: 7.8 KiB |
BIN
icons/bed.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
icons/boat.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
BIN
icons/book.png
Normal file
|
After Width: | Height: | Size: 7 KiB |
BIN
icons/bottle.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
icons/bow.png
Normal file
|
After Width: | Height: | Size: 5.7 KiB |
BIN
icons/bowl.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
icons/bread.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
icons/brush.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
BIN
icons/camera.png
Normal file
|
After Width: | Height: | Size: 4.2 KiB |
BIN
icons/candy.png
Normal file
|
After Width: | Height: | Size: 4 KiB |
BIN
icons/car.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
icons/cat.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
icons/chain.png
Normal file
|
After Width: | Height: | Size: 5.7 KiB |
BIN
icons/chalk.png
Normal file
|
After Width: | Height: | Size: 4.9 KiB |
BIN
icons/clock.png
Normal file
|
After Width: | Height: | Size: 6.1 KiB |
BIN
icons/computer.png
Normal file
|
After Width: | Height: | Size: 2.6 KiB |
BIN
icons/cord.png
Normal file
|
After Width: | Height: | Size: 1.7 KiB |
BIN
icons/cork.png
Normal file
|
After Width: | Height: | Size: 4.7 KiB |
BIN
icons/desk.png
Normal file
|
After Width: | Height: | Size: 1.6 KiB |
BIN
icons/door.png
Normal file
|
After Width: | Height: | Size: 6.5 KiB |
BIN
icons/dot.png
Normal file
|
After Width: | Height: | Size: 2.5 KiB |
BIN
icons/elephant.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
icons/eraser.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
icons/fan.png
Normal file
|
After Width: | Height: | Size: 8 KiB |
BIN
icons/fork.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
131
icons/gameServer.java
Normal file
|
|
@ -0,0 +1,131 @@
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
public class gameServer {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
ServerSocket listener = new ServerSocket(8901);
|
||||||
|
System.out.println("Game Server is Running");
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
Game game = new Game();
|
||||||
|
Game.Player player1 = game.new Player(listener.accept());
|
||||||
|
Game.Player player2 = game.new Player(listener.accept());
|
||||||
|
player1.setOpponent(player2);
|
||||||
|
player2.setOpponent(player1);
|
||||||
|
game.currentPlayer = player1;
|
||||||
|
player1.start();
|
||||||
|
player2.start();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
listener.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Game {
|
||||||
|
Player currentPlayer;
|
||||||
|
|
||||||
|
// winner
|
||||||
|
public boolean hasWinner() {
|
||||||
|
return
|
||||||
|
(board[0] != null && board[0] == board[1] && board[0] == board[2])
|
||||||
|
||(board[3] != null && board[3] == board[4] && board[3] == board[5])
|
||||||
|
||(board[6] != null && board[6] == board[7] && board[6] == board[8])
|
||||||
|
||(board[0] != null && board[0] == board[3] && board[0] == board[6])
|
||||||
|
||(board[1] != null && board[1] == board[4] && board[1] == board[7])
|
||||||
|
||(board[2] != null && board[2] == board[5] && board[2] == board[8])
|
||||||
|
||(board[0] != null && board[0] == board[4] && board[0] == board[8])
|
||||||
|
||(board[2] != null && board[2] == board[4] && board[2] == board[6]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// no empty squares
|
||||||
|
public boolean boardFilledUp() {
|
||||||
|
for (int i = 0; i < board.length; i++) {
|
||||||
|
if (board[i] == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// thread when player tries a move
|
||||||
|
public synchronized boolean legalMove(int location, Player player) {
|
||||||
|
if (player == currentPlayer && board[location] == null) {
|
||||||
|
board[location] = currentPlayer;
|
||||||
|
currentPlayer = currentPlayer.opponent;
|
||||||
|
currentPlayer.otherPlayerMoved(location);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
class Player extends Thread {
|
||||||
|
char mark;
|
||||||
|
Player opponent;
|
||||||
|
Socket socket;
|
||||||
|
BufferedReader input;
|
||||||
|
PrintWriter output;
|
||||||
|
// thread handler to initialize stream fields
|
||||||
|
public Player(Socket socket) {
|
||||||
|
this.socket = socket;
|
||||||
|
this.mark = mark;
|
||||||
|
try {
|
||||||
|
input = new BufferedReader(
|
||||||
|
new InputStreamReader(socket.getInputStream()));
|
||||||
|
output = new PrintWriter(socket.getOutputStream(), true);
|
||||||
|
output.println("WELCOME " + mark);
|
||||||
|
output.println("MESSAGE Waiting for opponent to connect");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Player died: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Accepts notification of who the opponent is.
|
||||||
|
public void setOpponent(Player opponent) {
|
||||||
|
this.opponent = opponent;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//Handles the otherPlayerMoved message.
|
||||||
|
public void otherPlayerMoved(int location) {
|
||||||
|
output.println("OPPONENT_MOVED " + location);
|
||||||
|
output.println(
|
||||||
|
hasWinner() ? "DEFEAT" : boardFilledUp() ? "TIE" : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
// The thread is only started after everyone connects.
|
||||||
|
output.println("MESSAGE All players connected");
|
||||||
|
|
||||||
|
// Tell the first player that it is his/her turn.
|
||||||
|
if (mark == 'X') {
|
||||||
|
output.println("MESSAGE Your move");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Repeatedly get commands from the client and process them.
|
||||||
|
while (true) {
|
||||||
|
String command = input.readLine();
|
||||||
|
if (command.startsWith("MOVE")) {
|
||||||
|
int location = Integer.parseInt(command.substring(5));
|
||||||
|
if (legalMove(location, this)) {
|
||||||
|
output.println("VALID_MOVE");
|
||||||
|
output.println(hasWinner() ? "VICTORY"
|
||||||
|
: boardFilledUp() ? "TIE"
|
||||||
|
: "");
|
||||||
|
} else {
|
||||||
|
output.println("MESSAGE ?");
|
||||||
|
}
|
||||||
|
} else if (command.startsWith("QUIT")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Player died: " + e);
|
||||||
|
} finally {
|
||||||
|
try {socket.close();} catch (IOException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
icons/glasses.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
icons/hammer.png
Normal file
|
After Width: | Height: | Size: 2.2 KiB |
BIN
icons/key.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
icons/keyboard.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
icons/mirror.png
Normal file
|
After Width: | Height: | Size: 4 KiB |
BIN
icons/money.png
Normal file
|
After Width: | Height: | Size: 6.6 KiB |
BIN
icons/nail.png
Normal file
|
After Width: | Height: | Size: 4.4 KiB |
BIN
icons/paper.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
icons/pencil.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
icons/person.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
icons/phone.png
Normal file
|
After Width: | Height: | Size: 7 KiB |
BIN
icons/purse.png
Normal file
|
After Width: | Height: | Size: 2.1 KiB |
BIN
icons/ring.png
Normal file
|
After Width: | Height: | Size: 5.1 KiB |
BIN
icons/screw.png
Normal file
|
After Width: | Height: | Size: 1.8 KiB |
BIN
icons/sign.png
Normal file
|
After Width: | Height: | Size: 4.3 KiB |
BIN
icons/sock.png
Normal file
|
After Width: | Height: | Size: 4.5 KiB |
BIN
icons/sponge.png
Normal file
|
After Width: | Height: | Size: 3.6 KiB |
BIN
icons/spoon.png
Normal file
|
After Width: | Height: | Size: 5 KiB |
BIN
icons/table.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
icons/thread.png
Normal file
|
After Width: | Height: | Size: 8.5 KiB |
BIN
icons/tomato.png
Normal file
|
After Width: | Height: | Size: 3.5 KiB |
BIN
icons/towel.png
Normal file
|
After Width: | Height: | Size: 1.9 KiB |
BIN
icons/toy.png
Normal file
|
After Width: | Height: | Size: 8.1 KiB |
BIN
icons/tree.png
Normal file
|
After Width: | Height: | Size: 8.2 KiB |
1
manifest.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Main-Class: GameRunner
|
||||||