JAVA课程设计 五子棋内附完整代码.docx

上传人:b****5 文档编号:2946641 上传时间:2022-11-16 格式:DOCX 页数:54 大小:209.02KB
下载 相关 举报
JAVA课程设计 五子棋内附完整代码.docx_第1页
第1页 / 共54页
JAVA课程设计 五子棋内附完整代码.docx_第2页
第2页 / 共54页
JAVA课程设计 五子棋内附完整代码.docx_第3页
第3页 / 共54页
JAVA课程设计 五子棋内附完整代码.docx_第4页
第4页 / 共54页
JAVA课程设计 五子棋内附完整代码.docx_第5页
第5页 / 共54页
点击查看更多>>
下载资源
资源描述

JAVA课程设计 五子棋内附完整代码.docx

《JAVA课程设计 五子棋内附完整代码.docx》由会员分享,可在线阅读,更多相关《JAVA课程设计 五子棋内附完整代码.docx(54页珍藏版)》请在冰豆网上搜索。

JAVA课程设计 五子棋内附完整代码.docx

JAVA课程设计五子棋内附完整代码

JAVA课程设计

 

设计题目:

五子棋游戏

一.简要的介绍五子棋

1.五子棋的起源

五子棋,又被称为“连五子、五子连、串珠、五目、五目碰、五格、五石、五法、五联、京棋”。

五子棋相传起源于四千多年前的尧帝时期,比围棋的历史还要悠久,可能早在“尧造围棋”之前,民间就已有五子棋游戏。

有关早期五子棋的文史资料与围棋有相似之处,因为古代五子棋的棋具与围棋是完全相同的。

2.现在五子棋标准棋盘(如图所示)

3.五子棋的棋子

五子棋采用两种颜色棋子,黑色棋子和白色棋子,和围棋相同,

 

4.五子棋规则

五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交叉线上,由于黑棋先行,优势太大,所以对黑棋设了禁手,又规定了“三手交换”,

就是黑棋下第2手棋,盘面第3着棋之后,白方在应白2之前,如感觉黑方棋形不利于己方,可出交换,即执白棋一方变为执黑棋一方。

和“五手两打法”,就是黑棋在下盘面上关键的第5手时,必须下两步棋,让白方在这两步棋中任选一步,然后再续下。

不过一般爱好者不需要遵循这么多规则。

 

二.程序流程

三.代码设计与分析

main方法创建了ChessFrame类的一个实例对象(cf),

并启动屏幕显示显示该实例对象。

publicclassFiveChessAppletDemo{

publicstaticvoidmain(Stringargs[]){

ChessFramecf=newChessFrame();

cf.show();

}

}

用类ChessFrame创建五子棋游戏主窗体和菜单

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

importjavax.swing.*;

importjava.io.PrintStream;

importjavax.swing.JComponent;

importjavax.swing.JPanel;

classChessFrameextendsJFrameimplementsActionListener{

privateString[]strsize={"标准棋盘","改进棋盘","扩大棋盘"};

privateString[]strmode={"人机对战","人人对战"};

publicstaticbooleaniscomputer=true,checkcomputer=true;

privateintwidth,height;

privateChessModelcm;

privateMainPanelmp;

构造五子棋游戏的主窗体

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

rmenu=makeRadioButtonMenuItem(strsize[j],target);

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

rmenu=makeRadioButtonMenuItem(strmode[h],target);

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

returnnull;

if(targetinstanceofActionListener)

r.addActionListener((ActionListener)target);

returnr;

}

构造五子棋游戏的单选按钮式菜单项

publicJRadioButtonMenuItemmakeRadioButtonMenuItem(

Objectitem,Objecttarget){

JRadioButtonMenuItemr=null;

if(iteminstanceofString)

r=newJRadioButtonMenuItem((String)item);

elseif(iteminstanceofJRadioButtonMenuItem)

r=(JRadioButtonMenuItem)item;

else

returnnull;

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.updateComponentTreeUI(this);

}

if(arg.equals("改进棋盘")){

this.width=18;

this.height=18;

cm=newChessModel

(2);

MapSize(this.width,this.height);

SwingUtilities.updateComponentTreeUI(this);

}

if(arg.equals("扩大棋盘")){

this.width=22;

this.height=22;

cm=newChessModel(3);

MapSize(this.width,th

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

当前位置:首页 > 表格模板 > 合同协议

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

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