java五子棋代码1Word文档下载推荐.docx

上传人:b****5 文档编号:18747021 上传时间:2023-01-01 格式:DOCX 页数:15 大小:19.46KB
下载 相关 举报
java五子棋代码1Word文档下载推荐.docx_第1页
第1页 / 共15页
java五子棋代码1Word文档下载推荐.docx_第2页
第2页 / 共15页
java五子棋代码1Word文档下载推荐.docx_第3页
第3页 / 共15页
java五子棋代码1Word文档下载推荐.docx_第4页
第4页 / 共15页
java五子棋代码1Word文档下载推荐.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

java五子棋代码1Word文档下载推荐.docx

《java五子棋代码1Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java五子棋代码1Word文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。

java五子棋代码1Word文档下载推荐.docx

//标识当前应该黑棋还是白棋下下一步

booleanisBlack=true;

//标识当前游戏是否可以继续

booleancanPlay=true;

//保存显示的提示信息

Stringmessage="

黑方先行"

;

//保存最多拥有多少时间(秒)

intmaxTime=0;

//做倒计时的线程类

Threadt=newThread(this);

//保存黑方与白方的剩余时间

intblackTime=0;

intwhiteTime=0;

//保存双方剩余时间的显示信息

StringblackMessage="

无限制"

StringwhiteMessage="

@SuppressWarnings("

deprecation"

publicFiveChessFrame(){

//设置标题

this.setTitle("

五子棋"

);

//设置窗体大小

this.setSize(500,500);

//设置窗体出现位置

this.setLocation((width-500)/2,(height-500)/2);

//将窗体设置为大小不可改变

this.setResizable(false);

//将窗体的关闭方式设置为默认关闭后程序结束

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//为窗体加入监听器

this.addMouseListener(this);

//将窗体显示出来

this.setVisible(true);

t.start();

t.suspend();

StringimagePath="

"

;

try{

imagePath=System.getProperty("

user.dir"

)+"

/bin/image/background.jpg"

bgImage=ImageIO.read(newFile(imagePath.replaceAll("

\\\\"

"

/"

)));

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

//刷新屏幕,防止开始游戏时出现无法显示的情况.

this.repaint();

}

publicvoidpaint(Graphicsg){

//双缓冲技术防止屏幕闪烁

BufferedImagebi=newBufferedImage(500,500,

BufferedImage.TYPE_INT_RGB);

Graphicsg2=bi.createGraphics();

g2.setColor(Color.BLACK);

//绘制背景

g2.drawImage(bgImage,1,20,this);

//输出标题信息

g2.setFont(newFont("

黑体"

Font.BOLD,20));

g2.drawString("

游戏信息:

+message,130,60);

//输出时间信息

宋体"

0,14));

黑方时间:

+blackMessage,30,470);

白方时间:

+whiteMessage,260,470);

//绘制棋盘

for(inti=0;

i<

19;

i++){

g2.drawLine(10,70+20*i,370,70+20*i);

g2.drawLine(10+20*i,70,10+20*i,430);

//标注点位

g2.fillOval(68,128,4,4);

g2.fillOval(308,128,4,4);

g2.fillOval(308,368,4,4);

g2.fillOval(68,368,4,4);

g2.fillOval(308,248,4,4);

g2.fillOval(188,128,4,4);

g2.fillOval(68,248,4,4);

g2.fillOval(188,368,4,4);

g2.fillOval(188,248,4,4);

/*

*//绘制棋子x=(x-10)/20*20+10;

y=(y-70)/20*20+70;

*//黑子g.fillOval(x-7,y-7,14,14);

//白子g.setColor(Color.WHITE);

*g.fillOval(x-7,y-7,14,14);

g.setColor(Color.BLACK);

*g.drawOval(x-7,y-7,14,14);

*/

//绘制全部棋子

for(intj=0;

j<

j++){

if(allChess[i][j]==1){

//黑子

inttempX=i*20+10;

inttempY=j*20+70;

g2.fillOval(tempX-7,tempY-7,14,14);

}

if(allChess[i][j]==2){

//白子

g2.setColor(Color.WHITE);

g2.setColor(Color.BLACK);

g2.drawOval(tempX-7,tempY-7,14,14);

}

g.drawImage(bi,0,0,this);

publicvoidmouseClicked(MouseEvente){

//TODOAuto-generatedmethodstub

publicvoidmouseEntered(MouseEvente){

publicvoidmouseExited(MouseEvente){

publicvoidmousePressed(MouseEvente){

*System.out.println("

X:

+e.getX());

System.out.println("

Y:

+e.getY());

if(canPlay==true){

x=e.getX();

y=e.getY();

if(x>

=10&

&

x<

=370&

y>

=70&

y<

=430){

x=(x-10)/20;

y=(y-70)/20;

if(allChess[x][y]==0){

//判断当前要下的是什么颜色的棋子

if(isBlack==true){

allChess[x][y]=1;

isBlack=false;

message="

轮到白方"

}else{

allChess[x][y]=2;

isBlack=true;

轮到黑方"

}

//判断这个棋子是否和其他的棋子连成5连,即判断游戏是否结束

booleanwinFlag=this.checkWin();

if(winFlag==true){

JOptionPane.showMessageDialog(this,"

游戏结束,"

+(allChess[x][y]==1?

"

黑方"

:

白方"

)+"

获胜!

canPlay=false;

}else{

JOptionPane.showMessageDialog(this,"

当前位置已经有棋子,请重新落子!

this.repaint();

/*System.out.println(e.getX()+"

--"

+e.getY());

//点击开始游戏按钮

if(e.getX()>

=400&

e.getX()<

=470&

e.getY()>

=70

&

e.getY()<

=100){

intresult=JOptionPane.showConfirmDialog(this,"

是否重新开始游戏?

if(result==0){

//现在重新开始游戏

//重新开始所要做的操作:

1)把棋盘清空,allChess这个数组中全部数据归0.

//2)将游戏信息:

的显示改回到开始位置

//3)将下一步下棋的改为黑方

for(inti=0;

for(intj=0;

allChess[i][j]=0;

//另一种方式allChess=newint[19][19];

message="

isBlack=true;

blackTime=maxTime;

whiteTime=maxTime;

if(maxTime>

0){

blackMessage=maxTime/3600+"

:

+(maxTime/60-maxTime/3600*60)+"

+(maxTime-maxTime/60*60);

whiteMessage=maxTime/3600+"

t.resume();

blackMessage="

whiteMessage="

this.canPlay=true;

//点击游戏设置按钮

=120

=150){

Stringinput=JOptionPane

.showInputDialog("

请输入游戏的最大时间(单位:

分钟),如果输入0,表示没有时间限制:

try{

maxTime=Integer.parseInt(input)*60;

if(maxTime<

请输入正确信息,不允许输入负数!

if(maxTime==0){

intresult=JOptionPane.showConfirmDialog(this,

"

设置完成,是否重新开始游戏?

if(result==0){

for(inti=0;

for(intj=0;

allChess[i][j]=0;

}

}

//另一种方式allChess=newint[19][19];

blackTime=maxTime;

whiteTime=maxTime;

blackMessage="

whiteMessage="

this.canPlay=true;

this.repaint();

blackMessage=maxTime/3600+"

+(maxTime/60-maxTime/3600*60)+"

+(maxTime-maxTime/60*60);

whiteMessage=maxTime/3600+"

t.resume();

}catch(NumberFormatExceptione1){

//TODOAuto-generatedcatchblock

JOptionPane.showMessageDialog(this,"

请正确输入信息!

//点击游戏说明按钮

=170

=200){

JOptionPane.showMessageDialog(this,

"

这个一个五子棋游戏程序,黑白双方轮流下棋,当某一方连到五子时,游戏结束。

//点击认输按钮

=270

=300){

是否确认认输?

if(isBlack){

黑方已经认输,游戏结束!

白方已经认输,游戏结束!

canPlay=false;

//点击关于按钮

=320

=350){

本游戏由汪宣开制作,有相关问题可以咨询本人。

//点击退出按钮

=370

=400){

JOptionPane.showMessageDialog(this,"

游戏结束"

System.exit(0);

publicvoidmouseReleased(MouseEvente){

privatebooleancheckWin(){

booleanflag=false;

//保存共有相同颜色多少棋子相连

intcount=1;

//判断横向是否有5个棋子相连,特点纵坐标是相同,即allChess[x][y]中y值是相同

intcolor=allChess[x][y];

*if(color==allChess[x+1][y]){count++;

if(color==

*allChess[x+2][y]){count++;

if(color==allChess[x+3][y]){

*count++;

}}}

//通过循环来做棋子相连的判断

*inti=1;

while(color==allChess[x+i][y+0]){count++;

i++;

*i=1;

while(color==allChess[x-i][y-0]){count++;

}if

*(count>

=5){flag=true;

}//纵向的判断inti2=1;

intcount2=1;

*while(color==allChess[x+0][y+i2]){count2++;

i2++;

}i2=1;

*while(color==allChess[x-0][y-i2]){count2++;

*(count2>

=5){flag=true;

}//斜方向的判断(右上+左下)inti3=1;

int

*count3=1;

while(color==allChess[x+i3][y-i3]){count3++;

*i3++;

}i3=1;

while(color==allChess[x-i3][y+i3]){count3++;

}if(count3>

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1