鱼C论坛

 找回密码
 立即注册
查看: 2154|回复: 0

[作品展示] java写的一个五子棋,求大佬们指点

[复制链接]
发表于 2020-10-9 19:45:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
棋盘类:
  1. public class Board {
  2.     protected static int rows;
  3.     protected static int cols;
  4.     protected static int size;
  5.     protected static int margin;

  6.     public static void main(String[] args){
  7.         Board.init(6,6,30,20);
  8.         CoordinateLogical c1 = new CoordinateLogical(2,3);
  9.         CoordinatePhysical c2 = new CoordinatePhysical(10,20);

  10.         CoordinateLogical c21 = Board.convert(c2);
  11.         CoordinatePhysical c11 = Board.convert(c1);

  12.         System.out.printf("(%d,%d)->(%d,%d)",c2.x,c2.y,c21.col,c21.row);
  13.         System.out.printf("(%d,%d)->(%d,%d)",c1.col,c1.row,c11.x,c11.y);
  14.     }

  15.     public static void init(int n1,int n2,int n3,int n4){
  16.         rows = n1;
  17.         cols = n2;
  18.         size = n3;
  19.         margin = n4;
  20.     }

  21.     /**
  22.      * 物理坐标转换为逻辑坐标
  23.      * @param c
  24.      * @return
  25.      */
  26.     public static CoordinateLogical convert(CoordinatePhysical c){
  27.         CoordinateLogical c1 = new CoordinateLogical();
  28.         c1.col = Math.round((c.x-margin)/(float)size);
  29.         c1.row = Math.round((c.y - margin)/(float)size);

  30.         return c1;
  31.     }

  32.     /**
  33.      * 逻辑坐标转换为物理坐标
  34.      * @param c
  35.      * @return
  36.      */
  37.     public static CoordinatePhysical convert(CoordinateLogical c){
  38.         CoordinatePhysical c1 = new CoordinatePhysical();
  39.         c1.x = margin + size * c.col;
  40.         c1.y = margin + size * c.row;
  41.         return c1;
  42.     }

  43.     public static int getWidth() {
  44.         return margin*2+size*(rows-1);
  45.     }

  46.     public static int getHeight() {
  47.         return margin*2+size*(cols-1);
  48.     }
  49. }
复制代码

棋局类:
  1. public class Chess {
  2.     int flag = 0;
  3.     public static int NONE = 0;//无子
  4.     public static int WHITE = 1;//白子
  5.     public static int BLACK = -1;//黑子

  6.     private int [][]chesses;//左右落点的状态
  7.     private int turn;//当前轮到白方(1)/黑方(-1)落子

  8.     public Chess(int rows,int cols){

  9.         //创建所有的落点
  10.         chesses = new int[rows][cols];

  11.         //初始化每个落点为无子
  12.         for (int row = 0; row < rows; row++) {
  13.             for (int col = 0; col < cols; col++) {
  14.                 chesses[row][col] = NONE;
  15.             }
  16.         }

  17.         //黑方落子
  18.         turn = BLACK;
  19.     }

  20.     /**
  21.      * 判断某个位置是否有棋子
  22.      * @param c 逻辑位置
  23.      * @return 是否由棋子
  24.      */
  25.     public boolean exist(CoordinateLogical c){

  26.         return chesses[c.row][c.col] != NONE;
  27.     }

  28.     /**
  29.      * 落子
  30.      * @param c 落子位置
  31.      * @return 落子颜色
  32.      */
  33.     public int add(CoordinateLogical c){
  34.         int color = turn;
  35.         chesses[c.row][c.col] = color;
  36.         turn = - turn;
  37.         return color;
  38.     }

  39.     public int isWin1(CoordinateLogical c){
  40.         int color = chesses[c.row][c.col];
  41.         //垂直方向
  42.         if(extend(c,0)+extend(c,4)>=4){
  43.             return color;
  44.         }
  45.         //负对角线方向
  46.         if(extend(c,1)+extend(c,5)>=4){
  47.             return color;
  48.         }
  49.         //水平方向
  50.         if(extend(c,2)+extend(c,6)>=4){
  51.             return color;
  52.         }
  53.         //主对角线方向
  54.         if(extend(c,3)+extend(c,7)>=4){
  55.             return color;
  56.         }
  57.         return 0;
  58.     }

  59.     /**
  60.      * 从指定位置往某个方向扩展
  61.      * @param c 位置
  62.      * @param direction 方向 0:上 1:右上 2:右 3:右下 4:下 5:左下 6:左 7:左上
  63.      * @return
  64.      */
  65.     private int extend(CoordinateLogical c,int direction){
  66.         int count = 0;
  67.         int row = c.row;
  68.         int col = c.col;
  69.         int color = chesses[row][col];

  70.         //一个方向拓展的次数不可能超过4
  71.         for (int i = 0; i < 4; i++) {
  72.             switch (direction){
  73.                 case 0:
  74.                     row--;
  75.                     break;
  76.                 case 1:
  77.                     row--;
  78.                     col++;
  79.                     break;
  80.                 case 2:
  81.                     col++;
  82.                     break;
  83.                 case 3:
  84.                     row++;
  85.                     col++;
  86.                     break;
  87.                 case 4:
  88.                     row++;
  89.                     break;
  90.                 case 5:
  91.                     row++;
  92.                     col--;
  93.                     break;
  94.                 case 6:
  95.                     col--;
  96.                     break;
  97.                 case 7:
  98.                     col--;
  99.                     row--;
  100.                     break;
  101.             }
  102.             if(row<0||row>=Board.rows||col<0||col>=Board.cols){
  103.                 break;
  104.             }
  105.             if(chesses[row][col]==color){
  106.                 count++;
  107.             }else{
  108.                 break;
  109.             }
  110.         }
  111.         return count;

  112.     }

  113.     /**
  114.      * 判断在指定位置的周围是否形成了五个以上同色的棋子
  115.      * @param p 位置
  116.      * @return
  117.      */
  118.     public int isWin(CoordinateLogical p){

  119.         if((count(p,0,-1,-turn)+count(p,0,1,-turn))-1>=5)
  120.         {
  121.             return -turn;
  122.         }
  123.         if((count(p,1,0,-turn)+count(p,-1,0,-turn))-1>=5)
  124.         {
  125.             return -turn;
  126.         }
  127.         if((count(p,1,-1,-turn)+count(p,-1,1,-turn))-1>=5){
  128.             return -turn;
  129.         }
  130.         if((count(p,-1,-1,-turn)+count(p,1,1,-turn))-1>=5){
  131.             return -turn;
  132.         }

  133.      return 0;
  134.     }

  135.     private int count(CoordinateLogical p,int movX,int movY,int turn){
  136.         CoordinateLogical p1 = new CoordinateLogical(0,0);

  137.             if (chesses[p.row][ p.col] == turn) {
  138.                 if ((p.row < 0 || p.row >= Board.rows) || p.col < 0 || p.col >= Board.cols) {
  139.                     return 0;
  140.                 }
  141.                 p1.col = p.col + movX;
  142.                 p1.row = p.row + movY;
  143.                 return 1+count(p1,movX,movY,turn);

  144.             } else {
  145.                 return 0;
  146.             }


  147.     }
  148. }
复制代码

逻辑坐标类
  1. //逻辑坐标
  2. public class CoordinateLogical {
  3.     public int col;
  4.     public int row;

  5.     public CoordinateLogical(int col,int row){
  6.         this.col = col;
  7.         this.row = row;
  8.     }

  9.     public CoordinateLogical(){
  10.         this.col = 0;
  11.         this.row = 0;
  12.     }
  13. }
复制代码

物理坐标类:
  1. //物理坐标
  2. public class CoordinatePhysical {
  3.     public int x;
  4.     public int y;

  5.     public CoordinatePhysical(int x,int y){
  6.         this.x = x;
  7.         this.y = y;
  8.     }
  9.     public CoordinatePhysical(){
  10.         this.x = 0;
  11.         this.y = 0;
  12.     }
  13. }
复制代码

游戏控制类:
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.MouseAdapter;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.WindowAdapter;
  6. import java.awt.event.WindowEvent;

  7. public class Game {
  8.     private static Frame frame;//窗体
  9.     private static Canvas canvas;//画布
  10.     private static Graphics graphics;//绘图对象
  11.     private static Button button;//按钮


  12.     private static Chess chess;

  13.     /**
  14.      * 构造函数
  15.      *
  16.      * @param rows
  17.      * @param cols
  18.      * @param size
  19.      * @param margin
  20.      */
  21.     public Game(int rows, int cols, int size, int margin) {
  22.         Board.init(rows, cols, size, margin);
  23.         chess = new Chess(rows, cols);
  24.     }

  25.     public static void main(String[] args) {
  26.         Game game = new Game(19, 19, 30, 20);

  27.         //初始化窗体
  28.         int width = Board.getWidth();
  29.         int height = Board.getHeight() + 100;
  30.         frame = new Frame("我的五子棋");
  31.         frame.setSize(width, height);
  32.         frame.setLayout(new FlowLayout());
  33.         //关闭窗口
  34.         frame.addWindowListener(new WindowAdapter(){
  35.             public void windowClosing(WindowEvent e) {
  36.                 System.exit(0);//退出
  37.             }});
  38.         //准备画布
  39.         canvas = new Canvas();
  40.         canvas.setSize(width, height);
  41.         frame.add(canvas);

  42.         button = new Button("Start");
  43.         button.setSize(100, 40);
  44.         frame.add(button);


  45.         button.addMouseListener(new MouseAdapter() {
  46.             @Override
  47.             public void mouseClicked(MouseEvent mouseEvent) {
  48.                 graphics = canvas.getGraphics();
  49.                 drawBoard();
  50.                 super.mouseClicked(mouseEvent);
  51.             }
  52.         });

  53.         //鼠标响应事件
  54.         canvas.addMouseListener(new MouseAdapter() {
  55.             @Override
  56.             public void mouseClicked(MouseEvent e) {
  57.                 CoordinatePhysical c = new CoordinatePhysical(e.getX(), e.getY());
  58.                 int result = handle(c);
  59.                 if (result != Chess.NONE) {
  60.                     String actor = result==Chess.BLACK?"黑方":"白方";
  61.                     String msg = String.format("恭喜%s获胜",actor);
  62.                     JOptionPane.showMessageDialog(null, msg, "游戏结束", JOptionPane.OK_CANCEL_OPTION, null);
  63.                     //System.exit(1);
  64.                     graphics = canvas.getGraphics();
  65.                     drawBoard();
  66.                 }

  67.                 super.mouseClicked(e);
  68.             }
  69.         });

  70.         frame.setVisible(true);//显示窗体

  71.     }


  72.     /**
  73.      * 画棋盘
  74.      */
  75.     public static void drawBoard(){

  76.         graphics.setColor(Color.ORANGE);
  77.         graphics.fillRect(0,0,Board.getWidth(),Board.getHeight());
  78.         //绘制纬线
  79.         graphics.setColor(Color.BLACK);
  80.         int x1 = Board.margin;
  81.         int y1 = Board.margin;
  82.         int x2 = x1+Board.size*(Board.cols-1);
  83.         int y2 = y1;
  84.         for (int i = 0; i < Board.rows; i++) {
  85.             graphics.drawLine(x1,y1,x2,y2);
  86.             y1 += Board.size;
  87.             y2 += Board.size;
  88.         }

  89.         //绘制经线
  90.         x1 = Board.margin;
  91.         y1 = Board.margin;
  92.         x2 = x1;
  93.         y2 = y1+Board.size*(Board.rows-1);
  94.         for (int i = 0; i < Board.rows; i++) {
  95.             graphics.drawLine(x1,y1,x2,y2);
  96.             x1 += Board.size;
  97.             x2 += Board.size;
  98.         }

  99.     }

  100.     /**
  101.      * 画棋子
  102.      * @param c 坐标
  103.      * @param color 颜色
  104.      */
  105.     public static void drawChess(CoordinateLogical c,int color){
  106.         int r = (int)(Board.size*0.3);

  107.         CoordinatePhysical c1 = Board.convert(c);
  108.         int x = c1.x - r;
  109.         int y = c1.y - r;
  110.         int width = 2*r;
  111.         int height = 2*r;
  112.         graphics.setColor(color==-1?Color.BLACK:Color.WHITE);
  113.         graphics.fillArc(x,y,width,height,0,360);
  114.         graphics.drawArc(x,y,width,height,0,360);
  115.     }

  116.     /**
  117.      * 在棋盘上单击的逻辑处理
  118.      * @param c 单击的位置
  119.      * @return 输赢结果
  120.      */
  121.     public static int handle(CoordinatePhysical c){
  122.         int result;
  123.         CoordinateLogical c1 = Board.convert(c);

  124.         //如果该位置已经有棋子,则直接忽略本次操作
  125.         if(chess.exist(c1)){
  126.             return Chess.NONE;
  127.         }


  128.         int color = chess.add(c1);
  129.         drawChess(c1,color);
  130.         result = chess.isWin1(c1);


  131.         return result;
  132.     }
  133. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-27 18:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表