评估井字游戏的获胜状态
Evaluate the winning states of 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);
}
}
}
}
我没有看到您在哪里评估获胜状态,但请尝试从点击侦听器中删除无限循环。
@Override
public void actionPerformed(ActionEvent e) {
JButton _buttonPressed = (JButton) e.getSource();
_buttonPressed.setText("" + turn);
_buttonPressed.setEnabled(false);
if (turn == 'X') {
turn = 'O';
} else {
turn = 'X';
}
}
这些主要是建议,如果您愿意,请随时忽略它。
使 TicTacToe class 成为 JFrame
本身 - 无需实例化。
public class TicTacToe extends JFrame implements ActionListener {
private JButton[][] cells = new JButton[3][3];
private char turn = 'X';
private boolean playing;
public TicTacToe() {
setTitle("TicTacToe");
setSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
createCells();
pack();
}
其次,启动 Swing 应用程序的推荐方式是像这样在自己的线程上启动
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TicTacToe ttt = new TicTacToe();
ttt.setVisible(true);
}
});
}
寻找井字游戏赢家的好方法:
您只需要检查用户的最后一步是否导致棋盘进入获胜状态。您还知道用户在位置 board[x][y] 上放置了 X 或 O。
它将看起来像这样:
public bool gameOver(cells, x, y)
if cells[0,y] == cells[1,y] == cells[2,y]
//win on vertical line
if cells[x,0] == cells[x,1] == cells[x,2]
//win on horizontal line
if x == y and cells[0,0] == cells[1,1] == cells[2,2]
//win on diagonal
if x == y and cells[0,2] == cells[1,1] == cells[2,0]
//win on other diagonal
return false
我想尝试评估下井字游戏中的获胜状态。
所以我最终通常做的是尝试在 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);
}
}
}
}
我没有看到您在哪里评估获胜状态,但请尝试从点击侦听器中删除无限循环。
@Override
public void actionPerformed(ActionEvent e) {
JButton _buttonPressed = (JButton) e.getSource();
_buttonPressed.setText("" + turn);
_buttonPressed.setEnabled(false);
if (turn == 'X') {
turn = 'O';
} else {
turn = 'X';
}
}
这些主要是建议,如果您愿意,请随时忽略它。
使 TicTacToe class 成为 JFrame
本身 - 无需实例化。
public class TicTacToe extends JFrame implements ActionListener {
private JButton[][] cells = new JButton[3][3];
private char turn = 'X';
private boolean playing;
public TicTacToe() {
setTitle("TicTacToe");
setSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
createCells();
pack();
}
其次,启动 Swing 应用程序的推荐方式是像这样在自己的线程上启动
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TicTacToe ttt = new TicTacToe();
ttt.setVisible(true);
}
});
}
寻找井字游戏赢家的好方法:
您只需要检查用户的最后一步是否导致棋盘进入获胜状态。您还知道用户在位置 board[x][y] 上放置了 X 或 O。
它将看起来像这样:
public bool gameOver(cells, x, y)
if cells[0,y] == cells[1,y] == cells[2,y]
//win on vertical line
if cells[x,0] == cells[x,1] == cells[x,2]
//win on horizontal line
if x == y and cells[0,0] == cells[1,1] == cells[2,2]
//win on diagonal
if x == y and cells[0,2] == cells[1,1] == cells[2,0]
//win on other diagonal
return false