diff options
author | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2020-09-30 00:08:24 +0100 |
---|---|---|
committer | Manuel Palenzuela <manuelpalenzuelamerino@gmail.com> | 2020-09-30 00:08:24 +0100 |
commit | c3797ca7acf4b4c089c08523e78072f9aef5f8a3 (patch) | |
tree | f750cfda0f16e251390ff347bea71175d389b401 | |
download | Minesweeper-c3797ca7acf4b4c089c08523e78072f9aef5f8a3.tar.gz Minesweeper-c3797ca7acf4b4c089c08523e78072f9aef5f8a3.tar.bz2 Minesweeper-c3797ca7acf4b4c089c08523e78072f9aef5f8a3.zip |
-rw-r--r-- | .idea/misc.xml | 6 | ||||
-rw-r--r-- | .idea/modules.xml | 8 | ||||
-rw-r--r-- | .idea/uiDesigner.xml | 124 | ||||
-rw-r--r-- | .idea/vcs.xml | 6 | ||||
-rw-r--r-- | .idea/workspace.xml | 124 | ||||
-rw-r--r-- | MineSweeper.iml | 11 | ||||
-rw-r--r-- | out/production/MineSweeper/Board.class | bin | 0 -> 6466 bytes | |||
-rw-r--r-- | out/production/MineSweeper/BoardSquareButton.class | bin | 0 -> 4405 bytes | |||
-rw-r--r-- | out/production/MineSweeper/META-INF/MineSweeper.kotlin_module | bin | 0 -> 16 bytes | |||
-rw-r--r-- | out/production/MineSweeper/Psymp7Main.class | bin | 0 -> 3217 bytes | |||
-rw-r--r-- | src/Board.java | 221 | ||||
-rw-r--r-- | src/BoardSquareButton.java | 202 | ||||
-rw-r--r-- | src/Psymp7Main.java | 83 |
13 files changed, 785 insertions, 0 deletions
diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e0844bc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4119049 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/MineSweeper.iml" filepath="$PROJECT_DIR$/MineSweeper.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Palette2"> + <group name="Swing"> + <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> + </item> + <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> + <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> + <initial-values> + <property name="text" value="Button" /> + </initial-values> + </item> + <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="RadioButton" /> + </initial-values> + </item> + <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="CheckBox" /> + </initial-values> + </item> + <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> + <initial-values> + <property name="text" value="Label" /> + </initial-values> + </item> + <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> + <preferred-size width="150" height="-1" /> + </default-constraints> + </item> + <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> + <preferred-size width="150" height="50" /> + </default-constraints> + </item> + <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> + <preferred-size width="200" height="200" /> + </default-constraints> + </item> + <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> + </item> + <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> + </item> + <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> + <preferred-size width="-1" height="20" /> + </default-constraints> + </item> + <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> + <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> + </item> + <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> + <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> + </item> + </group> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..5869cf8 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,124 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ChangeListManager"> + <list default="true" id="2f838a2d-7d37-4293-a80b-4b5110ac8456" name="Default Changelist" comment=""> + <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/out/production/MineSweeper/Psymp7Main.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/MineSweeper/Psymp7Main.class" afterDir="false" /> + <change beforePath="$PROJECT_DIR$/src/Psymp7Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Psymp7Main.java" afterDir="false" /> + </list> + <option name="SHOW_DIALOG" value="false" /> + <option name="HIGHLIGHT_CONFLICTS" value="true" /> + <option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> + <option name="LAST_RESOLUTION" value="IGNORE" /> + </component> + <component name="FileTemplateManagerImpl"> + <option name="RECENT_TEMPLATES"> + <list> + <option value="Class" /> + </list> + </option> + </component> + <component name="Git.Settings"> + <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> + </component> + <component name="ProjectId" id="1YdNl8NSkZsItSxONPkJnSIURLu" /> + <component name="ProjectViewState"> + <option name="hideEmptyMiddlePackages" value="true" /> + <option name="showExcludedFiles" value="true" /> + <option name="showLibraryContents" value="true" /> + </component> + <component name="PropertiesComponent"> + <property name="RunOnceActivity.ShowReadmeOnStart" value="true" /> + <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> + <property name="last_opened_file_path" value="$PROJECT_DIR$" /> + </component> + <component name="RunManager"> + <configuration name="Psymp7Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> + <option name="MAIN_CLASS_NAME" value="Psymp7Main" /> + <module name="MineSweeper" /> + <method v="2"> + <option name="Make" enabled="true" /> + </method> + </configuration> + <recent_temporary> + <list> + <item itemvalue="Application.Psymp7Main" /> + </list> + </recent_temporary> + </component> + <component name="SvnConfiguration"> + <configuration /> + </component> + <component name="TaskManager"> + <task active="true" id="Default" summary="Default task"> + <changelist id="2f838a2d-7d37-4293-a80b-4b5110ac8456" name="Default Changelist" comment="" /> + <created>1583268659552</created> + <option name="number" value="Default" /> + <option name="presentableId" value="Default" /> + <updated>1583268659552</updated> + </task> + <servers /> + </component> + <component name="WindowStateProjectService"> + <state x="566" y="297" width="403" height="236" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes" timestamp="1584306555127"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state x="566" y="297" width="403" height="236" key="#com.intellij.refactoring.move.moveClassesOrPackages.MoveClassesOrPackagesDialog.classes/0.0.1536.824@0.0.1536.824" timestamp="1584306555127" /> + <state x="447" y="0" key="#com.intellij.refactoring.rename.AutomaticRenamingDialog" timestamp="1583446061500"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state x="447" y="0" key="#com.intellij.refactoring.rename.AutomaticRenamingDialog/0.0.1536.824@0.0.1536.824" timestamp="1583446061500" /> + <state width="1493" height="210" key="GridCell.Tab.0.bottom" timestamp="1584524730770"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="218" key="GridCell.Tab.0.bottom/0.0.1366.768@0.0.1366.768" timestamp="1584018994760" /> + <state width="1493" height="210" key="GridCell.Tab.0.bottom/0.0.1536.824@0.0.1536.824" timestamp="1584524730770" /> + <state width="1493" height="210" key="GridCell.Tab.0.center" timestamp="1584524730770"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="218" key="GridCell.Tab.0.center/0.0.1366.768@0.0.1366.768" timestamp="1584018994759" /> + <state width="1493" height="210" key="GridCell.Tab.0.center/0.0.1536.824@0.0.1536.824" timestamp="1584524730770" /> + <state width="1493" height="210" key="GridCell.Tab.0.left" timestamp="1584524730770"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="218" key="GridCell.Tab.0.left/0.0.1366.768@0.0.1366.768" timestamp="1584018994759" /> + <state width="1493" height="210" key="GridCell.Tab.0.left/0.0.1536.824@0.0.1536.824" timestamp="1584524730770" /> + <state width="1493" height="210" key="GridCell.Tab.0.right" timestamp="1584524730770"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="218" key="GridCell.Tab.0.right/0.0.1366.768@0.0.1366.768" timestamp="1584018994759" /> + <state width="1493" height="210" key="GridCell.Tab.0.right/0.0.1536.824@0.0.1536.824" timestamp="1584524730770" /> + <state width="1493" height="261" key="GridCell.Tab.1.bottom" timestamp="1584392510833"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="225" key="GridCell.Tab.1.bottom/0.0.1366.768@0.0.1366.768" timestamp="1584015128568" /> + <state width="1493" height="261" key="GridCell.Tab.1.bottom/0.0.1536.824@0.0.1536.824" timestamp="1584392510833" /> + <state width="1493" height="261" key="GridCell.Tab.1.center" timestamp="1584392510833"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="225" key="GridCell.Tab.1.center/0.0.1366.768@0.0.1366.768" timestamp="1584015128539" /> + <state width="1493" height="261" key="GridCell.Tab.1.center/0.0.1536.824@0.0.1536.824" timestamp="1584392510833" /> + <state width="1493" height="261" key="GridCell.Tab.1.left" timestamp="1584392510833"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="225" key="GridCell.Tab.1.left/0.0.1366.768@0.0.1366.768" timestamp="1584015128539" /> + <state width="1493" height="261" key="GridCell.Tab.1.left/0.0.1536.824@0.0.1536.824" timestamp="1584392510833" /> + <state width="1493" height="261" key="GridCell.Tab.1.right" timestamp="1584392510833"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state width="1300" height="225" key="GridCell.Tab.1.right/0.0.1366.768@0.0.1366.768" timestamp="1584015128558" /> + <state width="1493" height="261" key="GridCell.Tab.1.right/0.0.1536.824@0.0.1536.824" timestamp="1584392510833" /> + <state x="470" y="236" key="com.intellij.ide.util.TipDialog" timestamp="1583268663456"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state x="470" y="236" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1583268663456" /> + <state x="425" y="187" key="run.anything.popup" timestamp="1584309865317"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state x="425" y="187" key="run.anything.popup/0.0.1536.824@0.0.1536.824" timestamp="1584309865317" /> + <state x="431" y="145" width="672" height="678" key="search.everywhere.popup" timestamp="1584350677379"> + <screen x="0" y="0" width="1536" height="824" /> + </state> + <state x="431" y="145" width="672" height="678" key="search.everywhere.popup/0.0.1536.824@0.0.1536.824" timestamp="1584350677379" /> + </component> +</project> \ No newline at end of file diff --git a/MineSweeper.iml b/MineSweeper.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/MineSweeper.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/out/production/MineSweeper/Board.class b/out/production/MineSweeper/Board.class new file mode 100644 index 0000000..7cf3c85 --- /dev/null +++ b/out/production/MineSweeper/Board.class Binary files differdiff --git a/out/production/MineSweeper/BoardSquareButton.class b/out/production/MineSweeper/BoardSquareButton.class new file mode 100644 index 0000000..6ac219d --- /dev/null +++ b/out/production/MineSweeper/BoardSquareButton.class Binary files differdiff --git a/out/production/MineSweeper/META-INF/MineSweeper.kotlin_module b/out/production/MineSweeper/META-INF/MineSweeper.kotlin_module new file mode 100644 index 0000000..a49347a --- /dev/null +++ b/out/production/MineSweeper/META-INF/MineSweeper.kotlin_module Binary files differdiff --git a/out/production/MineSweeper/Psymp7Main.class b/out/production/MineSweeper/Psymp7Main.class new file mode 100644 index 0000000..b40a15c --- /dev/null +++ b/out/production/MineSweeper/Psymp7Main.class Binary files differdiff --git a/src/Board.java b/src/Board.java new file mode 100644 index 0000000..69bc123 --- /dev/null +++ b/src/Board.java @@ -0,0 +1,221 @@ +import javax.swing.*; +import java.util.ArrayList; +import java.util.Random; + +public class Board { + + private int x; + private int y; + private int mines; + private ArrayList<BoardSquareButton> buttons = new ArrayList<>(); + private ArrayList<BoardSquareButton> investigateQueue = new ArrayList<>(); + + public Board(int x, int y, int mines) + { + this.x = x; + this.y = y; + this.mines = mines; + + System.out.println("Board x: " + getColumns() + " y: " + getRows()); + + this.createButtons(x, y); + this.initialiseButtons(); + this.createMines(mines); + this.updateSurrounding(); + } + + public void importButtons(JFrame frame) + { + for(BoardSquareButton button : buttons) + { + frame.add(button); + } + } + + public void initialiseButtons() + { + for(BoardSquareButton button : buttons) + { + button.initialise(); + } + } + + public void finished() + { + for(BoardSquareButton button : buttons) + { + button.setInvestigated(true); + } + } + + public void reset() + { + this.initialiseButtons(); + this.createMines(mines); + this.updateSurrounding(); + } + + private void createButtons(int x, int y) { + for (int i = 1; i <= x; i++) { + for(int j = 1; j <= y; j++) { + this.storeButton(new BoardSquareButton(i, j)); + System.out.println("Adding button with column: " + i + "and row: " + j); + } + } + } + + private void createMines(int mines) + { + Random random = new Random(); + System.out.println("Creating "+ mines + " mines!"); + while(mines > 0) + { + int x = random.nextInt(this.getColumns()) + 1; + int y = random.nextInt(this.getRows()) + 1; + + System.out.print("(Setting mines) Querying for button with x: " + y + "and y: " + x); + + BoardSquareButton button = this.getButton(y, x); + if(!button.isMine()) + { + button.setMine(true); + mines--; + System.out.println("Is now mine!"); + } + } + } + + public void investigateButton(BoardSquareButton button) + { + System.out.println("Investigating button with row: " + button.getRow() + " and column: " + button.getColumn()); + if(button.getSurroundingMines() == 0 && !button.isMine()) + investigateQueue.add(button); + preformInvestigation(); + for(BoardSquareButton btn : investigateQueue) + { + btn.setInvestigated(true); + } + investigateQueue.clear(); + } + + private void preformInvestigation() { + System.out.println("Investigating..."); + boolean done; + do { + done = true; + for (int i = 0; i < investigateQueue.size(); i++) + { + BoardSquareButton button = investigateQueue.get(i); + for (BoardSquareButton btn : getSurroundingButtons(button)) + { + if (!investigateQueue.contains(btn) && btn.getSurroundingMines() == 0 && !btn.isMine()) + { + System.out.println("adding to the queue"); + investigateQueue.add(btn); + done = false; + } + } + } + } + while(!done); + } + public boolean hasWon() + { + boolean won = true; + + for(BoardSquareButton button : buttons) + { + if(!button.isInvestigated() && !button.isMine()) + won = false; + } + + return won; + } + + public boolean hasLost() + { + boolean lost = false; + + for(BoardSquareButton button : buttons) + { + if(button.isInvestigated() && button.isMine()) + lost = true; + } + + return lost; + } + + public ArrayList<BoardSquareButton> getSurroundingButtons(BoardSquareButton button) + { + ArrayList<BoardSquareButton> returnarray = new ArrayList<>(); + try { returnarray.add(getButton(button.getColumn() - 1, button.getRow() - 1).getSelf()); } catch(NullPointerException e) {} + try { returnarray.add(getButton(button.getColumn(), button.getRow() - 1).getSelf()); } catch(NullPointerException e) {} + try { returnarray.add(getButton(button.getColumn() + 1, button.getRow() - 1).getSelf()); } catch(NullPointerException e) {} + + try { returnarray.add(getButton(button.getColumn() - 1, button.getRow()).getSelf()); } catch(NullPointerException e) {} + try { returnarray.add(getButton(button.getColumn() + 1, button.getRow()).getSelf()); } catch(NullPointerException e) {} + + try { returnarray.add(getButton(button.getColumn() - 1, button.getRow() + 1).getSelf()); } catch(NullPointerException e) {} + try { returnarray.add(getButton( button.getColumn(), button.getRow() + 1).getSelf()); } catch(NullPointerException e) {} + try { returnarray.add(getButton( button.getColumn() + 1, button.getRow() + 1).getSelf()); } catch(NullPointerException e) {} + + return returnarray; + } + + public int countSurrounding(BoardSquareButton button) + { + int count = 0; + for(BoardSquareButton btn : getSurroundingButtons(button)) + { + if(btn.isMine()) + count++; + } + + System.out.println("Button with x: " + button.getRow() + " and y: " + button.getColumn() + " has " + count + " surrounding mines!"); + + return count; + } + + public void updateSurrounding() + { + for(BoardSquareButton button : buttons) + { + System.out.println("Counting surrounding for button with x: " + button.getRow() + " and y: " + button.getColumn()); + button.setSurroundingMines(countSurrounding(button)); + } + } + + /* + GETTERS AND SETTERS + */ + public int getColumns() + { + return x; + } + + public int getRows() + { + return y; + } + + public ArrayList<BoardSquareButton> getButtons() + { + return buttons; + } + + public BoardSquareButton getButton(int x, int y) + { + for(BoardSquareButton button : buttons) + { + if(button.getRow() == y && button.getColumn() == x) + return button; + } + + return null; + } + + public void storeButton(BoardSquareButton button) + { + buttons.add(button); + } +} diff --git a/src/BoardSquareButton.java b/src/BoardSquareButton.java new file mode 100644 index 0000000..9c0a1e4 --- /dev/null +++ b/src/BoardSquareButton.java @@ -0,0 +1,202 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + + +/* +* TODO: move the mouselistener to main so we can call restart from there + we can investigate surrounding buttons from there +* +* */ + +public class BoardSquareButton extends JButton { + + public static final int DEFAULT_WIDTH = 100; + public static final int DEFAULT_HEIGHT = 100; + public static final String DEFAULT_TEXT = "?"; + public static final String MINE_TEXT = "O"; + public static final Color DEFAULT_COLOR = Color.GRAY; + public static final Color INVESTIGATED_COLOR = Color.GREEN; + public static final Color MINE_COLOR = Color.BLACK; + public static final Color POTENTIAL_COLOR = Color.RED; + + private int width; + private int height; + private Color color; + private int x; + private int y; + private int surroundingMines; + private boolean mine; + private boolean investigated; + private boolean potential; + + /* + CONSTRUCTORS + */ + + public BoardSquareButton(int x, int y, int width, int height, Color color) + { + this.x = x; + this.y = y; + + initialise(); + + this.setDefaultDimension(width, height); + this.setColor(color); + + this.addNewMouseListener(); + } + + public BoardSquareButton(int x, int y) + { + this.x = x; + this.y = y; + + initialise(); + + this.addNewMouseListener(); + } + + private void addNewMouseListener() + { + super.addMouseListener(new Psymp7Main()); + } + + public void initialise() + { + this.setDefaultDimension(DEFAULT_WIDTH, DEFAULT_HEIGHT); + super.setFont(new Font("Ariel", Font.PLAIN, 40)); + super.setText("?"); + super.setBackground(DEFAULT_COLOR); + this.setColor(DEFAULT_COLOR); + this.setMine(false); + this.setInvestigated(false); + this.setPotential(false); + } + + /* + GETTERS AND SETTERS + */ + public BoardSquareButton getSelf() + { + return this; + } + + public void setDefaultDimension(int x, int y) + { + this.setWidth(x); + this.setHeight(y); + super.setPreferredSize(new Dimension(x, y)); + } + + public int getWidth() + { + return width; + } + + public void setWidth(int width) + { + this.width = width; + } + + public int getHeight() + { + return height; + } + + public void setHeight(int height) + { + this.height = height; + } + + public int getSurroundingMines() + { + return surroundingMines; + } + + public void setSurroundingMines(int mines) + { + this.surroundingMines = mines; + } + + public Color getColor() + { + return color; + } + + public void setColor(Color color) + { + this.color = color; + super.setBackground(color); + } + + public int getRow() + { + return x; + } + + public void setRow(int x) { this.x = x; } + + public int getColumn() + { + return y; + } + + public void setColumn(int y) + { + this.y = y; + } + + public boolean isMine() { + return mine; + } + + public void setMine(boolean mine) + { + this.mine = mine; + } + + public boolean isInvestigated() + { + return investigated; + } + + public void setInvestigated(boolean investigated) + { + this.investigated = investigated; + if (investigated) + { + this.setPotential(false); + if (mine) + { + this.setColor(MINE_COLOR); + this.setText(MINE_TEXT); + } else + { + this.setColor(INVESTIGATED_COLOR); + this.setText("" + this.getSurroundingMines()); + } + } + else + { + this.setColor(DEFAULT_COLOR); + this.setText(DEFAULT_TEXT); + } + } + public boolean isPotential() + { + return potential; + } + + public void setPotential(boolean potential) + { + this.potential = potential; + if(potential) + this.setColor(POTENTIAL_COLOR); + else + this.setColor(DEFAULT_COLOR); + } + +} diff --git a/src/Psymp7Main.java b/src/Psymp7Main.java new file mode 100644 index 0000000..d3d5ce6 --- /dev/null +++ b/src/Psymp7Main.java @@ -0,0 +1,83 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +public class Psymp7Main implements MouseListener +{ + public final static int WIDTH = 600; + public final static int HEIGHT = 600; + public final static int NUM_BUTTONS_X = 6; + public final static int NUM_BUTTONS_Y = 6; + public final static int NUM_MINES = 5; + + private static Board board; + + public static void main(String[] args) { + new Psymp7Main().start(); + } + + public void start() { + if(NUM_MINES > (NUM_BUTTONS_X * NUM_BUTTONS_Y)) + { + System.out.println("Too many mines!"); + return; + } + JFrame mainframe = new JFrame("MineSweeper"); + + mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainframe.setResizable(false); + mainframe.setSize(WIDTH, HEIGHT); + mainframe.setLayout(new GridLayout(NUM_BUTTONS_Y, NUM_BUTTONS_X)); + + board = new Board(NUM_BUTTONS_Y, NUM_BUTTONS_X, NUM_MINES); + board.importButtons(mainframe); + mainframe.pack(); + mainframe.setVisible(true); + } + + @Override + public void mousePressed(MouseEvent mouseEvent) + { + BoardSquareButton source = (BoardSquareButton) mouseEvent.getSource(); + + if (mouseEvent.getButton() == MouseEvent.BUTTON1) // left click + { + source.setInvestigated(true); + if(board.hasWon()) + { + board.finished(); + JOptionPane.showMessageDialog(null, "You won!"); + board.reset(); + } + else if(board.hasLost()) + { + board.finished(); + JOptionPane.showMessageDialog(null, "You loose!"); + board.reset(); + } + else if(source.getSurroundingMines() == 0 && !source.isMine()) + { + board.investigateButton(source); + } + System.out.println("Clicked in x: " + source.getColumn() + " y: " + source.getRow()); + } + if (mouseEvent.getButton() == MouseEvent.BUTTON3)//right click + { + if(!source.isInvestigated()) + source.setPotential(!source.isPotential()); + } + } + + @Override + public void mouseClicked(MouseEvent mouseEvent) {} + + @Override + public void mouseReleased(MouseEvent mouseEvent) {} + + @Override + public void mouseEntered(MouseEvent mouseEvent) {} + + @Override + public void mouseExited(MouseEvent mouseEvent) {} +} \ No newline at end of file |