0

私は connect four のようなプログラムを書いており、特定の単語 (TOOT または OTTO) が綴られたときに 4 つのボタンの背景を変更する必要があります。現在、私のコードは背景を変更していません。どんな助けでも素晴らしいでしょう。

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.lang.Integer;
import javax.swing.AbstractButton;
import java.awt.Color;
import javax.swing.UIManager;

public class TootAndOtto extends JFrame implements ActionListener {


  private int rows;
  private int columns;
  private JButton[][] gameBoard;
  private String player = "T";


  public TootAndOtto() {
    rows = 6;
    columns = 6;
    runGame();
    this.setVisible(true);
  }


/**
 * Creates the board, as well as the grid and buttons that are placed on the board
 */
  public void runGame() {
    this.setSize(600,600);
    this.setLocation(300,100); 
    JPanel board = new JPanel(new GridLayout(rows, columns));
    board.setVisible(true);
    this.getContentPane().add(board);
    gameBoard = new JButton[rows][columns];
    for (int i = 0; i < rows; i++) {
      for (int i2 = 0; i2 < columns; i2++) {
        gameBoard[i][i2] = new JButton();
        gameBoard[i][i2].addActionListener(this);
        board.add(gameBoard[i][i2]);
      }
    }
  }

/**
 * Runs the program anytime a button is clicked. This takes care of finding what button was clicked, placing a letter on a button,
 * and determining if a winner exists
 * @param e - the information of a button once is is clicked
 */
  public void actionPerformed(ActionEvent e) {
    JButton b = (JButton)e.getSource();
    int r = findRow(b);
    int c = findColumn(r, b);
    placeLabel(c);
    checkVertical(r, c);
  }

  /**
 * Finds the row of the button that is clicked
 * @param b - The address of the button that is clicked
 * @return An int that is the number of the row that but clicked button is in.
 */
  public int findRow(JButton b) {
    int r = -1;
    for (int i = 0; i < rows; i++) {
      for (int i2 = 0; i2 < columns; i2++) {
        if (b.equals(gameBoard[i][i2])){
          r = i;
        }
      }   
    }               
    return r;
  }

    /**
 * Finds the column of the button that is clicked
 * @param b - The address of the button that is clicked
 * @return An int that is the number of the column that but clicked button is in.
 */
  public int findColumn(int r, JButton b) {
    int c = -1;
    for (int z = 0; z < columns; z++) {
      if (b.equals(gameBoard[r][z])) {
        c = z;
      }
    }
    return c;
  }

   /**
 * places alternation player letters of T and O into the board if there is an empty space.
 * @param c - The column that the clicked button is in
 */
  public void placeLabel(int c) {
    boolean playMade = false;
    for(int i = rows-1; i >= 0; i--) {
      if(gameBoard[i][c].getText() == "" && playMade == false) {
        gameBoard[i][c].setText(player);
        playMade = true;
        if (player == "T")
          player = "O";
        else 
          player = "T";
      }
    }
  }

  /**
 * Checks for a vertical win in the column that the letter is placed in
 * @param r - the row that the clicked button is in 
 * @param c - The column that the clicked button is in
 */
  public void checkVertical(int r, int c) {
    if(r < (rows - 3)) {
      for (int i = r; i < rows; i++) {
        String wordCheck = "";
        if ((i + 3) < rows) {
          wordCheck = gameBoard[i][c].getText() + gameBoard[i+1][c].getText() + gameBoard[i+2][c].getText() + gameBoard[i+3][c].getText();
          if (wordCheck == "OTTO" || wordCheck == "TOOT") {
            gameBoard[i][c].setBackground(Color.yellow);
            gameBoard[i+1][c].setBackground(Color.yellow);
            gameBoard[i+2][c].setBackground(Color.yellow);
            gameBoard[i+3][c].setBackground(Color.yellow);
          }
        }
      }
    }
  }
4

1 に答える 1

1

Stringと を比較しないでください==equalsまたはisEmpty代わりに使用します。

if(gameBoard[i][c].getText().isEmpty() && playMade == false)
...
if(player.equals("T"))
...
if(wordCheck.equals("OTTO") || wordCheck.equals("TOOT"))

-operator は、2 つの-references がまったく同じ -object を指している==場合にのみ true を返します。StringString

equalsString2 つのオブジェクトの内容が同じ場合に true を返します。それらは同じオブジェクトである場合とそうでない場合があります。

于 2013-04-14T03:18:33.387 に答える