diff --git a/App.jar b/App.jar new file mode 100644 index 0000000..b8a4ab6 Binary files /dev/null and b/App.jar differ diff --git a/GameRunner$1.class b/GameRunner$1.class new file mode 100644 index 0000000..db31050 Binary files /dev/null and b/GameRunner$1.class differ diff --git a/GameRunner.class b/GameRunner.class new file mode 100644 index 0000000..50d042e Binary files /dev/null and b/GameRunner.class differ diff --git a/GameRunner.java b/GameRunner.java new file mode 100644 index 0000000..7747907 --- /dev/null +++ b/GameRunner.java @@ -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(); + } + }); + } +} diff --git a/GridDialog$1$1$1.class b/GridDialog$1$1$1.class new file mode 100644 index 0000000..b6fdac1 Binary files /dev/null and b/GridDialog$1$1$1.class differ diff --git a/GridDialog$1$1$2.class b/GridDialog$1$1$2.class new file mode 100644 index 0000000..1277ba3 Binary files /dev/null and b/GridDialog$1$1$2.class differ diff --git a/GridDialog$1$1$3.class b/GridDialog$1$1$3.class new file mode 100644 index 0000000..4ff64ad Binary files /dev/null and b/GridDialog$1$1$3.class differ diff --git a/GridDialog$1$1$4.class b/GridDialog$1$1$4.class new file mode 100644 index 0000000..9327555 Binary files /dev/null and b/GridDialog$1$1$4.class differ diff --git a/GridDialog$1$1$5.class b/GridDialog$1$1$5.class new file mode 100644 index 0000000..82f2a48 Binary files /dev/null and b/GridDialog$1$1$5.class differ diff --git a/GridDialog$1$1$6.class b/GridDialog$1$1$6.class new file mode 100644 index 0000000..a46d39e Binary files /dev/null and b/GridDialog$1$1$6.class differ diff --git a/GridDialog$1$1$7.class b/GridDialog$1$1$7.class new file mode 100644 index 0000000..b3651c3 Binary files /dev/null and b/GridDialog$1$1$7.class differ diff --git a/GridDialog$1$1.class b/GridDialog$1$1.class new file mode 100644 index 0000000..b0c2bbd Binary files /dev/null and b/GridDialog$1$1.class differ diff --git a/GridDialog$1$2.class b/GridDialog$1$2.class new file mode 100644 index 0000000..aa3d8b3 Binary files /dev/null and b/GridDialog$1$2.class differ diff --git a/GridDialog$1$3.class b/GridDialog$1$3.class new file mode 100644 index 0000000..c1bba6c Binary files /dev/null and b/GridDialog$1$3.class differ diff --git a/GridDialog$1$4.class b/GridDialog$1$4.class new file mode 100644 index 0000000..66173f9 Binary files /dev/null and b/GridDialog$1$4.class differ diff --git a/GridDialog$1$5.class b/GridDialog$1$5.class new file mode 100644 index 0000000..c897931 Binary files /dev/null and b/GridDialog$1$5.class differ diff --git a/GridDialog$1.class b/GridDialog$1.class new file mode 100644 index 0000000..d027f88 Binary files /dev/null and b/GridDialog$1.class differ diff --git a/GridDialog$2.class b/GridDialog$2.class new file mode 100644 index 0000000..f30c927 Binary files /dev/null and b/GridDialog$2.class differ diff --git a/GridDialog.class b/GridDialog.class new file mode 100644 index 0000000..3a399bd Binary files /dev/null and b/GridDialog.class differ diff --git a/GridDialog.java b/GridDialog.java new file mode 100644 index 0000000..5ab6f5b --- /dev/null +++ b/GridDialog.java @@ -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; + } +} diff --git a/Main$1.class b/Main$1.class new file mode 100644 index 0000000..b6d83b5 Binary files /dev/null and b/Main$1.class differ diff --git a/Main$2.class b/Main$2.class new file mode 100644 index 0000000..0f9b64d Binary files /dev/null and b/Main$2.class differ diff --git a/Main$3.class b/Main$3.class new file mode 100644 index 0000000..fec6e18 Binary files /dev/null and b/Main$3.class differ diff --git a/Main$4.class b/Main$4.class new file mode 100644 index 0000000..6723250 Binary files /dev/null and b/Main$4.class differ diff --git a/Main.class b/Main.class new file mode 100644 index 0000000..74bbbf8 Binary files /dev/null and b/Main.class differ diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..5e1c7e9 --- /dev/null +++ b/Main.java @@ -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; + } +} diff --git a/card.png b/card.png new file mode 100644 index 0000000..5255c22 Binary files /dev/null and b/card.png differ diff --git a/cardFront.png b/cardFront.png new file mode 100644 index 0000000..37d3e26 Binary files /dev/null and b/cardFront.png differ diff --git a/compilerRunner.bat b/compilerRunner.bat new file mode 100644 index 0000000..4b863e8 --- /dev/null +++ b/compilerRunner.bat @@ -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 diff --git a/compilerRunnerSchool.bat b/compilerRunnerSchool.bat new file mode 100644 index 0000000..d2eaccc --- /dev/null +++ b/compilerRunnerSchool.bat @@ -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 diff --git a/icons/baby.png b/icons/baby.png new file mode 100644 index 0000000..4b768ff Binary files /dev/null and b/icons/baby.png differ diff --git a/icons/banana.png b/icons/banana.png new file mode 100644 index 0000000..f2af466 Binary files /dev/null and b/icons/banana.png differ diff --git a/icons/bed.png b/icons/bed.png new file mode 100644 index 0000000..3d0cf46 Binary files /dev/null and b/icons/bed.png differ diff --git a/icons/boat.png b/icons/boat.png new file mode 100644 index 0000000..c2a362b Binary files /dev/null and b/icons/boat.png differ diff --git a/icons/book.png b/icons/book.png new file mode 100644 index 0000000..4c49647 Binary files /dev/null and b/icons/book.png differ diff --git a/icons/bottle.png b/icons/bottle.png new file mode 100644 index 0000000..33132c1 Binary files /dev/null and b/icons/bottle.png differ diff --git a/icons/bow.png b/icons/bow.png new file mode 100644 index 0000000..af1d610 Binary files /dev/null and b/icons/bow.png differ diff --git a/icons/bowl.png b/icons/bowl.png new file mode 100644 index 0000000..15211fd Binary files /dev/null and b/icons/bowl.png differ diff --git a/icons/bread.png b/icons/bread.png new file mode 100644 index 0000000..40cee5c Binary files /dev/null and b/icons/bread.png differ diff --git a/icons/brush.png b/icons/brush.png new file mode 100644 index 0000000..f393e0b Binary files /dev/null and b/icons/brush.png differ diff --git a/icons/camera.png b/icons/camera.png new file mode 100644 index 0000000..9937e0c Binary files /dev/null and b/icons/camera.png differ diff --git a/icons/candy.png b/icons/candy.png new file mode 100644 index 0000000..d3e248a Binary files /dev/null and b/icons/candy.png differ diff --git a/icons/car.png b/icons/car.png new file mode 100644 index 0000000..d26df53 Binary files /dev/null and b/icons/car.png differ diff --git a/icons/cat.png b/icons/cat.png new file mode 100644 index 0000000..1004610 Binary files /dev/null and b/icons/cat.png differ diff --git a/icons/chain.png b/icons/chain.png new file mode 100644 index 0000000..584aa14 Binary files /dev/null and b/icons/chain.png differ diff --git a/icons/chalk.png b/icons/chalk.png new file mode 100644 index 0000000..6a1fa48 Binary files /dev/null and b/icons/chalk.png differ diff --git a/icons/clock.png b/icons/clock.png new file mode 100644 index 0000000..22d60a2 Binary files /dev/null and b/icons/clock.png differ diff --git a/icons/computer.png b/icons/computer.png new file mode 100644 index 0000000..06a6199 Binary files /dev/null and b/icons/computer.png differ diff --git a/icons/cord.png b/icons/cord.png new file mode 100644 index 0000000..56f9b40 Binary files /dev/null and b/icons/cord.png differ diff --git a/icons/cork.png b/icons/cork.png new file mode 100644 index 0000000..05111c9 Binary files /dev/null and b/icons/cork.png differ diff --git a/icons/desk.png b/icons/desk.png new file mode 100644 index 0000000..0685f24 Binary files /dev/null and b/icons/desk.png differ diff --git a/icons/door.png b/icons/door.png new file mode 100644 index 0000000..0105295 Binary files /dev/null and b/icons/door.png differ diff --git a/icons/dot.png b/icons/dot.png new file mode 100644 index 0000000..f93c6d6 Binary files /dev/null and b/icons/dot.png differ diff --git a/icons/elephant.png b/icons/elephant.png new file mode 100644 index 0000000..ec87287 Binary files /dev/null and b/icons/elephant.png differ diff --git a/icons/eraser.png b/icons/eraser.png new file mode 100644 index 0000000..355b58e Binary files /dev/null and b/icons/eraser.png differ diff --git a/icons/fan.png b/icons/fan.png new file mode 100644 index 0000000..51f93fa Binary files /dev/null and b/icons/fan.png differ diff --git a/icons/fork.png b/icons/fork.png new file mode 100644 index 0000000..cbd2811 Binary files /dev/null and b/icons/fork.png differ diff --git a/icons/gameServer.java b/icons/gameServer.java new file mode 100644 index 0000000..a453c94 --- /dev/null +++ b/icons/gameServer.java @@ -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) {} + } + } + } +} diff --git a/icons/glasses.png b/icons/glasses.png new file mode 100644 index 0000000..dd8417e Binary files /dev/null and b/icons/glasses.png differ diff --git a/icons/hammer.png b/icons/hammer.png new file mode 100644 index 0000000..3fe1b80 Binary files /dev/null and b/icons/hammer.png differ diff --git a/icons/key.png b/icons/key.png new file mode 100644 index 0000000..0f74f01 Binary files /dev/null and b/icons/key.png differ diff --git a/icons/keyboard.png b/icons/keyboard.png new file mode 100644 index 0000000..08fd09d Binary files /dev/null and b/icons/keyboard.png differ diff --git a/icons/mirror.png b/icons/mirror.png new file mode 100644 index 0000000..eb14f9d Binary files /dev/null and b/icons/mirror.png differ diff --git a/icons/money.png b/icons/money.png new file mode 100644 index 0000000..642ab1b Binary files /dev/null and b/icons/money.png differ diff --git a/icons/nail.png b/icons/nail.png new file mode 100644 index 0000000..a4b845a Binary files /dev/null and b/icons/nail.png differ diff --git a/icons/paper.png b/icons/paper.png new file mode 100644 index 0000000..cab7d48 Binary files /dev/null and b/icons/paper.png differ diff --git a/icons/pencil.png b/icons/pencil.png new file mode 100644 index 0000000..b312752 Binary files /dev/null and b/icons/pencil.png differ diff --git a/icons/person.png b/icons/person.png new file mode 100644 index 0000000..7e1822b Binary files /dev/null and b/icons/person.png differ diff --git a/icons/phone.png b/icons/phone.png new file mode 100644 index 0000000..4204923 Binary files /dev/null and b/icons/phone.png differ diff --git a/icons/purse.png b/icons/purse.png new file mode 100644 index 0000000..4f08db8 Binary files /dev/null and b/icons/purse.png differ diff --git a/icons/ring.png b/icons/ring.png new file mode 100644 index 0000000..b444716 Binary files /dev/null and b/icons/ring.png differ diff --git a/icons/screw.png b/icons/screw.png new file mode 100644 index 0000000..9e0d2fd Binary files /dev/null and b/icons/screw.png differ diff --git a/icons/sign.png b/icons/sign.png new file mode 100644 index 0000000..b276762 Binary files /dev/null and b/icons/sign.png differ diff --git a/icons/sock.png b/icons/sock.png new file mode 100644 index 0000000..992a9d9 Binary files /dev/null and b/icons/sock.png differ diff --git a/icons/sponge.png b/icons/sponge.png new file mode 100644 index 0000000..4112e73 Binary files /dev/null and b/icons/sponge.png differ diff --git a/icons/spoon.png b/icons/spoon.png new file mode 100644 index 0000000..4fceb87 Binary files /dev/null and b/icons/spoon.png differ diff --git a/icons/table.png b/icons/table.png new file mode 100644 index 0000000..9bc300a Binary files /dev/null and b/icons/table.png differ diff --git a/icons/thread.png b/icons/thread.png new file mode 100644 index 0000000..9408104 Binary files /dev/null and b/icons/thread.png differ diff --git a/icons/tomato.png b/icons/tomato.png new file mode 100644 index 0000000..b7ca6b0 Binary files /dev/null and b/icons/tomato.png differ diff --git a/icons/towel.png b/icons/towel.png new file mode 100644 index 0000000..352b489 Binary files /dev/null and b/icons/towel.png differ diff --git a/icons/toy.png b/icons/toy.png new file mode 100644 index 0000000..99ff6bf Binary files /dev/null and b/icons/toy.png differ diff --git a/icons/tree.png b/icons/tree.png new file mode 100644 index 0000000..bd6de46 Binary files /dev/null and b/icons/tree.png differ diff --git a/manifest.txt b/manifest.txt new file mode 100644 index 0000000..abdfb9f --- /dev/null +++ b/manifest.txt @@ -0,0 +1 @@ +Main-Class: GameRunner