From 78f6c8a37abd85a4cfba3d0ecc1ff52167531aa7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=A1niel?= <Dániel@kovax-pc>
Date: Wed, 13 Nov 2013 21:44:38 +0100
Subject: [PATCH] =?UTF-8?q?J=C3=A1t=C3=A9kfel=C3=BClet,=20aknasz=C3=A1moz?=
 =?UTF-8?q?=C3=A1s,=20aknaelhelyez=C3=A9s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .gitignore                             |   2 +
 src/GUI/game/ButtonActionListener.java |  39 ++++++
 src/GUI/game/Controller.java           |  26 +++-
 src/GUI/game/Game.java                 |  26 +++-
 src/GUI/game/Model.java                | 169 ++++++++++++++++++++++++-
 src/GUI/game/View.java                 |  42 +++---
 src/Program/Game.java                  |   7 +-
 src/Program/Program.java               |  10 +-
 src/Program/Settings.java              |  23 ++++
 9 files changed, 297 insertions(+), 47 deletions(-)
 create mode 100644 .gitignore
 create mode 100644 src/GUI/game/ButtonActionListener.java
 create mode 100644 src/Program/Settings.java

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..46cba4a
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/nbproject/private/
+/build/
\ No newline at end of file
diff --git a/src/GUI/game/ButtonActionListener.java b/src/GUI/game/ButtonActionListener.java
new file mode 100644
index 0000000..de4bf03
--- /dev/null
+++ b/src/GUI/game/ButtonActionListener.java
@@ -0,0 +1,39 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package GUI.game;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import javax.swing.JButton;
+
+/**
+ *
+ * @author Dániel
+ */
+public class ButtonActionListener implements ActionListener
+{
+    Controller controller;
+    
+    public ButtonActionListener(Controller controller)
+    {
+        this.controller = controller;
+    }
+    
+    @Override
+    public void actionPerformed(ActionEvent event) 
+    {
+        JButton button;
+        button = (JButton) event.getSource();
+        
+        controller.gameMineButtonPressed(button.getName());
+        
+       
+    }
+   
+
+    
+    
+    
+}
diff --git a/src/GUI/game/Controller.java b/src/GUI/game/Controller.java
index c35d745..e4b7be9 100644
--- a/src/GUI/game/Controller.java
+++ b/src/GUI/game/Controller.java
@@ -6,6 +6,8 @@
 
 package GUI.game;
 
+import Program.Settings;
+
 
 /**
  *
@@ -15,13 +17,15 @@ public class Controller
 {
     public View view;
     private Model model;
+    public Settings settings;
     
     private Boolean isStopButtonPressed = false;
     
-    Controller() 
+    public Controller(Settings settings)
     {
-       
+        this.settings = settings;
     }
+    
     public void registerView(View view)
     {
         this.view = view;
@@ -41,6 +45,24 @@ public class Controller
     {
         this.isStopButtonPressed = false;
     }
+    
+    private int[] getButtonPos(String buttonName)
+    {
+        String[] splitted =  buttonName.split(":");
+        int[] pos = new int[2];
+        pos[0] = Integer.parseInt(splitted[0]);
+        pos[1] = Integer.parseInt(splitted[1]);
+        
+        return pos;
+    }
+    
+    public void gameMineButtonPressed(String button)
+    {
+        int[]pos = this.getButtonPos(button);
+        model.calculateButtonStyle(pos);
+        
+    }
+    
 
     
     
diff --git a/src/GUI/game/Game.java b/src/GUI/game/Game.java
index 3daddc6..9a4822e 100644
--- a/src/GUI/game/Game.java
+++ b/src/GUI/game/Game.java
@@ -6,21 +6,35 @@
 
 package GUI.game;
 
+import Program.Program;
+import Program.Settings;
+
+
+
 /**
  *
  * @author Kovax
  */
-public class Game 
+public class Game  
 {
     public Controller controller;
+    public Settings settings;
     
-    public Game()
+    public Game(Settings settings)
     {
-        Model gameModel = new Model();
-        Controller game = new Controller();
-        View gameView = new View(game,gameModel);
+        this.settings = settings;
+        this.startGUI();
+    }
+ 
+    public synchronized void startGUI()
+    {
+        Controller game    = new Controller(this.settings);
+        
+        Model gameModel    = new Model(game);
+        View gameView      = new View(game,gameModel);
+        game.registerModel(gameModel);
+        game.registerView(gameView);
         
         this.controller = game;
     }
-   
 }
diff --git a/src/GUI/game/Model.java b/src/GUI/game/Model.java
index b4a31a6..0d79939 100644
--- a/src/GUI/game/Model.java
+++ b/src/GUI/game/Model.java
@@ -6,10 +6,175 @@
 
 package GUI.game;
 
+import java.util.Random;
+import javax.swing.JButton;
+
 /**
  *
  * @author Kovax
  */
-public class Model {
+public class Model 
+{
+    Controller controller;
+    public JButton[][] squares;
     
-}
+    public Model(Controller controller)
+    {
+        this.controller = controller;
+        squares = new JButton[controller.settings.size][controller.settings.size];
+    }
+    /*
+     *   012
+     *   345
+     *   678
+     */
+    private String[] getNeighbours(int[] pos)
+    {
+        int actX = (pos[0]);
+        int actY = (pos[1]);
+        
+        String[] neighbours = new String[9];
+        
+
+        neighbours[0] = (Integer.toString(actX-1) + ":" + Integer.toString(actY+1));
+        neighbours[1] = (Integer.toString(actX)   + ":" + Integer.toString(actY+1));
+        neighbours[2] = (Integer.toString(actX+1) + ":" + Integer.toString(actY+1));
+        neighbours[3] = (Integer.toString(actX-1) + ":" + Integer.toString(actY));
+        neighbours[4] = (Integer.toString(actX)   + ":" + Integer.toString(actY));
+        neighbours[5] = (Integer.toString(actX+1) + ":" + Integer.toString(actY));
+        neighbours[6] = (Integer.toString(actX-1) + ":" + Integer.toString(actY-1));
+        neighbours[7] = (Integer.toString(actX)   + ":" + Integer.toString(actY-1));
+        neighbours[8] = (Integer.toString(actX+1) + ":" + Integer.toString(actY-1));
+
+        if( (actX + 1) >= this.controller.settings.size)
+        {
+            neighbours[2] = "-1:-1";
+            neighbours[5] = "-1:-1";
+            neighbours[8] = "-1:-1";
+        }
+        if( (actY+1) >= this.controller.settings.size )
+        {
+            neighbours[0] = "-1:-1";
+            neighbours[1] = "-1:-1";
+            neighbours[2] = "-1:-1";
+        }
+            
+        return neighbours;
+      
+    }
+    public void calculateButtonStyle(int[] pos)
+    {
+    //    System.out.println("Actual:" + pos[0] + ":" + pos[1]);
+    //    for (int i = 0; i < 9; i++) {
+    //        System.out.println(i + "-" + this.getNeighbours(pos)[i]);
+    //    }
+        
+    }
+    public int[]  getPosFromString(String pos)
+    {
+        String[] splitted;
+        splitted = pos.split(":");
+        
+        int[] po = new int[2];
+        po[0] = Integer.parseInt(splitted[0]);
+        po[1] = Integer.parseInt(splitted[1]);
+        
+        return po;
+    }
+    private boolean isMined(int x, int y)
+    {
+        if( x < 0 || y < 0 || x > this.controller.settings.size || y > this.controller.settings.size )
+        {
+            return false;
+        }
+        String name = this.squares[x][y].getName();
+        String[] splitted = name.split(":");
+        
+        if( splitted[2].equals("b") )
+        {
+           return true;
+        }
+        return false;
+    }
+    private int calculateMinesAround(int x,int y)
+    {
+        int pos[] = new int[2];
+        pos[0] = x;
+        pos[1] = y;
+        String[] neighbours = this.getNeighbours(pos);
+        
+        int minesAround = 0;
+        for (int i = 0; i < 9; i++) 
+        {
+            int[] positionOfNeighbour = getPosFromString(neighbours[i]);
+            if( isMined(positionOfNeighbour[0], positionOfNeighbour[1]))
+            {
+                minesAround++;
+            }
+        }
+        return minesAround;
+    }
+    public boolean generateMines(int minesYet)
+    {
+        Random random = new Random();
+        
+        int rand=0;
+        if( this.controller.settings.level == 1 )
+            rand = 8;
+        else if( this.controller.settings.level == 2 )
+            rand = 5;
+        else if( this.controller.settings.level == 3)
+            rand = 3;
+        
+       if( random.nextInt(10000)%rand == 1 )
+       {
+           return true;
+       }
+       return false;
+       
+        
+    }
+    public void build()
+    {
+        int minesYet = 0;
+        for(int i=0;i<controller.settings.size;i++)
+        {
+             for(int j=0;j<controller.settings.size;j++)
+             {
+                  squares[i][j] = new JButton();
+                  squares[i][j].setSize(400,400);
+                  String name;
+                  
+                  if( this.generateMines(minesYet))
+                  {
+                      //tegyunk be egyet
+                      name = Integer.toString(i) + ":" + Integer.toString(j) + ":b";
+                      squares[i][j].setText("*");
+                      minesYet++;
+                  }                  
+                  else
+                  {
+                      name = Integer.toString(i) + ":" + Integer.toString(j) + ":0";
+                  }
+                  
+                  
+                  squares[i][j].setName(name);
+                  squares[i][j].addActionListener(new ButtonActionListener(this.controller));
+             }
+        }
+        for(int i=0;i<controller.settings.size;i++)
+        {
+             for(int j=0;j<controller.settings.size;j++)
+             {
+                  int minesAround = 0;
+                  minesAround = this.calculateMinesAround(i,j);
+                  String name = squares[i][j].getName();
+                  squares[i][j].setName(name + ":" + minesAround);
+                  
+                  
+                  if( !(squares[i][j].getText() == "*") )
+                      squares[i][j].setText(Integer.toString(minesAround));
+             }
+        }
+    }
+}   
diff --git a/src/GUI/game/View.java b/src/GUI/game/View.java
index e364aa0..fb253c9 100644
--- a/src/GUI/game/View.java
+++ b/src/GUI/game/View.java
@@ -10,7 +10,6 @@ import GUI.Window.Window;
 import java.awt.GridLayout;
 import javax.swing.JButton;
 import javax.swing.JFrame;
-import javax.swing.JPanel;
 
 /**
  *
@@ -20,26 +19,28 @@ public class View extends Window
 {   
     private Controller controller;
     private Model model;
-    
-    private JButton[][] squares;
     private JFrame frame;
     
-    public View()
+    public View(Controller game, Model gameModel)
     {
+        this.model      = gameModel;
+        this.controller = game;
+        
         this.setSize(400,400);
-        this.setLayout(new GridLayout(6,6));
-        squares = new JButton[6][6];
-    
-    }
+        this.setLayout(new GridLayout(controller.settings.size,controller.settings.size));
 
-    View(Controller game, Model gameModel) 
-    {
-        this.model = gameModel;
-        this.controller = game;
+        this.draw();
     }
     public void draw()
     {
-        this.build();
+        model.build();
+        for(int i=0;i<controller.settings.size;i++)
+        {
+            for(JButton actButton : model.squares[i])
+            {
+                this.add(actButton);
+            }
+        }
         if( this.frame != null )
         {
               frame.dispose();
@@ -51,17 +52,6 @@ public class View extends Window
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
     }
-    public void build()
-    {
-        for(int i=0;i<6;i++)
-        {
-             for(int j=0;j<6;j++)
-             {
-                  squares[i][j] = new JButton();
-                  squares[i][j].setSize(400,400);
-                  this.add(squares[i][j]);
-                 
-             }
-        }
-    }
+    
+    
 }
diff --git a/src/Program/Game.java b/src/Program/Game.java
index 298392c..de6c372 100644
--- a/src/Program/Game.java
+++ b/src/Program/Game.java
@@ -16,13 +16,14 @@ public class Game
 {
     Menu menu;
     GUI.game.Game game;
+    Settings settings;
     
     private int window;
     
-    public Game()
+    public Game(Settings settings)
     {
         this.window = 0;
-        
+        this.settings = settings;
 
         while( true )
         {
@@ -44,7 +45,7 @@ public class Game
     }
     public int gameWindow()
     {
-        this.game = new GUI.game.Game();
+        this.game = new GUI.game.Game(this.settings);
         
         while( true )
         {
diff --git a/src/Program/Program.java b/src/Program/Program.java
index d35d204..a60fbe3 100644
--- a/src/Program/Program.java
+++ b/src/Program/Program.java
@@ -6,13 +6,6 @@
 
 package Program;
 
-import Program.Game;
-import GUI.menu.Controller;
-import GUI.menu.Menu;
-import GUI.menu.Model;
-import GUI.menu.View;
-import java.util.ArrayList;
-
 /**
  *
  * @author Kovax
@@ -22,7 +15,8 @@ public class Program
     public static void main(String[] args)  
     {
        
-       Game game = new Game();
+       Settings settings = new Settings(5,"UnnamedPlayer",3);
+       Game game = new Game(settings);
  
     }
      
diff --git a/src/Program/Settings.java b/src/Program/Settings.java
new file mode 100644
index 0000000..2a30c41
--- /dev/null
+++ b/src/Program/Settings.java
@@ -0,0 +1,23 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package Program;
+
+/**
+ *
+ * @author Dániel
+ */
+public class Settings 
+{
+    public int size;
+    public String playerName;
+    public int level;
+    
+    public Settings(int size,String playerName, int level)
+    {
+        this.size       = size;
+        this.playerName = playerName;
+        this.level      = level;
+    }
+}
-- 
GitLab