跟大家共享我写的一个五子棋游戏, 带有很强的禁手识别,可以选择规则
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public final class Renju extends JPanel implements ActionListener {
private static final int GRID_DIMENSION = 30;
private int rule;//to set traditional rule or professinal rule
private int gridNum = 15;
private String s;
private String displayStr;
private int x;//to record grid's X corrdinate
private int y;//to recourd grid's Y coodinate
private int count;// to count steps;
private int[][] grid;
private int colorFlag = 1;
private static JPanel buttonPane;
private static JButton newGame;
private static JButton previous;
private static JButton next;
private static JMenuBar menu;
private Hashtable hashtable;//to store the detail information of every
// step
public Renju() {
newGame = new JButton("重新开始");
newGame.setBounds(500, 60, 80, 40);
previous = new JButton("上一步");
previous.setBounds(500, 100, 80, 40);
next = new JButton("下一步");
next.setBounds(500, 140, 80, 40);
menu = new JMenuBar();
menu.setBounds(0, 0, 400, 25);
JMenu ruleChoice = new JMenu("规则选择");
menu.add(ruleChoice);
JMenuItem traRule = new JMenuItem("无禁规则");
JMenuItem proRule = new JMenuItem("专业规则");
ruleChoice.add(traRule);
ruleChoice.add(proRule);
newGame.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
hashtable = new Hashtable();
grid = new int[gridNum][gridNum];
proRule.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rule = 0;
displayStr = "注意! 你选择了专业规则,黑方走双三,双四,长链,均算输!";
display();
}
});
traRule.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
rule = 1;
displayStr = "注意! 你选择了无禁手的规则!";
display();
}
});
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if ((15 < e.getX() && e.getX() < 461)
&& (15 < e.getY() && e.getY() < 470)) {
x = (e.getX() - GRID_DIMENSION / 2) / GRID_DIMENSION;
y = (15 * GRID_DIMENSION - e.getY() + GRID_DIMENSION / 2)
/ GRID_DIMENSION;
if (grid[x][y] == 0) {
s = x + "," + y;
grid[x][y] = colorFlag;
hashtable.put(Integer.toString(++count), s);
Renju.this.repaint();
if (rule == 1)
traditionalRule();
else
professionalRule();
colorFlag = -colorFlag;
}
}
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == newGame) {
for (int i = 0; i < gridNum; i++)
for (int j = 0; j < gridNum; j++)
grid[i][j] = 0;
colorFlag = 1;
repaint();
} else if (e.getSource() == previous) {
if (count > 0) {
String str = "";
int x = 0;
int y = 0;
str = (String) hashtable.get(Integer.toString(count--));
x = Integer.parseInt(str.substring(0, str.indexOf(",")));
y = Integer.parseInt(str.substring(str.indexOf(",") + 1));
grid[x][y] = 0;
colorFlag = -colorFlag;
repaint();
}
} else if (e.getSource() == next) {
if (count < hashtable.size()) {
String str = "";
int x = 0;
int y = 0;
str = (String) hashtable.get(Integer.toString(++count));
x = Integer.parseInt(str.substring(0, str.indexOf(",")));
y = Integer.parseInt(str.substring(str.indexOf(",") + 1));
grid[x][y] = colorFlag;
colorFlag = -colorFlag;
repaint();
}
}
}
public void paint(Graphics g) {
super.paint(g);
for (int i = 1, num = 15; i <= gridNum; i++, num--) {
g.drawString(Integer.toString(num), 15, i * GRID_DIMENSION + 3);
g.drawLine(30, i * GRID_DIMENSION, gridNum * GRID_DIMENSION, i
* GRID_DIMENSION);
}
char a = 'A';
for (int i = 1; i <= gridNum; i++, a++) {
g.drawLine(i * GRID_DIMENSION, 30, i * GRID_DIMENSION, gridNum
* GRID_DIMENSION);
g.drawString(Character.toString(a), i * GRID_DIMENSION - 3, gridNum
* GRID_DIMENSION + 15);
}
g.fillRect(238, 238, 6, 6);
for (int i = 0; i < gridNum; i++) {
for (int j = 0; j < gridNum; j++) {
if (grid[i][j] == 1) {
g.setColor(Color.BLACK);
g.fillOval((i + 1) * GRID_DIMENSION - 14, (15 - j)
* GRID_DIMENSION - 14, 28, 28);
} else if (grid[i][j] == -1) {
g.setColor(Color.WHITE);
g.fillOval((i + 1) * GRID_DIMENSION - 14, (15 - j)
* GRID_DIMENSION - 14, 28, 28);
}
}
}
}
public void traditionalRule() {
int countNum = 1;//to count the number of the chess,if CountNum== 5,then win
for (int X = x, Y = y; ++X < 15 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
for (int X = x, Y = y; --X > 0 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
countNum = 1;
for (int X = x, Y = y; ++Y < 15 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
for (int X = x, Y = y; --Y > 0 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
countNum = 1;
for (int X = x, Y = y; ++Y < 15 && ++X < 15 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
for (int X = x, Y = y; --Y > 0 && --X > 0 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
countNum = 1;
for (int X = x, Y = y; ++Y < 15 && --X > 0 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
for (int X = x, Y = y; --Y > 0 && ++X < 15 && grid[X][Y] == colorFlag;)
victoryJudge(++countNum, colorFlag);
}
public void victoryJudge(int countNum, int colorFlag) {
if (countNum == 5) {
if (colorFlag == 1)
displayStr = "黑胜 !";
else
displayStr = "白胜 !";
display();
}
}
public void professionalRule() {
if (colorFlag == -1)//if the chess droped is white ,apply the traditinal rule
traditionalRule();
else {
int ctnNumber = hasFive(x, y);
if (ctnNumber == 5) {
displayStr = "黑胜 !";
display();
} else if (ctnNumber > 5) {
displayStr = "白方胜! 黑方长链禁手";
display();
} else if (hasDoubleFour(x, y)) {
displayStr = "白方胜! 黑方双四禁手";
display();
} else if (hasDoubleThree(x, y)) {
displayStr = "白方胜! 黑方双三禁手";
display();
}
}
}
public boolean hasDoubleFour(int x, int y) {
int number = horizontalFour(x, y) + verticalFour(x, y)
+ EN_diagonalFour(x, y) + WN_diagonalFour(x, y);
if (number >= 2 && number != 9)
return true;
return false;
}
public int horizontalFour(int x, int y) {
int blackCount = 0;
int amountOfFour = 0;
boolean ecountWhite = false;
for (int X = x, Y = y; ++X < 15;) {
if (grid[X][Y] == 0) {
if (hasFive(X, Y) == 5)
amountOfFour++;
break;
} else if (grid[X][Y] == -1) {
ecountWhite = true;
break;
} else
blackCount++;
}
for (int X = x, Y = y; --X > 0 && grid[X][Y] != -1;) {
if (grid[X][Y] == 0)
return result(X, Y, blackCount, amountOfFour, ecountWhite);
else
blackCount++;
}
return amountOfFour;
}