黑马程序员技术交流社区

标题: 一道java面试题 [打印本页]

作者: joeywr    时间: 2015-8-22 12:37
标题: 一道java面试题
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

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

作者: SYAD    时间: 2015-8-22 13:47
这是5子棋的原理吧,谁先4个子在一行就赢
作者: 放学后来我家    时间: 2015-8-22 14:14
难为人木
作者: 三分之一    时间: 2015-8-22 14:20
表示完全看不懂、、、
作者: taojunqiu225    时间: 2015-8-22 16:32
我也完全看不懂,能中文翻译一下吗!!!
作者: 以梦为马123    时间: 2015-8-22 16:43
大神吊炸天
作者: 小朱    时间: 2015-8-22 17:14
五子棋啊
作者: 收获远眺    时间: 2015-8-22 17:26
看见就晕  怎么做到的
作者: 季995    时间: 2015-8-22 17:31
什么意思?
作者: AndyYuan    时间: 2015-8-22 18:21
英文不好,表示看不懂
作者: joeywr    时间: 2015-8-23 22:41
小朱 发表于 2015-8-22 17:14
五子棋啊

嗯  
利用二维数组可以解决,每走一步,统计该坐标的水平、垂直、对角线是否连续达到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);
        }
}
作者: 欲,再梦一回    时间: 2015-8-23 22:44
不明觉厉啊。。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2