2016-12-14 79 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”。 –