Skip to content
Snippets Groups Projects
Commit e0670337 authored by Kovax's avatar Kovax
Browse files

Lehet nyerni a játékban

parent 13e03a08
No related branches found
No related tags found
No related merge requests found
......@@ -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();
}
......
......@@ -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;
}
}
......@@ -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();
}
}
}
/*
* 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");
}
}
}
}
}
......@@ -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);
}
}
......@@ -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);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment