Java编写的简单五子棋Word下载.docx
- 文档编号:18266645
- 上传时间:2022-12-14
- 格式:DOCX
- 页数:8
- 大小:16.26KB
Java编写的简单五子棋Word下载.docx
《Java编写的简单五子棋Word下载.docx》由会员分享,可在线阅读,更多相关《Java编写的简单五子棋Word下载.docx(8页珍藏版)》请在冰豆网上搜索。
booleanb;
//用来判断白棋还是黑棋
intblackcount,whitecount;
//计算悔棋
/**
*构造方法
*/
publicwuziqi(){
super("
五子棋"
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//关闭按钮
Containercon=this.getContentPane();
this.addMouseListener(this);
//添加监听
tool.add(btnstart);
//添加按钮
tool.add(btnstop);
this.setSize(550,500);
//设置窗体大小
this.setVisible(true);
intw=20;
//间距大小是双数
intpx=100,py=100;
//棋盘的坐标
intpxw=(px+w),pyw=(py+w);
intwidth=w*16,height=w*16;
intvline=(width+px);
//垂直线的长度
inthline=(height+py);
//水平线的长度
*画棋盘
publicvoidpaint(Graphicsg){
g.clearRect(0,0,this.getWidth(),this.getHeight());
//清除面板
g.setColor(Color.BLACK);
//设置网格颜色
g.drawRect(px,py,width,height);
//网格大小
g.drawString("
简易五子棋"
110,70);
for(inti=0;
i<
15;
i++){
g.drawLine(pxw+i*w,py,pxw+i*w,hline);
//每条横线和竖线
g.drawLine(px,pyw+i*w,vline,pyw+i*w);
}
for(intx=0;
x<
v.size();
x++){
Stringstr=(String)v.get(x);
Stringtmp[]=str.split("
-"
inta=Integer.parseInt(tmp[0]);
intb=Integer.parseInt(tmp[1]);
a=a*w+px;
b=b*w+py;
if(x%2==0){
g.setColor(Color.CYAN);
}else{
g.setColor(Color.BLACK);
}
g.fillArc(a-w/2,b-w/2,w,w,0,360);
publicvoidupdeta(Graphicsg){
this.paint(g);
publicvoidvictory(intx,inty,Vectorcontain){//判断胜利的方法
intcv=0;
//计算垂直的变量
intch=0;
//计算水平的变量
intci1=0;
//计算斜面的变量1
intci2=0;
//计算斜面的变量2
for(inti=1;
5;
if(contain.contains((x+i)+"
+y))
ch++;
else
break;
System.out.println("
前面执行了"
+ch+"
次"
if(contain.contains((x-i)+"
后面执行了"
if(contain.contains(x+"
+(y+i)))
cv++;
+(y-i)))
ci1++;
ci2++;
if(ch>
=4||cv>
=4||ci1>
=4||ci2>
=4){
System.out.println(v.size()+"
步棋"
if(v.size()%2==0){//判断偶数是黑棋胜利,奇数白棋胜利
JOptionPane.showMessageDialog(null,"
恭喜你黑棋赢了"
}
else{
恭喜你白棋赢了"
this.v.clear();
this.black.clear();
this.white.clear();
this.repaint();
System.out.println(ch+"
"
+cv+"
+ci1+"
+ci2);
publicvoidmouseClicked(MouseEvente){
if(e.getButton()==e.BUTTON1){
intx=e.getX();
inty=e.getY();
x=(x-x%w)+(x%w>
w/2?
w:
0);
y=(y-y%w)+(y%w>
x=(x-px)/w;
y=(y-py)/w;
if(x>
=0&
&
y>
=16&
y<
=16){
if(v.contains(x+"
+y)){
System.out.println("
已有棋了"
v.add(x+"
+y);
this.repaint();
if(v.size()%2==0){
black.add(x+"
this.victory(x,y,black);
System.out.println("
黑棋"
}
else{
white.add(x+"
this.victory(x,y,white);
白棋"
System.out.println(e.getX()+"
+e.getY());
}
else{
System.out.println(e.getX()+"
+e.getY()+"
|"
+x+"
+y+"
\t超出边界"
if(e.getButton()==e.BUTTON3){//悔棋方法全在这里
System.out.println("
鼠标右键-悔棋"
if(v.isEmpty()){
JOptionPane.showMessageDialog(this,"
没有棋可以悔"
if(v.size()%2==0){//判断是白方悔棋还是黑方悔棋
blackcount++;
if(blackcount>
3){
JOptionPane.showMessageDialog(this,"
黑棋已经悔了三步"
v.remove(v.lastElement());
this.repaint();
}else{
whitecount++;
if(whitecount>
白棋已经悔了三步"
}else{
publicvoidmouseEntered(MouseEvente){
publicvoidmouseExited(MouseEvente){
publicvoidmousePressed(MouseEvente){
publicvoidmouseReleased(MouseEvente){
}
二、简要说明
该程序为一简易五子棋,棋盘为15*15,白子先落。
可以悔棋,点击鼠标右键悔棋,最多悔3步。
在每次下子前,先判断该处有无棋子,有则不能落子,超出边界不能落子。
下子时白子先落,判断胜利为偶数是黑棋胜利,奇数白棋胜利。
任何一方有达到横、竖、撇、捺连到5子则胜利。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 编写 简单 五子棋