我的第一个java双人五子棋
说明:
这是大学二年级学java时写的窗体程序,代码中的算法实现了判断五颗棋子是否在一条连续的直线上,
因为完全是凭自己的计算和思考去写的五子棋,所以写的可能有点难看懂。。不过经过测试没有bug,可以用。
ok,贴上主要的代码
<span style="font-size:14px;">/**
* *************************双人五子棋***************************
*/
public class Chessboard extends JFrame {
public static int n = 0;
public static int hangcount = 0;//横向计数
public static int shucount = 0;//竖向计数
public static int xiecount_up = 0;//斜向计数
public static int xiecount_down = 0;//斜向计数
public static int sign[][] = new int[8][8];
ImageIcon hei = new ImageIcon("image/hei.jpg");//图片记得自己贴上去
ImageIcon bai = new ImageIcon("image/bai.jpg");
JButton button[][] = new JButton[8][8];
int a;
int b;
public Chessboard() {
setLayout(new GridLayout(8, 8, 0, 0));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
button[i][j] = new JButton();
if (i % 2 == 0) {
if (j % 2 == 0) {
button[i][j].setBackground(Color.WHITE);
} else {
button[i][j].setBackground(Color.BLACK);
}
} else {
if (j % 2 == 0) {
button[i][j].setBackground(Color.BLACK);
} else {
button[i][j].setBackground(Color.WHITE);
}
}
add(button[i][j]);
}
}
for (a = 0; a < 8; a++) {
for (b = 0; b < 8; b++) {
button[a][b].addActionListener(new MyButtonActionListener(a, b));
}
}
}
public class MyButtonActionListener implements ActionListener {
private int a;
private int b;
public MyButtonActionListener(int a, int b) {
this.a = a;
this.b = b;
}
public void actionPerformed(ActionEvent ae) {
if (sign[a][b] == 0) {
n++;
if (n % 2 == 1) {
button[a][b].setIcon(hei);
sign[a][b] = 2;
} else {
button[a][b].setIcon(bai);
sign[a][b] = 1;
}
judge(a, b);//判断棋局
}
}
/**
* **************************判断算法**************************
*/
private void judge(int a, int b) {
/**
* ******************横向判断******************
*/
for (int m = 0; m < 7; m++) {
if (sign[a][m] == sign[a][m + 1] && (sign[a][m] != 0)) {
hangcount++;
} else {
if (hangcount < 4) {
hangcount = 0;//出现少于5个棋子的情况时hangcount清零,防止重复计算
}
}
}
/**
* *******************竖向判断******************
*/
for (int n = 0; n < 7; n++) {
if (sign[n][b] == sign[n + 1][b] && (sign[n][b] != 0)) {
shucount++;
} else {
if (shucount < 4) {
shucount = 0;
}
}
}
/**
* *******************斜向判断(分为四种情况)*******************
*/
if (a >= b) {
for (int temp1 = a - b, t = 0; temp1 < 7; temp1++, t++) {
if (sign[temp1][t] == sign[temp1 + 1][t + 1] && (sign[temp1][t] != 0)) {
xiecount_up++;
} else {
if (xiecount_up < 4) {
xiecount_up = 0;//出现少于5个棋子的情况时hangcount清零,防止重复计算
}
}
}
if (a + b <= 7) {
for (int temp1 = a + b, t = 0; temp1 > 0; temp1--, t++) {
if (sign[temp1][t] == sign[temp1 - 1][t + 1] && (sign[temp1][t] != 0)) {
xiecount_down++;
} else {
if (xiecount_down < 4) {
xiecount_down = 0;
}
}
}
} else {
for (int temp1 = a + b - 7, t = 7; temp1 < 7; temp1++, t--) {
if (sign[t][temp1] == sign[t - 1][temp1 + 1] && (sign[t][temp1] != 0)) {
xiecount_down++;
} else {
if (xiecount_down < 4) {
xiecount_down = 0;
}
}
}
}
} else {
for (int temp1 = b - a, t = 0; temp1 < 7; temp1++, t++) {
if (sign[t][temp1] == sign[t + 1][temp1 + 1] && (sign[t][temp1] != 0)) {
xiecount_up++;
} else {
if (xiecount_up < 4) {
xiecount_up = 0;
}
}
}
if (a + b <= 7) {
for (int temp1 = a + b, t = 0; temp1 > 0; temp1--, t++) {
if (sign[t][temp1] == sign[t + 1][temp1 - 1] && (sign[t][temp1] != 0)) {
xiecount_down++;
} else {
if (xiecount_down < 4) {
xiecount_down = 0;
}
}
}
} else {
for (int temp1 = a + b - 7, t = 7; temp1 < 7; temp1++, t--) {
if (sign[temp1][t] == sign[temp1 + 1][t - 1] && (sign[temp1][t] != 0)) {
xiecount_down++;
} else {
if (xiecount_down < 4) {
xiecount_down = 0;
}
}
}
}
}
if (hangcount >= 4 || shucount >= 4 || xiecount_up >= 4 || xiecount_down >= 4) {
if (sign[a][b] == 2) {
JOptionPane.showMessageDialog(null, "黑方胜利");
} else {
JOptionPane.showMessageDialog(null, "白方胜利");
}
System.exit(0);
}
hangcount = 0;//全部清零
shucount = 0;
xiecount_up = 0;
xiecount_down = 0;
}
}
public static void main(String[] args) {
Chessboard chessboard = new Chessboard();
chessboard.setTitle("五子棋");
chessboard.setSize(410, 410);
chessboard.setLocationRelativeTo(null);
chessboard.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chessboard.setResizable(false);
chessboard.setVisible(true);
}
}</span>