我在Java中创建了一个小TicTacToe游戏,我想编写更有效的代码,我会创建一个for循环来创建9个按钮。
我遇到的问题是弄清楚如何测试现在按哪个按钮以确定胜利者。我已经注释掉了我的旧测试代码,因为它不再有效。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe extends JFrame implements ActionListener, WindowListener{
public JFrame window = new JFrame("window");
public String letter = "";
public String lastLetter = "";
public int count = 0;
public boolean win;
public boolean isTieGame;
public static int x;
public TicTacToe()
{
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
int x;
JButton[] b = new JButton[9];
for(x = 0; x < 9; x++)
{
b[x] = new JButton();
window.add(b[x]);
b[x].addActionListener(this);
}
window.setVisible(true);
}
public static void main(String[] args) {
new TicTacToe();
}
public void counter()
{
if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9)
{
letter = "X";
}
else
{
letter = "O";
}
}
/*
public boolean isGameOver()
{
if(b1.getText().equals(b2.getText()) && b2.getText().equals(b3.getText()) && b1.getText() != "")
{
win = true;
}
else if(b4.getText().equals(b5.getText()) && b5.getText().equals(b6.getText()) && b4.getText() != "")
{
win = true;
}
else if(b7.getText().equals(b8.getText()) && b8.getText().equals(b9.getText()) && b7.getText() != "")
{
win = true;
}
else if(b1.getText().equals(b5.getText()) && b5.getText().equals(b9.getText()) && b1.getText() != "")
{
win = true;
}
else if(b3.getText().equals(b5.getText()) && b5.getText().equals(b7.getText()) && b3.getText() != "")
{
win = true;
}
else if(b1.getText().equals(b4.getText()) && b4.getText().equals(b7.getText()) && b1.getText() != "")
{
win = true;
}
else if(b2.getText().equals(b5.getText()) && b5.getText().equals(b8.getText()) && b2.getText() != "")
{
win = true;
}
else if(b3.getText().equals(b6.getText()) && b6.getText().equals(b9.getText()) && b3.getText() != "" )
{
win = true;
}
else
{
win = false;
}
return win;
}
*/
public void endOrReset()
{
if(win)
{
JOptionPane.showMessageDialog(null, lastLetter() + " WINS!");
int playAgain = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Play Again.", JOptionPane.YES_NO_OPTION);
if(playAgain == (JOptionPane.YES_OPTION))
{
win = false;
}
else if(playAgain == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null, "Goodbye");
System.exit(0);
}
}
else if(isTieGame)
{
JOptionPane.showMessageDialog(null, "Tie Game!");
int playAgain = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Play Again.", JOptionPane.YES_NO_OPTION);
if(playAgain == JOptionPane.YES_OPTION)
{
}
else if(playAgain == JOptionPane.NO_OPTION)
{
JOptionPane.showMessageDialog(null, "Goodbye");
System.exit(0);
}
}
}
public void actionPerformed(ActionEvent a) {
count++;
counter();
//isGameOver();
endOrReset();
((JButton)a.getSource()).setText(letter);
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
public String lastLetter()
{
String lastLetter;
if(letter == "O")
{
lastLetter = "X";
}
else
{
lastLetter = "O";
}
return lastLetter;
}
public boolean isTieGame()
{
if(count >= 9 && win == false)
{
isTieGame = true;
}
return isTieGame();
}
}
答案 0 :(得分:0)
我认为最简单的解决方案是将JButton[] b
设为字段,因此将其声明在构造函数上方。
在这种情况下,您还应该使用更具描述性的名称。
或者你可以拿着一个char数组,其中一个按钮记录当前的玩家符号(X或O),这就是我前一段时间解决这个问题的方法
答案 1 :(得分:0)
我喜欢做的是拥有一个独立于GUI的9长度数组来保持游戏状态,如下所示:
0 1 2
3 4 5
6 7 8
如果您考虑指数,
3*(index/3) + index % 3
这通常是一个枚举数组,但你也可以使用String数组,
public enum State
{
EMPTY,
X,
O;
}
然后
State[] gridState = new State[9];
for(int i = 0; i < gridState.length; i++) {
gridState = State.EMPTY;
}
如果您根据指标根据GUI初始化按钮,
public class MyButton extends JButton {
private int index;
public MyButton(int index) {
super();
this.index = index;
}
public int getIndex() {
return index;
}
}
然后当有人点击你的按钮时,你可以说 ((myButton的)a.getSource())。getIndex()
您将获得这个按钮的索引。
如果tic tac toe被定义为3x3和2D,我喜欢根据索引来检查胜利,我将存储一组数组,这些数组包含胜利的水平,垂直和对角线索引。
int[][] winIndices = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
然后你可以根据
检查胜利public boolean checkWin() {
for(int[] indexArray : winIndices) {
boolean same = (gridState[indexArray[0]] == gridState[indexArray[1]]) &&
(gridState[indexArray[1]] == gridState[indexArray[2]])
if(same == true && gridState[indexArray[0]] != State.EMPTY) {
return true;
}
}
return false;
}
或类似的东西。我曾经把它写得更好了,但这不是在这台电脑上,但它是类似的东西:)