A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© joeywr 中级黑马   /  2015-8-22 12:37  /  677 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

There are two players to play a game with 4x4 positions.Each player can take one position for his turn,The winner is the fist one to take all of four positions in one line.
The figure shows three winning cases.
Let's make a simple application to make the judgment which player wins a game

  大家可以一起来做一做。。

评分

参与人数 1黑马币 +10 收起 理由
小朱 + 10 神马都是浮云

查看全部评分

11 个回复

倒序浏览
这是5子棋的原理吧,谁先4个子在一行就赢
回复 使用道具 举报
难为人木
回复 使用道具 举报
表示完全看不懂、、、
回复 使用道具 举报
我也完全看不懂,能中文翻译一下吗!!!
回复 使用道具 举报
大神吊炸天
回复 使用道具 举报
小朱 中级黑马 2015-8-22 17:14:22
7#
五子棋啊
回复 使用道具 举报
看见就晕  怎么做到的
回复 使用道具 举报
季995 中级黑马 2015-8-22 17:31:59
9#
什么意思?
回复 使用道具 举报
英文不好,表示看不懂
回复 使用道具 举报

嗯  
利用二维数组可以解决,每走一步,统计该坐标的水平、垂直、对角线是否连续达到4个。下面是一个简单的demo:
public class Game {

        private int[][] chessboard;
        private boolean gameover = false;
        private int winLength;
        private int current;
        private int total;
       
        /**
         * @param size 棋盘大小
         * @param winLength 赢的连续数量
         */
        public Game(int size,int winLength){
                if(size<1||size<winLength){
                        throw new RuntimeException("棋局参数错误!");
                }
                chessboard = new int[size][size];
                total = size*size;
                this.winLength = winLength;
        }
       
        public int step(int v,int h,int player){
                if(gameover||current==total){
                        System.out.println("游戏已经结束");
                        return 0;
                }
                if(chessboard[v][h]==0){
                        chessboard[v][h] = player;
                        current ++;
                        return judge(v, h, player);
                }
                return -1;
        }
       
        private int judge(int v,int h,int player){
                int count = 0;
                //判断水平方向是否满足4连
                count += statsHorizontal(v, player, -1, h-1);
                count += statsHorizontal(v, player, 1, h);
                //前面和后面的数量达到4,胜出
                if(count>=winLength){
                        win(count, player);
                        return count;
                }
                count = 0;
               
                //判断垂直方向是否满足4连
                count += statsVertical(h, player, -1, v-1);
                count += statsVertical(h, player, 1, v);
                if(count>=winLength){
                        win(count, player);
                        return count;
                }
                count = 0;
               
                //对角线4连 \方向
                count += statsDiagonal_1(v, h, player);
                if(count>=winLength){
                        win(count, player);
                        return count;
                }
                count = 0;
               
                //对角线4连 /方向
                count += statsDiagonal_2(v, h, player);
                if(count>=winLength){
                        win(count, player);
                        return count;
                }
               
                return count;
        }
       
        private void win(int count,int player){
                System.out.println("Player "+player+" win");
                gameover = true;
        }
       
        /**
         * 统计水平方向连续数量
         */
        private int statsHorizontal(int v, int player,int step,int position) {
                int count = 0;
                for(int i=position;i>=0&&i<chessboard[v].length;i+=step){
                        if (chessboard[v] == player) {
                                count++;
                                continue;
                        }
                        break;
                }
                return count;
        }
       
        /**
         * 统计垂直方向连续数量
         */
        private int statsVertical(int h,int player,int step,int position){
                int count = 0;
                for(int i=position;i>=0&&i<chessboard.length;i+=step){
                        if(chessboard[h]==player){
                                count++;
                                continue;
                        }
                        break;
                }
                return count;
        }
       
        private int statsDiagonal_1(int v,int h,int player){
                int count=0;
                for(int i=1;v-i>=0&&h-i>=0;i++){
                        if(chessboard[v-i][h-i] == player){
                                count++;
                                continue;
                        }
                        break;
                }
                for(int i=0;v+i<chessboard.length&&h+i<chessboard[v+i].length;i++){
                        if(chessboard[v+i][h+i] == player){
                                count++;
                                continue;
                        }
                        break;
                }
                return count;
        }
       
        private int statsDiagonal_2(int v,int h,int player){
                int count=0;
                for(int i=v;v+i<chessboard.length&&h-i>=0;i++){
                        if(chessboard[v+i][h-i] == player){
                                count++;
                                continue;
                        }
                        break;
                }
                for(int i=h;v-i>=0&&h+i<chessboard[v-i].length;i++){
                        if(chessboard[v-i][h+i] == player){
                                count++;
                                continue;
                        }
                        break;
                }
                return count;
        }
       
        public static void main(String[] args) {
                Game g = new Game(5,4);
                g.step(0, 0, 1);
                g.step(1, 1, 1);
                g.step(2, 2, 1);
                g.step(3, 3, 1);
        }
}
回复 使用道具 举报
不明觉厉啊。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马