Hi ich habe ein kleines Problem bei der Verbindung zwischen meiner Minesweeper Gui und Minesweeper Logik .Bei dem compilen beider Codes kommt folgender Fehler:
Java:
Msgui.java:68: error: local variables referenced from an inner class must be final or effectively final
sf.linksKlick(i, j);
^
Msgui.java:68: error: local variables referenced from an inner class must be final or effectively final
sf.linksKlick(i, j);
Code:
class Spielfeld{
int h;
int b;
int am;
Feld[][]s;
Spielfeld(int h, int b, int am){
this.h = h;
this.b = b;
this.am = am;
s = new Feld[h][b];
for(int i=0; i<h;i++){
for(int j=0; j<b;j++){
s[i][j] = new Feld();
}
}
setMine();
setanzahl();
}
public void setMine(){
boolean anzahlMine = true;
int counter = 0;
if(am<=0){
anzahlMine = false;
}
while(anzahlMine){
int h = (int)(Math.random()*this.h);
int b = (int)(Math.random()*this.b);
if(!s[h][b].getMine()){
s[h][b].setMine();
counter++;
}
if(counter == am){
anzahlMine = false;
}
}
}
public void setanzahl(){
for(int x = 0; x<s.length;x++){
for(int y=0;y<s[0].length;y++){
if(!s[x][y].getMine()){
int nachbar = 0;
if(x>0 && y>0 && s[x-1][y-1].getMine()){ //hoch links
nachbar++;
}
if(x>0 && y<s[0].length-1 && s[x-1][y+1].getMine()){ // unten links
nachbar++;
}
if(x>0 && s[x-1][y].getMine()){// links
nachbar++;
}
if(x<s.length-1 && y>0 && s[x+1][y-1].getMine()){//hoch rechts
nachbar++;
}
if(x<s.length-1 && y<s[0].length-1 && s[x+1][y+1].getMine()){ // unten rechts
nachbar++;
}
if(x<s.length-1 && s[x+1][y].getMine()){ // rechts
nachbar++;
}
if(y>0 && s[x][y-1].getMine()){ //hoch
nachbar++;
}
if(y<s[0].length-1 && s[x][y+1].getMine()){// unten
nachbar++;
}
s[x][y].setanzahl(nachbar);
}
}
}
}
public void print(){
for(int i=0; i<h; i++){
System.out.println(" ");
for(int j=0; j<b; j++){
String z = "?";
if(s[i][j].getgeoeffnet())
z = "+";
else
z = "-";
if(s[i][j].getMine()){
System.out.print(" *"+z);
}else{
System.out.print(" "+s[i][j].getanzahl()+z);
}
}
}
}
public void linksKlick(int x, int y){
if(x>=0 && y>=0 && x< s.length && y<s[0].length){
if(s[x][y].istgeoeffnet == false) {
s[x][y].istgeoeffnet = true;
if(s[x][y].getMine()){
System.out.println("Verloren");
}
if(s[x][y].anzahlMine == 0){
linksKlick(x -1, y -1);
linksKlick(x -1, y +1);
linksKlick(x -1, y);
linksKlick(x +1, y -1);
linksKlick(x +1, y +1);
linksKlick(x +1, y );
linksKlick(x , y -1);
linksKlick(x , y +1);
}
}
}
}
}
Java:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.event.*;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Color;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.FileInputStream;
public class Msgui extends Application{
Spielfeld sf;
public void start(Stage stage){
stage.setTitle("Minesweeper");
BorderPane root = new BorderPane();
HBox oben = new HBox();
GridPane spiel = createSpiel();
MenuBar menuBar = createMenu();
oben.getChildren().addAll(menuBar);
root.setTop(oben);
root.setCenter(spiel);
Scene scene = new Scene(root);
root.setStyle("-fx-background-color: #C1CDCD;");
stage.setScene(scene);
stage.show();
}
public static void main(String[] a){
launch(a);
}
GridPane createSpiel(){
sf = new Spielfeld(15, 15, 35);
GridPane spiel = new GridPane();
for(int i=0; i < 15; i++){
for(int j=0; j < 15; j++){
Button t = new Button();
t.setStyle("-fx-background-color: #676664; -fx-border-width: 1px; -fx-border-color: #ffffff;");
t.setPrefSize(25, 25);
t.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
sf.linksKlick(i, j);
update();
}
});
spiel.add(t,i,j);
}
}
return spiel;
}
MenuBar createMenu(){
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("Datei");
MenuItem newItem = new MenuItem("Neu");
MenuItem quitItem = new MenuItem("Beenden");
menu.setStyle("-fx-selection-bar: #FFA500;");
quitItem.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
System.exit(1);
}
});
menu.getItems().addAll(newItem,quitItem);
menuBar.getMenus().addAll(menu);
return menuBar;
}
void update(){
/* t.setStyle("-fx-background-color: #C1CDCD; -fx-border-width: 1px; -fx-border-color: #FFFFFF;");*/
}
}