diff --git a/MemoryGame/Lib/commons-lang-2.6.jar b/MemoryGame/Lib/commons-lang-2.6.jar new file mode 100644 index 00000000..98467d3a Binary files /dev/null and b/MemoryGame/Lib/commons-lang-2.6.jar differ diff --git a/MemoryGame/Lib/commons-logging-1.1.1.jar b/MemoryGame/Lib/commons-logging-1.1.1.jar new file mode 100644 index 00000000..115772f9 Binary files /dev/null and b/MemoryGame/Lib/commons-logging-1.1.1.jar differ diff --git a/MemoryGame/Lib/hsqldb.jar b/MemoryGame/Lib/hsqldb.jar new file mode 100644 index 00000000..aee69efd Binary files /dev/null and b/MemoryGame/Lib/hsqldb.jar differ diff --git a/MemoryGame/Lib/jackcess-2.0.6.jar b/MemoryGame/Lib/jackcess-2.0.6.jar new file mode 100644 index 00000000..cffe1d59 Binary files /dev/null and b/MemoryGame/Lib/jackcess-2.0.6.jar differ diff --git a/MemoryGame/Lib/ucanaccess-2.0.9.2.jar b/MemoryGame/Lib/ucanaccess-2.0.9.2.jar new file mode 100644 index 00000000..2304b586 Binary files /dev/null and b/MemoryGame/Lib/ucanaccess-2.0.9.2.jar differ diff --git a/MemoryGame/Rankings.accdb b/MemoryGame/Rankings.accdb new file mode 100644 index 00000000..36798578 Binary files /dev/null and b/MemoryGame/Rankings.accdb differ diff --git a/MemoryGame/Readme.md b/MemoryGame/Readme.md new file mode 100644 index 00000000..171e296f --- /dev/null +++ b/MemoryGame/Readme.md @@ -0,0 +1,32 @@ +## Objective +Develop a GUI based Java desktop application for the flip-flop memory game, also known as the concentration game. The application should allow players to test and improve their memory skills by finding matching pairs of cards. The game should utilize multi-threading to enhance performance, and it should incorporate file handling and database functionality based on your own business logic and project needs. + +## Requirements +### User Interface +- Design and implement a graphical user interface (GUI) for the game. +- Display a grid of cards/tiles, initially facedown. +- Each card/tile should have a distinct design or image on its face. +- Provide a score or timer display to track the player's progress. + +### Game Logic +- The game should have a set of cards/tiles with matching pairs. +- Randomly shuffle the cards/tiles before the game starts. +- Implement the flip functionality, allowing the player to click on a card/tile to flip it over and reveal its face. +- Implement the logic to check if the two flipped cards/tiles match or not. +- If the cards/tiles match, keep them face up and allow the player to continue their turn. +- If the cards/tiles do not match, flip them back face down after a short delay. +- Track the player's score or time taken to complete the game. + +### Multi-threading +- Identify a suitable module in the game where multi-threading can be implemented and implement multi-threading in the chosen module. + +### File Handling +- Implement file handling functionality based on your project's requirements. + +### Database Integration +- Utilize a database system (e.g., MySQL) based on your project's needs. Identify the relevant data to be stored in the database. + +### Error Handling +- Handle any unexpected errors or exceptions. + +Note: The specific implementation details and requirements for file handling, Database functionality, and Multi-Threading are left to the student based on their own business logic and project needs. diff --git a/MemoryGame/highscore.txt b/MemoryGame/highscore.txt new file mode 100644 index 00000000..0dd217ac --- /dev/null +++ b/MemoryGame/highscore.txt @@ -0,0 +1 @@ +880 \ No newline at end of file diff --git a/MemoryGame/shapes/2blankTile.png b/MemoryGame/shapes/2blankTile.png new file mode 100644 index 00000000..19c448c9 Binary files /dev/null and b/MemoryGame/shapes/2blankTile.png differ diff --git a/MemoryGame/shapes/Blank.png b/MemoryGame/shapes/Blank.png new file mode 100644 index 00000000..36d7484c Binary files /dev/null and b/MemoryGame/shapes/Blank.png differ diff --git a/MemoryGame/shapes/chick.png b/MemoryGame/shapes/chick.png new file mode 100644 index 00000000..adf551ab Binary files /dev/null and b/MemoryGame/shapes/chick.png differ diff --git a/MemoryGame/shapes/frog.png b/MemoryGame/shapes/frog.png new file mode 100644 index 00000000..75fb9c03 Binary files /dev/null and b/MemoryGame/shapes/frog.png differ diff --git a/MemoryGame/shapes/game-over.png b/MemoryGame/shapes/game-over.png new file mode 100644 index 00000000..f063ac2a Binary files /dev/null and b/MemoryGame/shapes/game-over.png differ diff --git a/MemoryGame/shapes/ghost.png b/MemoryGame/shapes/ghost.png new file mode 100644 index 00000000..5a1c15c8 Binary files /dev/null and b/MemoryGame/shapes/ghost.png differ diff --git a/MemoryGame/shapes/giraffe.png b/MemoryGame/shapes/giraffe.png new file mode 100644 index 00000000..7f48d70e Binary files /dev/null and b/MemoryGame/shapes/giraffe.png differ diff --git a/MemoryGame/shapes/high-score.png b/MemoryGame/shapes/high-score.png new file mode 100644 index 00000000..33a34777 Binary files /dev/null and b/MemoryGame/shapes/high-score.png differ diff --git a/MemoryGame/shapes/lion.png b/MemoryGame/shapes/lion.png new file mode 100644 index 00000000..7075044b Binary files /dev/null and b/MemoryGame/shapes/lion.png differ diff --git a/MemoryGame/shapes/owl.png b/MemoryGame/shapes/owl.png new file mode 100644 index 00000000..1c45d1b1 Binary files /dev/null and b/MemoryGame/shapes/owl.png differ diff --git a/MemoryGame/shapes/panda.png b/MemoryGame/shapes/panda.png new file mode 100644 index 00000000..6f7172ca Binary files /dev/null and b/MemoryGame/shapes/panda.png differ diff --git a/MemoryGame/shapes/teddy-bear.png b/MemoryGame/shapes/teddy-bear.png new file mode 100644 index 00000000..8af8dc03 Binary files /dev/null and b/MemoryGame/shapes/teddy-bear.png differ diff --git a/MemoryGame/shapes/win.png b/MemoryGame/shapes/win.png new file mode 100644 index 00000000..c7b87792 Binary files /dev/null and b/MemoryGame/shapes/win.png differ diff --git a/MemoryGame/src/main/java/com/mycompany/flipflopgame/FlipFlopGame.form b/MemoryGame/src/main/java/com/mycompany/flipflopgame/FlipFlopGame.form new file mode 100644 index 00000000..aef3a0dc --- /dev/null +++ b/MemoryGame/src/main/java/com/mycompany/flipflopgame/FlipFlopGame.form @@ -0,0 +1,617 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MemoryGame/src/main/java/com/mycompany/flipflopgame/FlipFlopGame.java b/MemoryGame/src/main/java/com/mycompany/flipflopgame/FlipFlopGame.java new file mode 100644 index 00000000..6b9cbc35 --- /dev/null +++ b/MemoryGame/src/main/java/com/mycompany/flipflopgame/FlipFlopGame.java @@ -0,0 +1,1191 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ +package main.java.com.mycompany.flipflopgame; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import javax.swing.ImageIcon; +import java.sql.*; +import javax.swing.JOptionPane; + +/** + * + * @author Imaan + */ +public class FlipFlopGame extends javax.swing.JFrame { + + /** + * Creates new form FlipFlopGame + */ + public FlipFlopGame() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + Score = new javax.swing.JTextField(); + jLabel1 = new javax.swing.JLabel(); + startGameButtonActionPerformed = new javax.swing.JButton(); + QuitButtonActionPerformed = new javax.swing.JButton(); + Tile12 = new javax.swing.JButton(); + Tile11 = new javax.swing.JButton(); + Tile13 = new javax.swing.JButton(); + Tile14 = new javax.swing.JButton(); + Tile15 = new javax.swing.JButton(); + Tile16 = new javax.swing.JButton(); + Tile17 = new javax.swing.JButton(); + Tile18 = new javax.swing.JButton(); + Tile19 = new javax.swing.JButton(); + Tile20 = new javax.swing.JButton(); + Tile21 = new javax.swing.JButton(); + Tile22 = new javax.swing.JButton(); + Tile23 = new javax.swing.JButton(); + Tile24 = new javax.swing.JButton(); + Tile25 = new javax.swing.JButton(); + Tile26 = new javax.swing.JButton(); + PairsRecord = new javax.swing.JLabel(); + PairsLeft = new javax.swing.JTextField(); + jLabel2 = new javax.swing.JLabel(); + HighScore = new javax.swing.JTextField(); + jLabel3 = new javax.swing.JLabel(); + Score2 = new javax.swing.JTextField(); + Rankings = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + Score.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N + Score.setText("0"); + Score.setToolTipText(""); + Score.setAlignmentX(0.0F); + Score.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); + Score.setDisabledTextColor(new java.awt.Color(0, 0, 0)); + Score.setEnabled(false); + Score.setFocusable(false); + Score.setSelectedTextColor(new java.awt.Color(0, 0, 0)); + Score.setSelectionEnd(0); + Score.setSelectionStart(0); + Score.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + ScoreActionPerformed(evt); + } + }); + + jLabel1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N + jLabel1.setText("Points:"); + jLabel1.setAlignmentX(0.5F); + jLabel1.setMaximumSize(new java.awt.Dimension(54, 19)); + jLabel1.setMinimumSize(new java.awt.Dimension(54, 19)); + jLabel1.setPreferredSize(new java.awt.Dimension(54, 19)); + + startGameButtonActionPerformed.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N + startGameButtonActionPerformed.setText("Start"); + startGameButtonActionPerformed.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + startGameButtonActionPerformedActionPerformed(evt); + } + }); + + QuitButtonActionPerformed.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N + QuitButtonActionPerformed.setText("Quit"); + QuitButtonActionPerformed.setToolTipText(""); + QuitButtonActionPerformed.setPreferredSize(new java.awt.Dimension(55, 20)); + QuitButtonActionPerformed.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + QuitButtonActionPerformedActionPerformed(evt); + } + }); + + Tile12.setEnabled(false); + Tile12.setFocusable(false); + Tile12.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile12.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile12.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile12ActionPerformed(evt); + } + }); + + Tile11.setEnabled(false); + Tile11.setFocusable(false); + Tile11.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile11.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile11.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile11ActionPerformed(evt); + } + }); + + Tile13.setEnabled(false); + Tile13.setFocusable(false); + Tile13.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile13.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile13.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile13ActionPerformed(evt); + } + }); + + Tile14.setEnabled(false); + Tile14.setFocusable(false); + Tile14.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile14.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile14.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile14ActionPerformed(evt); + } + }); + + Tile15.setEnabled(false); + Tile15.setFocusable(false); + Tile15.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile15.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile15.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile15ActionPerformed(evt); + } + }); + + Tile16.setEnabled(false); + Tile16.setFocusable(false); + Tile16.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile16.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile16.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile16ActionPerformed(evt); + } + }); + + Tile17.setEnabled(false); + Tile17.setFocusable(false); + Tile17.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile17.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile17.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile17ActionPerformed(evt); + } + }); + + Tile18.setEnabled(false); + Tile18.setFocusable(false); + Tile18.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile18.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile18.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile18ActionPerformed(evt); + } + }); + + Tile19.setEnabled(false); + Tile19.setFocusable(false); + Tile19.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile19.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile19.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile19ActionPerformed(evt); + } + }); + + Tile20.setEnabled(false); + Tile20.setFocusable(false); + Tile20.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile20.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile20.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile20ActionPerformed(evt); + } + }); + + Tile21.setEnabled(false); + Tile21.setFocusable(false); + Tile21.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile21.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile21.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile21ActionPerformed(evt); + } + }); + + Tile22.setEnabled(false); + Tile22.setFocusable(false); + Tile22.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile22.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile22.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile22ActionPerformed(evt); + } + }); + + Tile23.setEnabled(false); + Tile23.setFocusable(false); + Tile23.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile23.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile23.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile23ActionPerformed(evt); + } + }); + + Tile24.setEnabled(false); + Tile24.setFocusable(false); + Tile24.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile24.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile24.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile24ActionPerformed(evt); + } + }); + + Tile25.setEnabled(false); + Tile25.setFocusable(false); + Tile25.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile25.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile25.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile25ActionPerformed(evt); + } + }); + + Tile26.setEnabled(false); + Tile26.setFocusable(false); + Tile26.setMaximumSize(new java.awt.Dimension(32, 10)); + Tile26.setMinimumSize(new java.awt.Dimension(32, 10)); + Tile26.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Tile26ActionPerformed(evt); + } + }); + + PairsRecord.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N + PairsRecord.setText("Left Pairs:"); + PairsRecord.setAlignmentX(0.5F); + PairsRecord.setMaximumSize(new java.awt.Dimension(54, 19)); + PairsRecord.setMinimumSize(new java.awt.Dimension(54, 19)); + PairsRecord.setPreferredSize(new java.awt.Dimension(54, 19)); + + PairsLeft.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N + PairsLeft.setText("0"); + PairsLeft.setToolTipText(""); + PairsLeft.setAlignmentX(0.0F); + PairsLeft.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); + PairsLeft.setDisabledTextColor(new java.awt.Color(0, 0, 0)); + PairsLeft.setEnabled(false); + PairsLeft.setFocusable(false); + PairsLeft.setSelectedTextColor(new java.awt.Color(0, 0, 0)); + PairsLeft.setSelectionEnd(0); + PairsLeft.setSelectionStart(0); + PairsLeft.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + PairsLeftActionPerformed(evt); + } + }); + + jLabel2.setFont(new java.awt.Font("Arial", 0, 15)); // NOI18N + jLabel2.setText("High Score:"); + jLabel2.setAlignmentX(0.5F); + jLabel2.setMaximumSize(new java.awt.Dimension(54, 19)); + jLabel2.setMinimumSize(new java.awt.Dimension(54, 19)); + jLabel2.setPreferredSize(new java.awt.Dimension(54, 19)); + + HighScore.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N + HighScore.setText("0"); + HighScore.setToolTipText(""); + HighScore.setAlignmentX(0.0F); + HighScore.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); + HighScore.setDisabledTextColor(new java.awt.Color(0, 0, 0)); + HighScore.setEnabled(false); + HighScore.setFocusable(false); + HighScore.setSelectedTextColor(new java.awt.Color(0, 0, 0)); + HighScore.setSelectionEnd(0); + HighScore.setSelectionStart(0); + HighScore.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + HighScoreActionPerformed(evt); + } + }); + + jLabel3.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N + jLabel3.setText("Score:"); + jLabel3.setAlignmentX(0.5F); + jLabel3.setMaximumSize(new java.awt.Dimension(54, 19)); + jLabel3.setMinimumSize(new java.awt.Dimension(54, 19)); + jLabel3.setPreferredSize(new java.awt.Dimension(54, 19)); + + Score2.setFont(new java.awt.Font("Dialog", 0, 24)); // NOI18N + Score2.setText("0"); + Score2.setToolTipText(""); + Score2.setAlignmentX(0.0F); + Score2.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR)); + Score2.setDisabledTextColor(new java.awt.Color(0, 0, 0)); + Score2.setEnabled(false); + Score2.setFocusable(false); + Score2.setSelectedTextColor(new java.awt.Color(0, 0, 0)); + Score2.setSelectionEnd(0); + Score2.setSelectionStart(0); + Score2.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + Score2ActionPerformed(evt); + } + }); + + Rankings.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N + Rankings.setText("Rankings"); + Rankings.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + RankingsActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(22, 22, 22) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(Tile23, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile24, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile25, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile26, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(Tile19, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile20, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile21, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile22, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(Tile15, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile16, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile17, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile18, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(Tile11, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile12, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile13, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Tile14, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Score, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(PairsRecord, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(Score2, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addComponent(HighScore) + .addComponent(PairsLeft, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) + .addGroup(layout.createSequentialGroup() + .addComponent(startGameButtonActionPerformed, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(QuitButtonActionPerformed, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(Rankings, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))) + .addContainerGap(18, Short.MAX_VALUE)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addGap(19, 19, 19) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(HighScore, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(Score2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(Rankings, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(startGameButtonActionPerformed, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(Score, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(PairsRecord, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(PairsLeft, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(QuitButtonActionPerformed, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(Tile12, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile13, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile11, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile14, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(Tile15, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile16, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile17, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile18, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(Tile19, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile20, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile21, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile22, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(Tile23, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile24, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile25, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(Tile26, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(70, 70, 70)) + ); + + Score.getAccessibleContext().setAccessibleName(""); + + pack(); + }// //GEN-END:initComponents + + private void ScoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ScoreActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_ScoreActionPerformed + + private void loadHighScore() { + String filePath = ".//highscore.txt"; + + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + String line = reader.readLine(); + if (line != null) { + highScore = Integer.parseInt(line.trim()); + } + } catch (IOException e) { + e.printStackTrace(); + } +} + + private void startGameButtonActionPerformedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startGameButtonActionPerformedActionPerformed + + UserGuess1 = 404; + UserGuess2 = 404; + match = false; + count = 0; + points = 0; + LeftPairs = 16; + score = 0; + + Score.setText(Integer.toString(points)); + PairsLeft.setText(Integer.toString(LeftPairs)); + + //setting position to each tile + generateRandomTiles(); + + //show shapes for some seconds + showGlimpse(); + + //allow user to flip tiles + enableButtons(); + loadHighScore(); + }//GEN-LAST:event_startGameButtonActionPerformedActionPerformed + + private void Tile11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile11ActionPerformed + int position = 0; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile11ActionPerformed + + private void Tile12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile12ActionPerformed + int position = 1; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile12ActionPerformed + + private void Tile13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile13ActionPerformed + int position = 2; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile13ActionPerformed + + private void Tile14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile14ActionPerformed + int position = 3; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile14ActionPerformed + + private void Tile15ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile15ActionPerformed + int position = 4; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile15ActionPerformed + + private void Tile16ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile16ActionPerformed + int position = 5; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile16ActionPerformed + + private void Tile17ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile17ActionPerformed + int position = 6; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile17ActionPerformed + + private void Tile18ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile18ActionPerformed + int position = 7; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile18ActionPerformed + + private void Tile19ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile19ActionPerformed + int position = 8; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile19ActionPerformed + + private void Tile20ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile20ActionPerformed + int position = 9; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile20ActionPerformed + + private void Tile21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile21ActionPerformed + int position = 10; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile21ActionPerformed + + private void Tile22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile22ActionPerformed + int position = 11; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile22ActionPerformed + + private void Tile23ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile23ActionPerformed + int position = 12; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile23ActionPerformed + + private void Tile24ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile24ActionPerformed + int position = 13; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile24ActionPerformed + + private void Tile25ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile25ActionPerformed + int position = 14; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile25ActionPerformed + + private void Tile26ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Tile26ActionPerformed + int position = 15; + int type; + + type = tiles.getTileType(position); + buttonPress(position, type); + }//GEN-LAST:event_Tile26ActionPerformed + + private void PairsLeftActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PairsLeftActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_PairsLeftActionPerformed + + private void QuitButtonActionPerformedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuitButtonActionPerformedActionPerformed + System.exit(0); + }//GEN-LAST:event_QuitButtonActionPerformedActionPerformed + + private void HighScoreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HighScoreActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_HighScoreActionPerformed + + private void Score2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Score2ActionPerformed + // TODO add your handling code here: + }//GEN-LAST:event_Score2ActionPerformed + + private void RankingsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RankingsActionPerformed + + Connection conn = null; + Statement stmt = null; + ArrayList scoresList = new ArrayList<>(); + ArrayList namesList = new ArrayList<>(); + + try{ + + conn = DriverManager.getConnection("jdbc:ucanaccess://.\\Rankings.accdb"); + stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("Select * from Userdata"); + + while(rs.next()){ + scoresList.add(rs.getInt("Score")); + namesList.add(rs.getString("Username")); + } + RankingsTable table = new RankingsTable(); + table.setRankings(namesList, scoresList); + table.setVisible(true); + rs.close(); + stmt.close(); + conn.close(); + + }catch(Exception e){ + e.printStackTrace(); + } + }//GEN-LAST:event_RankingsActionPerformed + + void enableButtons(){ + Tile11.setEnabled(true); + Tile12.setEnabled(true); + Tile13.setEnabled(true); + Tile14.setEnabled(true); + Tile15.setEnabled(true); + Tile16.setEnabled(true); + Tile17.setEnabled(true); + Tile18.setEnabled(true); + Tile19.setEnabled(true); + Tile20.setEnabled(true); + Tile21.setEnabled(true); + Tile22.setEnabled(true); + Tile23.setEnabled(true); + Tile24.setEnabled(true); + Tile25.setEnabled(true); + Tile26.setEnabled(true); + } + //Sets the icons to all of the buttons to BlankTile.png. Doesn't reset the actual game. + private void clearBoard(){ + Tile11.setIcon(Blank); + Tile12.setIcon(Blank); + Tile13.setIcon(Blank); + Tile14.setIcon(Blank); + Tile15.setIcon(Blank); + Tile16.setIcon(Blank); + Tile17.setIcon(Blank); + Tile18.setIcon(Blank); + Tile19.setIcon(Blank); + Tile20.setIcon(Blank); + Tile21.setIcon(Blank); + Tile22.setIcon(Blank); + Tile23.setIcon(Blank); + Tile24.setIcon(Blank); + Tile25.setIcon(Blank); + Tile26.setIcon(Blank); + } + + void generateRandomTiles(){ + //generate random #'s and assign them to shapes as IDs. + tiles.initializePostion(); + for (int i = 0; i<= 15; i++){ + System.out.print(i + ": "); + int type = tiles.getTileType(i); + } + } + + private void showTileShape(int ID, int type){ + // id is position of shape in board, type is which shape it is + ArrayList shapes = new ArrayList(); + shapes.add(Chick); + shapes.add(Ghost); + shapes.add(panda); + shapes.add(Teddy); + shapes.add(Giraffe); + shapes.add(Frog); + shapes.add(Owl); + shapes.add(Lion); + + //now based on the position passed, the tile at that case will search the array using type, + //and set the icon to the one it matches. + switch(ID){ + // for type = 0 set circle + case 0: + // returns data at this index i.e return Chick if type is + Tile11.setIcon(shapes.get(type)); + break; + case 1: + Tile12.setIcon(shapes.get(type)); + break; + case 2: + Tile13.setIcon(shapes.get(type)); + break; + case 3: + Tile14.setIcon(shapes.get(type)); + break; + case 4: + Tile15.setIcon(shapes.get(type)); + break; + case 5: + Tile16.setIcon(shapes.get(type)); + break; + case 6: + Tile17.setIcon(shapes.get(type)); + break; + case 7: + Tile18.setIcon(shapes.get(type)); + break; + case 8: + Tile19.setIcon(shapes.get(type)); + break; + case 9: + Tile20.setIcon(shapes.get(type)); + break; + case 10: + Tile21.setIcon(shapes.get(type)); + break; + case 11: + Tile22.setIcon(shapes.get(type)); + break; + case 12: + Tile23.setIcon(shapes.get(type)); + break; + case 13: + Tile24.setIcon(shapes.get(type)); + break; + case 14: + Tile25.setIcon(shapes.get(type)); + break; + case 15: + Tile26.setIcon(shapes.get(type)); + break; + } + } + + void showGlimpse() { + System.out.println("Memorize the tiles"); + + Thread thread = new Thread(() -> { + try { + for (int i = 0; i <= 15; i++) { + int type = tiles.getTileType(i); + showTileShape(i, type); + } + Thread.sleep(1000); + clearBoard(); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + }); + + thread.start(); + + System.out.println("Now guess the tiles."); + } + + //Shows the shapes to the player for a small duration of time, then trturns the buttons to blank again. + private void ShowShapes(){ + for (int i = 0; i<= 15; i++){ + int type = tiles.getTileType(i); + showTileShape(i, type); + } + } + + //Hide the shape and set icon to blank a particular button/Tile. + private void hideSelectedTile(int ID){ + switch(ID){ + case 0: + Tile11.setIcon(Blank); + break; + case 1: + Tile12.setIcon(Blank); + break; + case 2: + Tile13.setIcon(Blank); + break; + case 3: + Tile14.setIcon(Blank); + break; + case 4: + Tile15.setIcon(Blank); + break; + case 5: + Tile16.setIcon(Blank); + break; + case 6: + Tile17.setIcon(Blank); + break; + case 7: + Tile18.setIcon(Blank); + break; + case 8: + Tile19.setIcon(Blank); + break; + case 9: + Tile20.setIcon(Blank); + break; + case 10: + Tile21.setIcon(Blank); + break; + case 11: + Tile22.setIcon(Blank); + break; + case 12: + Tile23.setIcon(Blank); + break; + case 13: + Tile24.setIcon(Blank); + break; + case 14: + Tile25.setIcon(Blank); + break; + case 15: + Tile26.setIcon(Blank); + break; + } + } + + // allow only those buttons to user that are not matched yet set remaining disbale + //if state is false it will disable the button + private void DisableButtons(boolean state, int ID){ + switch(ID){ + case 0: + Tile11.setEnabled(state); + break; + case 1: + Tile12.setEnabled(state); + break; + case 2: + Tile13.setEnabled(state); + break; + case 3: + Tile14.setEnabled(state); + break; + case 4: + Tile15.setEnabled(state); + break; + case 5: + Tile16.setEnabled(state); + break; + case 6: + Tile17.setEnabled(state); + break; + case 7: + Tile18.setEnabled(state); + break; + case 8: + Tile19.setEnabled(state); + break; + case 9: + Tile20.setEnabled(state); + break; + case 10: + Tile21.setEnabled(state); + break; + case 11: + Tile22.setEnabled(state); + break; + case 12: + Tile23.setEnabled(state); + break; + case 13: + Tile24.setEnabled(state); + break; + case 14: + Tile25.setEnabled(state); + break; + case 15: + Tile26.setEnabled(state); + break; + } + } + + //do the 2 buttons chosen have matching shapes? + private boolean matchingTiles(int type1, int type2){ + boolean match = false; + + if (type1 == type2){ + match = true; + } + + return match; + } + + boolean checkAllDisable(){ + if(!Tile11.isEnabled() && !Tile12.isEnabled() && !Tile13.isEnabled() && !Tile14.isEnabled() && + !Tile15.isEnabled() && !Tile16.isEnabled() && !Tile17.isEnabled() && !Tile18.isEnabled() && + !Tile19.isEnabled() && !Tile20.isEnabled() && !Tile21.isEnabled() && !Tile22.isEnabled() && + !Tile23.isEnabled() && !Tile24.isEnabled() && !Tile25.isEnabled() && !Tile26.isEnabled()) + return true; + return false; + } + + private void updateHighScore(){ + if(score>highScore){ + JOptionPane.showMessageDialog(rootPane,highScoreMessage+score,"High Score!",1, HIGHSCORE); + highScore = score; + writeHighScore(); + } + } + + private void writeHighScore(){ + String filePath = ".\\highscore.txt"; + + try { + FileWriter fileWriter = new FileWriter(filePath); + fileWriter.write(Integer.toString(highScore)); + fileWriter.close(); + System.out.println("highScore stored successfully in the file."); + } catch (IOException e) { + e.printStackTrace(); + } + } + + private void getUserName() + { + while(username == null || username.equals("") || username.length() > 10) + { + username = JOptionPane.showInputDialog(this, "Enter your name:", "Input Name", JOptionPane.PLAIN_MESSAGE); + if(username.length() > 10) + JOptionPane.showMessageDialog(this, "Name too long", "Error", JOptionPane.INFORMATION_MESSAGE); + } + } + + void addToDatabase(){ + Connection conn = null; + + try { + conn = DriverManager.getConnection("jdbc:ucanaccess://.\\Rankings.accdb"); + String insertSql = "INSERT INTO Userdata (Username, Score) VALUES (?, ?)"; + PreparedStatement insertStmt = conn.prepareStatement(insertSql); + insertStmt.setString(1, username); // Set the username parameter value + insertStmt.setInt(2, score); // Set GamesPlayed to 0 + + // Execute the insert statement + insertStmt.executeUpdate(); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + //All tiles occupied of not + private void winCheck(){ + if(checkAllDisable()){ + score = (LeftPairs*100)+(points)*10; + JOptionPane.showMessageDialog(rootPane,winMessage+score,"You Win!",1, WIN); + Score2.setText(String.valueOf(score)); + updateHighScore(); + HighScore.setText(String.valueOf(highScore)); + getUserName(); + addToDatabase(); + } + } + + private void loseCheck(){ + + if(LeftPairs<=0){ + System.out.println("============GAME=OVER===========\n"); + JOptionPane.showMessageDialog(rootPane,loseMessage,"You Lose!",1, GAMEOVER); + } + } + + void waitAndHideTile(int id1, int id2){ + Thread thread = new Thread(() -> { + try { + Thread.sleep(1000); + hideSelectedTile(id1); + hideSelectedTile(id2); + } catch (InterruptedException ex) { + ex.printStackTrace(); + } + }); + + thread.start(); + } + + //every tile that is pressed on the board uses this in order to make decisions in the game. + private void buttonPress(int ID, int type){ + if (count == 0){ + showTileShape(ID, type); + UserGuess1 = ID; + Type1 = type; + count++; + + } + else if (count == 1){ + System.out.println("show tile"); + UserGuess2 = ID; + Type2 = type; + LeftPairs = LeftPairs-1; + sLeftPairs = Integer.toString(LeftPairs); + PairsLeft.setText(sLeftPairs); + + //one tile cannot be selected twice + if (UserGuess1 == UserGuess2){ + //dialog box containing a message to show warnings to the user. + JOptionPane.showMessageDialog( + //parent component of dialogue box, 1 = width, null = icons + rootPane,ErrorMessage, ErrorTitle, 1, null); + hideSelectedTile(UserGuess1); + UserGuess1 = 404; + UserGuess2 = 404; + DisableButtons(true,UserGuess1); + } + else { + showTileShape(ID, type); + match = matchingTiles(Type1, Type2); + + if (!match){ + waitAndHideTile(UserGuess1, UserGuess2); + spoints = Integer.toString(points); + Score.setText(spoints); + } + else{ + points = points + 1; + spoints = Integer.toString(points); + Score.setText(spoints); + + DisableButtons(false, UserGuess1); + DisableButtons(false, UserGuess2); + + winCheck(); + } + } + count = 0; + loseCheck(); + } + } + + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(FlipFlopGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(FlipFlopGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(FlipFlopGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(FlipFlopGame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new FlipFlopGame().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JTextField HighScore; + private javax.swing.JTextField PairsLeft; + private javax.swing.JLabel PairsRecord; + private javax.swing.JButton QuitButtonActionPerformed; + private javax.swing.JButton Rankings; + private javax.swing.JTextField Score; + private javax.swing.JTextField Score2; + private javax.swing.JButton Tile11; + private javax.swing.JButton Tile12; + private javax.swing.JButton Tile13; + private javax.swing.JButton Tile14; + private javax.swing.JButton Tile15; + private javax.swing.JButton Tile16; + private javax.swing.JButton Tile17; + private javax.swing.JButton Tile18; + private javax.swing.JButton Tile19; + private javax.swing.JButton Tile20; + private javax.swing.JButton Tile21; + private javax.swing.JButton Tile22; + private javax.swing.JButton Tile23; + private javax.swing.JButton Tile24; + private javax.swing.JButton Tile25; + private javax.swing.JButton Tile26; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JLabel jLabel3; + private javax.swing.JButton startGameButtonActionPerformed; + // End of variables declaration//GEN-END:variables + + TilesCreation tiles = new TilesCreation(); + //make object of all icons by placing images to it + + ImageIcon Chick = new javax.swing.ImageIcon(".\\shapes\\chick.png"); + ImageIcon Ghost = new javax.swing.ImageIcon(".\\shapes\\ghost.png"); + ImageIcon panda = new javax.swing.ImageIcon(".\\shapes\\panda.png"); + ImageIcon Teddy = new javax.swing.ImageIcon(".\\shapes\\teddy-bear.png"); + ImageIcon Giraffe = new javax.swing.ImageIcon(".\\shapes\\giraffe.png"); + ImageIcon Frog = new javax.swing.ImageIcon(".\\shapes\\frog.png"); + ImageIcon Owl = new javax.swing.ImageIcon(".\\shapes\\owl.png"); + ImageIcon Lion = new javax.swing.ImageIcon(".\\shapes\\lion.png"); + ImageIcon Blank = new javax.swing.ImageIcon(".\\shapes\\Blank.png"); + ImageIcon WIN = new javax.swing.ImageIcon(".\\shapes\\win.png"); + ImageIcon GAMEOVER = new javax.swing.ImageIcon(".\\shapes\\game-over.png"); + ImageIcon HIGHSCORE = new javax.swing.ImageIcon(".\\shapes\\high-score.png"); + + + int count = 0; //check of how many tiles flipped + int UserGuess1 = 404, UserGuess2 = 404; //locations of tiles user guessed + int Type1, Type2; //Type of shapes at the locations + boolean match; //catches the result from matchingTiles sub. + int points; //saves the player's points + String spoints; //Label Placing as label is string + int LeftPairs; //Keeps track of how many pairs left + String sLeftPairs; + int score; + int highScore; + String username; + String ErrorMessage="You cannot pick the same tile twice\n. Pick 2 distinct tiles"; + String ErrorTitle = "Invalid Move"; + String loseMessage = "You have used all the Pairs.\nYou were supposed to match all the tiles \nwithin allowed pairs."; + String winMessage = "Congratulations! You finished the game successfully with score of "; + String highScoreMessage = "You earned High Score\nNew High score is:"; +} diff --git a/MemoryGame/src/main/java/com/mycompany/flipflopgame/RankingsTable.form b/MemoryGame/src/main/java/com/mycompany/flipflopgame/RankingsTable.form new file mode 100644 index 00000000..f93f6330 --- /dev/null +++ b/MemoryGame/src/main/java/com/mycompany/flipflopgame/RankingsTable.form @@ -0,0 +1,112 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + <Editor/> + <Renderer/> + </Column> + <Column maxWidth="-1" minWidth="-1" prefWidth="-1" resizable="true"> + <Title/> + <Editor/> + <Renderer/> + </Column> + </TableColumnModel> + </Property> + <Property name="tableHeader" type="javax.swing.table.JTableHeader" editor="org.netbeans.modules.form.editors2.JTableHeaderEditor"> + <TableHeader reorderingAllowed="true" resizingAllowed="true"/> + </Property> + </Properties> + </Component> + </SubComponents> + </Container> + <Component class="javax.swing.JLabel" name="Rank"> + <Properties> + <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> + <Font name="Arial" size="18" style="0"/> + </Property> + <Property name="text" type="java.lang.String" value="Rankings"/> + </Properties> + </Component> + <Component class="javax.swing.JButton" name="Exit"> + <Properties> + <Property name="font" type="java.awt.Font" editor="org.netbeans.beaninfo.editors.FontEditor"> + <Font name="Arial" size="14" style="0"/> + </Property> + <Property name="text" type="java.lang.String" value="Exit"/> + </Properties> + <Events> + <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="ExitActionPerformed"/> + </Events> + </Component> + </SubComponents> +</Form> diff --git a/MemoryGame/src/main/java/com/mycompany/flipflopgame/RankingsTable.java b/MemoryGame/src/main/java/com/mycompany/flipflopgame/RankingsTable.java new file mode 100644 index 00000000..fa1c8ab7 --- /dev/null +++ b/MemoryGame/src/main/java/com/mycompany/flipflopgame/RankingsTable.java @@ -0,0 +1,264 @@ +package main.java.com.mycompany.flipflopgame; + + +import java.util.ArrayList; +import javax.swing.table.DefaultTableModel; + +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/GUIForms/JFrame.java to edit this template + */ + +/** + * + * @author Imaan + */ +public class RankingsTable extends javax.swing.JFrame { + + /** + * Creates new form RankingsTable + */ + public RankingsTable() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane1 = new javax.swing.JScrollPane(); + Table = new javax.swing.JTable(); + Rank = new javax.swing.JLabel(); + Exit = new javax.swing.JButton(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + Table.setModel(new javax.swing.table.DefaultTableModel( + new Object [][] { + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null}, + {null, null} + }, + new String [] { + "Username", "Score" + } + ) { + Class[] types = new Class [] { + java.lang.String.class, java.lang.Integer.class + }; + boolean[] canEdit = new boolean [] { + false, false + }; + + public Class getColumnClass(int columnIndex) { + return types [columnIndex]; + } + + public boolean isCellEditable(int rowIndex, int columnIndex) { + return canEdit [columnIndex]; + } + }); + jScrollPane1.setViewportView(Table); + + Rank.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N + Rank.setText("Rankings"); + + Exit.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N + Exit.setText("Exit"); + Exit.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + ExitActionPerformed(evt); + } + }); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap(19, Short.MAX_VALUE)) + .addGroup(layout.createSequentialGroup() + .addGap(21, 21, 21) + .addComponent(Rank, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(Exit, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(27, 27, 27)) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(Exit, javax.swing.GroupLayout.DEFAULT_SIZE, 31, Short.MAX_VALUE) + .addComponent(Rank, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(7, 7, 7)) + ); + + pack(); + }// </editor-fold>//GEN-END:initComponents + + public void setRankings(ArrayList<String> names, ArrayList<Integer> scores) + { + // Get the table model from the JTable + DefaultTableModel tableModel = (DefaultTableModel) Table.getModel(); + + // Modify the data in the table model + + for(int i=0; i<names.size(); i++) + { + tableModel.setValueAt(names.get(i), i, 0); + tableModel.setValueAt(scores.get(i), i, 1); + } + } + + private void ExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ExitActionPerformed + this.setVisible(false); + }//GEN-LAST:event_ExitActionPerformed + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(RankingsTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(RankingsTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(RankingsTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(RankingsTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + //</editor-fold> + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new RankingsTable().setVisible(true); + } + }); + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton Exit; + private javax.swing.JLabel Rank; + private javax.swing.JTable Table; + private javax.swing.JScrollPane jScrollPane1; + // End of variables declaration//GEN-END:variables +} diff --git a/MemoryGame/src/main/java/com/mycompany/flipflopgame/TilesCreation.java b/MemoryGame/src/main/java/com/mycompany/flipflopgame/TilesCreation.java new file mode 100644 index 00000000..01759787 --- /dev/null +++ b/MemoryGame/src/main/java/com/mycompany/flipflopgame/TilesCreation.java @@ -0,0 +1,110 @@ +package main.java.com.mycompany.flipflopgame; +import java.util.*; + +/** + * @author Imaan + */ +public class TilesCreation { + //GLOBALS + + //these variables will have location of each icon + int chick1, chick2; + int ghost1, gost2; + int panda1, panda2; + int teddy1, teddy2; + int giraffe1, giraffe2; + int frog1, frog2; + int owl1, owl2; + int lion1, lion2; + + + //generate random numbers & assign them to each shape twice. its their location on the game board. + public void initializePostion(){ + int n = 0; + + //first add all the numbers from 0 to 15 into an array list, then shuffle it + List<Integer> randomCollection = new ArrayList<>(); + for (int i = 0; i <= 15; i++){ + randomCollection.add(i); + } + Collections.shuffle(randomCollection); + + //assign each shape their locations by getting value against index + chick1 = randomCollection.get(n); + n++; + chick2 = randomCollection.get(n); + n++; + ghost1 = randomCollection.get(n); + n++; + gost2 = randomCollection.get(n); + n++ ; + panda1 = randomCollection.get(n); + n++; + panda2 = randomCollection.get(n); + n++; + teddy1 = randomCollection.get(n); + n++; + teddy2 = randomCollection.get(n); + n++; + giraffe1 = randomCollection.get(n); + n++; + giraffe2 = randomCollection.get(n); + n++; + frog1 = randomCollection.get(n); + n++; + frog2 = randomCollection.get(n); + n++; + owl1 = randomCollection.get(n); + n++; + owl2 = randomCollection.get(n); + n++; + lion1 = randomCollection.get(n); + n++; + lion2 = randomCollection.get(n); + n++; + } + +// type will be from 0 to 7 and tell what type of shape it is + //forexample if type == 0 that means its chick + public int getTileType(int position){ + int type = 100; + + System.out.print("icons type "); + if (position == chick1 || position == chick2){ + type = 0; + System.out.print(type + " - Chick"); + } + else if (position == ghost1 || position == gost2){ + type = 1; + System.out.print(type + " - Ghost"); + } + else if (position == panda1 || position == panda2){ + type = 2; + System.out.print(type + " - Panda"); + } + else if (position == teddy1 || position == teddy2){ + type = 3; + System.out.print(type + " - Donut"); + } + else if (position == giraffe1 || position == giraffe2){ + type = 4; + System.out.print(type + " - Mushroom"); + } + else if (position == frog1 || position == frog2){ + type = 5; + System.out.print(type + " - Frog"); + } + else if (position == owl1 || position == owl2){ + type = 6; + System.out.print(type + " - Owl"); + } + else if (position == lion1 || position == lion2){ + type = 7; + System.out.print(type + " - Lion"); + } + System.out.println(); + + return type; + } +} + \ No newline at end of file