From 781aa281c373ca1a34d1becff1abf790ccc5fee2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?D=C3=A1niel?= <Dániel@kovax-pc>
Date: Wed, 20 Nov 2013 11:27:18 +0100
Subject: [PATCH] =?UTF-8?q?Refactor=20+=20sz=C3=A9less=C3=A9g=20rendberak?=
 =?UTF-8?q?=C3=A1s?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/GUI/game/Controller.java     | 114 +++++++++++++++++--------------
 src/GUI/game/EndOfGamePopUp.java |   6 +-
 src/GUI/game/Game.java           |   9 +--
 src/GUI/game/Model.java          |  53 +++++---------
 src/GUI/game/ScoreCounter.java   |  18 +++--
 src/GUI/game/View.java           |  22 +++---
 src/Program/Game.java            |   2 +-
 7 files changed, 114 insertions(+), 110 deletions(-)

diff --git a/src/GUI/game/Controller.java b/src/GUI/game/Controller.java
index 7719a55..5d9dd18 100644
--- a/src/GUI/game/Controller.java
+++ b/src/GUI/game/Controller.java
@@ -20,46 +20,30 @@ public class Controller
     public Model model;
     public Settings settings;
     public Game game;
-    public StopWatch watch;
-    public ScoreCounter counter;
     
+    //Mutatók külső threadekre
+    public StopWatch stopWatch;
+    public ScoreCounter scoreCounter;
+    
+    //ha igaz, a játék leáll
     private Boolean isStopButtonPressed;
     
     public Controller(Settings settings)
     {
         this.settings               = settings;
         this.isStopButtonPressed    = false;
-       
    }
-    public void stopButtonPressed()
-    {
-        this.isStopButtonPressed = true;
-    }
-    public void registerView(View view)
-    {
-        this.view = view;
-    }
-    public void registerModel(Model model)
-    {
-        this.model = model;
-    }
-    public void registerGame(Game game)
-    {
-        this.game = game;
-    }
-    public int getButtonState()
-    {
-        if( isStopButtonPressed )
-            return 1;
-        return -1;
-        
-    }
-    public void resetButtonState()
-    {
-        this.isStopButtonPressed = false;
-    }
     
-    private int[] getButtonPos(String buttonName)
+    /* setterek */
+    public void setStopButtonPressed(Boolean is) { this.isStopButtonPressed = is; }
+    public void setView(View view) { this.view = view; }
+    public void setModel(Model model) { this.model = model; }
+    public void setGame(Game game){ this.game = game; }
+    /* getterek */
+    public int getButtonState() { if( isStopButtonPressed ) return 1; return -1; }
+    
+    /* Gomb lenyomasok */
+    private int[] buttonPosStringToIntArr(String buttonName)
     {
         String[] splitted =  buttonName.split(":");
         int[] pos = new int[2];
@@ -71,57 +55,87 @@ public class Controller
     
     public void gameLeftButtonPressed(String button)
     {
-        int[]pos = this.getButtonPos(button);
+        int[]pos = this.buttonPosStringToIntArr(button);
         model.calculateButtonStyle(pos);  
     }
     public void gameRightButtonPressed(String button)
     {
-        int[] pos = this.getButtonPos(button);
+        int[] pos = this.buttonPosStringToIntArr(button);
         model.flagButton(pos);
     }
+    
+    /* Jatek vezerlesek */
     public void gameStart()
     {
         if( this.model != null && this.view != null )
         {
+            this.stopWatch      = new StopWatch(this);
+            stopWatch.start();
+            stopWatch.startTimer();
             
-            this.watch      = new StopWatch(this);
-            watch.start();
-            watch.startTimer();
-            
-            this.counter    = new ScoreCounter(this);
-            counter.resetCounter();
-            counter.startCounter();
-            counter.start();
+            this.scoreCounter    = new ScoreCounter(this);
+            scoreCounter.setCounter();
+            scoreCounter.startCounter();
+            scoreCounter.start();
             
         }
     }
     public void gameOver()
     {
-        EndOfGamePopUp popup;  
-        popup = new EndOfGamePopUp(this);
+        //stopper leallitasa
+        this.stopWatch.stopTimer();
+        this.stopWatch = null;
+        
+        this.scoreCounter.stopCounter();
+        this.scoreCounter = null;
         
-        this.watch.stopTimer();
         this.model.isGameOver = true;
         
+        EndOfGamePopUp popup;  
+        popup = new EndOfGamePopUp(this);
+
     }
+    
+    /* Eventek */
     public void timeLeft()
     {
         view.timeLeft();
     }
-    public void newGame()
-    {
-        this.game.newGame();
-    }
+    //public void newGame()
+    //{
+    //    this.game.newGame();
+    //}
 
-    void showMines(Color color) {
+    void showMines(Color color) 
+    {
        this.model.showAllMines(color);
     }
 
-    void setScore(int score) 
+    void scoreChanged(int score) 
     {
         this.model.score = score;
         this.view.scoreChanged();
     }
+
+    /*
+     Játék végi popup ablak kezelése
+     */
+    void endOfGameNewGameButtonPressed() 
+    {
+        this.view = null;
+        this.model = null;
+        this.game.newGame();
+    }
+
+    void endOfGameNewNoMoreGameButtonPressed() 
+    {
+        this.isStopButtonPressed = true;
+    }
+
+    void endOfGameExitGameButtonPressed() 
+    {
+        
+    }
     
 
     
diff --git a/src/GUI/game/EndOfGamePopUp.java b/src/GUI/game/EndOfGamePopUp.java
index f6734c0..d2d2e04 100644
--- a/src/GUI/game/EndOfGamePopUp.java
+++ b/src/GUI/game/EndOfGamePopUp.java
@@ -45,15 +45,15 @@ public class EndOfGamePopUp
 
             if( n == 0 )
             {
-                this.controller.newGame();
+                this.controller.endOfGameNewGameButtonPressed();
             }
             else if( n == 1 )
             {
-                this.controller.stopButtonPressed();
+                this.controller.endOfGameNewNoMoreGameButtonPressed();
             }
             else if( n == 2 )
             {
-                System.exit(0);
+                this.controller.endOfGameExitGameButtonPressed();
             }
         }
     }
diff --git a/src/GUI/game/Game.java b/src/GUI/game/Game.java
index 49e2693..5fb2674 100644
--- a/src/GUI/game/Game.java
+++ b/src/GUI/game/Game.java
@@ -32,9 +32,9 @@ public class Game
         
         Model gameModel    = new Model(game);
         View gameView      = new View(game,gameModel);
-        game.registerModel(gameModel);
-        game.registerView(gameView);
-        game.registerGame(this);
+        game.setModel(gameModel);
+        game.setView(gameView);
+        game.setGame(this);
         
         gameView.draw();
         game.gameStart();
@@ -43,8 +43,9 @@ public class Game
     }
     public void newGame()
     {
-        this.controller.model = null;
         this.controller.view.closeWindow();
+        this.controller.model = null;
+        
         this.controller.view = null;
         this.controller = null;
         this.startGUI();
diff --git a/src/GUI/game/Model.java b/src/GUI/game/Model.java
index 3a9108e..e8b9335 100644
--- a/src/GUI/game/Model.java
+++ b/src/GUI/game/Model.java
@@ -19,25 +19,25 @@ import javax.swing.JButton;
  */
 public class Model 
 {
-    private Controller      controller;
-    public  JButton[][]     squares;
-    public  int             timeLeft = 0;
-    public  Boolean         calculateDone;
-    public  ArrayList<int[]> nulls;
-    public  int             mines;
-    public  Boolean         isGameOver;
-    public Boolean          isWinner;
-    public int              score;
+    private Controller          controller;
+    public  JButton[][]         squares;
+    public  int                 timeLeft = 0;
+    public  Boolean             calculateDone;
+    public  ArrayList<int[]>    nulls;
+    public  int                 mines;
+    public  Boolean             isGameOver;
+    public  Boolean             isWinner;
+    public  int                 score;
     
     
     public Model(Controller controller)
     {
-        this.timeLeft = 0;
+        this.timeLeft   = 0;
         this.controller = controller;
-        squares = new JButton[controller.settings.size][controller.settings.size];
-        int mines = 0;
-        isGameOver = false;
-        isWinner = false;
+        squares         = new JButton[controller.settings.size][controller.settings.size];
+       // int mines = 0;
+        isGameOver      = false;
+        isWinner        = false;
     }
     
     /*
@@ -148,8 +148,6 @@ public class Model
         if( isGameOver )
             return;
         
-      
-        
         //EndOfGamePopUp popup;
         if( this.isMined(pos[0], pos[1]))
         {
@@ -161,9 +159,7 @@ public class Model
                     {
                         squares[i][j].setText("*");
                         this.controller.gameOver();
-                    }
-                    
-                    
+                    }                   
                 }
             }
             if( isGameOver )
@@ -205,7 +201,6 @@ public class Model
                         if( x >= 0 && y >= 0)
                         {
                             int actMines = this.calculateMinesAround(x,y);
-                            System.out.println(x + ":" +  y);
                             this.squares[x][y].setText(Integer.toString(actMines));
                             
                         }  
@@ -245,10 +240,6 @@ public class Model
         }
         return false;
     }
-    
-    
-    
-    
     public int calculateMinesAround(int x,int y)
     {
         int pos[] = new int[2];
@@ -284,9 +275,7 @@ public class Model
            this.mines++;
            return true;
        }
-       return false;
-       
-        
+       return false;    
     }
     public void build()
     {
@@ -309,9 +298,7 @@ public class Model
                   else
                   {
                       name = Integer.toString(i) + ":" + Integer.toString(j) + ":0";
-                  }
-                  
-                  
+                  }              
                   squares[i][j].setName(name);
                   //squares[i][j].addActionListener(new ButtonActionListener(this.controller));
                   squares[i][j].addMouseListener(new ButtonActionListener(controller));
@@ -325,12 +312,6 @@ public class Model
                   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));
-                  
-                 // squares[i][j].setText(" ");
              }
         }
         this.controller.view.flagged = this.mines;
diff --git a/src/GUI/game/ScoreCounter.java b/src/GUI/game/ScoreCounter.java
index 6b0e643..79ce96d 100644
--- a/src/GUI/game/ScoreCounter.java
+++ b/src/GUI/game/ScoreCounter.java
@@ -19,23 +19,27 @@ public class ScoreCounter extends Thread
     public ScoreCounter(Controller controller)
     {
         this.controller = controller;
-        this.enabled = false;
+        this.enabled = true;
     }
     
-    public void resetCounter()
-    {
-        this.score = 0;
-    }
+    /*Setterek*/
+    
+    /*Getterek*/
+    public void setCounter() { this.score = 0; }
     public void startCounter()
     {
         this.enabled = true;
     }
+    public void stopCounter()
+    {
+        this.enabled = false;
+    }
     @Override
     public void run()
     {
         synchronized(this)
         {
-            while( true )
+            while( enabled )
             {            
                 try 
                 {
@@ -62,7 +66,7 @@ public class ScoreCounter extends Thread
                         this.score = ( mines * 15 ) / time+1 + covered;
                     }
                     
-                    this.controller.setScore(score);
+                    this.controller.scoreChanged(score);
                     
                     sleep(1000);
                 } catch (InterruptedException ex) 
diff --git a/src/GUI/game/View.java b/src/GUI/game/View.java
index 3f54995..ab9e612 100644
--- a/src/GUI/game/View.java
+++ b/src/GUI/game/View.java
@@ -42,28 +42,32 @@ public class View extends Window
         this.timeLeft = 0;
         this.model      = gameModel;
         this.controller = game;
-        
-        //this.setSize(controller.settings.size*46+200,controller.settings.size*26+200);
-        //w41 h26
         this.setLayout(new GridLayout(controller.settings.size,controller.settings.size));
-        //this.createWindow();
-        
     }
     public void timeLeft()
     {
         this.timeLeft++;
         this.timeLeftLabel.setText(Integer.toString(this.timeLeft));
-        
     }
     public void flagChanged(int i)
     {
         this.flagged += i;
         this.mineFlaggedLabel.setText(Integer.toString(this.flagged));
     }
-
+    private int calculateFrameWidth()
+    {
+        int width = controller.settings.size*46;
+        if( width < 350 )
+            return 350;
+        else
+            return width;
+    }
+    private int calculateFrameHeight()
+    {
+        return (controller.settings.size+4)*26+100;
+    }
     public void draw()
     {
-
         model.build();
         for(int i=0;i<controller.settings.size;i++)
         {
@@ -82,7 +86,7 @@ public class View extends Window
         
         //Frame
         this.frame = new JFrame("Minesweeper");
-        frame.setSize(controller.settings.size*46,(controller.settings.size+4)*26+100);
+        frame.setSize(this.calculateFrameWidth(),this.calculateFrameHeight());
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setVisible(true);
         GridBagConstraints c = new GridBagConstraints();
diff --git a/src/Program/Game.java b/src/Program/Game.java
index f268311..0f450b2 100644
--- a/src/Program/Game.java
+++ b/src/Program/Game.java
@@ -52,7 +52,7 @@ public class Game
             if( this.game.controller.getButtonState() != -1 )
                 {
                     int state = this.game.controller.getButtonState();
-                    this.game.controller.resetButtonState();
+                    this.game.controller.setStopButtonPressed(Boolean.FALSE);
                     this.game.controller.view.close();
                     this.window = 0;
                     return 0;
-- 
GitLab