Skip to content
Snippets Groups Projects
Model.java 8.66 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;

Dániel's avatar
Dániel committed
import java.awt.List;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;

Kovax's avatar
v2
Kovax committed
/**
 *
 * @author Kovax
 */
public class Model 
{
    Controller controller;
    public JButton[][] squares;
Kovax's avatar
v2
Kovax committed
    
    public Model(Controller controller)
    {
        this.controller = controller;
        squares = new JButton[controller.settings.size][controller.settings.size];
    }
    /*
     *   012
     *   345
     *   678
     */
    private String[] getNeighbours(int[] pos)
    {
        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;
      
    }
Dániel's avatar
Dániel committed
    /*
     * A gomb uj kinezetet szamolja ki
     */
    public void calculateButtonStyle(int[] pos)
    {
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("*");
                    }
                    else
                    {
                        
                    }
                }
            }
        }
        else
        {
            int mines = this.calculateMinesAround(pos[0], pos[1]);
            if( mines == 0 )
            {
                ArrayList<int[]> nulls = this.getNullFieldAround(pos[0], pos[1], null,null);
                for( int[] nullMiner : nulls )
                {
                    this.squares[nullMiner[0]][nullMiner[1]].setText("0");
                }
            }
            else
            {
                squares[pos[0]][pos[1]].setText(Integer.toString(mines));
            }
        }
    //    System.out.println("Actual:" + pos[0] + ":" + pos[1]);
    //    for (int i = 0; i < 9; i++) {
    //        System.out.println(i + "-" + this.getNeighbours(pos)[i]);
    //    }
        
    }
    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;
    }
Dániel's avatar
Dániel committed
    private ArrayList<int[]> getNullFieldAround(int x, int y,ArrayList<int[]> nulls,ArrayList<int[]> wasThere)
    {
        if( wasThere == null )
            wasThere = new ArrayList<>();
        
        ArrayList<int[]> nullsAround;
        if( nulls != null )
            nullsAround = nulls;
        else
            nullsAround = new ArrayList<>();
        
        int pos[] = new int[2];
        pos[0] = x;
        pos[1] = y;
        String[] neighbours = this.getNeighbours(pos);
        
        for (int i = 0; i < 9; i++) 
        {
            int[] posN = getPosFromString(neighbours[i]);
            if( posN[0] == -1 || posN[1] == -1 )
            {
                int dummy = 10;
            }
            else if( this.calculateMinesAround(posN[0], posN[1]) == 0 )
            {
                if( !this.isContains(wasThere, posN) )
                {
                    nullsAround.add(posN);
                    wasThere.add(posN);
                    ArrayList<int[]> newNulls = this.getNullFieldAround(posN[0], posN[1], nullsAround,wasThere);
                    
                    nullsAround.addAll(newNulls);
                    for( int[] actNull : newNulls )
                    {
                        this.addUnique(nullsAround, actNull);
                    }
                } 
            }
        }
        return nullsAround;
    }
    private ArrayList<int[]> addUnique(ArrayList<int[]> to, int[] add)
    {
        for (int[] actTo : to  ) 
        {
            if( actTo == add )
                return to;
        }
        to.add(add);
        return to;
    }
    private Boolean isContains(ArrayList<int[]> container, int[] position)
    {
        for( int[] act : container)
        {
            if( act[0] == position[0] && act[1] == position[1] )
                return true;
        }
        return false;
    }
    private int calculateMinesAround(int x,int y)
    {
        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);
                  squares[i][j].addActionListener(new ButtonActionListener(this.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(" ");