Skip to content
Snippets Groups Projects
Commit 781aa281 authored by Dániel's avatar Dániel
Browse files

Refactor + szélesség rendberakás

parent e0670337
Branches
No related tags found
No related merge requests found
......@@ -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;
}
/* 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; }
private int[] getButtonPos(String buttonName)
/* Gomb lenyomasok */
private int[] buttonPosStringToIntArr(String buttonName)
{
String[] splitted = buttonName.split(":");
int[] pos = new int[2];
......@@ -71,58 +55,88 @@ 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()
{
}
......
......@@ -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();
}
}
}
......
......@@ -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();
......
......@@ -35,7 +35,7 @@ public class Model
this.timeLeft = 0;
this.controller = controller;
squares = new JButton[controller.settings.size][controller.settings.size];
int mines = 0;
// 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]))
{
......@@ -162,8 +160,6 @@ 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];
......@@ -285,8 +276,6 @@ public class Model
return true;
}
return false;
}
public void build()
{
......@@ -310,8 +299,6 @@ public class Model
{
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;
......
......@@ -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)
......
......@@ -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();
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment