检查用户输入是否与同一列中的任何数字匹配

时间:2014-02-14 00:40:35

标签: java netbeans

程序是10x10板,我需要检查用户的输入是否是同一列中任何数字的副本,但我无法弄清楚。当我尝试它时,它总是检查同一个盒子。例如,如果我在[1] [1]中输入4(通过10x10网格),它会在我输入之后立即自动检查[1] [1]与我的输入相同并将其删除。我的教授要我用“CheckWinner”方法检查它。这是我在eventhandler中的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firstgui;
import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;


/**
 *
 * @author douglas moody
 */
 public class EventHandler  implements ActionListener{
     // EventBoard is the name of the array containing all the JButton withi teh EventHandler
     // board (in the other program)  is the name of the array containing all the JButton withi the FirstGui program
     JButton[][] EventBoard;
     private  static String player = " ";
     // this method is called from FirstGui to tell the Eventhandler the board array
     public void setEventBoard (JButton[][] inboard){
        EventBoard = inboard;
     }      

     public  void actionPerformed(ActionEvent e) {
        JButton clickedbutton;

        // clickedbutton will now point to the Button actually clicked
        clickedbutton = (JButton) e.getSource();


        player = JOptionPane.showInputDialog("Enter a number from 1-10");
        if(player.matches("[1-9]|10")){
            clickedbutton.setText(player);
        }else{
            JOptionPane.showMessageDialog(null, "Invalid Input", "Invalid Input", JOptionPane.ERROR_MESSAGE);
        }

        // call the routine to check if the player who just moved won
        if (CheckWinner(player)){
            JOptionPane.showMessageDialog(null,   "Player: " + player + " won");
        }
    }   

    private boolean CheckWinner(String inplayer) {
        int count = 0, count2 = 0;
        // this loop checks the columns on the Board
        for (int i=0; i<=9; i++){
            for (int j=0; j<=9; j++) {
                if (EventBoard[i][j].getText().equals( inplayer )){
                    JOptionPane.showMessageDialog(null, "copy");
                    EventBoard[i][j].setText("");
                }
            }
            if (count == 10 && count2 == 10) return true;
        }
        return false;
    }
 }

1 个答案:

答案 0 :(得分:0)

将引发操作的JButton引用传递给CheckWinner方法,并在执行检查时忽略它...

例如......

private boolean CheckWinner(JButton source, String inplayer) {
    //...
    if (EventBoard[i][j] != source && 
            EventBoard[i][j].getText().equals( inplayer )){
        JOptionPane.showMessageDialog(null, "copy");
        EventBoard[i][j].setText("");
    }

<强>更新...

要检查给定列中的值,您需要知道要检查的列。我没有对此进行过测试,但下面的示例应该有所帮助...

private boolean CheckWinner(JButton source, String inplayer) {
    int buttonColumn = -1;
    for (int col = 0; col < EventBoard.length; col++) {
        for (JButton item : EventBoard[col]) {
            if (item == source) {
                buttonColumn = col;
                break;
            }
        }
    }
    boolean match = false;
    for (JButton item : EventBoard[buttonColumn]) {
        if (source != item && item.getText().equals(inplayer)) {
            match = true;
            break;
        }
    }
    return match;
}