2016-05-03 3 views
0

Wie sortiere ich ein Array von Schaltflächen, um zu sehen, ob nur noch eine übrig ist? Ich erstelle einen Sudoku-Solver und muss prüfen, ob in jeder Zeile, Spalte oder in jedem Feld nur noch eine Schaltfläche ausgewählt ist und ob die letzte verbleibende Option hervorgehoben werden soll. Ich kann keinen Befehl finden, mit dem ich jede einzelne Zelle in der Reihe überprüfen kann, um zu sehen, ob NUR eine übrig ist. Dies ist, was ich bisherWie sortiere ich ein Array von Schaltflächen, um zu sehen, ob nur noch eins aktiviert ist?

public class SudoHelper extends Application 

{ 




public boolean [][][] DisabledCell = new boolean[3][9][9]; // Creates our array 
Scene scene; 
Pane pane; 
Pane main; 
BookMark bane; 

@Override 
public void start (Stage primaryStage) 
{ 
    for(int a = 0;a<3;a++) 
    { 
     for(int b=0;b<9;b++) 
     { 
      for(int c=0; c<9;c++) 
      { 
       DisabledCell[a][b][c]=false; 
      } 
     } 
    } 
    mouseClicks = new MouseEvent[0]; 
    SmartCell[] currentGame = new SmartCell[81]; 
    pane = new Pane(); 
    pane.setPrefSize(684, 702); 
    int x,y; 
    x=y=0; 
    main = new Pane(); 
    for(int i=0; i<81; i++) 
    { 
     currentGame[i]= new SmartCell(i); 
     currentGame[i].setLayoutX(x); 
     currentGame[i].setLayoutY(y); 
     pane.getChildren().add(currentGame[i]); 
     x+=76;          // Sets the layout for out array of SmartCells 
     if(x==684)         // and puts our additional buttons on the screen 
     {           // With our scene and stage 
      x=0; 
      y+=78;     
     }   
    } 

    main.setPrefSize(1100, 702); 
    main.getChildren().add(pane); 
    bane= new BookMark(); 
    scene = new Scene(main); 
    main.getChildren().add(bane); 
    bane.setPrefSize(416, 702); 
    bane.setLayoutX(685); 
    bane.setLayoutY(0); 
    primaryStage.setScene(scene); 
    primaryStage.setTitle("Sudoku Helper"); 
    primaryStage.show(); 


} 
public static void main(String[] args) 
{ 
    Application.launch(args);  // Starts the game 


} 
class BookMark extends Pane 
{ 
    class List 
    { 
     String[] MyList=new String[0]; 
     public void Add(String Add) 
     { 
      if(this.MyList.length>0) 
      { 
      String[] Resize = new String[this.MyList.length+1]; 

      for(int i=0;i<this.MyList.length;i++) 
      { 
       Resize[i]=this.MyList[i]; 
      } 
      Resize[this.MyList.length+1]=Add; 
      } 
      else 
      { 
       this.MyList = new String[1]; 
       this.MyList[0]=Add; 
      } 
     } 
     public void Clear() 
     { 
      this.MyList = new String[0]; 
     } 
    } 

    Button lkm = new Button("Load Bookmark"); // Creates our load bookmark button 
    Button bkm = new Button("Save Bookmark"); // Creates out save bookmark button' 
    final ToggleGroup group1 = new ToggleGroup(); 
    final ToggleGroup group2 = new ToggleGroup(); 
    RadioButton rl1a = new RadioButton("Rule One All"); // Creates rule one radiobutton 
    RadioButton rl1s = new RadioButton("Rule One Click"); // Creates Rule one click radiobutton 
    RadioButton rl2s = new RadioButton("Rule Two Click"); // Creates rule two click radiobutton 
    RadioButton rl2a = new RadioButton("Rule Two All"); // Create rule two radiobutton 

       void ruleTwo() 
     { 

     } 
     void ruleOne() 
     { 

      int _x,_y; 
      int B=10; 
      String[]Getloc = name.split("~"); 
      int loc = Integer.parseInt(Getloc[1]); 
      _y = loc%9; 
      if(loc<8) // checking which row we are looking at 
      { 
       _x=0; 
       for(_x=0; loc<8;) 
       { 
        if() 
          { 

          } 
       } 
      } 

      else if(loc<18) 
      { 
       _x=1; 
      } 
      else if(loc<27) 
      { 
       _x=2; 
      } 
      else if(loc<36) 
      { 
       _x=3; 
      } 
      else if(loc<45) 
      { 
       _x=4; 
      } 
      else if(loc<54) 
      { 
       _x=5; 
      } 
      else if(loc<63) 
      { 
       _x=6; 
      } 
      else if(loc<72) 
      { 
       _x=7; 
      } 
      else 
      { 
       _x=8; // checks blocks were looking at 
      } 

      if(_y>=0&&_y<=2) // checks which block we are in 
      { 
        if(_x>=0&&_x<=2) 
        { 
         B=0; 
        } 
        else if(_x>=3&&_x<=5) 
        { 
         B=1; 
        } 
        else if(_x>=6&&_x<=8) 
        { 
         B=2; 
        } 
       } 
       else if(_y>=3&&_y<=5) 
       { 
        if(_x>=0&&_x<=2) 
        { 
         B=3; 
        } 
        else if(_x>=3&&_x<=5) 
        { 
         B=4; 
        } 
        else if(_x>=6&&_x<=8) 
        { 
         B=5; 
        } 
       } 
       else if(_y>=6&&_y<=8) 
       { 
        if(_x>=0&&_x<=2) 
        { 
         B=6; 
        } 
        else if(_x>=3&&_x<=5) 
        { 
         B=7; 
        } 
        else if(_x>=6&&_x<=8) 
        { 
         B=8; 
        } 
       } 


     } 
     BookMark() // Our buttons specifications (Location font ect) 
     { 


      this.bkm = new Button("Save Bookmark"); 
      this.lkm = new Button("Load Bookmark"); 
      this.bkm.setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      this.bkm.setLayoutX(10); 
      this.bkm.setLayoutY(10); 
      this.lkm.setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      this.lkm.setLayoutX(150); 
      this.lkm.setLayoutY(10); 
      this.rl1a.setLayoutX(10); 
      this.rl1a.setLayoutY(250); 
      this.rl2a.setLayoutX(10); 
      this.rl2a.setLayoutY(500); 
      this.rl2s.setLayoutX(250); 
      this.rl1s.setLayoutY(250); 
      this.rl1s.setLayoutX(250); 
      this.rl2s.setLayoutY(500); 
      this.rl1a.setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      this.rl1s.setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      this.rl2a.setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      this.rl2s.setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      this.group1.getToggles().add(this.rl1a); 
      this.group1.getToggles().add(this.rl1s); 
      this.group2.getToggles().add(this.rl2a); 
      this.group2.getToggles().add(this.rl2s); 
      BookMark.this.getChildren().add(this.lkm); 
      BookMark.this.getChildren().add(this.bkm); 
      BookMark.this.getChildren().add(this.rl1a); 
      BookMark.this.getChildren().add(this.rl1s); 
      BookMark.this.getChildren().add(this.rl2a); 
      BookMark.this.getChildren().add(this.rl2s); 

      // bkm.setOnMouseClicked(e -> Save(e)); 




     } 
} 
private MouseEvent[] mouseClicks; 
class SmartCell extends StackPane 
{ 
    GridPane buttonPane; 
    Pane valPane; 
    Text textVal; 
    Button [] btn; 
    String name; 
    SmartCell(int nameint) 
    { 
     buttonPane = new GridPane(); 
     btn = new Button[10]; 
     for(int i = 1; i <= 9; i++) 
     { 
      btn[i] = new Button(i+""); // Turns i into a String 
      btn[i].setFont(Font.font("Ariel", FontWeight.BOLD, FontPosture.REGULAR, 12)); 
      btn[i].setOnMouseClicked(e -> mouseHandler(e)); 
      buttonPane.add(btn[i], (i-1)%3, (i-1)/3); 



     } 

     // When the user clicks one of the 9 buttons, we want to take the number of the button 
     // they clicked, and set the text on the text pane to that number, hide the 9 buttons, 
     // and show the text pane. 
     name = "SmartCell~"+String.valueOf(nameint); 
     textVal = new Text(25, 55, ""); 
     textVal.setFont(Font.font("Arial", 48)); 
     textVal.setTextAlignment(TextAlignment.CENTER); 
     valPane = new Pane(); 
     valPane.setStyle("-fx-border-color:black; -fx-border-stroke-width:1"); 
     valPane.getChildren().add(textVal); 




     getChildren().add(buttonPane); // Add the pane with the 9 buttons to the cell 
     getChildren().add(valPane); // Add the pane with the one piece of text to the cell 
     buttonPane.setVisible(true); // We start out showing the 9 buttons 
     valPane.setVisible(false);  // ...NOT showing the pane with the single text 

    } // end constructor 



    void disqual(MouseEvent e) 
    { 
     MouseEvent[] ResizeMouse = new MouseEvent[SudoHelper.this.mouseClicks.length+1]; 
     ResizeMouse[ResizeMouse.length-1]=e; 
     SudoHelper.this.mouseClicks = ResizeMouse; 
     int _x,_y; 
     int B=10; 
     String[]Getloc = name.split("~"); 
     int loc = Integer.parseInt(Getloc[1]); 
     _y = loc%9; 
     if(loc<8) // checking which row we are looking at 
     { 
      _x=0; 
     } 
     else if(loc<18) 
     { 
      _x=1; 
     } 
     else if(loc<27) 
     { 
      _x=2; 
     } 
     else if(loc<36) 
     { 
      _x=3; 
     } 
     else if(loc<45) 
     { 
      _x=4; 
     } 
     else if(loc<54) 
     { 
      _x=5; 
     } 
     else if(loc<63) 
     { 
      _x=6; 
     } 
     else if(loc<72) 
     { 
      _x=7; 
     } 
     else 
     { 
      _x=8; // checks blocks were looking at 
     } 

     if(_y>=0&&_y<=2) // checks which block we are in 
     { 
       if(_x>=0&&_x<=2) 
       { 
        B=0; 
       } 
       else if(_x>=3&&_x<=5) 
       { 
        B=1; 
       } 
       else if(_x>=6&&_x<=8) 
       { 
        B=2; 
       } 
      } 
      else if(_y>=3&&_y<=5) 
      { 
       if(_x>=0&&_x<=2) 
       { 
        B=3; 
       } 
       else if(_x>=3&&_x<=5) 
       { 
        B=4; 
       } 
       else if(_x>=6&&_x<=8) 
       { 
        B=5; 
       } 
      } 
      else if(_y>=6&&_y<=8) 
      { 
       if(_x>=0&&_x<=2) 
       { 
        B=6; 
       } 
       else if(_x>=3&&_x<=5) 
       { 
        B=7; 
       } 
       else if(_x>=6&&_x<=8) 
       { 
        B=8; 
       } 
      }; 
      int CellNum =Integer.parseInt(((Button)e.getSource()).getText()); 
      if(DisabledCell[0][_x][CellNum-1]==true||DisabledCell[1][_y][CellNum-1]==true||DisabledCell[2][B][CellNum-1]==true) 
      { 
       disqualify(Integer.parseInt(((Button)e.getSource()).getText())); 
       return; 
      } 
      DisabledCell[0][_x][CellNum-1]=true; 
      DisabledCell[1][_y][CellNum-1]=true; 
      DisabledCell[2][B][CellNum-1]=true; 
      textVal.setText(((Button)e.getSource()).getText()); 
      buttonPane.setVisible(false); 
      valPane.setVisible(true); 

      for(int i = 1; i <= 9; i++)disqualify(i); // Since we have locked in, all others are out of play 
      // in this cell 
      int c = 0; 
      int z = 0; 
      switch(B) 
      { // Figures out which block our selected number is in. 
      case 0: z=0; 
       break; //^
      case 1: z=27; 
       break; //^
      case 2: z=54; 
       break; //^
      case 3: z=3; 
       break; //^
      case 4: z=30; 
       break; //^
      case 5: z=57; 
       break; //^
      case 6: z=6; 
       break; //^
      case 7: z=33; 
       break; //^
      case 8: z=60; 
       break; //^

      } 
      for(int w = 0; w<9; w++) 
      { 

       Object xob =SudoHelper.this.pane.getChildren().get((_x*9)+w); 
       SmartCell d =SmartCell.class.cast(xob); // Disqualifies x cells 
       d.disqualify(CellNum); 
       xob=d; 
       Object yob =SudoHelper.this.pane.getChildren().get(_y+(9*w)); 
       SmartCell b =SmartCell.class.cast(yob); // Disqualifies Y Cells 
       b.disqualify(CellNum); 
       yob=b; 
       Object zob =SudoHelper.this.pane.getChildren().get(z+c); 
       SmartCell a =SmartCell.class.cast(zob); 
       a.disqualify(CellNum); // Disqualifies boxes 
       zob=a; 
       c++; 
       if(c==3) 
       { 
        z+=9; 
        c=0; 

       } 
      } 




    } 
    void mouseHandler(MouseEvent e) 
    { 
     // When any button gets clicked, we take the text from the button, put it on the Text 
     // shape, hide the pane with the 9 buttons, and show the text pane, making it look like 
     // the 9 buttons have "gone", and the new value that we have "locked in" has taken their place. 
     // 

     if(e.getSource() instanceof Button) 
     { 
      // If this was a right click, then just disable this button; If it was a left click then lock 
      // in the value this button represents. 
      if(e.getButton() == MouseButton.SECONDARY) 
      { 
       disqualify(Integer.parseInt(((Button)e.getSource()).getText())); 
       // disables button after clicked 
       return; 
      } 
      // System.out.print("A button was clicked"); // for debugging 

     disqual(e); 
     } // end if source was a button 
     } // end mouseHandler 

    void disqualify(int buttonNo) 
    { 
     // When we are called, we disable button #buttonNo in this cell 
     btn[buttonNo].setDisable(true); 
     btn[buttonNo].setStyle("-fx-base:black; -fx-text-fill:black; -fx-opacity:1.0"); // Sets color of cells and numbers after disabled. 
    } 
    public String toString() 
    { 
     // The toString representation of a cell is a string containing a list of 
     // the values "still in play" --- the remaining candidate values --- for the cell 
     // 
     // Start with an empty string. Visit all 9 buttons, and if a given button is 
     // not disabled (i.e still in play), then add its number (from the text on the 
     // button) to our string 
     // 
     String result = ""; 
     for(int i = 1; i <= 9; i++) 
      if(!btn[i].isDisabled()) 
       result += i; 
     return result; 

    } 
} 

     private boolean[] InitalizeTile() // Initalizes our board of 81 cells 
     { 
      boolean[] newbool = new boolean[81]; 
      for(int i=0; i<81; i++) 
      { 
       newbool[i] = false; 
      } 
      Random R = new Random(); 
      for(int j=0; j<0; j++) 
      { 
       while(true) 
        { 
         int W = R.nextInt(81); 
         if(newbool[W]==false) 
         { 
          newbool[W]=true; 
          break; 
         } 
        } 
      } 
      return newbool; 
     } 

}  
+1

Bitte Postleitzahl, die für das Problem relevant ist Sie fragen. Sie haben nicht einmal eine Reihe von Schaltflächen im Code. Siehe [mcve] (http://stackoverflow.com/help/mcve), versuchen Sie, Code zu schreiben, der sowohl minimal ist (zeigt nur das vorliegende Problem an), als auch abgeschlossen (eine in sich abgeschlossene Anwendung, die erfüllt und ausgeführt werden kann) Kopieren und Einfügen ohne Änderung). – jewelsea

+0

Ich übermittelte meinen Code mit dem hilfreicheren Code erneut – NBaumbick

+0

Ja, es ist hilfreicher, aber es ist auch wirklich lang. Die Idee mit einem mcve ist, dass Sie den Code auf den minimalen Code reduzieren, der benötigt wird, um das vorliegende Problem zu demonstrieren, während Sie weiterhin ein vollständiges ausführbares Programm führen. – jewelsea

Antwort

0

Alles, was ich wirklich wissen muß, ist, wie bei einer Reihe von Tasten schauen und sehen, wenn nur ein aktiviert ist links.

Arrays.stream(buttons).filter(button -> !button.isDisabled()).count() == 1 

Anwendungsbeispiel:

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.scene.control.Button; 
import javafx.stage.Stage; 

import java.util.Arrays; 

public class DisabledButtonCount extends Application { 
    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     Button[] buttons = { 
       new Button("1"), 
       new Button("2"), 
       new Button("3") 
     }; 

     buttons[1].setDisable(true); 
     buttons[2].setDisable(true); 

     System.out.println(
       "Only one button enabled? " + 
         (Arrays.stream(buttons).filter(button -> !button.isDisabled()).count() == 1) 
     ); 

     Platform.exit(); 
    } 
} 
Verwandte Themen