2016-12-14 4 views
1

여기 내 스도쿠 게임 코드가 있습니다. 빈 셀에 대한 입력을 허용하지만 입력이 옳은지 또는 잘못되었는지를 나타내는 어떤 이유로 인해 색상이 변경되지 않습니다. 어떤 제안이라도 대단히 감사하겠습니다. 당신이 당신의 actionPerformed의 상자 빨간색을 설정하는 catch를 추가 해결하려면 y = Integer.parseInt(x);자바 스도쿠 올바른 번호 입력

을 시도 할 때

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을 얻고있다.

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'의 끝에서'masks [rowSelected] [colSelected]'를 체크하면 모든 행과 cols에'true'가 있는지 검사합니다. 'true'가 아닌 경우 게임이 종료되고 팝업이 표시됩니다. 현재 선택된 현재 마스크가 참인지 확인하고 있습니다. –

+0

또한 사용자가 숫자를 올바르게 입력 한 후에는 마스크를 'false'로 설정하지 않습니다. –