tic-tac-toeゲームの勝利状態を評価しようとします。Tic-Tac-Toeの勝利状態を評価
だから私は、forループでボタンのgetText()
メソッドを使用してcells[0][i].getText()
がcells[0][1]
とcells[0][2]
に等しい場合、たとえば、評価しようとしている通常やって終わるものを。
次に、cells[0][2]
が空白でない(" "
と等しくない)場合は、最初の行が終了します。
問題は、これがうまくいかず、誰かが私にこのアプローチのよりよい代替手段を提供してくれれば感謝します。
package tictactoe;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TicTacToe implements ActionListener
{
JFrame window;
static JButton[][]cells=new JButton[3][3];
boolean playing;
char turn='X';
public TicTacToe()
{
//SETS UP THE WINDOW
window=new JFrame("TicTacToe");
window.setSize(new Dimension(400,400));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
//**********************//
createCells();
window.setVisible(true);
}
public static void main(String[] args)
{
new TicTacToe();
}
private void createCells()
{
window.setLayout(new GridLayout(3,3));
for(int i=0;i<cells.length;i++)
{
for(int j=0;j<cells.length;j++)
{
cells[i][j]=new JButton();
cells[i][j].addActionListener(this);
window.add(cells[i][j]);
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
playing=true;
JButton _buttonPressed=(JButton)e.getSource();
while(playing)
{
if(turn=='X')
{
_buttonPressed.setText("X");
_buttonPressed.setEnabled(false);
turn='O';
}
else
{
_buttonPressed.setText("O");
_buttonPressed.setEnabled(false);
}
}
}
}
を、あなたは、デバッガを使用してプログラムもののステップしようとしたことがありますか? –