跟大家共享我写的一个五子棋游戏, 带有很强的禁手识别,可以选择规则

virgin_killer 2004-12-29 10:29:17
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;
}
...全文
357 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
virgin_killer 2005-01-04
  • 打赏
  • 举报
回复
学了五个月了啊,也不短了
感觉知道的太少了!,没有机会实践啊
yztlg 2005-01-04
  • 打赏
  • 举报
回复
运行不过的人恐怕是类的名字和楼主的没统一吧~~他里面有几处地方都需要统一类名的,要不你就把自己的类直接起名为Renju,这样应该就可以了~
yztlg 2005-01-04
  • 打赏
  • 举报
回复
老大,强了点吧,你学JAVA多久了?崇拜中~~~~下次把AI加上哦,
virgin_killer 2005-01-04
  • 打赏
  • 举报
回复
怎么沉了?
yztlg 2005-01-04
  • 打赏
  • 举报
回复
等待你将游戏做的更完美~~~
lanbinger336 2005-01-04
  • 打赏
  • 举报
回复
做的不错,如能再改进一下,开发成一个分布式样五子棋applet,使两个用户可以在网络不同的机器上玩游戏 会更好些 .我编过三子棋的applet.
virgin_killer 2005-01-04
  • 打赏
  • 举报
回复
回复人: yztlg(文斌) ( ) 信誉:100 2005-01-04 13:28:00 得分: 0


人工AI还要去专门找些书看看吧,听说很难~~~CSDN上怎么发信息啊,我初来还不太了解,楼主讲一下~


================================================
到小类里面,比如你进的这个小类就是"Java J2SE / 基础类 ",然后点击屏幕右上方的"提问"就可以发表了
yztlg 2005-01-04
  • 打赏
  • 举报
回复
任何语言都可以调用directX包吧,还有openGl,要不你怎么把你的游戏变成3D的啊?
fitLion 2005-01-04
  • 打赏
  • 举报
回复
mark
有空再来研究研究
yztlg 2005-01-04
  • 打赏
  • 举报
回复
人工AI还要去专门找些书看看吧,听说很难~~~CSDN上怎么发信息啊,我初来还不太了解,楼主讲一下~
华生豆 2005-01-04
  • 打赏
  • 举报
回复
真的挺不错的,界面方面可以进一步美化~~~
virgin_killer 2005-01-04
  • 打赏
  • 举报
回复
公司里不能上QQ哎,有什么要交流的,可以在CSDN上发短信
virgin_killer 2005-01-04
  • 打赏
  • 举报
回复
没调用directX中的包,全都是JAVA API
java 能调用这个么?
yztlg 2005-01-04
  • 打赏
  • 举报
回复
我QQ54421939,有机会可以交流一下吧,我才学1个月,希望楼主可以在一些技术上帮帮我~
yztlg 2005-01-04
  • 打赏
  • 举报
回复
请问楼主学JSP了没?你在编5子棋的代码中要调用directX包吗?我也是个初学者,呵呵,不过很希望能编出一个自己的游戏。不过我的朋友说JAVA是不适合编桌面游戏的~~你应该学嵌入类游戏的开发才有前途呢~
virgin_killer 2004-12-30
  • 打赏
  • 举报
回复
bigc2000(勇者无惧) ( ) 信誉:99
---------------------------
谢谢,因为涉及到禁手的判别,所以代码多了许多啊,等有空时间我再优化一下,顺便在里面加上些人工智能,这样就能跟电脑火拼了
射天狼 2004-12-29
  • 打赏
  • 举报
回复
UP!
ccwg 2004-12-29
  • 打赏
  • 举报
回复
up
drugon 2004-12-29
  • 打赏
  • 举报
回复
努力。
lawyu 2004-12-29
  • 打赏
  • 举报
回复
运行过了,不错
继续加油
加载更多回复(13)

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧