欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现模拟消灭星星(控制台版)

程序员文章站 2024-01-27 14:01:22
...

消灭星星java控制台版实现

最近喜欢玩消灭星星,打算自己弄一个出来,为了验证可能性,所以先弄了一个控制台版的来测试一下

消灭星星逻辑主要有这几个

  1. 随机生成星星
  2. 找到所有与选中的星星相邻且颜色相同的其他星星
  3. 消去所有相连的星星
  4. 如消去的星星上方有其他星星,则需要下沉
  5. 算分

话不多说,直接上代码(注:下面说的坐标是相对于二维数组来说的

import java.util.*;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class Main {
    /*
     * 1.红色
     * 2.蓝色
     * 3.黄色
     * 4.绿色
     * 模拟星星颜色
     * */
    //定义一个二维数组(主界面)
    private int [][] forms ;

    //总分
    private int totalPoint = 0;

    //定义控制台输入变量
    static Scanner scanner = new Scanner(System.in);

    //定义一个map,临时存放与目标点相连的点
    Map<Integer,Integer> map ;

    //存放目标点的所有值相同且相连的点的映射集合(对比作用,避免重复存放,死循环)
    MultiValueMap<Integer,Integer> totalMap ;

    //存放目标点的所有值相同且相连的点
    Stack<Map<Integer,Integer>> stack;

    //统计相连个数(用来计分)
    static int sum =0;

    //初始化(不传参数默认是10*10)
    public Main(){
        forms = new int[10][10];
        map = new HashMap<>();
        totalMap = new LinkedMultiValueMap<>();
        stack = new Stack<>();
        for (int i = 0;i<forms.length;i++){
            for (int j = 0;j<forms[i].length;j++){
                forms[i][j] = (int)(Math.random()*4+1);
            }
        }
    }
    //初始化
    public Main(int x,int y ){
        forms = new int[x][y];
        map = new HashMap<>();
        totalMap = new LinkedMultiValueMap<>();
        stack = new Stack<>();
        for (int i = 0;i<forms.length;i++){
            for (int j = 0;j<forms[i].length;j++){
                forms[i][j] = (int)(Math.random()*4+1);
            }
        }
    }

    //判断一个点的相连点的值是否相同
   private boolean judgeValue(int[][] arr,int x,int y){
        //0不判断,0代表消去的点
        if (arr[x][y] == 0)
            return false;
        //坐标原点
        if (x==0 && y==0){
            //跟下点比较
            if (arr[x][y] == arr[x+1][y]){
                if (totalMap.containsKey(x+1)){
                    if (!judgeMap(x+1,y)){
                        totalMap.add(x+1,y);
                        map.put(x+1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x+1,y);
                    map.put(x+1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            //跟右点比较
            if (arr[x][y] == arr[x][y+1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y+1)){
                        totalMap.add(x,y+1);
                        map.put(x,y+1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y+1);
                    map.put(x,y+1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //坐标末端点
        else if (x==arr.length-1 && y==arr.length-1){
            //跟上点比较
            if (arr[x][y] == arr[x-1][y]){
                if (totalMap.containsKey(x-1)){
                    if (!judgeMap(x-1,y)){
                        totalMap.add(x-1,y);
                        map.put(x-1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x-1,y);
                    map.put(x-1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            //跟左点比较
            if (arr[x][y] == arr[x][y-1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y-1)){
                        totalMap.add(x,y-1);
                        map.put(x,y-1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y-1);
                    map.put(x,y-1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //右上端点
        else if ( x==0 && y==arr.length-1 ){
            if (arr[x][y] == arr[x][y-1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y-1)){
                        totalMap.add(x,y-1);
                        map.put(x,y-1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y-1);
                    map.put(x,y-1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x+1][y]){
                if (totalMap.containsKey(x+1)){
                    if (!judgeMap(x+1,y)){
                        totalMap.add(x+1,y);
                        map.put(x+1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x+1,y);
                    map.put(x+1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //左下端点
        else if (x==arr.length-1 && y==0){
            if (arr[x][y] == arr[x-1][y]){
                if (totalMap.containsKey(x-1)){
                    if (!judgeMap(x-1,y)){
                        totalMap.add(x-1,y);
                        map.put(x-1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x-1,y);
                    map.put(x-1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y+1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y+1)){
                        totalMap.add(x,y+1);
                        map.put(x,y+1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y+1);
                    map.put(x,y+1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //左边缘点(除了两端)
        else if (y==0){
            if (arr[x][y] == arr[x+1][y]){
                if (totalMap.containsKey(x+1)){
                    if (!judgeMap(x+1,y)){
                        totalMap.add(x+1,y);
                        map.put(x+1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x+1,y);
                    map.put(x+1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x-1][y]){
                if (totalMap.containsKey(x-1)){
                    if (!judgeMap(x-1,y)){
                        totalMap.add(x-1,y);
                        map.put(x-1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x-1,y);
                    map.put(x-1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y+1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y+1)){
                        totalMap.add(x,y+1);
                        map.put(x,y+1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y+1);
                    map.put(x,y+1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //上边缘点(除了两端)
        else if (x==0){
            if (arr[x][y] == arr[x][y-1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y-1)){
                        totalMap.add(x,y-1);
                        map.put(x,y-1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y-1);
                    map.put(x,y-1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y+1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y+1)){
                        totalMap.add(x,y+1);
                        map.put(x,y+1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y+1);
                    map.put(x,y+1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x+1][y]){
                if (totalMap.containsKey(x+1)){
                    if (!judgeMap(x+1,y)){
                        totalMap.add(x+1,y);
                        map.put(x+1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x+1,y);
                    map.put(x+1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //下边缘点(除了两端)
        else if (x==arr.length-1){
            if (arr[x][y] == arr[x][y-1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y-1)){
                        totalMap.add(x,y-1);
                        map.put(x,y-1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y-1);
                    map.put(x,y-1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y+1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y+1)){
                        totalMap.add(x,y+1);
                        map.put(x,y+1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y+1);
                    map.put(x,y+1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x-1][y]){
                if (totalMap.containsKey(x-1)){
                    if (!judgeMap(x-1,y)){
                        totalMap.add(x-1,y);
                        map.put(x-1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x-1,y);
                    map.put(x-1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //右边缘点(除了两端)
        else if (y==arr.length-1){
            if (arr[x][y] == arr[x-1][y]){
                if (totalMap.containsKey(x-1)){
                    if (!judgeMap(x-1,y)){
                        totalMap.add(x-1,y);
                        map.put(x-1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x-1,y);
                    map.put(x-1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x+1][y]){
                if (totalMap.containsKey(x+1)){
                    if (!judgeMap(x+1,y)){
                        totalMap.add(x+1,y);
                        map.put(x+1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x+1,y);
                    map.put(x+1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y-1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y-1)){
                        totalMap.add(x,y-1);
                        map.put(x,y-1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y-1);
                    map.put(x,y-1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //除去四周的点
        else {
            if (arr[x][y] == arr[x-1][y]){
                if (totalMap.containsKey(x-1)){
                    if (!judgeMap(x-1,y)){
                        totalMap.add(x-1,y);
                        map.put(x-1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x-1,y);
                    map.put(x-1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x+1][y]){
                if (totalMap.containsKey(x+1)){
                    if (!judgeMap(x+1,y)){
                        totalMap.add(x+1,y);
                        map.put(x+1,y);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x+1,y);
                    map.put(x+1,y);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y-1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y-1)){
                        totalMap.add(x,y-1);
                        map.put(x,y-1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y-1);
                    map.put(x,y-1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
            if (arr[x][y] == arr[x][y+1]){
                if (totalMap.containsKey(x)){
                    if (!judgeMap(x,y+1)){
                        totalMap.add(x,y+1);
                        map.put(x,y+1);
                        stack.push(map);
                        map = new HashMap<>();
                    }
                }
                else {
                    totalMap.add(x,y+1);
                    map.put(x,y+1);
                    stack.push(map);
                    map = new HashMap<>();
                }
            }
        }
        //若存在此点,返回true
        if (!stack.isEmpty()){
            return true;
        }
        return false;
    }

    //判断totalMap中的某个key是否包含某个value
    public boolean judgeMap(int key,int value){
        List<Integer> list = totalMap.get(key);
        for (int i = 0;i<list.size();i++){
            if (list.get(i)==value)
                return true;
        }
        return false;
    }

    //消去的点的值全部换成0 (代表当前值已消去)
    public static void valueChangeZeo(int[][] arr,MultiValueMap<Integer,Integer> totalMap){
        Set<Integer> keySet =totalMap.keySet();
        //换0操作
        for (int key :keySet){
            List<Integer>list =totalMap.get(key);
            for (int i =0;i<list.size();i++){
                arr[key][list.get(i)] = 0;
            }
        }
    }

    //模拟消去0(下沉的过程)
    public static void deleteZeo(int[][] arr,MultiValueMap<Integer,Integer> totalMap){
        Set<Integer> keySet =totalMap.keySet();
        int[]sort = new int[keySet.size()];
        //取出key的值,即横坐标的值
        Iterator<Integer> iterator = keySet.iterator();
        for(int i =0;iterator.hasNext();i++){
            sort[i] = iterator.next();
        }
        //排序(方便下沉交换值)
        Arrays.sort(sort);
        //下沉操作(值与0交换位置)
        for (int i = 0;i<sort.length;i++){
            List<Integer>list =totalMap.get(sort[i]);
            for (int j = 0;j<list.size();j++){
                int status = sort[i];
                while (status-1>=0){
                    if (arr[status-1][list.get(j)]!=0){
                        int temp=0;
                        temp = arr[status][list.get(j)];
                        arr[status][list.get(j)] = arr[status-1][list.get(j)];
                        arr[status-1][list.get(j)] = temp;
                    }
                    status--;
                }
            }
        }
    }
    //返回二维数组中与点(x,y)值相同的点的总个数sum
    public int connectedValue(int[][] arr,int x,int y){
        //递归结束条件
        if (!judgeValue(arr,x,y))
            return 0;
        Map<Integer,Integer> methodMap = stack.pop();
        for (int key:methodMap.keySet()){
            x = key;
            y = methodMap.get(key);
            sum++;
            //递归查找相连的点
            connectedValue(arr,x,y);
    }
        return sum;
    }

    //打印二维数组
    public static void soutArrays(int [][]arrs){
        for (int i = 0;i<arrs.length;i++) {
            for (int j = 0; j <arrs[i].length; j++)
                System.out.print(arrs[i][j]+"  ");
            System.out.println();
        }
    }

    //打印Map中的点
    public static void soutPoint(MultiValueMap<Integer,Integer> totalMap){
        Set<Integer> keySet =totalMap.keySet();
        for (int key :keySet){
            List<Integer>list =totalMap.get(key);
            for (int i =0;i<list.size();i++)
                System.out.print("点:"+key+" "+list.get(i)+" ");
            System.out.println();
        }
    }

    //判断目前是否存在相连点(不存在:游戏结束)
    public boolean existConnectionPoint(int[][] arr){
        for (int i =0;i<arr.length;i++){
            for (int j = 0;j<arr[i].length;j++){
                if (arr[i][j] != 0){
                    if (judgeValue(arr,i,j))
                        return true;
                }
            }
        }
        return false;
    }

    //重置所有变量
    private void reset(){
        totalMap = new LinkedMultiValueMap<>();
        sum = 0;
        stack = new Stack<>();
    }

    //计分
    public int calculatePoint(int num){
        //参考消灭星星的计分方法
        return 5*num*num;
    }
    public static void main(String[] args) {
        System.out.println("***********************");
        System.out.println("欢迎来到消灭数字游戏");
        System.out.println("***********************");
        //接收输入的坐标
        int x,y;
        Main main = new Main(10,10);
        //显示二维数组
        soutArrays(main.forms);
        //如果存在相连接的点则继续游戏(否则游戏结束)
        while (main.existConnectionPoint(main.forms)){
            //existConnectionPoint()方法会让stack等有值,需要重置
            main.reset();
            //设置边界
            int border = main.forms.length-1;
            System.out.println("请输入你所选择的点的坐标(x,y) ps:坐标大小为 0-"+border);
            x = scanner.nextInt();
            y = scanner.nextInt();

            //x,y值不合法,重新输入
            while (x <0 || y<0 || x>=main.forms.length || y>=main.forms.length){
                System.out.println("输入的坐标不合法,请重新输入:");
                x = scanner.nextInt();
                y = scanner.nextInt();
            }
            System.out.println("共有"+main.connectedValue(main.forms, x, y)+"个点与("+x+","+y+")相连且值相等(包含本身)");

            //打印相连的点
            soutPoint(main.totalMap);

            System.out.println("所得分数:"+main.calculatePoint(sum));
            System.out.println();

            //计算总分
            main.totalPoint+=main.calculatePoint(sum);
            System.out.println("当前界面:(0为已消去的点)"+" 总分:" + main.totalPoint);
            //值换0
            valueChangeZeo(main.forms,main.totalMap);
            //下沉(消去0)
            deleteZeo(main.forms,main.totalMap);
            //展示二维数组
            soutArrays(main.forms);
            //重置
            main.reset();
        }
        System.out.println();
        System.out.println("当前没有相连的点!!!");
        System.out.println("GAME OVER! YOUR SCORE IS :"+ main.totalPoint);
    }
}

代码要用到MultiValueMap这个类,所以要添加一个Spring的核心包 spring-core-3.2.0.RC2.jar

下面是演示结果

游戏开始:
java实现模拟消灭星星(控制台版)
输入x和y的值
java实现模拟消灭星星(控制台版)
游戏结束时候
java实现模拟消灭星星(控制台版)

总体上来说逻辑方面没有什么问题(暂时还没发现),细节方面有些处理的不太好,毕竟这个是练手版的。大家如果有什么建议或者优化的地方可以多多提出。

PS:递归找相连的点有没有更好的方法?

相关标签: java 消灭星星