Skip to content
Snippets Groups Projects
Model.java 11.9 KiB
Newer Older
Kovax's avatar
v2
Kovax committed
/*
 * 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;

Kovax's avatar
Kovax committed
import java.awt.Color;
Dániel's avatar
Dániel committed
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;

Kovax's avatar
v2
Kovax committed
/**
 *
 * @author Kovax
 */
public class Model 
{
Kovax's avatar
Kovax committed
    private Controller      controller;
    public  JButton[][]     squares;
Kovax's avatar
Kovax committed
    public  int             timeLeft = 0;
Kovax's avatar
Kovax committed
    public  Boolean         calculateDone;
    public  ArrayList<int[]> nulls;
    public  int             mines;
    public  Boolean         isGameOver;
Kovax's avatar
Kovax committed
    public Boolean          isWinner;
    public int              score;
Kovax's avatar
v2
Kovax committed
    
    public Model(Controller controller)
    {
Dániel's avatar
Dániel committed
        this.timeLeft = 0;
        this.controller = controller;
        squares = new JButton[controller.settings.size][controller.settings.size];
Kovax's avatar
Kovax committed
        isWinner = false;
Dániel's avatar
Dániel committed
    
Kovax's avatar
Kovax committed
    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;
    }
    {
        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;
      
Kovax's avatar
Kovax committed
        
    }
    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);
                }
            }
        }
Dániel's avatar
Dániel committed
    /*
     * A gomb uj kinezetet szamolja ki
     */
    public void calculateButtonStyle(int[] pos)
    {
Kovax's avatar
Kovax committed
      
        
        //EndOfGamePopUp popup;
Dániel's avatar
Dániel committed
        if( this.isMined(pos[0], pos[1]))
        {
            for (int i = 0; i < this.controller.settings.size; i++) 
            {
                for (int j = 0; j < this.controller.settings.size; j++) 
                {
                    if( isMined(i, j))
                    {
                        squares[i][j].setText("*");
Dániel's avatar
Dániel committed
                    }
Kovax's avatar
Kovax committed
                //popup = new EndOfGamePopUp(this.controller);
Dániel's avatar
Dániel committed
        }
        else
        {
            int mines = this.calculateMinesAround(pos[0], pos[1]);
            if( mines == 0 )
            {
                //ArrayList<int[]> nulls = this.getNullFieldAround(pos[0], pos[1], null,null);
                RecursiveNullFinder nullFinder = new RecursiveNullFinder(pos[0], pos[1], null, null,controller);
                this.calculateDone=false;
                nullFinder.start();
                while( !this.calculateDone  )
                {
                    synchronized(this)
                    {
                    try {
                        wait(100);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Model.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    }
                }
                for( int[] nullMiner : this.nulls )
Dániel's avatar
Dániel committed
                {
                    this.squares[nullMiner[0]][nullMiner[1]].setText("0");
                    
                    String[] neighbours = this.getNeighbours(nullMiner);
                    for (int i = 0; i < 9; i++) 
                    {
                        String[] posN = neighbours[i].split(":");
                        int x = Integer.parseInt(posN[0]);
                        int y = Integer.parseInt(posN[1]);
                        if( x >= 0 && y >= 0)
                        {
                            int actMines = this.calculateMinesAround(x,y);
                            System.out.println(x + ":" +  y);
                            this.squares[x][y].setText(Integer.toString(actMines));
                            
                        }  
                    }
                        
Dániel's avatar
Dániel committed
                }
            }
            else
            {
                squares[pos[0]][pos[1]].setText(Integer.toString(mines));
            }
    }
    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;
    }
    {
        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";
Dániel's avatar
Dániel committed
                      squares[i][j].setText(" ");
                      minesYet++;
                  }                  
                  else
                  {
                      name = Integer.toString(i) + ":" + Integer.toString(j) + ":0";
                  }
                  
                  
                  squares[i][j].setName(name);
Dániel's avatar
Dániel committed
                  //squares[i][j].addActionListener(new ButtonActionListener(this.controller));
                  squares[i][j].addMouseListener(new ButtonActionListener(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);
                  
                  
Dániel's avatar
Dániel committed
                //  if( !(squares[i][j].getText() == "*") )
                  //   squares[i][j].setText(Integer.toString(minesAround));
                  
                 // squares[i][j].setText(" ");
        this.controller.view.flagged = this.mines;
Kovax's avatar
Kovax committed
    public void colorField(int x,int y, Color color, Color colorF)
    {
            this.squares[x][y].setBackground(color);
            this.squares[x][y].setForeground(colorF);
    }
Dániel's avatar
Dániel committed
    void flagButton(int[] pos) 
    {
Kovax's avatar
Kovax committed
        
Dániel's avatar
Dániel committed
        String text = squares[pos[0]][pos[1]].getText();
        if( text.equals("?"))
        {
            squares[pos[0]][pos[1]].setText("");
Kovax's avatar
Kovax committed
            this.colorField(pos[0], pos[1], null, null);
Dániel's avatar
Dániel committed
        }
        else if( text.equals(""))
        {
            squares[pos[0]][pos[1]].setText("?");
Kovax's avatar
Kovax committed
             this.colorField(pos[0], pos[1], Color.BLUE, Color.YELLOW);
Dániel's avatar
Dániel committed
        }
        else if( text.equals(" "))
        {
            squares[pos[0]][pos[1]].setText("?");
Kovax's avatar
Kovax committed
             this.colorField(pos[0], pos[1], Color.BLUE, Color.YELLOW);
Dániel's avatar
Dániel committed
        }
Kovax's avatar
Kovax committed
          if( isGameOverAndWinner() )
          {
              this.isWinner = true;
            this.isGameOver = true;
            this.controller.gameOver();
          }
Dániel's avatar
Dániel committed
    }