JAVA课程设计五子棋.docx
- 文档编号:8054775
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:48
- 大小:139.49KB
JAVA课程设计五子棋.docx
《JAVA课程设计五子棋.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计五子棋.docx(48页珍藏版)》请在冰豆网上搜索。
JAVA课程设计五子棋
JAVA课程设计
设计题目:
五子棋游戏
简要的介绍五子棋
1.五子棋的起源
五子棋,乂被称为“连五子、五子连、申珠、五目、五目碰、五格、五石、五法、五联、京棋”。
五子棋相传起源丁四千多年前的尧帝时期,比围棋的历史还要悠久,可能早在“尧造围棋”之前,民间就已有五子棋游戏。
有关早期五子棋的文史资料与围棋有相似之处,因为古代五子棋的棋具与围棋是完全相同的。
2.现在五子棋标准棋盘(如图所示)
3.五子棋的棋子
五子棋采用两种颜色棋子,黑色棋子和白色棋子,和围棋相同,
4.五子棋规则
五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交义线上,由丁黑棋先行,优势太大,所以对黑棋设了禁手,乂规定了“三手交换”,
就是黑棋下第2手棋,盘面第3着棋之后,白方在应白2之前,如感觉黑方棋形不利丁己方,可出交换,即执白棋一方变为执黑棋一方。
和“五手两打法”,就是黑棋在下盘面上关键的第5手时,必须下两步棋,让白方在这两步棋中任选一步,然后再续下。
不过一般爱好者不需要遵循这么多规则。
2.程序流程
3.代码设计与分析
main方法创建了ChessFrame类的一个实例对象(cf),
并启动屏幕显示显示该实例对象。
publicclassFiveChessAppletDemo(
publicstaticvoidmain(Stringargs[]){
ChessFramecf=newChessFrame();
cf.show();
}
}
用类ChessFrame^J建五子棋游戏主窗体和菜单
import
java.awt.*;
import
java.awt.event.*;
import
java.applet.*;
import
javax.swing.*;
import
java.io.PrintStream;
importjavax.swing.JComponent;
importjavax.swing.JPanel;
class
ChessFrame
extends
JFrameimplementsActionListener{
private
String[]
strsize
={"标准棋盘","
改进棋盘","扩大棋盘"}
private
String[]
strmode
={"人机对战","
人人对战"};
public
staticbooleaniscomputer=true
checkcomputer=true
private
intwidth
height
;
private
ChessModel
cm;
private
MainPanel
mp;
构造五子棋游戏的主窗体
publicChessFrame(){this.setTitle("五子棋游戏”);
cm=newChessModel
(1);
mp=newMainPanel(cm);
Containercon=this.getContentPane();
con.add(mp,"Center");
this.setResizable(false);
this.addWindowListener(newChessWindowEvent());
MapSize(14,14);
JMenuBarmbar=newJMenuBar();
this.setJMenuBar(mbar);
JMenugameMenu=newJMenu("游戏”);
mbar.add(makeMenu(gameMenu,newObject[]{
"开局",null,"棋盘",null,"模式",null,"退出"},this));
JMenulookMenu=newJMenu("外观”);
mbar.add(makeMenu(lookMenu,newObject[]{
"类型一","类型二","类型三"},this));
JMenuhelpMenu=newJMenu("版本”);
mbar.add(makeMenu(helpMenu,newObject[]{
"关于"
},this));
}
构造五子棋游戏的主菜单
publicJMenumakeMenu(Objectparent,Objectitems[],Objecttarget){JMenum=null;
if(parentinstanceofJMenu)
m=(JMenu)parent;
elseif(parentinstanceofString)m=newJMenu((String)parent);
else
returnnull;
for(inti=0;i if(items[i]==null) m.addSeparator(); elseif(items[i]=="棋盘”){ JMenujm=newJMenu("棋盘”);ButtonGroupgroup=newButtonGroup(); JRadioButtonMenuItemrmenu; for(intj=0;j if(j==0)rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); }m.add(jm); }elseif(items[i]=="模式"){ JMenujm=newJMenu("模式");ButtonGroupgroup=newButtonGroup(); JRadioButtonMenuItemrmenu; for(inth=0;h if(h==0)rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); } m.add(jm); }else m.add(makeMenuItem(items[i],target)); returnm; } 构造五子棋游戏的菜单项 publicJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null; if(iteminstanceofString)r=newJMenuItem((String)item); elseif(iteminstanceofJMenuItem) r=(JMenuItem)item; else return null if(targetinstanceofActionListener)r.addActionListener((ActionListener)target); returnr; ) 构造五子棋游戏的单选按钮式菜单项 publicJRadioButtonMenuItemmakeRadioButtonMenuItem(Objectitem,Objecttarget){JRadioButtonMenuItemr=null; if(iteminstanceofString) r=newJRadioButtonMenuItem((String)item); elseif(iteminstanceofJRadioButtonMenuItem)r=(JRadioButtonMenuItem)item; elsereturnnull; if(targetinstanceofActionListener) r.addActionListener((ActionListener)target); returnr; ) publicvoidMapSize(intw,inth){setSize(w*24,h*27); if(this.checkcomputer)this.iscomputer=true; else this.iscomputer=false; mp.setModel(cm); mp.repaint(); ) publicbooleangetiscomputer(){returnthis.iscomputer; ) publicvoidrestart(){ intmodeChess=cm.getModeChess(); if(modeChess<=3&&modeChess>=0){ cm=newChessModel(modeChess); MapSize(cm.getWidth(),cm.getHeight()); ) ) publicvoidactionPerformed(ActionEvente)( Stringarg=e.getActionCommand(); try( if(arg.equals("类型三")) UIManager.setLookAndFeel( com.sun.java.swing.plaf.windows.WindowsLookAndFeel” ); elseif(arg.equals("类型二")) UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel”else ); UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel"); SwingUtilities.updateComponentTreeUI(this); }catch(Exceptionee)(} if(arg.equals("标准棋盘”)){this.width=14; this.height=14; cm=newChessModel (1); MapSize( this.width,this.height); SwingUtilities. } if(arg.equals( updateComponentTreeUI "改进棋盘")){ (this); this.width=18; this.height=18; cm=newChessModel (2); MapSize( this.width,this.height); SwingUtilities. } if(arg.equals( updateComponentTreeUI "扩大棋盘")){ (this); this.width=22; this.height=22; cm=newChessModel(3); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("人机对战”)){ this.checkcomputer=true; this.iscomputer=true;cm=newChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); if(arg.equals( "人人对战")){ MapSize(SwingUtilities.} this.checkcomputer=false; this.iscomputer=false; cm=newChessModel(cm.getModeChess()); cm.getWidth(),cm.getHeight()); (this); updateComponentTreeUI if(arg.equals(restart(); } if(arg.equals(JOptionPane. ",JOptionPane. if(arg.equals( System.exit } "关于")) showMessageDialog(null PLAIN_MESSAGE); "退出")) (0); "第一版","版本 用类ChessModel实现了整个五子棋程序算法的核心 importjava.awt.*; importjava.awt.event.*; importjava.applet.*; importjavax.swing.*; importjava.io.PrintStream; importjavax.swing.JComponent; importjavax.swing.JPanel; classChessModel{ 规定棋盘的宽度、高度、棋盘的模式 privateintwidth,height,modeChess; 规定棋盘方格的横向、纵向坐标 privateintx=0,y=0; 棋盘方格的横向、纵向坐标所对应的棋子颜色, 数组arrMapSho*有3个值: 1,2,3,-1, 其中1代表该棋盘方格上下的棋子为黑子, 2代表该棋盘方格上下的棋子为白子, 3代表为该棋盘方格上没有棋子, -1代表该棋盘方格不能够下棋子 privateint[][]arrMapShow; 交换棋手的标识,棋盘方格上是否有棋子的标识符 privatebooleanisOdd,isExist; publicChessModel(){} 该构造方法根据不同的棋盘模式(modeChess来构建对应大小的棋盘 publicChessModel(intmodeChess){this.isOdd=true;if(modeChess==1){ PanelInit(14,14,modeChess);} if(modeChess==2){PanelInit(18,18,modeChess); }if(modeChess==3){ PanelInit(22,22,modeChess);}} 按照棋盘模式构建棋盘大小 privatevoidPanelInit(intwidth,intheight,intmodeChess){this.width=width;this.height=height;this.modeChess=modeChess;arrMapShow=newint[width+1][height+1];for(inti=0;i<=width;i++){ for(intj=0;j<=height;j++){arrMapShow[i][j]=-1; }}} 获取是否交换棋手的标识符 publicbooleangetisOdd(){returnthis.isOdd; 设置交换棋手的标识符 publicvoidsetisOdd(booleanisodd)(if(isodd) this.isOdd=true; else this.isOdd=false; } 获取某棋盘方格是否有棋子的标识值 publicbooleangetisExist(){returnthis.isExist; } 获取棋盘宽度 publicintgetWidth(){returnthis.width; } 获取棋盘高度 publicintgetHeight(){returnthis.height; } 获取棋盘模式 publicintgetModeChess(){ returnthis.modeChess; } 获取棋盘方格上棋子的信息 publicint[][]getarrMapShow(){returnarrMapShow; } 判断下子的横向、纵向坐标是否越界 privatebooleanbadxy(intx,inty){ if(x>=width+20||x<0) returntrue returny>=height+20||y<0; 计算棋盘上某一方格上八个方向棋子的最大值, 这八个方向分别是: 左、右、上、下、左上、左下、右上、右下 publicbooleanchessExist(inti,intj)( if(this.arrMapShow[i][j]==1||this.arrMapShow[i][j]==2) returntrue; returnfalse; ) 判断该坐标位置是否可下棋子 publicvoidreadyplay(intx,inty)(if(badxy(x,y)) return; if(chessExist(x,y))return; this.arrMapShow[x][y]=3; ) 在该坐标位置下棋子 publicvoidplay(intx,inty)(if(badxy(x,y)) return; if(chessExist(x,y))( this.isExist=true; return; )else this.isExist=false; if(getisOdd()){ setisOdd(false); this.arrMapShow[x][y]=1; )else{ setisOdd(true); this.arrMapShow[x][y]=2; ) ) 计算机走棋 说明: 用穷举法判断每一个坐标点的四个方向的的最大棋子数, 最后得出棋子数最大值的坐标,下子 publicvoidcomputerDo(intwidth,intheight)(intmax_black,max_white,max_temp,max=0; setisOdd(true); System.out.println("计算机走棋..."); for(inti=0;i<=width;i++)(for(intj=0;j<=height;j++){ 算法判断是否下子 if(! chessExist(i,j)){ 判断白子的最大值 max_white=checkMax(i,j,2); 判断黑子的最大值 max_black=checkMax(i,j,1);max_temp=Math.max(max_white,max_black); if(max_temp>max){max=max_temp; this.x=i; this.y=j; } } } } setX(this.x); setY(this.y); this.arrMapShow[this.x][this.y]=2; } 记录电脑下子后的横向坐标 publicvoidsetX(intx){this.x=x; } 记录电脑下子后的纵向坐标 publicvoidsetY(inty){ this.y=y; 获取电脑下子的横向坐标 publicintgetX()(returnthis.x; ) 获取电脑下子的纵向坐标 publicintgetY()(returnthis.y; ) 计算棋盘上某一方格上八个方向棋子的最大值, 这八个方向分别是: 左、右、上、下、左上、左下、右上、右下 publicintcheckMax(intx,inty,intblack_or_white){intnum=0,max_num,max_temp=0;intx_temp=x,y_temp=y;intx_temp1=x_temp,y_temp1=y_temp; 判断右边 for(inti=1;i<5;i++){x_temp1+=1; if(x_temp1>this.width)break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; ) 判断左边 x_temp1=x_temp; for(inti=1;i<5;i++){x_temp1-=1; if(x_temp1<0)break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; if(num<5)max_temp=num; 判断上面 x_temp1=x_temp; y_temp1=y_temp; num=0; for(inti=1;i<5;i++)( y_temp1-=1; if(y_temp1<0)break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++; else break; } 判断下面 y_temp1=y_temp; for(inti=1;i<5;i++)( y_temp1+=1; if(y_temp1>this.height)break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5)max_temp=num; 判断左上方 x_temp1=x_temp; y_temp1=y_temp; num=0; for(inti=1;i<5;i++)( x_temp1-=1; y_temp1-=1;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 课程设计 五子棋