2016-12-14 52 views
1

這是我的數獨遊戲的代碼。它將接受空白單元格的輸入,但由於某些原因,它不會改變顏色以指示輸入是正確還是錯誤。任何建議將不勝感激。Java數獨正確號碼輸入

import java.awt.*;  
import java.awt.event.*; 
import java.util.concurrent.TimeUnit; 

import javax.swing.*;  


public class Sudoku extends JFrame 
{ 
    long starttime = System.currentTimeMillis(); 
    public static final int gridDim = 9;//board size 
    public static final int subGridDim = 3; //sub grid size 
    public static final int cellSize = 60; 
    public static final int width = 630; 
    public static final int height = 630; 
    public static final Color emptyCell= Color.blue; 
    public static final Color correct = new Color(0, 255, 0); 
    public static final Color incorrect = Color.RED; 
    public static final Color backgroundColor = new Color(240, 240, 240); 
    public static final Color backgroundColorColor = Color.BLACK; 
    public static final Font fontColor = new Font("Times New Roman", Font.PLAIN, 20); 

    private JTextField[][] tfCells = new JTextField[9][9]; 

    String x; 
    int y; 
    private int[][] puzzle = {{5, 3, 4, 6, 7, 8, 9, 1, 2}, 
      {6, 7, 2, 1, 9, 5, 3, 4, 8}, 
      {1, 9, 8, 3, 4, 2, 5, 6, 7}, 
      {8, 5, 9, 7, 6, 1, 4, 2, 3}, 
      {4, 2, 6, 8, 5, 3, 7, 9, 1}, 
      {7, 1, 3, 9, 2, 4, 8, 5, 6}, 
      {9, 6, 1, 5, 3, 7, 2, 8, 4}, 
      {2, 8, 7, 4, 1, 9, 6, 3, 5}, 
      {3, 4, 5, 2, 8, 6, 1, 7, 9}}; 

    private boolean[][] masks = {{true, false, false, false, false, true, false, false, false}, 
      {false, true, false, true, false, false, false, false, true}, 
      {true, false, false, false, false, false, false, false, false}, 
      {true, false, false, false, true, true, false, false, false}, 
      {false, false, false, false, false, false, false, false, false}, 
      {false, false, false, true, false, true, false, false, false}, 
      {true, true, false, false, false, false, false, false, false}, 
      {false, false, false, true, false, false, false, false, false}, 
      {true, false, true, false, false, true, false, false, false}}; 

    public Sudoku() 
    { 
    Container cp = getContentPane(); 
    cp.setLayout(new GridLayout(9,9)); 
    InputListener listener = new InputListener(); 

    for (int row = 0; row < gridDim; ++row) //build 9x9 text fields 
    { 
     for (int col = 0; col < gridDim; ++col) 
     { 
     tfCells[row][col] = new JTextField(); 
     cp.add(tfCells[row][col]); 
     if (masks[row][col]) //if empty cell 
     { 
      tfCells[row][col].setText(""); 
      tfCells[row][col].setEditable(true); 
      tfCells[row][col].setBackground(correct); 
      tfCells[row][col].addActionListener(listener); 
     } 
     else //if cell is filled 
     { 
      tfCells[row][col].setText(puzzle[row][col] + ""); 
      tfCells[row][col].setEditable(false); 
      tfCells[row][col].setBackground(backgroundColorColor); 
      tfCells[row][col].setForeground(backgroundColor); 
     } 

     tfCells[row][col].setHorizontalAlignment(JTextField.CENTER); // fix cells 
     tfCells[row][col].setFont(fontColor); 
     } 
    } 
    cp.setPreferredSize(new Dimension(width, height)); //set window size 
    pack(); 

    setTitle("Sudoku"); 
    setVisible(true); 
    } 
    public static void main(String[] args) 
    { 
    Sudoku app = new Sudoku(); 
    app.setDefaultCloseOperation(EXIT_ON_CLOSE); 
    } 
    private class InputListener implements ActionListener 
    { 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     int rowSelected = -1; //determines which row/column is being looked at 
     int colSelected = -1; 
     JTextField source = (JTextField)e.getSource(); 
     boolean found = false; 
     for (int row = 0; row < gridDim && !found; ++row)// Scan fields for all rows and columns + 
                  //match with the source object 
     { 
     for (int col = 0; col < gridDim && !found; ++col) 
     { 
      if (tfCells[row][col] == source) 
      { 
      rowSelected = row; 
      colSelected = col; 
      found = true; // break the inner/outer loops 
      } 
     } 
     } 
     x = tfCells[rowSelected][colSelected].getText(); //get input 
     y = Integer.parseInt(x); 
     if(y==puzzle[rowSelected][colSelected]) 
     { 
     tfCells[rowSelected][colSelected].setBackground(Color.green); 
     } 
     else 
     tfCells[rowSelected][colSelected].setBackground(Color.red); 

     if(masks[rowSelected][colSelected])//if all answers are correct 
     { 
     long endtime = System.currentTimeMillis(); 
     long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(endtime - starttime); // time in seconds 
     JOptionPane.showMessageDialog(null, "Finished in: " + timeSeconds); 
     } 
    } 
    } 
} 

回答

0

你得到一個NumberFormatException當你嘗試y = Integer.parseInt(x);

要解決此添加catch,將設置在actionPerformed箱紅色。

x = tfCells[rowSelected][colSelected].getText(); //get input 
try { 
    y = Integer.parseInt(x); 
    System.out.println(y); 
    if(y==puzzle[rowSelected][colSelected]) { 
     tfCells[rowSelected][colSelected].setBackground(Color.green); 
    } else { 
     tfCells[rowSelected][colSelected].setBackground(Color.red); 
    } 
    if(masks[rowSelected][colSelected]) { //if all answers are correct 
     long endtime = System.currentTimeMillis(); 
     long timeSeconds = TimeUnit.MILLISECONDS.toSeconds(endtime - starttime); // time in seconds 
     JOptionPane.showMessageDialog(null, "Finished in: " + timeSeconds); 
    } 
} catch (NumberFormatException exc){ 
    tfCells[rowSelected][colSelected].setBackground(Color.red); 
} 
+0

我的彈出框只應該出現,如果拼圖完全完成和正確。相反,它會在每次輸入後出現,無論是否正確。有沒有辦法來解決這個問題? –

+0

當你在'actionPerformed'的末尾檢查'mask [rowSelected] [colSelected]'時,檢查所有的行和列是否爲'true'。如果沒有任何「真實」,則遊戲結束並顯示彈出窗口。目前您正在檢查當前選中的蒙版是否爲真。 –

+0

另外,在用戶獲得正確的數字之後,您從不將掩碼設置爲「false」。 –