📄 sudokudemo.java
字号:
clips.reset(); clips.assertString("(phase expand-any)"); clips.assertString("(size 3)"); /*======================================*/ /* Remember the initial starting values */ /* of the puzzle for the reset command. */ /*======================================*/ for (int i = 0; i < 9; i++) { JTable theTable = (JTable) mainGrid.getComponent(i); int rowGroup = i / 3; int colGroup = i % 3; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { resetValues[i][r][c] = theTable.getValueAt(r,c); String assertStr; assertStr = "(possible (row " + (r + (rowGroup * 3) + 1) + ") " + "(column " + (c + (colGroup * 3) + 1) + ") " + "(group " + (i + 1) + ") " + "(id " + ((i * 9) + (r * 3) + c + 1) + ") "; if ((resetValues[i][r][c] == null) || (resetValues[i][r][c].equals(""))) { assertStr = assertStr + "(value any))"; } else { assertStr = assertStr + "(value " + resetValues[i][r][c] + "))"; } clips.assertString(assertStr); } } } /*===================================*/ /* Update the status of the buttons. */ /*===================================*/ solved = true; resetButton.setEnabled(true); solveButton.setEnabled(false); techniquesButton.setEnabled(true); /*===================*/ /* Solve the puzzle. */ /*===================*/ clips.run(); /*===================================*/ /* Retrieve the solution from CLIPS. */ /*===================================*/ for (int i = 0; i < 9; i++) { JTable theTable = (JTable) mainGrid.getComponent(i); int rowGroup = i / 3; int colGroup = i % 3; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { resetValues[i][r][c] = theTable.getValueAt(r,c); if ((resetValues[i][r][c] != null) && (! resetValues[i][r][c].equals(""))) { continue; } String evalStr = "(find-all-facts ((?f possible)) " + "(and (eq ?f:row " + (r + (rowGroup * 3) + 1) + ") " + "(eq ?f:column " + (c + (colGroup * 3) + 1) + ")))"; MultifieldValue pv = (MultifieldValue) clips.eval(evalStr); if (pv.listValue().size() != 1) continue; FactAddressValue fv = (FactAddressValue) pv.listValue().get(0); theTable.setValueAt(" " + fv.getFactSlot("value") + " ",r,c); } } } /*===============================================*/ /* Any cells that have not been assigned a value */ /* are given a '?' for their content. */ /*===============================================*/ for (int i = 0; i < 9; i++) { JTable theTable = (JTable) mainGrid.getComponent(i); for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { if ((theTable.getValueAt(r,c) == null) || (theTable.getValueAt(r,c).equals(""))) { theTable.setValueAt("?",r,c); } } } } } /*===============================*/ /* Handle the Techniques button. */ /*===============================*/ else if (ae.getActionCommand().equals("Techniques")) { String evalStr; String messageStr = "<html><p style=\"font-size:95%\">"; evalStr = "(find-all-facts ((?f technique)) TRUE)"; MultifieldValue pv = (MultifieldValue) clips.eval(evalStr); int tNum = pv.listValue().size(); for (int i = 1; i <= tNum; i++) { evalStr = "(find-fact ((?f technique-employed)) " + "(eq ?f:priority " + i + "))"; pv = (MultifieldValue) clips.eval(evalStr); if (pv.listValue().size() == 0) continue; FactAddressValue fv = (FactAddressValue) pv.listValue().get(0); messageStr = messageStr + ((IntegerValue) fv.getFactSlot("priority")).intValue() + ". " + ((StringValue) fv.getFactSlot("reason")).getValue() + "<br>"; } JOptionPane.showMessageDialog(jfrm,messageStr,"Solution Techniques",JOptionPane.PLAIN_MESSAGE); } } /*#######################*/ /* FocusListener Methods */ /*#######################*/ /***************/ /* focusGained */ /***************/ public void focusGained(FocusEvent e) {} /*************/ /* focusLost */ /*************/ public void focusLost(FocusEvent e) { JTable theTable = (JTable) e.getComponent(); int r = theTable.getEditingRow(); int c = theTable.getEditingColumn(); /*====================================================*/ /* If a cell wasn't being edited, do nothing further. */ /*====================================================*/ if ((r == -1) || (c == -1)) return; /*========================*/ /* Stop editing the cell. */ /*========================*/ TableCellEditor tableCellEditor = theTable.getCellEditor(r,c); tableCellEditor.stopCellEditing(); /*=================================*/ /* Clear selections for the table. */ /*=================================*/ theTable.clearSelection(); } /*#####################*/ /* KeyListener Methods */ /*#####################*/ /**************/ /* keyPressed */ /**************/ public void keyPressed(KeyEvent e) {} /***************/ /* keyReleased */ /***************/ public void keyReleased(KeyEvent e) {} /************/ /* keyTyped */ /************/ public void keyTyped( KeyEvent e) { JTable theTable = (JTable) e.getComponent(); int row = theTable.getSelectedRow(); int col = theTable.getSelectedColumn(); /*=================================*/ /* Cells can't be change while the */ /* puzzle is in solution state. */ /*=================================*/ if (solved) return; /*=================================================*/ /* If a cell isn't selected, ignore the typed key. */ /*=================================================*/ if ((row == -1) || (col == -1)) return; /***************************/ /* Retrieve the typed key. */ /***************************/ char theChar = e.getKeyChar(); /*=======================================================*/ /* A backspace removes the value from the selected cell. */ /*=======================================================*/ if (theChar == '\b') { theTable.setValueAt("",row,col); return; } /*=========================================================*/ /* Any character other than the digits 1 to 9 is invalid. */ /*=========================================================*/ if ((theChar != '1') && (theChar != '2') && (theChar != '3') && (theChar != '4') && (theChar != '5') && (theChar != '6') && (theChar != '7') && (theChar != '8') && (theChar != '9')) { Toolkit.getDefaultToolkit().beep(); return; } /*=====================================*/ /* Set the value of the selected cell. */ /*=====================================*/ String theCharStr = Character.toString(theChar); theTable.setValueAt(theCharStr,row,col); /*===========================================*/ /* Remove any other occurences of this digit */ /* from the same 3x3 grid. */ /*===========================================*/ for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { if (((r != row) || (c != col)) && (theCharStr.equals(theTable.getValueAt(r,c)))) { theTable.setValueAt("",r,c); } } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -