您的位置:首页 > 编程语言 > Java开发

连连看游戏判断两个块是否可以消去,并计算最小转弯数

2015-05-19 15:09 387 查看
import java.util.Queue;

import java.util.concurrent.LinkedBlockingDeque;

public class lianliankan_core {

    //连连看布局

    private int[][] myArray = new int[][]{{0,0,0,0,0},

                                          {0,1,2,1,0},

                                          {0,2,0,5,0},

                                          {0,1,0,4,0},

                                          {0,4,5,1,0},

                                          {0,0,0,0,0}};

    //是否访问过标志

    private int[][] visited = new int[][]{{0,0,0,0,0},

                                          {0,0,0,0,0},

                                          {0,0,0,0,0},

                                          {0,0,0,0,0},

                                          {0,0,0,0,0},

                                          {0,0,0,0,0}};

    //方向左、右、下、上

    private Point[] direction = new Point[]{new Point(-1,0),

                                            new Point(1,0),

                                            new Point(0, -1),

                                            new Point(0, 1)};

    private Point p1;//起始点

    private Point p2;//终点

    //点的队列

    private Queue<Point> q1 = new LinkedBlockingDeque<Point>();

    

    public lianliankan_core(){

        //(x1,y1)=(4,2)

        p1 = new Point(4,2);

        //(x2,y2)=(2,3)

        p2 = new Point(2,3);

        

        visited[p1.x][p1.y] = 1;

        q1.add(p1);

    }

    

    //将point点可以直线到达的点放入队列q1中

    public void find(){

        while(!q1.isEmpty()){

            Point newPoint = (Point) q1.poll();

            for (Point p : direction){

                Point nextPoint = new Point(newPoint.x, newPoint.y, newPoint.turnCount);

4000

                nextPoint.plus(p);

                while (nextPoint.check()){

                    q1.add(new Point(nextPoint.x, nextPoint.y, nextPoint.turnCount + 1));

                    nextPoint.plus(p);

                }

                if(nextPoint.equals(p2) && nextPoint.turnCount <= 2){

                    if(myArray[nextPoint.x][nextPoint.y] == myArray[p2.x][p2.y]){

                        System.out.println("可以消去!");

                        System.out.println("最小转变数为:" + nextPoint.turnCount);

                        return;

                    }else{

                        System.out.println("不能消去!");

                        return;

                    }

                }

            }

        }

    }

    

    private class Point{

        private int x;

        private int y;

        private int turnCount;//转弯数

        

        public Point(int x, int y){

            this.x = x;

            this.y = y;

            this.turnCount = 0;

        }

        

        public Point(int x, int y, int turnCount){

            this.x = x;

            this.y = y;

            this.turnCount = turnCount;

        }

        

        public Point plus(Point p1){

            this.x += p1.x;

            this.y += p1.y;

            this.turnCount += p1.turnCount;

            return this;

        }

        

        public boolean check(){

            if(this.x >= 0 && this.x < 6 && this.y >= 0 && this.y < 5 && myArray[this.x][this.y] == 0 && visited[this.x][this.y] == 0){

                visited[this.x][this.y] = 1;

                return true;

            }

            return false;

        }

        

        public String toString(){

            return "Point(" + x + ", " + y +")";

        }

        

        public boolean equals(Point p){

            if(this.x == p.x && this.y == p.y)

                return true;

            return false;

        }

        

    }

    

    public static void main(String[] args){

        lianliankan_core llk = new lianliankan_core();

        llk.find();

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 编程之美
相关文章推荐