最近在翻学长留下的资料,发现了一个很奇怪的TIC TAC TOE的实现:
不知道是不是自己比较笨,第二种是现实想了好久好久(⊙﹏⊙)b,但总算还是想出来了:主要是用ArrayList + Comparable的借口实现排序(感觉说起来很简单的样子,不知道为什么搞了两天多)。。。
现在的问题就是:
记录步数有限:由于用了一个全局变量来记录总步数并将步数分配到每个按键上,导致最终的游戏总步数有限(虽然我想2^31-1步没人能玩下来),姑且算一个不足。
希望各位大大给出建议~
代码如下:
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.awt.event.*;
import java.util.*;
class ButtonX extends Button implements Comparable{
int btID = 0;
public int compareTo(ButtonX other){
if(this.btID < other.btID) return -1;
if(this.btID == other.btID) return 0;
return 1;
}
}
public class TicMain extends Frame {
public static final int GAME_SIZE = 450;
public static final int DISAPPEAR_START = 6;
int totalStep = 0;
ButtonX bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9;
boolean player = true;
List btList = new ArrayList();
public void launchFrame() {
this.setLayout(new GridLayout(3, 3, 0, 0));
this.setTitle("TicTacToe! By CasterKKK");
this.setLocation(400, 400);
this.setSize(GAME_SIZE, GAME_SIZE);
this.setBackground(Color.WHITE);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.setResizable(false);
bt1 = new ButtonX();
bt1.btID = Integer.MAX_VALUE;
btList.add(bt1);
add(bt1);
bt2 = new ButtonX();
bt2.btID = Integer.MAX_VALUE;
btList.add(bt2);
add(bt2);
bt3 = new ButtonX();
bt3.btID = Integer.MAX_VALUE;
btList.add(bt3);
add(bt3);
bt4 = new ButtonX();
bt4.btID = Integer.MAX_VALUE;
btList.add(bt4);
add(bt4);
bt5 = new ButtonX();
bt5.btID = Integer.MAX_VALUE;
btList.add(bt5);
add(bt5);
bt6 = new ButtonX();
bt6.btID = Integer.MAX_VALUE;
btList.add(bt6);
add(bt6);
bt7 = new ButtonX();
bt7.btID = Integer.MAX_VALUE;
btList.add(bt7);
add(bt7);
bt8 = new ButtonX();
bt8.btID = Integer.MAX_VALUE;
btList.add(bt8);
add(bt8);
bt9 = new ButtonX();
bt9.btID = Integer.MAX_VALUE;
btList.add(bt9);
add(bt9);
bt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
/**
* Actually it is the first version of the TicTacToe. In this
* version, you can put mostly nine chess pieces at all, and the
* next version is focused on a new chessing process: the
* chessboard can put six chess at all, while put on the seventh
* chess piece, the first one will gone.
*/
// The first version
// if (player && bt1.getLabel() == "") {
// bt1.setLabel("X");
// player = !player;
// } else if (!player && bt1.getLabel() == "") {
// bt1.setLabel("O");
// player = !player;
// }
// The second version
if (totalStep < DISAPPEAR_START && bt1.getLabel() == "") {
if (player) {
bt1.setLabel("X");
player = !player;
totalStep++;
bt1.btID = totalStep;
} else if (!player) {
bt1.setLabel("O");
player = !player;
totalStep++;
bt1.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt1.setLabel("X");
player = !player;
totalStep++;
bt1.btID = totalStep;
} else if (!player) {
bt1.setLabel("O");
player = !player;
totalStep++;
bt1.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// The first version
// if (player && bt2.getLabel() == "") {
// bt2.setLabel("X");
// player = !player;
// } else if (!player && bt2.getLabel() == "") {
// bt2.setLabel("O");
// player = !player;
// }
// The second version
if (totalStep < DISAPPEAR_START && bt2.getLabel() == "") {
if (player) {
bt2.setLabel("X");
player = !player;
totalStep++;
bt2.btID = totalStep;
} else if (!player) {
bt2.setLabel("O");
player = !player;
totalStep++;
bt2.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt2.setLabel("X");
player = !player;
totalStep++;
bt2.btID = totalStep;
} else if (!player) {
bt2.setLabel("O");
player = !player;
totalStep++;
bt2.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First version
// if (player && bt3.getLabel() == "") {
// bt3.setLabel("X");
// player = !player;
// } else if (!player && bt3.getLabel() == "") {
// bt3.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt3.getLabel() == "") {
if (player) {
bt3.setLabel("X");
player = !player;
totalStep++;
bt3.btID = totalStep;
} else if (!player) {
bt3.setLabel("O");
player = !player;
totalStep++;
bt3.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt3.setLabel("X");
player = !player;
totalStep++;
bt3.btID = totalStep;
} else if (!player) {
bt3.setLabel("O");
player = !player;
totalStep++;
bt3.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First version
// if (player && bt4.getLabel() == "") {
// bt4.setLabel("X");
// player = !player;
// } else if (!player && bt4.getLabel() == "") {
// bt4.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt4.getLabel() == "") {
if (player) {
bt4.setLabel("X");
player = !player;
totalStep++;
bt4.btID = totalStep;
} else if (!player) {
bt4.setLabel("O");
player = !player;
totalStep++;
bt4.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt4.setLabel("X");
player = !player;
totalStep++;
bt4.btID = totalStep;
} else if (!player) {
bt4.setLabel("O");
player = !player;
totalStep++;
bt4.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First Version
// if (player && bt5.getLabel() == "") {
// bt5.setLabel("X");
// player = !player;
// } else if (!player && bt5.getLabel() == "") {
// bt5.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt5.getLabel() == "") {
if (player) {
bt5.setLabel("X");
player = !player;
totalStep++;
bt5.btID = totalStep;
} else if (!player) {
bt5.setLabel("O");
player = !player;
totalStep++;
bt5.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt5.setLabel("X");
player = !player;
totalStep++;
bt5.btID = totalStep;
} else if (!player) {
bt5.setLabel("O");
player = !player;
totalStep++;
bt5.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First version
// if (player && bt6.getLabel() == "") {
// bt6.setLabel("X");
// player = !player;
// } else if (!player && bt6.getLabel() == "") {
// bt6.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt6.getLabel() == "") {
if (player) {
bt6.setLabel("X");
player = !player;
totalStep++;
bt6.btID = totalStep;
} else if (!player) {
bt6.setLabel("O");
player = !player;
totalStep++;
bt6.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt6.setLabel("X");
player = !player;
totalStep++;
bt6.btID = totalStep;
} else if (!player) {
bt6.setLabel("O");
player = !player;
totalStep++;
bt6.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First Version
// if (player && bt7.getLabel() == "") {
// bt7.setLabel("X");
// player = !player;
// } else if (!player && bt7.getLabel() == "") {
// bt7.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt7.getLabel() == "") {
if (player) {
bt7.setLabel("X");
player = !player;
totalStep++;
bt7.btID = totalStep;
} else if (!player) {
bt7.setLabel("O");
player = !player;
totalStep++;
bt7.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt7.setLabel("X");
player = !player;
totalStep++;
bt7.btID = totalStep;
} else if (!player) {
bt7.setLabel("O");
player = !player;
totalStep++;
bt7.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Frist version
// if (player && bt8.getLabel() == "") {
// bt8.setLabel("X");
// player = !player;
// } else if (!player && bt8.getLabel() == "") {
// bt8.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt8.getLabel() == "") {
if (player) {
bt8.setLabel("X");
player = !player;
totalStep++;
bt8.btID = totalStep;
} else if (!player) {
bt8.setLabel("O");
player = !player;
totalStep++;
bt8.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt8.setLabel("X");
player = !player;
totalStep++;
bt8.btID = totalStep;
} else if (!player) {
bt8.setLabel("O");
player = !player;
totalStep++;
bt8.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
bt9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// First version
// if (player && bt9.getLabel() == "") {
// bt9.setLabel("X");
// player = !player;
// } else if (!player && bt9.getLabel() == "") {
// bt9.setLabel("O");
// player = !player;
// }
// Second version
if (totalStep < DISAPPEAR_START && bt9.getLabel() == "") {
if (player) {
bt9.setLabel("X");
player = !player;
totalStep++;
bt9.btID = totalStep;
} else if (!player) {
bt9.setLabel("O");
player = !player;
totalStep++;
bt9.btID = totalStep;
}
} else {
Collections.sort(btList);
btList.get(0).setLabel("");
btList.get(0).btID = Integer.MAX_VALUE;
if (player) {
bt9.setLabel("X");
player = !player;
totalStep++;
bt9.btID = totalStep;
} else if (!player) {
bt9.setLabel("O");
player = !player;
totalStep++;
bt9.btID = totalStep;
}
}
if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 1) {
JOptionPane.showMessageDialog(null, "X wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == -1) {
JOptionPane.showMessageDialog(null, "O wins! One more game?");
initGame();
} else if (checkWin(bt1.getLabel(), bt2.getLabel(), bt3.getLabel(), bt4.getLabel(), bt5.getLabel(),
bt6.getLabel(), bt7.getLabel(), bt8.getLabel(), bt9.getLabel()) == 0) {
JOptionPane.showMessageDialog(null, "Equal! One more game?");
initGame();
}
}
});
}
public int checkWin(String bt1, String bt2, String bt3, String bt4, String bt5, String bt6, String bt7, String bt8,
String bt9) {
if (((bt1 == bt2 && bt1 == bt3 && bt1 == "X") || (bt4 == bt5 && bt4 == bt6 && bt4 == "X")
|| (bt7 == bt8 && bt7 == bt9 && bt7 == "X") || (bt1 == bt4 && bt1 == bt7 && bt1 == "X")
|| (bt2 == bt5 && bt5 == bt8 && bt2 == "X") || (bt3 == bt6 && bt6 == bt9 && bt3 == "X")
|| (bt1 == bt5 && bt5 == bt9 && bt1 == "X") || (bt3 == bt5 && bt5 == bt7 && bt3 == "X"))) {
return 1;
} else if (((bt1 == bt2 && bt1 == bt3 && bt1 == "O") || (bt4 == bt5 && bt4 == bt6 && bt4 == "O")
|| (bt7 == bt8 && bt7 == bt9 && bt7 == "O") || (bt1 == bt4 && bt1 == bt7 && bt1 == "O")
|| (bt2 == bt5 && bt5 == bt8 && bt2 == "O") || (bt3 == bt6 && bt6 == bt9 && bt3 == "O")
|| (bt1 == bt5 && bt5 == bt9 && bt1 == "O") || (bt3 == bt5 && bt5 == bt7 && bt3 == "O"))) {
return -1;
} else if (bt1 != "" && bt2 != "" && bt3 != "" && bt4 != "" && bt5 != "" && bt6 != "" && bt7 != "" && bt8 != ""
&& bt9 != "") {
return 0;
}
return 2;
}
public void initGame() {
bt1.setLabel("");
bt2.setLabel("");
bt3.setLabel("");
bt4.setLabel("");
bt5.setLabel("");
bt6.setLabel("");
bt7.setLabel("");
bt8.setLabel("");
bt9.setLabel("");
}
public static void main(String[] args) {
TicMain tm = new TicMain();
tm.launchFrame();
}
PS:那个注释部分是第一种实现,没有把它融入整个游戏中;
PPS:没有注释真是不好意思;
PPPS:INTELLJ真是意外好用,貌似eclipse不能在ActionListener里头设断点,但IntellJ可以,而且Debug时顺手多了。