博客
关于我
Java控制台推箱子小游戏
阅读量:208 次
发布时间:2019-02-28

本文共 5344 字,大约阅读时间需要 17 分钟。

package sokoban;import java.util.Scanner;/** * author:WanAkiko * 指导老师:杨*宇 */public class SokobanPojo {    static Scanner sc = new Scanner(System.in);    static int[][] map = new int[10][10];    static int playerX = 1, playerY = 1; // 玩家坐标    static int boxX = 8, boxY = 3; // 箱子坐标    static int terminusX = 2, terminusY = 8; // 终点坐标    public static void main(String[] args) {        map[playerX][playerY] = 1; // 玩家坐标对应的值        map[boxX][boxY] = 2; // 箱子坐标对应的值        map[terminusX][terminusY] = 3; // 终点坐标对应的值        boolean starFlag = false;        while (true) {            InitializationMap(); // 初始化地图            try {                TheInputPrompt(); // 玩家行动            } catch (Exception e) {                System.out.println("提示:您已越界,游戏失败!");                return;            }            GoIntoAction(); // 坐标更迭            // 星星奖励            if (playerX == 2 && playerY == 8) {                starFlag = true;                if (starFlag) {                    TheStarAwards();                    System.out.println("(*^▽^*)画个星星祝福你!!!\n");                }            }            // 到达终点            if (playerX == 8 && playerY == 3) {                CongratulationsToPass();                break;            }        }    }    /**     * 恭喜过关     */    private static void CongratulationsToPass() {        System.out.println("※※※※※※※※※※※※※※※");        System.out.println("※■■■■■■■■■■■■■※");        System.out.println("※■■■■恭喜过关!■■■■※");        System.out.println("※■■■■■■■■■■■■■※");        System.out.println("※※※※※※※※※※※※※※※");        InitializationMap();    }    /**     * 星星奖励     * 源代码:https://blog.csdn.net/qq_44645104/article/details/89458986     */    private static void TheStarAwards() {        int touHigh = 6;        int jianHigh = 25;        int kuang = 50;        for (int i = 1; i <= touHigh + jianHigh; i++) {            for (int j = 1; j <= kuang; j++) {                // 上三角                if (i <= touHigh) {                    if (j >= (kuang / 2 + 1) + 1 - i && j <= (kuang / 2 + 1) - 1 + i) {                        System.out.print("*");                    } else {                        System.out.print(" ");                    }                }                // 上半部分                if (i > touHigh && i <= jianHigh) {                    if (j >= (kuang / 2 + 1) + 2 - i && j <= kuang - 3 * (i - touHigh)) {                        System.out.print("*");                    } else if (j < (kuang / 2 + 1) - 3 + i && j >= 3 * (i - touHigh)) {                        System.out.print("*");                    } else {                        System.out.print(" ");                    }                }            }            System.out.println("");        }    }    /**     * 开始行动     */    private static void GoIntoAction() {        if ("w".equals(sc)) {            if (map[playerX - 1][playerY] == map[boxX][boxY]) {                boxX--;                map[boxX][boxY] = 2;            }            map[playerX][playerY] = 0;            playerX--;            map[playerX][playerY] = 1;        } else if ("s".equals(sc)) {            if (map[playerX - 1][playerY] == map[boxX][boxY]) {                boxX++;                map[boxX][boxY] = 2;            }            map[playerX][playerY] = 0;            playerX++;            map[playerX][playerY] = 1;        } else if ("a".equals(sc)) {            if (map[playerX - 1][playerY] == map[boxX][boxY]) {                boxY--;                map[boxX][boxY] = 2;            }            map[playerX][playerY] = 0;            playerY--;            map[playerX][playerY] = 1;        } else if ("d".equals(sc)) {            if (map[playerX - 1][playerY] == map[boxX][boxY]) {                boxY++;                map[boxX][boxY] = 2;            }            map[playerX][playerY] = 0;            playerY++;            map[playerX][playerY] = 1;        }    }    /**     * 输入提示     */    private static void TheInputPrompt() {        System.out.println("移动指示:上-w 下-s 左-a 右-d");        System.out.print("请输入:");        String move = sc.next();        switch (move) {            case "w":                map[playerX][playerY] = 0;                playerX--;                map[playerX][playerY] = 1;                break;            case "s":                map[playerX][playerY] = 0;                playerX++;                map[playerX][playerY] = 1;                break;            case "a":                map[playerX][playerY] = 0;                playerY--;                map[playerX][playerY] = 1;                break;            case "d":                map[playerX][playerY] = 0;                playerY++;                map[playerX][playerY] = 1;                break;            default:                System.out.println("提示:输入有误,请重新输入!");                break;        }    }    /**     * 初始化地图     */    private static void InitializationMap() {        for (int i = 0; i < map.length; i++) {            for (int j = 0; j < map[0].length; j++) {                if (map[i][j] == 1) {                    System.out.print("人 ");                } else if (map[i][j] == 2) {                    System.out.print("■ ");                } else if (map[i][j] == 3) {                    System.out.print("★ ");                } else {                    System.out.print("□ ");                }            }            System.out.println();        }    }}
你可能感兴趣的文章
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>