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

0 aknák rekurzív keresése

parent 78f6c8a3
No related branches found
No related tags found
No related merge requests found
/nbproject/private/
/build/
\ No newline at end of file
/build/
/dist/
\ No newline at end of file
......@@ -26,7 +26,7 @@ public class ButtonActionListener implements ActionListener
{
JButton button;
button = (JButton) event.getSource();
System.out.println(button.getSize());
controller.gameMineButtonPressed(button.getName());
......
......@@ -61,6 +61,12 @@ public class Controller
int[]pos = this.getButtonPos(button);
model.calculateButtonStyle(pos);
}
public void reDraw()
{
this.view.draw();
}
......
......@@ -6,6 +6,8 @@
package GUI.game;
import java.awt.List;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JButton;
......@@ -62,8 +64,46 @@ public class Model
return neighbours;
}
/*
* A gomb uj kinezetet szamolja ki
*/
public void calculateButtonStyle(int[] pos)
{
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]);
......@@ -96,6 +136,66 @@ public class Model
}
return false;
}
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];
......@@ -149,7 +249,7 @@ public class Model
{
//tegyunk be egyet
name = Integer.toString(i) + ":" + Integer.toString(j) + ":b";
squares[i][j].setText("*");
squares[i][j].setText(" ");
minesYet++;
}
else
......@@ -172,8 +272,10 @@ public class Model
squares[i][j].setName(name + ":" + minesAround);
if( !(squares[i][j].getText() == "*") )
squares[i][j].setText(Integer.toString(minesAround));
// if( !(squares[i][j].getText() == "*") )
// squares[i][j].setText(Integer.toString(minesAround));
// squares[i][j].setText(" ");
}
}
}
......
......@@ -7,9 +7,14 @@
package GUI.game;
import GUI.Window.Window;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
*
......@@ -20,19 +25,22 @@ public class View extends Window
private Controller controller;
private Model model;
private JFrame frame;
private JPanel buttons;
public View(Controller game, Model gameModel)
{
this.model = gameModel;
this.controller = game;
this.setSize(400,400);
this.setSize(controller.settings.size*46,controller.settings.size*26);
//w41 h26
this.setLayout(new GridLayout(controller.settings.size,controller.settings.size));
this.draw();
}
public void draw()
{
model.build();
for(int i=0;i<controller.settings.size;i++)
{
......@@ -45,12 +53,57 @@ public class View extends Window
{
frame.dispose();
}
//Kulso panel
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
//Frame
this.frame = new JFrame("Aknakereső");
frame.add(this);
frame.setSize(400,400);
frame.setSize(controller.settings.size*46,(controller.settings.size+4)*26);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
GridBagConstraints c = new GridBagConstraints();
JPanel upper = new JPanel();
upper.setSize(controller.settings.size*41,100);
JLabel timeLeft = new JLabel();
JLabel points = new JLabel();
JButton stopButton = new JButton();
timeLeft.setText("asd");
points.setText("points");
stopButton.setText("buton");
timeLeft.setSize(1000, 100);
c.fill = GridBagConstraints.EAST;
c.gridx = 0;
c.gridy = 0;
upper.add(timeLeft);
upper.add(stopButton);
upper.add(points);
panel.add(upper,c);
JPanel buttons = new JPanel();
buttons.add(this);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
panel.add(buttons,c);
frame.add(panel);
}
......
......@@ -14,8 +14,8 @@ import javax.swing.JPanel;
* @author Kovax
*/
public class Window extends JPanel {
protected int windowHeight = 400;
protected int windowWidth = 400;
protected int windowHeight = 1000;
protected int windowWidth = 1000;
protected JFrame frame;
......
......@@ -15,7 +15,7 @@ public class Program
public static void main(String[] args)
{
Settings settings = new Settings(5,"UnnamedPlayer",3);
Settings settings = new Settings(5,"UnnamedPlayer",1);
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