• Java实现五子棋


    代码:

    package com.hotusm.datastructure.list;
    
    import com.hotusm.datastructure.Log;
    
    /**
     * @author luqibao
     * @date 2017/3/17
     */
    public class Chess {
    
        public static final int WHITE = 1; //白棋
        public static final int BLACK = 2; //黑棋
    
    
        private int[][] chessboard;
        private int xSize;
        private int ySize;
    
        public Chess(int size) {
            chessboard = new int[size][size];
            xSize = size;
            ySize = size;
        }
    
        public Chess() {
            chessboard = new int[16][16];
            xSize = 16;
            ySize = 16;
        }
    
        public void play(int x, int y, int who) {
    
            if (x > xSize || y > ySize) {
                Log.warn("超出范围!");
                return;
            }
            if (chessboard[x][y] != 0) {
                Log.warn("该位置不能下");
                return;
            }
            chessboard[x][y] = who;
            if (isSuccess()) {
                Log.info("who:" + who + "已经获胜");
            }
        }
    
        public boolean isSuccess() {
            //x轴
            for (int i = 0; i < ySize; i++) {
                for (int j = 0; j < xSize; j++) {
                    if (xSize - j < 5) {
                        break;
                    }
                    if (arriSSame(chessboard[i], j, j + 5)) {
                        return true;
                    }
                }
            }
            //y轴
            for (int i = 0; i < xSize; i++) {
                for (int j = 0; j < ySize; j++) {
                    if (ySize - j < 5) {
                        break;
                    }
                    if (arriSSame(copyY2X(chessboard, i), j, j + 5)) {
                        return true;
                    }
                }
            }
            //交叉
            if (isCrossSame()) {
                return true;
            }
            return false;
        }
    
        /**
         * 将数组的Y列数据拷贝到一维数组中去
         *
         * @param arr
         * @param y
         * @return
         */
        private int[] copyY2X(int[][] arr, int y) {
            int[] nArr = new int[ySize];
            for (int i = 0; i < ySize; i++) {
                nArr[i] = arr[i][y];
            }
            return nArr;
        }
    
        /**
         * 是否有5个相同
         *
         * @param arr
         * @param x1
         * @param x2
         * @return
         */
        private boolean arriSSame(int[] arr, int x1, int x2) {
            int sameNum = 1;
            for (int i = x1; i < x2 - 1; i++) {
                if (arr[i] == arr[i + 1] && arr[i] != 0) {
                    sameNum++;
                    if (sameNum == 5) {
                        return true;
                    }
                } else {
                    sameNum = 1;
                }
            }
            return false;
        }
    
        /**
         * 交叉情况下是否有5个相同
         *
         * @return
         */
        private boolean isCrossSame() {
            if (cross(ySize, "Y")) {
                return true;
            }
            if (cross(xSize, "X")) {
                return true;
            }
            for (int x = 5; x < xSize; x++) {
                int sameNum = 1;
                for (int i = x - 1, j = 0; i > 0; i--, j++) {
                    if (chessboard[j][i] == chessboard[j + 1][i - 1] && chessboard[j][i] != 0) {
                        sameNum++;
                        if (sameNum == 5) {
                            return true;
                        }
                    } else {
                        sameNum = 1;
                    }
                }
            }
            for (int y = ySize - 4; y > 0; y++) {
                int sameNum = 1;
                for (int i = y - 1, j = xSize - 1; i < ySize - 1; i++, j--) {
                    if (chessboard[i][j] == chessboard[i + 1][j - 1] && chessboard[i][j] != 0) {
                        sameNum++;
                        if (sameNum == 5) {
                            return true;
                        }
                    } else {
                        sameNum = 1;
                    }
                }
            }
    
            return false;
        }
    
        /**
         * @param xSize
         * @param flag  X 或者Y 表示X轴或者是Y轴
         * @return
         */
        private boolean cross(int xSize, String flag) {
            for (int y = xSize - 4; y > 0; y--) {
                int sameNum = 1;
                //y-1 为坐标
                for (int i = y - 1, j = 0; i < xSize - 1; i++, j++) {
                    if (flag.equals("X")) {
                        if (chessboard[j][i] == chessboard[j + 1][i + 1] && chessboard[j][i] != 0) {
                            sameNum++;
                            if (sameNum == 5) {
                                return true;
                            }
                        } else {
                            sameNum = 1;
                        }
                    } else {
                        if (chessboard[i][j] == chessboard[i + 1][j + 1] && chessboard[i][j] != 0) {
                            sameNum++;
                            if (sameNum == 5) {
                                return true;
                            }
                        } else {
                            sameNum = 1;
                        }
                    }
                }
            }
    
            return false;
        }
    }
  • 相关阅读:
    python中namedtuple介绍
    导入mysql数据的时候提示Field * doesn't have a default value解决方法
    Django中多表查询思路
    使用questionsModel.values()后不能获取模型中的属性对应的外键属性值的解决方式
    使用django UWSGI 出现 Bad Request (400)
    reverse函数实现指定页面跳转
    Model中内部类meta详解
    models中的pk主键用法
    binlog的几种复制形式
    Mysqldump参数大全(参数来源于mysql5.5.19源码)
  • 原文地址:https://www.cnblogs.com/zr520/p/6568336.html
Copyright © 2020-2023  润新知