diff --git a/src/GUI/game/Controller.java b/src/GUI/game/Controller.java index 8b2f56cd34f0c93e023d2dbe91f71deb9a405c49..7719a555c0435c179f57b6fd6315c9a8b57d9c01 100644 --- a/src/GUI/game/Controller.java +++ b/src/GUI/game/Controller.java @@ -7,6 +7,7 @@ package GUI.game; import Program.Settings; +import java.awt.Color; /** @@ -20,13 +21,15 @@ public class Controller public Settings settings; public Game game; public StopWatch watch; + public ScoreCounter counter; private Boolean isStopButtonPressed; public Controller(Settings settings) { - this.settings = settings; - this.isStopButtonPressed = false; + this.settings = settings; + this.isStopButtonPressed = false; + } public void stopButtonPressed() { @@ -80,15 +83,26 @@ public class Controller { if( this.model != null && this.view != null ) { - watch = new StopWatch(this); + + this.watch = new StopWatch(this); watch.start(); watch.startTimer(); + + this.counter = new ScoreCounter(this); + counter.resetCounter(); + counter.startCounter(); + counter.start(); + } } public void gameOver() { - this.model.isGameOver = true; + EndOfGamePopUp popup; + popup = new EndOfGamePopUp(this); + this.watch.stopTimer(); + this.model.isGameOver = true; + } public void timeLeft() { @@ -99,6 +113,17 @@ public class Controller this.game.newGame(); } + void showMines(Color color) { + this.model.showAllMines(color); + } + + void setScore(int score) + { + this.model.score = score; + this.view.scoreChanged(); + } + + diff --git a/src/GUI/game/EndOfGamePopUp.java b/src/GUI/game/EndOfGamePopUp.java index 288d87c46f87703b11a82f2d1bde2fb86292e39f..f6734c03b7abe1b987cd8e279a98070a6f7b8863 100644 --- a/src/GUI/game/EndOfGamePopUp.java +++ b/src/GUI/game/EndOfGamePopUp.java @@ -4,6 +4,7 @@ */ package GUI.game; +import java.awt.Color; import javax.swing.JFrame; import javax.swing.JOptionPane; @@ -19,28 +20,55 @@ public class EndOfGamePopUp public EndOfGamePopUp(Controller controller) { - this.endScore = 0; this.controller = controller; - Object[] options = {"Yes","No","Quit"}; - int n = JOptionPane.showOptionDialog(frame,"Your score: " + this.endScore + "\n Would you like to start a new game?","Game Over!", - JOptionPane.YES_NO_CANCEL_OPTION, - JOptionPane.QUESTION_MESSAGE, - null, - options, - options[2]); + this.endScore = this.controller.model.getScore(); - if( n == 0 ) - { - this.controller.newGame(); - } - else if( n == 1 ) + if( this.controller.model.isWinner ) { - this.controller.stopButtonPressed(); + this.controller.showMines(Color.green); + + Object[] options = {"Yes","No","Quit"}; + int n = JOptionPane.showOptionDialog(frame,"Your score: " + this.endScore + "\n Would you like to start a new game?","Winner!", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[2]); } - else if( n == 2 ) + else { - System.exit(0); + this.controller.showMines(Color.red); + + + int n = this.popUpWindow("Would you like to start a new game?"); + + + if( n == 0 ) + { + this.controller.newGame(); + } + else if( n == 1 ) + { + this.controller.stopButtonPressed(); + } + else if( n == 2 ) + { + System.exit(0); + } } } + private int popUpWindow(String msg) + { + Object[] options = {"Yes","No","Quit"}; + int n = JOptionPane.showOptionDialog(frame,"Your score: " + this.endScore + msg,"Game Over!", + JOptionPane.YES_NO_CANCEL_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, + options, + options[2]); + + return n; + } + } diff --git a/src/GUI/game/Model.java b/src/GUI/game/Model.java index 37b40ba1af254f0595f030bf42dfb15c05be798d..3a9108e6a10905e7c5f42e02f36f37c52b2012fd 100644 --- a/src/GUI/game/Model.java +++ b/src/GUI/game/Model.java @@ -6,6 +6,7 @@ package GUI.game; +import java.awt.Color; import java.util.ArrayList; import java.util.Random; import java.util.logging.Level; @@ -20,11 +21,13 @@ public class Model { private Controller controller; public JButton[][] squares; - public int timeLeft; + 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) @@ -34,6 +37,7 @@ public class Model squares = new JButton[controller.settings.size][controller.settings.size]; int mines = 0; isGameOver = false; + isWinner = false; } /* @@ -41,6 +45,25 @@ public class Model * 345 * 678 */ + public int getTimeLeft(){ return this.timeLeft; } + public int getMines() { return this.mines; } + public int getScore() { return this.score; } + + public int getUncoveredFields() + { + int uncovered = 0; + for (int i = 0; i < this.controller.settings.size; i++) + { + for (int j = 0; j < this.controller.settings.size; j++) + { + if((this.squares[i][j].getText().length() == 0) || this.squares[i][j].getText().equals(" ") || this.squares[i][j].getText().equals("?") ) + { + uncovered++; + } + } + } + return uncovered; + } public String[] getNeighbours(int[] pos) { int actX = (pos[0]); @@ -74,6 +97,48 @@ public class Model return neighbours; + + } + private Boolean isGameOverAndWinner() + { + Boolean isWinner = true ; + for (int i = 0; i < this.controller.settings.size; i++) + { + for (int j = 0; j < this.controller.settings.size; j++) + { + if( this.isMined(i, j) ) + { + JButton actualField = this.squares[i][j]; + + if( this.isFlagged(i, j) && !this.isGameOver ) + isWinner = isWinner; + else + isWinner = false; + } + } + } + return isWinner; + } + public Boolean isFlagged(int x, int y) + { + if( this.squares[x][y].getText().equals("?") ) + return true; + return false; + } + public void showAllMines(Color color) + { + for (int i = 0; i < this.controller.settings.size; i++) + { + for (int j = 0; j < this.controller.settings.size; j++) + { + JButton actField = this.squares[i][j]; + if( this.isMined(i, j) ) + { + actField.setText("*"); + actField.setBackground(color); + } + } + } } /* * A gomb uj kinezetet szamolja ki @@ -83,7 +148,9 @@ public class Model if( isGameOver ) return; - EndOfGamePopUp popup; + + + //EndOfGamePopUp popup; if( this.isMined(pos[0], pos[1])) { for (int i = 0; i < this.controller.settings.size; i++) @@ -101,7 +168,7 @@ public class Model } if( isGameOver ) { - popup = new EndOfGamePopUp(this.controller); + //popup = new EndOfGamePopUp(this.controller); return; } } @@ -268,28 +335,41 @@ public class Model } this.controller.view.flagged = this.mines; } - + public void colorField(int x,int y, Color color, Color colorF) + { + this.squares[x][y].setBackground(color); + this.squares[x][y].setForeground(colorF); + } void flagButton(int[] pos) { if( isGameOver ) return; + String text = squares[pos[0]][pos[1]].getText(); if( text.equals("?")) { squares[pos[0]][pos[1]].setText(""); this.controller.view.flagChanged(1); + this.colorField(pos[0], pos[1], null, null); } else if( text.equals("")) { squares[pos[0]][pos[1]].setText("?"); this.controller.view.flagChanged(-1); + this.colorField(pos[0], pos[1], Color.BLUE, Color.YELLOW); } else if( text.equals(" ")) { squares[pos[0]][pos[1]].setText("?"); this.controller.view.flagChanged(-1); + this.colorField(pos[0], pos[1], Color.BLUE, Color.YELLOW); } - + if( isGameOverAndWinner() ) + { + this.isWinner = true; + this.isGameOver = true; + this.controller.gameOver(); + } } } diff --git a/src/GUI/game/ScoreCounter.java b/src/GUI/game/ScoreCounter.java new file mode 100644 index 0000000000000000000000000000000000000000..6b0e6436c69d7ac035ac96aa0b8905f9df47bb8f --- /dev/null +++ b/src/GUI/game/ScoreCounter.java @@ -0,0 +1,76 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package GUI.game; + +/** + * + * @author Kovax + */ +public class ScoreCounter extends Thread +{ + Controller controller; + int score; + Boolean enabled; + + public ScoreCounter(Controller controller) + { + this.controller = controller; + this.enabled = false; + } + + public void resetCounter() + { + this.score = 0; + } + public void startCounter() + { + this.enabled = true; + } + @Override + public void run() + { + synchronized(this) + { + while( true ) + { + try + { + int time = controller.model.getTimeLeft(); + int mines = controller.model.getMines(); + + int uncovered = controller.model.getUncoveredFields(); + int covered = ( this.controller.settings.size * this.controller.settings.size ) - uncovered; + + if( this.controller.settings.level == 0 ) + { + //this.score += this.score*1.5; + } + else if( this.controller.settings.level == 1 ) + { + this.score = ( covered * mines * 8 ) / (time+1) + covered; + } + else if( this.controller.settings.level == 2 ) + { + this.score = ( mines * 10 ) / time+1 + covered; + } + else if( this.controller.settings.level == 3 ) + { + this.score = ( mines * 15 ) / time+1 + covered; + } + + this.controller.setScore(score); + + sleep(1000); + } catch (InterruptedException ex) + { + System.out.println("int error"); + } + } + } + } + +} diff --git a/src/GUI/game/View.java b/src/GUI/game/View.java index 583893db43c8b4fe1d3d588f0eb50a5f704b6818..3f5499500f6badad46dfff052d407066f4639f6a 100644 --- a/src/GUI/game/View.java +++ b/src/GUI/game/View.java @@ -8,6 +8,7 @@ package GUI.game; import GUI.Window.Window; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; @@ -32,6 +33,7 @@ public class View extends Window private JPanel buttons; private int timeLeft; int flagged; + JLabel scoreLabel; public View(Controller game, Model gameModel) @@ -58,6 +60,7 @@ public class View extends Window this.flagged += i; this.mineFlaggedLabel.setText(Integer.toString(this.flagged)); } + public void draw() { @@ -141,11 +144,11 @@ public class View extends Window quitBTN.addMouseListener(new ButtonActionListener(controller)); quitBTN.setName("quit"); - JLabel pointsLabel = new JLabel("Points:"); + scoreLabel = new JLabel("Points:" + this.model.score); JLabel recordLabel = new JLabel("MaxPoints:"); footer.add(quitBTN); - footer.add(pointsLabel); + footer.add(scoreLabel); footer.add(recordLabel); c.fill = GridBagConstraints.HORIZONTAL; @@ -164,6 +167,10 @@ public class View extends Window { this.frame.dispose(); } + + void scoreChanged() { + this.scoreLabel.setText("Score:" + this.model.score); + } } diff --git a/src/Program/Program.java b/src/Program/Program.java index 9acaf5a7bc1340ba31c3637622aa23b336383b05..e0da45ec9d285106bd1ffd7b1c704476c66b9739 100644 --- a/src/Program/Program.java +++ b/src/Program/Program.java @@ -15,7 +15,7 @@ public class Program public static void main(String[] args) { - Settings settings = new Settings(8,"UnnamedPlayer",1,600,600); + Settings settings = new Settings(4,"UnnamedPlayer",1,600,600); Game game = new Game(settings); }