简易的推箱子游戏

推箱子

package Demo;

import java.util.*;

public class Learn {
    // 游戏地图
    public static char[][] map = {
        {'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'},
        {'H', '&', ' ', 'H', ' ', ' ', ' ', ' ', ' ', 'H'},
        {'H', ' ', 'o', 'H', ' ', 'H', ' ', ' ', ' ', 'H'},
        {'H', ' ', ' ', 'H', ' ', 'H', 'H', ' ', 'H', 'H'},
        {'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'H', 'H'},
        {'H', ' ', ' ', ' ', 'H', 'H', 'H', ' ', ' ', 'H'},
        {'H', ' ', ' ', ' ', 'H', '*', ' ', ' ', ' ', 'H'},
        {'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'}
    };
    // 玩家位置
    static int playerRow = 1;
    static int playerCol = 1;
    // 箱子位置
    static int boxRow = 2;
    static int boxCol = 2;
    // 终点位置
    static final int destinRow = 6;
    static final int destinCol = 5;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(true) {
            // 打印当前游戏地图
            for(int i = 0; i < 8; i++) {
                for(int j = 0; j < 10; j++) {
                    System.out.print(map[i][j] + " ");
                }
                System.out.println();
            }
            System.out.println();

            System.out.println("请输入W/A/S/D来移动人物&");
            String option = sc.next();

            // 玩家操作
            switch(option) {
                case "W":
                    // 判断玩家是否撞墙
                    if(map[playerRow - 1][playerCol] != 'H') {
                        // 判断是否推箱
                        if(map[playerRow - 1][playerCol] == 'o') {
                            // 判断箱子是否撞墙
                            if(map[playerRow - 2][playerCol] != 'H') {
                                // 箱子不撞墙结果
                                map[playerRow][playerCol] = ' ';
                                map[playerRow - 1][playerCol] = '&';
                                map[playerRow - 2][playerCol] = 'o';

                                playerRow--;
                                boxRow--;
                            } else {
                                // 箱子撞墙结果
                                System.out.println("无法移动!");
                            }
                        } else {
                            // 不推箱移动结果
                            map[playerRow][playerCol] = ' ';
                            map[playerRow - 1][playerCol] = '&';

                            playerRow--;
                        }
                    } else {
                        // 玩家撞墙结果
                        System.out.println("无法移动!");
                    }
                    break;
                case "A":
                    // 判断玩家是否撞墙
                    if(map[playerRow][playerCol - 1] != 'H') {
                        // 判断是否推箱
                        if(map[playerRow][playerCol - 1] == 'o') {
                            // 判断箱子是否撞墙
                            if(map[playerRow][playerCol - 2] != 'H') {
                                // 箱子不撞墙结果
                                map[playerRow][playerCol] = ' ';
                                map[playerRow][playerCol - 1] = '&';
                                map[playerRow][playerCol - 2] = 'o';

                                playerCol--;
                                boxCol--;
                            } else {
                                // 箱子撞墙结果
                                System.out.println("无法移动!");
                            }
                        } else {
                            // 不推箱移动结果
                            map[playerRow][playerCol] = ' ';
                            map[playerRow][playerCol - 1] = '&';

                            playerCol--;
                        }
                    } else {
                        // 玩家撞墙结果
                        System.out.println("无法移动!");
                    }
                    break;
                case "S":
                    // 判断玩家是否撞墙
                    if(map[playerRow + 1][playerCol] != 'H') {
                        // 判断是否推箱
                        if(map[playerRow + 1][playerCol] == 'o') {
                            // 判断箱子是否撞墙
                            if(map[playerRow + 2][playerCol] != 'H') {
                                // 箱子不撞墙结果
                                map[playerRow][playerCol] = ' ';
                                map[playerRow + 1][playerCol] = '&';
                                map[playerRow + 2][playerCol] = 'o';

                                playerRow++;
                                boxRow++;
                            } else {
                                // 箱子撞墙结果
                                System.out.println("无法移动!");
                            }
                        } else {
                            // 不推箱移动结果
                            map[playerRow][playerCol] = ' ';
                            map[playerRow + 1][playerCol] = '&';

                            playerRow++;
                        }
                    } else {
                        // 玩家撞墙结果
                        System.out.println("无法移动!");
                    }
                    break;
                case "D":
                    // 判断玩家是否撞墙
                    if(map[playerRow][playerCol + 1] != 'H') {
                        // 判断是否推箱
                        if(map[playerRow][playerCol + 1] == 'o') {
                            // 判断箱子是否撞墙
                            if(map[playerRow][playerCol + 2] != 'H') {
                                // 箱子不撞墙结果
                                map[playerRow][playerCol] = ' ';
                                map[playerRow][playerCol + 1] = '&';
                                map[playerRow][playerCol + 2] = 'o';

                                playerCol++;
                                boxCol++;
                            } else {
                                // 箱子撞墙结果
                                System.out.println("无法移动!");
                            }
                        } else {
                            // 不推箱移动结果
                            map[playerRow][playerCol] = ' ';
                            map[playerRow][playerCol + 1] = '&';

                            playerCol++;
                        }
                    } else {
                        // 玩家撞墙结果
                        System.out.println("无法移动!");
                    }
                    break;
            }
            System.out.println();

            // 判断玩家操作结果
            if(boxRow == destinRow && boxCol == destinCol) {
                System.out.println("恭喜你,挑战成功!");
                return;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值