收藏 分销(赏)

JAVA专业课程设计五子棋内附完整代码.doc

上传人:天**** 文档编号:2864378 上传时间:2024-06-07 格式:DOC 页数:62 大小:337.54KB 下载积分:14 金币
下载 相关 举报
JAVA专业课程设计五子棋内附完整代码.doc_第1页
第1页 / 共62页
JAVA专业课程设计五子棋内附完整代码.doc_第2页
第2页 / 共62页


点击查看更多>>
资源描述
JAVA课程设计 设计题目:五子棋游戏 一. 简明介绍五子棋 1. 五子棋起源 五子棋,又被称为“连五子、五子连、串珠、五目、五目碰、五格、五石、五法、五联、京棋”。五子棋相传起源于四千多年前尧帝时期,比围棋历史还要悠久,可能早在“尧造围棋”之前,民间就已经有五子棋游戏。相关早期五子棋文史资料和围棋有相同之处,因为古代五子棋棋具和围棋是完全相同。 2.现在五子棋标准棋盘(图所表示) 3.五子棋棋子 五子棋采取两种颜色棋子,黑色棋子和白色棋子,和围棋相同, 4.五子棋规则 五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交叉线上,因为黑棋先行,优势太大,所以对黑棋设了禁手,又要求了“三手交换”, 就是黑棋下第 2 手棋,盘面第 3 着棋以后,白方在应白 2 之前,如感觉黑方棋形不利于己方,可出交换,即执白棋一方变为执黑棋一方。和“五手两打法”,就是黑棋在下盘面上关键第 5 手时,必需下两步棋,让白方在这两步棋中任选一步,然后再续下。不过通常爱好者不需要遵照这么多规则。 二.程序步骤 三.代码设计和分析 main方法创建了ChessFrame类一个实例对象(cf), 并开启屏幕显示显示该实例对象。 public class FiveChessAppletDemo { public static void main(String args[]){ ChessFrame cf = new ChessFrame(); cf.show(); } } 用类ChessFrame创建五子棋游戏主窗体和菜单 import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.io.PrintStream; import javax.swing.JComponent; import javax.swing.JPanel; class ChessFrame extends JFrame implements ActionListener { private String[] strsize={"标准棋盘","改善棋盘","扩大棋盘"}; private String[] strmode={"人机对战","人人对战"}; public static boolean iscomputer=true,checkcomputer=true; private int width,height; private ChessModel cm; private MainPanel mp; 结构五子棋游戏主窗体 public ChessFrame() { this.setTitle("五子棋游戏"); cm=new ChessModel(1); mp=new MainPanel(cm); Container con=this.getContentPane(); con.add(mp,"Center"); this.setResizable(false); this.addWindowListener(new ChessWindowEvent()); MapSize(14,14); JMenuBar mbar = new JMenuBar(); this.setJMenuBar(mbar); JMenu gameMenu = new JMenu("游戏"); mbar.add(makeMenu(gameMenu, new Object[] { "开局", null,"棋盘",null,"模式", null, "退出" }, this)); JMenu lookMenu =new JMenu("外观"); mbar.add(makeMenu(lookMenu,new Object[] { "类型一","类型二","类型三" },this)); JMenu helpMenu = new JMenu("版本"); mbar.add(makeMenu(helpMenu, new Object[] { "相关" }, this)); } 结构五子棋游戏主菜单 public JMenu makeMenu(Object parent, Object items[], Object target){ JMenu m = null; if(parent instanceof JMenu) m = (JMenu)parent; else if(parent instanceof String) m = new JMenu((String)parent); else return null; for(int i = 0; i < items.length; i++) if(items[i] == null) m.addSeparator(); else if(items[i] == "棋盘"){ JMenu jm = new JMenu("棋盘"); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int j=0;j<strsize.length;j++){ rmenu=makeRadioButtonMenuItem(strsize[j],target); if (j==0) rmenu.setSelected(true); jm.add(rmenu); group.add(rmenu); } m.add(jm); }else if(items[i] == "模式"){ JMenu jm = new JMenu("模式"); ButtonGroup group=new ButtonGroup(); JRadioButtonMenuItem rmenu; for (int h=0;h<strmode.length;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)); return m; } 结构五子棋游戏菜单项 public JMenuItem makeMenuItem(Object item, Object target){ JMenuItem r = null; if(item instanceof String) r = new JMenuItem((String)item); else if(item instanceof JMenuItem) r = (JMenuItem)item; else return null; if(target instanceof ActionListener) r.addActionListener((ActionListener)target); return r; } 结构五子棋游戏单选按钮式菜单项 public JRadioButtonMenuItem makeRadioButtonMenuItem( Object item, Object target){ JRadioButtonMenuItem r = null; if(item instanceof String) r = new JRadioButtonMenuItem((String)item); else if(item instanceof JRadioButtonMenuItem) r = (JRadioButtonMenuItem)item; else return null; if(target instanceof ActionListener) r.addActionListener((ActionListener)target); return r; } public void MapSize(int w,int h){ setSize(w * 24, h * 27); if(this.checkcomputer) this.iscomputer=true; else this.iscomputer=false; mp.setModel(cm); mp.repaint(); } public boolean getiscomputer(){ return this.iscomputer; } public void restart(){ int modeChess = cm.getModeChess(); if(modeChess <= 3 && modeChess >= 0){ cm = new ChessModel(modeChess); MapSize(cm.getWidth(),cm.getHeight()); } } public void actionPerformed(ActionEvent e){ String arg=e.getActionCommand(); try{ if (arg.equals("类型三")) UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); else if(arg.equals("类型二")) UIManager.setLookAndFeel( "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); else UIManager.setLookAndFeel( "javax.swing.plaf.metal.MetalLookAndFeel" ); SwingUtilities.updateComponentTreeUI(this); }catch(Exception ee){} if(arg.equals("标准棋盘")){ this.width=14; this.height=14; cm=new ChessModel(1); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("改善棋盘")){ this.width=18; this.height=18; cm=new ChessModel(2); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("扩大棋盘")){ this.width=22; this.height=22; cm=new ChessModel(3); MapSize(this.width,this.height); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("人机对战")){ this.checkcomputer=true; this.iscomputer=true; cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("人人对战")){ this.checkcomputer=false; this.iscomputer=false; cm=new ChessModel(cm.getModeChess()); MapSize(cm.getWidth(),cm.getHeight()); SwingUtilities.updateComponentTreeUI(this); } if(arg.equals("开局")){ restart(); } if(arg.equals("相关")) JOptionPane.showMessageDialog(null, "第一版", "版本",JOptionPane.PLAIN_MESSAGE ); if(arg.equals("退出")) System.exit(0); } } 用类ChessModel实现了整个五子棋程序算法关键 import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.io.PrintStream; import javax.swing.JComponent; import javax.swing.JPanel; class ChessModel { 要求棋盘宽度、高度、棋盘模式 private int width,height,modeChess; 要求棋盘方格横向、纵向坐标 private int x=0,y=0; 棋盘方格横向、纵向坐标所对应棋子颜色, 数组arrMapShow只有3个值:1,2,3,-1, 其中1代表该棋盘方格上下棋子为黑子, 2代表该棋盘方格上下棋子为白子, 3代表为该棋盘方格上没有棋子, -1代表该棋盘方格不能够下棋子 private int[][] arrMapShow; 交换棋手标识,棋盘方格上是否有棋子标识符 private boolean isOdd,isExist; public ChessModel() {} 该结构方法依据不一样棋盘模式(modeChess)来构建对应大小棋盘 public ChessModel(int modeChess){ this.isOdd=true; if(modeChess == 1){ PanelInit(14, 14, modeChess); } if(modeChess == 2){ PanelInit(18, 18, modeChess); } if(modeChess == 3){ PanelInit(22, 22, modeChess); } } 根据棋盘模式构建棋盘大小 private void PanelInit(int width, int height, int modeChess){ this.width = width; this.height = height; this.modeChess = modeChess; arrMapShow = new int[width+1][height+1]; for(int i = 0; i <= width; i++){ for(int j = 0; j <= height; j++){ arrMapShow[i][j] = -1; } } } 获取是否交换棋手标识符 public boolean getisOdd(){ return this.isOdd; } 设置交换棋手标识符 public void setisOdd(boolean isodd){ if(isodd) this.isOdd=true; else this.isOdd=false; } 获取某棋盘方格是否有棋子标识值 public boolean getisExist(){ return this.isExist; } 获取棋盘宽度 public int getWidth(){ return this.width; } 获取棋盘高度 public int getHeight(){ return this.height; } 获取棋盘模式 public int getModeChess(){ return this.modeChess; } 获取棋盘方格上棋子信息 public int[][] getarrMapShow(){ return arrMapShow; } 判定下子横向、纵向坐标是否越界 private boolean badxy(int x, int y){ if(x >= width+20 || x < 0) return true; return y >= height+20 || y < 0; } 计算棋盘上某一方格上八个方向棋子最大值, 这八个方向分别是:左、右、上、下、左上、左下、右上、右下 public boolean chessExist(int i,int j){ if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2) return true; return false; } 判定该坐标位置是否可下棋子 public void readyplay(int x,int y){ if(badxy(x,y)) return; if (chessExist(x,y)) return; this.arrMapShow[x][y]=3; } 在该坐标位置下棋子 public void play(int x,int y){ 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; } } 计算机走棋 说明:用穷举法判定每一个坐标点四个方向最大棋子数, 最终得出棋子数最大值坐标,下子 public void computerDo(int width,int height){ int max_black,max_white,max_temp,max=0; setisOdd(true); System.out.println("计算机走棋 ..."); for(int i = 0; i <= width; i++){ for(int j = 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; } 统计电脑下子后横向坐标 public void setX(int x){ this.x=x; } 统计电脑下子后纵向坐标 public void setY(int y){ this.y=y; } 获取电脑下子横向坐标 public int getX(){ return this.x; } 获取电脑下子纵向坐标 public int getY(){ return this.y; } 计算棋盘上某一方格上八个方向棋子最大值, 这八个方向分别是:左、右、上、下、左上、左下、右上、右下 public int checkMax(int x, int y,int black_or_white){ int num=0,max_num,max_temp=0; int x_temp=x,y_temp=y; int x_temp1=x_temp,y_temp1=y_temp; 判定右边 for(int i=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(int i=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(int i=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(int i=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(int i=1;i<5;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0 || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } 判定右下方 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) 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(int i=1;i<5;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0 || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } 判定左下方 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<5;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) num++; else break; } if(num>max_temp&&num<5) max_temp=num; max_num=max_temp; return max_num; } 判定胜败 public boolean judgeSuccess(int x,int y,boolean isodd){ int num=1; int arrvalue; int x_temp=x,y_temp=y; if(isodd) arrvalue=2; else arrvalue=1; int x_temp1=x_temp,y_temp1=y_temp; 判定右边胜败 for(int i=1;i<6;i++){ x_temp1+=1; if(x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } 判定左边胜败 x_temp1=x_temp; for(int i=1;i<6;i++){ x_temp1-=1; if(x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; 判定上方胜败 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ y_temp1-=1; if(y_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } 判定下方胜败 y_temp1=y_temp; for(int i=1;i<6;i++){ y_temp1+=1; if(y_temp1>this.height) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; 判定左上胜败 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1-=1; if(y_temp1<0 || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } 判定右下胜败 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1+=1; if(y_temp1>this.height || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; 判定右上胜败 x_temp1=x_temp; y_temp1=y_temp; num=1; for(int i=1;i<6;i++){ x_temp1+=1; y_temp1-=1; if(y_temp1<0 || x_temp1>this.width) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } 判定左下胜败 x_temp1=x_temp; y_temp1=y_temp; for(int i=1;i<6;i++){ x_temp1-=1; y_temp1+=1; if(y_temp1>this.height || x_temp1<0) break; if(this.arrMapShow[x_temp1][y_temp1]==arrvalue) num++; else break; } if(num==5) return true; return false; } 赢棋后提醒 public void showSuccess(JPanel jp){ JOptionPane.showMessageDialog(jp,"你赢了","结果", JOptionPane.INFORMATION_MESSAGE); } 输棋后提醒 public void showDefeat(JPanel jp){ JOptionPane.showMessageDialog(jp,"你输了","结果", JOptionPane.INFORMATION_MESSAGE); } } 用类MainPanel关键完成以下功效: 1、构建一个面板,在该面板上画上棋盘; 2、处理在该棋盘上鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等) import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.io.PrintStream; import javax.swing.JComponent; import javax.swing.JPanel; class MainPanel extends JPanel implements MouseListener,MouseMotionListener{ 设定棋盘宽度和高度 private int width,height; private ChessModel cm; 依据棋盘模式设定面板大小 MainPanel(ChessModel mm){ cm=mm; width=cm.getWidth();
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服