java五子棋游戏源代码汇总Word格式.docx

上传人:b****7 文档编号:22310025 上传时间:2023-02-03 格式:DOCX 页数:21 大小:19.13KB
下载 相关 举报
java五子棋游戏源代码汇总Word格式.docx_第1页
第1页 / 共21页
java五子棋游戏源代码汇总Word格式.docx_第2页
第2页 / 共21页
java五子棋游戏源代码汇总Word格式.docx_第3页
第3页 / 共21页
java五子棋游戏源代码汇总Word格式.docx_第4页
第4页 / 共21页
java五子棋游戏源代码汇总Word格式.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

java五子棋游戏源代码汇总Word格式.docx

《java五子棋游戏源代码汇总Word格式.docx》由会员分享,可在线阅读,更多相关《java五子棋游戏源代码汇总Word格式.docx(21页珍藏版)》请在冰豆网上搜索。

java五子棋游戏源代码汇总Word格式.docx

Stringmessage="

黑方先行"

;

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

intmaxTime=0;

//做倒计时的线程类

Threadt=newThread(this);

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

intblackTime=0;

intwhiteTime=0;

//保存时间信息

StringblackMessage="

无限制"

StringwhiteMessage="

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.setVisible(true);

//将窗体加入监听器

this.addMouseListener(this);

t.start();

t.suspend();

//刷新屏幕,防止游戏打开时无法显示。

this.repaint();

try{

bgimage=ImageIO.read(newFile("

F:

/background.jpg"

));

}catch(IOExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

publicvoidpaint(Graphicsg){

//双缓冲技术防止画面闪烁

BufferedImagebi=newBufferedImage(500,500,BufferedImage.TYPE_INT_ARGB);

Graphicsg2=bi.createGraphics();

g2.drawImage(bgimage,3,22,this);

g2.setFont(newFont("

宋体"

Font.BOLD,20));

g2.setColor(Color.black);

g2.drawString("

游戏信息:

"

+message,120,60);

华文行楷"

0,18));

//设置时间界面

黑方时间:

+blackMessage,28,470);

白方时间:

+whiteMessage,250,470);

for(inti=0;

i<

19;

i++){

//画棋盘线

g2.drawLine(13,72+20*i,373,72+20*i);

g2.drawLine(13+20*i,72,13+20*i,432);

//标注点位

g2.fillOval(71,130,4,4);

g2.fillOval(71,370,4,4);

g2.fillOval(311,130,4,4);

g2.fillOval(311,370,4,4);

g2.fillOval(191,250,5,5);

g2.fillOval(71,250,4,4);

g2.fillOval(311,250,4,4);

g2.fillOval(191,130,4,4);

g2.fillOval(191,370,4,4);

//繪製棋子

/*

*x=(x-13)/20*20+10;

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

g.fillOval(x-4,y-4,10,10);

*/

//繪製全部棋子

for(intj=0;

j<

j++){

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

//黑子

inttempX=i*20+13;

inttempY=j*20+73;

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

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

//繪製白子

g2.setColor(Color.white);

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

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

}

@Override

publicvoidmouseClicked(MouseEvente){

//TODOAuto-generatedmethodstub

publicvoidmousePressed(MouseEvente){

//System.out.println("

X:

+e.getX());

Y:

+e.getY());

if(canPlay==true){

x=e.getX();

y=e.getY();

if(x>

=13&

&

x<

=373&

y>

=73&

y<

=433){

x=(x-13)/20;

y=(y-72)/20;

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

//判断现在要下什么颜色的棋子

if(isBlack==true){

allChess[x][y]=1;

isBlack=false;

message="

轮到白方"

}else{

allChess[x][y]=2;

isBlack=true;

轮到黑方"

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

booleanWinflag=this.checkWin();

if(Winflag==true){

JOptionPane.showMessageDialog(this,"

游戏结束"

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

"

黑方"

:

白方"

)+"

获胜"

canPlay=false;

//this.checkWin();

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

子!

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

---"

//点击开始按钮

if(e.getX()>

=404&

e.getX()<

=472&

e.getY()>

=74&

e.getY()<

=104){

intresult=JOptionPane.showConfirmDialog(this,"

是否重新开始游戏?

if(result==0){

//现在重新开始游戏

//重新开始游戏所需的操作,1.把棋盘清空allcChess这个数组中的数全部

归零;

//2将游戏信息设置为初始位置;

//3将下一步器=棋设置为黑方开始;

for(inti=0;

i<

19;

i++){

for(intj=0;

j<

j++){

allChess[i][j]=0;

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

黑色先行"

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();

}else{

blackMessage="

whiteMessage="

//点击游戏设置

=124&

=154){

Stringinput=JOptionPane.showInputDialog("

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

钟),如果输入0,表示时间为无限制!

maxTime=Integer.parseInt(input)*60;

if(maxTime<

请输入正确信息,不允许

输入负数!

if(maxTime==0){

intresult=JOptionPane.showConfirmDialog(this,"

设置完成,是否

重新开始游戏?

设置完成,

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++;

}}}

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

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

是否相同

*inti=1;

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

i++;

}i=

*1;

while(color==allChess[x-i][y]);

{count++;

}if(count

*>

=5){flag=true;

*//縱方向的inti2=1;

intcount2=1;

while(color==allChess[x][y+i2]){

*count2++;

i2++;

}i2=1;

while(color==allChess[x][y-i2]);

{count2++;

*i2++;

}if(count2>

=5){flag=true;

}//判斷斜右上的左下inti3=1;

intcount3

*=1;

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

i3++;

}i3=1;

*while(color==allChess[x-i][y+i]);

{count3++;

}if(count3>

=5){

*flag=true;

}//斜下左上右下inti4=1;

intcount4=1;

while(color==

*allChess[x+i][y+i]){count4++;

i4++;

}i4=1;

while

*(color==allChess[x-i][y-i]);

{count4++;

}if(count4>

//判斷橫向

count=this.checkChess(1,0,color);

if(count>

=5){

flag=true;

//判斷縱向

count=this.checkChess(0,1,color);

//判斷右上、左下

count=this.checkChess(1,-1,color);

//判斷右下、左上

count=this.checkChess(1,1,color);

=5){

returnflag;

privateintcheckChess(intxChange,intyChange,intcolor){

intcount=1;

inttempX=xChange;

inttempY=yChange;

while(x+xChange>

=0&

x+xChange<

=18&

y+yChange>

y+yChange<

color==allChess[x+xChange][y+yChange]){

count++;

if(xChange!

=0)

xChange++;

if(yChange!

=0){

if(yChange>

0)

yChange++;

else{

yChange--;

xChange=tempX;

yChange=tempY;

while(x-xChange>

x-xChange<

=18&

y-yChange>

=0&

y-

yChange<

color==allChess[x-xChange][y-yChange]){

returncount;

publicvoidmouseEntered(MouseEvente){

publicvoidmouseExited(MouseEvente){

publicvoidrun(){

//判断是否有时间限制

while(true){

if(isBlack){

blackTime--;

if(blackTime==0){

黑方超時,游戏结束"

}else{

whit

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

当前位置:首页 > 求职职场 > 简历

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

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