java五子棋人机对战程序为什么不能运行

Posts - 39,
Articles - 0,
Comments - 0
关注Web技术,Web学习笔记,记录点滴
23:08 by lefan, ... 阅读,
&&&&& 断断续续看了马士兵的JavaSE基础教程,现在还有容器、IO和多线程没有看完。学习给我感触最深的是内存分析,知识点可以是先有个了解,需要用时再去翻书,看看具体怎么样,当然如果特别熟是更好的。
&&&&&& 通过对程序进行内存分析,理解了程序运行过程,在JAVA中,有六个不同的地方可以存储数据[1]:
1. 寄存器(register)。这是最快的存储区,因为它位于不同于其他存储区的地方&&处理器内部。但是寄存器的数量极其有限,所以寄存器由编译器根据需求进行分配。你不能直接控制,也不能在程序中感觉到寄存器存在的任何迹象。
2. 堆栈(stack)。位于通用RAM中,但通过它的&堆栈指针&可以从处理器哪里获得支持。堆栈指针若向下移动,则分配新的内存;若向上移动,则释放那些内存。这是一种快速有效的分配存储方法,仅次于寄存器。创建程序时候,JAVA编译器必须知道存储在堆栈内所有数据的确切大小和生命周期,因为它必须生成相应的代码,以便上下移动堆栈指针。这一约束限制了程序的灵活性,所以虽然某些JAVA数据存储在堆栈中&&特别是对象引用,但是JAVA对象不存储其中。
3. 堆(heap)。一种通用性的内存池(也存在于RAM中),用于存放所以的JAVA对象。堆不同于堆栈的好处是:编译器不需要知道要从堆里分配多少存储区域,也不必知道存储的数据在堆里存活多长时间。因此,在堆里分配存储有很大的灵活性。当你需要创建一个对象的时候,只需要new写一行简单的代码,当执行这行代码时,会自动在堆里进行存储分配。当然,为这种灵活性必须要付出相应的代码。用堆进行存储分配比用堆栈进行存储存储需要更多的时间。
4. 静态存储(static storage)。这里的&静态&是指&在固定的位置&。静态存储里存放程序运行时一直存在的数据。你可用关键字static来标识一个对象的特定元素是静态的,但JAVA对象本身从来不会存放在静态存储空间里。
5. 常量存储(constant storage)。常量值通常直接存放在程序代码内部,这样做是安全的,因为它们永远不会被改变。有时,在嵌入式系统中,常量本身会和其他部分分割离开,所以在这种情况下,可以选择将其放在ROM中.
6. 非RAM存储。如果数据完全存活于程序之外,那么它可以不受程序的任何控制,在程序没有运行时也可以存在。
程序的执行过程[2]:
&&&&& 在学了一部分常用类后,想看看程序运行结果,根据网上的五子棋源代码,阅读了两个java五子棋的程序,并且生成了游戏,成功对弈一盘。我看到的两种算法:1.利用分数代表每个位置的重要程度,越重要的位置的分数值会越高,当电脑下棋时会先将电脑和玩家棋型的分数分别计算一遍,然后选择二者中分数最高的点下子。如果玩家的分数高,那么代表电脑应该防守,如果电脑的分数高,那么代表电脑应该进攻。2.计算棋盘上某一方格上左、右、上、下、左上、左下、右上、右下八个方向棋子的最大值,判断该坐标位置是否可下棋子。计算机走棋时,用穷举法判断每一个坐标点的四个方向的的最大棋子数,最后得出棋子数最大值的坐标,然后下子。
& &&使用第二个方案的源代码生成的游戏,完了两局都,设计的还是不好,程序只是给了简单的判断准则,相对智能点还是实现不了,以后会留心着更智能的设计。
[1] 《Thinking in Java》中文第四版。
[2] &马士兵《J2SE课件》。java实现五子棋小游戏
投稿:hebedich
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了java实现五子棋小游戏的相关资料,十分简单实用,推荐给大家,需要的朋友可以参考下
java实现五子棋小游戏
import java.awt.T
import javax.swing.JF
public class GomokuFrame extends JFrame
//定义一个操作面板
OperatorPane op=
public GomokuFrame()
//设置名称
this.setTitle("五子棋");
//设置窗口大小
this.setSize(510,510);
//设置窗口位置
//取得电脑屏幕大小
int computer_width=Toolkit.getDefaultToolkit().getScreenSize().
int computer_height=Toolkit.getDefaultToolkit().getScreenSize().
System.out.println("电脑屏幕的宽度:\n"+computer_width+"\n电脑屏幕的高度:\n"+computer_height);
this.setLocation((computer_width-510)/2, (computer_height-510)/2);
//实例化幕布
op=new OperatorPane();
//导入幕布
this.add(op);
//添加鼠标监听
this.addMouseListener(op);
//设置窗口的显示
this.setVisible(true);
//设置窗口的正常关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//执行测试
public static void main(String[] args)
new GomokuFrame();
import java.awt.C
import java.awt.F
import java.awt.G
import java.awt.I
import java.awt.event.MouseE
import java.awt.event.MouseL
import java.awt.image.BufferedI
import java.awt.image.BufferedImageF
import javax.swing.ImageI
import javax.swing.JOptionP
import javax.swing.JP
public class OperatorPane extends JPanel implements MouseListener,Runnable
//定义背景图片对象
Image imageBackground =
//定义棋盘的格子的行数
int boardrows=18;
//定义棋盘的格子的列数
int boardcols=18;
//定义棋盘的格子的大小
int boardsize=20;
//保存棋子坐标
int x=0,y=0;
//保存之前下过的全部棋子坐标
// 其中数据内容 0: 表示这个点并没有棋子, 1: 表示这个点是黑子, 2:表示这个点是白子
int allchess[][]=new int [19][19];
//标记下一步下黑棋还是白棋
boolean isBlack=
//判断游戏是否能够继续
boolean canPlay=
//保存显示信息
String message="黑方先行";
// 保存最多拥有多少时间(秒)
int maxTime = 0;
// 做倒计时的线程类
Thread t = new Thread(this);
// 保存黑方与白方的剩余时间
int blackTime = 0;
int whiteTime = 0;
// 保存双方剩余时间的显示信息
String blackMessage = "无限制";
String whiteMessage = "无限制";
@SuppressWarnings("deprecation")
public OperatorPane()
t.start();
t.suspend();
imageBackground=new ImageIcon("image/background.jpg").getImage();
public void paint(Graphics g)
//双缓冲技术
BufferedImage b1=new BufferedImage(495,475,BufferedImage.TYPE_INT_ARGB);
Graphics g2=b1.createGraphics();
// 画出背景图片
g2.drawImage(imageBackground, 0, 0,495,475,null);
//画出棋盘线
Color c=g2.getColor();
g2.setColor(Color.BLACK);
for(int i=0;i&=i++)
g2.drawLine(10,50+boardsize*i,10+boardsize*boardrows,50+boardsize*i);
for(int i=0;i&=i++)
g2.drawLine(10+boardsize*i,50,10+boardsize*i,50+boardsize*boardcols);
//画出三三位置
g2.fillOval(67, 107, 6, 6);
g2.fillOval(67, 347, 6, 6);
g2.fillOval(307, 107, 6, 6);
g2.fillOval(307, 347, 6, 6);
//画出附点
g2.fillOval(67, 227, 6, 6);
g2.fillOval(307, 227, 6, 6);
g2.fillOval(187, 107, 6, 6);
g2.fillOval(187, 347, 6, 6);
//画出天元
g2.fillOval(187, 227, 6, 6);
//画出文字提示
/*Font f=new Font("黑体", Font.BOLD, 24);
g.setFont(f);*/
g2.setFont(new Font("黑体", Font.BOLD, 20));
g2.setColor(Color.BLACK);
g2.drawString("游戏信息:"+message, 130, 40);
g2.setFont(new Font("宋体", Font.ITALIC, 15));
g2.drawString("黑方时间:"+blackMessage,25, 445);
g2.drawString("白方时间:"+whiteMessage,245, 445);
//绘制全部棋子
for(int i=0;i&=i++)
for(int j=0;j&=j++)
//存储黑棋
if(allchess[i][j]==1)
int tempX=i*20-10;
int tempY=j*20+30;
//画出黑棋
g2.setColor(Color.BLACK);
g2.fillOval(tempX+12, tempY+13, 15, 15);
//存储白棋
if(allchess[i][j]==2)
int tempX=i*20-10;
int tempY=j*20+30;
//画出白棋
g2.setColor(Color.BLACK);
g2.drawOval(tempX+12, tempY+13, 15, 15);
g2.setColor(Color.WHITE);
g2.fillOval(tempX+12, tempY+13, 15, 15);
g2.setColor(c);
g.drawImage(b1,0,0,this);
private boolean checkWin()
boolean flag=
int color = allchess[x][y];
/*// 保存共有相同颜色多少棋子相连
int count1=1;
int count2=1;
int count3=1;
int count4=1;
// 判断横向是否有5个棋子相连,特点 纵坐标 是相同, 即allChess[x][y]中y值是相同
// 通过循环来做棋子相连的判断
int i = 1;
while (color == allchess[x+i][y])
//将i值复位
while (color == allchess[x-i][y])
if(count1 &= 5)
//判断纵向 ,即allChess[x][y]中x值是相同
int j = 1;
while (color == allchess[x][y+j])
//将j值复位
while (color == allchess[x][y-j])
if(count2&= 5)
//判断斜向"\"
while (color == allchess[x+m1][y+n1])
while (color == allchess[x-m1][y-n1])
if(count3&= 5)
//判断斜向"/"
while (color == allchess[x+m2][y-n2])
while (color == allchess[x-m2][y+n2])
if(count4&= 5)
//横向判断
count=this.checkCount(1, 0, color);
if(count&=5)
//纵向判断
count=this.checkCount(0, 1, color);
if(count&=5)
//斜向“/”
count=this.checkCount(1, 1, color);
if(count&=5)
//斜向“\”
count=this.checkCount(1, -1, color);
if(count&=5)
private int checkCount(int xChange,int yChange,int color)
int count=1;
int tempX=xC
int tempY=yC
while (color==allchess[x+xChange][y+yChange])
if(xChange!=0)
xChange++;
if(yChange!=0)
if(yChange&0)
yChange--;
yChange++;
xChange=tempX;
yChange=tempY;
while (color==allchess[x-xChange][y-yChange])
if(xChange!=0)
xChange++;
if(yChange!=0)
if(yChange&0)
yChange--;
yChange++;
public void mouseClicked(MouseEvent e)
System.out.println("x:"+e.getX()+"y:"+e.getY());
x=e.getX();
y=e.getY();
if(x&=10&&x&=(10+boardsize*boardrows+20)&&y&=50&&y&=(50+boardsize*boardcols+40))
//System.out.println("点在棋盘内。");
x=(x-10)/20;
y=(y-50-20)/20;
if(canPlay==true)
//判断当前要下什么颜色
if(allchess[x][y]==0)
if(isBlack==true)
allchess[x][y]=1;
message="轮到白方";
allchess[x][y]=2;
message="轮到黑方";
// 判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束
boolean winFlag=this.checkWin();
if(winFlag==true)
JOptionPane.showMessageDialog(this,"游戏结束!"+
(allchess[x][y]==1?"黑方":"白方")+"获胜。");
JOptionPane.showMessageDialog(this,"当前位置已经有棋子,请重新落子!");
this.repaint();
//点击,游戏开始按钮
//重新开始新的游戏
if(e.getX()&=400&&e.getX()&=470&&e.getY()&=80&&e.getY()&=110)
int result=JOptionPane.showConfirmDialog(this, "设置完成,是否重新开始游戏?");
if(result==0)
//重新开始的操作,allchess[][]数组中的信息全部为0
//清空棋盘
for (int i = 0; i & 19; i++)
for (int j = 0; j & 19; j++)
allchess[i][j] = 0;
//另一种方式 allChess = new int[19][19];
blackTime = maxT
whiteTime = maxT
if (maxTime & 0)
blackMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
t.resume();
blackMessage = "无限制";
whiteMessage = "无限制";
message = "黑方先行";
this.canPlay =
this.repaint();
//点击 游戏设置按钮
if(e.getX()&=400&&e.getX()&=470&&e.getY()&=130&&e.getY()&=160)
String input = JOptionPane
.showInputDialog("请输入游戏的最大时间(单位:分钟),如果输入0,表示没有时间限制:");
maxTime = Integer.parseInt(input) * 60;
if (maxTime & 0)
JOptionPane.showMessageDialog(this, "请输入正确信息,不允许输入负数!");
if (maxTime == 0)
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0)
for (int i = 0; i & 19; i++)
for (int j = 0; j & 19; j++)
allchess[i][j] = 0;
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
blackTime = maxT
whiteTime = maxT
blackMessage = "无限制";
whiteMessage = "无限制";
this.canPlay =
this.repaint();
if (maxTime & 0)
int result = JOptionPane.showConfirmDialog(this,
"设置完成,是否重新开始游戏?");
if (result == 0)
for (int i = 0; i & 19; i++)
for (int j = 0; j & 19; j++)
allchess[i][j] = 0;
// 另一种方式 allChess = new int[19][19];
message = "黑方先行";
blackTime = maxT
whiteTime = maxT
blackMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
whiteMessage = maxTime / 3600 + ":"
+ (maxTime / 60 - maxTime / 3600 * 60) + ":"
+ (maxTime - maxTime / 60 * 60);
t.resume();
this.canPlay =
this.repaint();
catch (NumberFormatException e1)
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(this, "请正确输入信息!");
//点击 游戏说明按钮
if(e.getX()&=400&&e.getX()&=470&&e.getY()&=180&&e.getY()&=210)
JOptionPane.showMessageDialog(this,"这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。");
//点击 认输按钮
if(e.getX()&=400&&e.getX()&=470&&e.getY()&=280&&e.getY()&=310)
int result=JOptionPane.showConfirmDialog(this,"是否确定认输?");
if (result == 0)
if (isBlack)
JOptionPane.showMessageDialog(this, "黑方已经认输,游戏结束!");
JOptionPane.showMessageDialog(this, "白方已经认输,游戏结束!");
//点击 关于按钮
if(e.getX()&=400&&e.getX()&=470&&e.getY()&=330&&e.getY()&=360)
JOptionPane.showMessageDialog(this,"本游戏由南木工作室制作,有相关问题可以访问");
//点击 退出按钮
if(e.getX()&=400&&e.getX()&=470&&e.getY()&=380&&e.getY()&=410)
JOptionPane.showMessageDialog(this, "游戏结束");
System.exit(0);
//************************//
public void mouseEntered(MouseEvent arg0)
// TODO Auto-generated method stub
public void mouseExited(MouseEvent arg0)
// TODO Auto-generated method stub
public void mousePressed(MouseEvent arg0)
// TODO Auto-generated method stub
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
public void run()
if (maxTime & 0)
while (true)
if (isBlack)
blackTime--;
if (blackTime == 0)
JOptionPane.showMessageDialog(this, "黑方超时,游戏结束!");
whiteTime--;
if (whiteTime == 0)
JOptionPane.showMessageDialog(this, "白方超时,游戏结束!");
blackMessage = blackTime / 3600 + ":"
+ (blackTime / 60 - blackTime / 3600 * 60) + ":"
+ (blackTime - blackTime / 60 * 60);
whiteMessage = whiteTime / 3600 + ":"
+ (whiteTime / 60 - whiteTime / 3600 * 60) + ":"
+ (whiteTime - whiteTime / 60 * 60);
this.repaint();
Thread.sleep(1000);
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(blackTime + " -- " + whiteTime);
以上所述就是本文的全部内容了,希望能够对大家熟练掌握java有所帮助。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具急! 求大神帮忙!JAVA
五子棋小游戏的程序排错【java吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:645,049贴子:
急! 求大神帮忙!JAVA
五子棋小游戏的程序排错收藏
JAVA一个课程设计
我做的是五子棋的小游戏程序
不会排错 求大神帮忙
网上找的代码
2017java学习来上市公司博为峰学java,入学即签就业协议,不就业不收费,查看java课程!java好学吗?java课程来博为峰学,java工程师就业年薪十几万!
谢谢大家帮忙了 /**
* &p&Description: 播放声音&/p&
index 播放音频文件序号(0-2)
public void playSound(int index){
!!!这有错!!!
switch(index){
case 1:{ //落子声
stream=this.getClass().getResourceAsStream("sound/waveUp.wav");
case 2:{ //游戏结束声
stream=this.getClass().getResourceAsStream("sound/waveEnd.wav");
case 3:{ //游戏结束声
stream=this.getClass().getResourceAsStream("sound/waveDrop.wav");
default:{ //落子声
stream=this.getClass().getResourceAsStream("sound/waveUp.wav");
audio=new AudioStream(stream);
!!!这有错!!!
AudioPlayer.player.start(audio);
!!!这有错!!!
catch(Exception e1){
e1.printStackTrace();
JOptionPane.showMessageDialog(null, e1.toString(), "播放音频文件产生异常", JOptionPane.ERROR_MESSAGE);
//鼠标点击事件
public void this_mousePressed(MouseEvent e) {
int tempX=e.getX();
int tempY=e.getY();
//设置边界
if(tempX&15){
if(tempY&15){
if(tempX&15+(size-1)*40){
tempX=15+(size-1)*40;
if(tempY&15+(size-1)*40){
tempY=15+(size-1)*40;
if(e.getButton()==e.BUTTON1){ //鼠标左键点击
if(Frame1.turn==0&&avail&&board[(int)(tempX+5)/40][(int)(tempY+5)/40]==0){ //轮到用户下子
for(int i=0;i&5;i++){
if(forbidAvail[i]){
if(forbidX[i]==(int)(tempX+5)/40&&forbidY[i]==(int)(tempY+5)/40){
forbidStyle=
if(forbidStyle){ //检测到禁手
Frame1.forbidStyle++;
forbidStyle=
playSound(3);
JOptionPane.showMessageDialog(null, "&html&&b&该点为&font color=blue&禁手&/font&位"
+",&font color=blue&游戏规则&/font&可以查看&font color=purple&帮助&/font&", "落子无效", RMATION_MESSAGE);
userX=(int)(tempX+5)/40;
userY=(int)(tempY+5)/40;
playSound(1);
Frame1.turn=1;
else if(!avail){ //未选择开始
playSound(3);
JOptionPane.showMessageDialog(null, "&html&&b&请在菜单中单击&font color=purple&开始&/font&进行游戏", "开始游戏", RMATION_MESSAGE);
else if(board[(int)(tempX+5)/40][(int)(tempY+5)/40]!=0){
playSound(3);
else if(e.getButton()==e.BUTTON3){ //鼠标右键点击
//用户鼠标移动
public void this_mouseMoved(MouseEvent e) {
登录百度帐号推荐应用

我要回帖

更多关于 java五子棋代码 的文章

 

随机推荐