`
moshangchenzi
  • 浏览: 51901 次
  • 性别: Icon_minigender_1
  • 来自: 南宁
社区版块
存档分类
最新评论

五子棋源玛

阅读更多
五子棋源玛
  1. import java.awt.Color;
  2. import java.awt.Container;
  3. import java.awt.Graphics;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseListener;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.event.WindowAdapter;
  10. import java.awt.event.WindowEvent;
  11. import javax.swing.ButtonGroup;
  12. import javax.swing.JFrame;
  13. import javax.swing.JMenu;
  14. import javax.swing.JMenuBar;
  15. import javax.swing.JMenuItem;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JRadioButtonMenuItem;
  19. import javax.swing.SwingUtilities;
  20. import javax.swing.UIManager;
  21. /*
  22.  *main方法创建了ChessFrame类的一个实例对象(cf),
  23.  *并启动屏幕显示显示该实例对象。
  24.  **/
  25. public class FiveChessAppletDemo {
  26.     public static void main(String args[]){
  27.         ChessFrame cf = new ChessFrame();
  28.         cf.setVisible(true);
  29.     }
  30. }
  31. /*
  32.  *类ChessFrame主要功能是创建五子棋游戏主窗体和菜单
  33.  **/
  34. class ChessFrame extends JFrame implements ActionListener {
  35.     /**
  36.      * 
  37.      */
  38.     private static final long serialVersionUID = 2183726320279905885L;
  39.     private String[] strsize={"20x15","30x20","40x30"};
  40.     private String[] strmode={"人机对弈","人人对弈"};
  41.     public static boolean iscomputer=true;
  42.     public static boolean checkcomputer=true;
  43.     private int width,height;
  44.     private ChessModel cm;
  45.     private MainPanel mp;
  46.     
  47.     //构造五子棋游戏的主窗体
  48.     public ChessFrame() {
  49.         this.setTitle("五子棋游戏");
  50.         cm=new ChessModel(1);
  51.         mp=new MainPanel(cm);
  52.         Container con=this.getContentPane();
  53.         con.add(mp,"Center");
  54.         this.setResizable(false);
  55.         this.addWindowListener(new ChessWindowEvent());
  56.         MapSize(20,15);
  57.         JMenuBar mbar = new JMenuBar();
  58.         this.setJMenuBar(mbar);
  59.         JMenu gameMenu = new JMenu("游戏");
  60.         mbar.add(makeMenu(gameMenu, new Object[] {
  61.             "开局""棋盘","模式"null"退出"
  62.             }, this));
  63.         JMenu lookMenu =new JMenu("视图");
  64.         mbar.add(makeMenu(lookMenu,new Object[] {
  65.             "Metal","Motif","Windows"
  66.             },this));
  67.         JMenu helpMenu = new JMenu("帮助");
  68.         mbar.add(makeMenu(helpMenu, new Object[] {
  69.             "关于"
  70.         }, this));
  71.     }
  72.     //构造五子棋游戏的主菜单
  73.     public  JMenu makeMenu(Object parent, Object items[], Object target){
  74.         JMenu m = null;
  75.         if(parent instanceof JMenu)
  76.             m = (JMenu)parent;
  77.         else if(parent instanceof String)
  78.             m = new JMenu((String)parent);
  79.         else
  80.             return null;
  81.         for(int i = 0; i < items.length; i++)
  82.             if(items[i] == null)
  83.                 m.addSeparator();
  84.             else if(items[i] == "棋盘"){
  85.                 JMenu jm = new JMenu("棋盘");
  86.                 ButtonGroup group=new ButtonGroup();
  87.                 JRadioButtonMenuItem rmenu;
  88.                 for (int j=0;j<strsize.length;j++){
  89.                     rmenu=makeRadioButtonMenuItem(strsize[j],target);
  90.                     if (j==0)
  91.                         rmenu.setSelected(true);
  92.                     jm.add(rmenu);
  93.                     group.add(rmenu);
  94.                 }
  95.                 m.add(jm);
  96.             }else if(items[i] == "模式"){
  97.                 JMenu jm = new JMenu("模式");
  98.                 ButtonGroup group=new ButtonGroup();
  99.                 JRadioButtonMenuItem rmenu;
  100.                 for (int h=0;h<strmode.length;h++){
  101.                     rmenu=makeRadioButtonMenuItem(strmode[h],target);
  102.                     if(h==0)
  103.                         rmenu.setSelected(true);
  104.                     jm.add(rmenu);
  105.                     group.add(rmenu);
  106.                 }
  107.                 m.add(jm);
  108.             }else
  109.                 m.add(makeMenuItem(items[i], target));
  110.         return m;
  111.     }
  112.     
  113.     //构造五子棋游戏的菜单项
  114.     public  JMenuItem makeMenuItem(Object item, Object target){
  115.         JMenuItem r = null;
  116.         if(item instanceof String)
  117.             r = new JMenuItem((String)item);
  118.         else if(item instanceof JMenuItem)
  119.             r = (JMenuItem)item;
  120.         else
  121.             return null;
  122.         if(target instanceof ActionListener)
  123.             r.addActionListener((ActionListener)target);
  124.         return r;
  125.     }
  126.     
  127.     //构造五子棋游戏的单选按钮式菜单项
  128.     public  JRadioButtonMenuItem makeRadioButtonMenuItem(
  129.         Object item, Object target){
  130.         JRadioButtonMenuItem r = null;
  131.         if(item instanceof String)
  132.             r = new JRadioButtonMenuItem((String)item);
  133.         else if(item instanceof JRadioButtonMenuItem)
  134.             r = (JRadioButtonMenuItem)item;
  135.         else
  136.             return null;
  137.         if(target instanceof ActionListener)
  138.             r.addActionListener((ActionListener)target);
  139.         return r;
  140.     }
  141.     
  142.     public void MapSize(int w,int h){
  143.         setSize(w * 20+50 , h * 20+100 );
  144.         if(!ChessFrame.checkcomputer) {
  145.             ChessFrame.iscomputer=false;
  146.         } else {
  147.             ChessFrame.iscomputer=true;
  148.         }
  149.         mp.setModel(cm);
  150.         mp.repaint();
  151.     }
  152.     
  153.     public boolean getiscomputer(){
  154.         return ChessFrame.iscomputer;
  155.     }
  156.     
  157.     public void restart(){
  158.         int modeChess = cm.getModeChess();
  159.         if(modeChess <= 3 && modeChess >= 1){
  160.             cm = new ChessModel(modeChess);
  161.             MapSize(cm.getWidth(),cm.getHeight());
  162.         }else{
  163.             System.out.println("\u81EA\u5B9A\u4E49");
  164.         }
  165.     }
  166.     
  167.     public void actionPerformed(ActionEvent e){
  168.         String arg=e.getActionCommand();
  169.         try{
  170.             if (arg.equals("Windows"))
  171.                 UIManager.setLookAndFeel(
  172.                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  173.             else if(arg.equals("Motif"))
  174.                 UIManager.setLookAndFeel(
  175.                     "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
  176.             else
  177.                 UIManager.setLookAndFeel(
  178.                     "javax.swing.plaf.metal.MetalLookAndFeel" );
  179.             SwingUtilities.updateComponentTreeUI(this);
  180.         }catch(Exception ee){}
  181.         if(arg.equals("20x15")){
  182.             this.width=20;
  183.             this.height=15;
  184.             cm=new ChessModel(1);
  185.             MapSize(this.width,this.height);
  186.             SwingUtilities.updateComponentTreeUI(this);
  187.         }
  188.         if(arg.equals("30x20")){
  189.             this.width=30;
  190.             this.height=20;
  191.             cm=new ChessModel(2);
  192.             MapSize(this.width,this.height);
  193.             SwingUtilities.updateComponentTreeUI(this);
  194.         }
  195.         if(arg.equals("40x30")){
  196.             this.width=40;
  197.             this.height=30;
  198.             cm=new ChessModel(3);
  199.             MapSize(this.width,this.height);
  200.             SwingUtilities.updateComponentTreeUI(this);
  201.         }
  202.         if(arg.equals("人机对弈")){
  203.             this.checkcomputer=true;
  204.             this.iscomputer=true;
  205.             cm=new ChessModel(cm.getModeChess());
  206.             MapSize(cm.getWidth(),cm.getHeight());
  207.             SwingUtilities.updateComponentTreeUI(this);
  208.         }
  209.         if(arg.equals("人人对弈")){
  210.             this.checkcomputer=false;
  211.             this.iscomputer=false;
  212.             cm=new ChessModel(cm.getModeChess());
  213.             MapSize(cm.getWidth(),cm.getHeight());
  214.             SwingUtilities.updateComponentTreeUI(this);
  215.         }
  216.         if(arg.equals("开局")){
  217.             restart();
  218.         }
  219.         if(arg.equals("关于"))
  220.             JOptionPane.showMessageDialog(this"五子棋游戏测试版本""关于"0);
  221.         if(arg.equals("退出"))
  222.             System.exit(0);
  223.     }
  224. }
  225. /*
  226.  *类ChessModel实现了整个五子棋程序算法的核心
  227.  */
  228. class ChessModel {
  229.     //棋盘的宽度、高度、棋盘的模式(如20×15)
  230.     private int width,height,modeChess;
  231.     //棋盘方格的横向、纵向坐标
  232.     private int x=0,y=0;
  233.     //棋盘方格的横向、纵向坐标所对应的棋子颜色,
  234.     //数组arrMapShow只有3个值:1,2,3,-5,
  235.     //其中1代表该棋盘方格上下的棋子为黑子,
  236.     //2代表该棋盘方格上下的棋子为白子,
  237.     //3代表为该棋盘方格上没有棋子,
  238.     //-5代表该棋盘方格不能够下棋子
  239.     private int[][] arrMapShow;
  240.     //交换棋手的标识,棋盘方格上是否有棋子的标识符
  241.     private boolean isOdd,isExist;
  242.     public ChessModel() {}
  243.     
  244.     //该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘
  245.     public ChessModel(int modeChess){
  246.         this.isOdd=true;
  247.         if(modeChess == 1){
  248.             PanelInit(2015, modeChess);
  249.         }
  250.         if(modeChess == 2){
  251.             PanelInit(3020, modeChess);
  252.         }
  253.         if(modeChess == 3){
  254.             PanelInit(4030, modeChess);
  255.         }
  256.     }
  257.     
  258.     //按照棋盘模式构建棋盘大小
  259.     private void PanelInit(int width, int height, int modeChess){
  260.         this.width = width;
  261.         this.height = height;
  262.         this.modeChess = modeChess;
  263.         arrMapShow = new int[width+1][height+1];
  264.         for(int i = 0; i <= width; i++){
  265.             for(int j = 0; j <= height; j++){
  266.                 arrMapShow[i][j] = -5;
  267.             }
  268.         }
  269.     }
  270.     
  271.     //获取是否交换棋手的标识符
  272.     public boolean getisOdd(){
  273.         return this.isOdd;
  274.     }
  275.     //设置交换棋手的标识符
  276.     public void setisOdd(boolean isodd){
  277.         if(isodd)
  278.             this.isOdd=true;
  279.         else
  280.             this.isOdd=false;
  281.     }
  282.     
  283.     //获取某棋盘方格是否有棋子的标识值
  284.     public boolean getisExist(){
  285.         return this.isExist;
  286.     }
  287.     
  288.     //获取棋盘宽度
  289.     public int getWidth(){
  290.         return this.width;
  291.     }
  292.     
  293.     //获取棋盘高度
  294.     public int getHeight(){
  295.         return this.height;
  296.     }
  297.     
  298.     //获取棋盘模式
  299.     public int getModeChess(){
  300.         return this.modeChess;
  301.     }
  302.     //获取棋盘方格上棋子的信息
  303.     public int[][] getarrMapShow(){
  304.         return arrMapShow;
  305.     }
  306.     //判断下子的横向、纵向坐标是否越界
  307.     private boolean badxy(int x, int y){
  308.         if(x >= width+20 || x < 0)
  309.             return true;
  310.         return y >= height+20 || y < 0;
  311.     }
  312.     //计算棋盘上某一方格上八个方向棋子的最大值,
  313.     //这八个方向分别是:左、右、上、下、左上、左下、右上、右下
  314.     public boolean chessExist(int i,int j){
  315.         if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)
  316.             return true;
  317.         return false;
  318.     }
  319.     //判断该坐标位置是否可下棋子
  320.     public void readyplay(int x,int y){
  321.         if(badxy(x,y))
  322.             return;
  323.         if (chessExist(x,y))
  324.             return;
  325.         this.arrMapShow[x][y]=3;
  326.     }
  327.     //在该坐标位置下棋子
  328.     public void play(int x,int y){
  329.         if(badxy(x,y))
  330.             return;
  331.         if(chessExist(x,y)){
  332.             this.isExist=true;
  333.             return;
  334.         }else
  335.             this.isExist=false;
  336.         if(getisOdd()){
  337.             setisOdd(false);
  338.         this.arrMapShow[x][y]=1;
  339.         }else{
  340.             setisOdd(true);
  341.             this.arrMapShow[x][y]=2;
  342.         }
  343.     }
  344.     //计算机走棋
  345.     /*
  346.      *说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,
  347.      *最后得出棋子数最大值的坐标,下子
  348.      **/
  349.     public void computerDo(int width,int height){
  350.         int max_black,max_white,max_temp,max=0;
  351.         setisOdd(true);
  352.         System.out.println("计算机走棋 ...");
  353.         for(int i = 0; i <= width; i++){
  354.             for(int j = 0; j <= height; j++){
  355.                 if(!chessExist(i,j)){//算法判断是否下子
  356.                     max_white=checkMax(i,j,2);//判断白子的最大值
  357.                     max_black=checkMax(i,j,1);//判断黑子的最大值
  358.                     max_temp=Math.max(max_white,max_black);
  359.                     if(max_temp>max){
  360.                         max=max_temp;
  361.                         this.x=i;
  362.                         this.y=j;
  363.                     }
  364.                 }
  365.             }
  366.         }
  367.         setX(this.x);
  368.         setY(this.y);
  369.         this.arrMapShow[this.x][this.y]=2;
  370.     }
  371.     
  372.     //记录电脑下子后的横向坐标
  373.     public void setX(int x){
  374.         this.x=x;
  375.     }
  376.     
  377.     //记录电脑下子后的纵向坐标
  378.     public void setY(int y){
  379.         this.y=y;
  380.     }
  381.     
  382.     //获取电脑下子的横向坐标
  383.     public int getX(){
  384.         return this.x;
  385.     }
  386.     
  387.     //获取电脑下子的纵向坐标
  388.     public int getY(){
  389.         return this.y;
  390.     }
  391.     //计算棋盘上某一方格上八个方向棋子的最大值,
  392.     //这八个方向分别是:左、右、上、下、左上、左下、右上、右下
  393.     public int checkMax(int x, int y,int black_or_white){
  394.         int num=0,max_num,max_temp=0;
  395.         int x_temp=x,y_temp=y;
  396.         int x_temp1=x_temp,y_temp1=y_temp;
  397.         //judge right
  398.         for(int i=1;i<5;i++){
  399.             x_temp1+=1;
  400.             if(x_temp1>this.width)
  401.                 break;
  402.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  403.                 num++;
  404.             else
  405.                 break;
  406.         }
  407.         //judge left
  408.         x_temp1=x_temp;
  409.         for(int i=1;i<5;i++){
  410.             x_temp1-=1;
  411.             if(x_temp1<0)
  412.                 break;
  413.             if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)
  414.                 num++;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics