超市管理系统在用户界面添加一个小游戏

import java.util.Scanner;

public class UserPlayGame {
    public void UserPlayGame(){
        System.out.println("用户您好欢迎来到小游戏界面");
        System.out.println("目前只有一款游戏为;“走出迷宫”游戏即将开始");
        System.out.println("欢迎来到走出迷宫第一关");
        System.out.println("你目前的位置为0;0,你需要走到10;10,你可以选择上下左右");
        int x=0;
        int y=0;
        while (x!=10&&y!=10){
            GameIn(x,y);
            x=GameIn(x,y);
        }
    }
    public int GameIn(int x,int y){
        Scanner zl=new Scanner(System.in);
        System.out.println("请选择 1 up 2 down 3 right 4 left");
        String zl1=zl.next();
        String A="1";
        String B="2";
        String C="3";
        String D="4";
        if (zl1.equals(A)) {
            Up(x, y);
            y=Up(x, y);
            System.out.println("目前位置为"+x+":"+y);
            return y;
        } else if (zl1.equals(B)) {
            Down(x, y);
            y=Down(x, y);
            System.out.println("目前位置为"+x+":"+y);
            return  y;
        }else if ((zl1.equals(C))){
            Right(x, y);
            x=Right(x, y);
            System.out.println("目前位置为"+x+":"+y);
            return x;
        }else if((zl1.equals(D))){
            Left(x, y);
            x=Left(x, y);
            System.out.println("目前位置为"+x+":"+y);
            return x;
        }
      return 0;
    }
    public int Up(int x,int y){
        return y+1;
    }
    public int Down(int x,int y){

        return y-1;
    }
    public int Right(int x,int y){
        return x+1;
    }
    public int Left(int x,int y){
        return x-1;
    }
}

以上为第一版,测试如下

你目前的位置为0;0,你需要走到10;10,你可以选择上下左右
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:1
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:1
请选择 1 up 2 down 3 right 4 left
1
目前位置为1:1
请选择 1 up 2 down 3 right 4 left

         明显可以看到代码有问题,那么我们可以对代码进行修改

import java.util.Scanner;

public class UserPlayGame {
    public void UserPlayGame(){
        System.out.println("用户您好欢迎来到小游戏界面");
        System.out.println("目前只有一款游戏为;“走出迷宫”游戏即将开始");
        System.out.println("欢迎来到走出迷宫第一关");
        System.out.println("你目前的位置为0;0,你需要走到10;10,你可以选择上下左右");
        int x=0;
        int y=0;
        GameIn(x,y);
    }
    public void GameIn(int x,int y){
        while (x!=10||y!=10) {
            Scanner zl = new Scanner(System.in);
            System.out.println("请选择 1 up 2 down 3 right 4 left");
            String zl1 = zl.next();
            String A = "1";
            String B = "2";
            String C = "3";
            String D = "4";
            if (zl1.equals(A)) {
                y++;
                System.out.println("目前位置为" + x + ":" + y);

            } else if (zl1.equals(B)) {

                y = y - 1;
                System.out.println("目前位置为" + x + ":" + y);

            } else if ((zl1.equals(C))) {

                x++;
                System.out.println("目前位置为" + x + ":" + y);

            } else if ((zl1.equals(D))) {

                x = x - 1;
                System.out.println("目前位置为" + x + ":" + y);

            }
        }
    }

}

        以上为第二版代码我们可以看到代码完成了游戏的基本流程
        测试如下

用户您好欢迎来到小游戏界面
目前只有一款游戏为;“走出迷宫”游戏即将开始
欢迎来到走出迷宫第一关
你目前的位置为0;0,你需要走到10;10,你可以选择上下左右
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:1
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:2
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:3
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:4
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:5
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:6
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:7
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:8
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:9
请选择 1 up 2 down 3 right 4 left
1
目前位置为0:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为1:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为2:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为3:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为4:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为5:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为6:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为7:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为8:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为9:10
请选择 1 up 2 down 3 right 4 left
3
目前位置为10:10

Process finished with exit code 0

        接下来对我们的游戏进行优化
1.当输入不为1,2,3,4时发生异常
2.打印出迷宫,显示出当前位置
        第三版代码如下
1.游戏界面

import java.util.Scanner;

public class UserPlayGame {
    public void UserPlayGame() {
        System.out.println("用户您好欢迎来到小游戏界面");
        System.out.println("目前只有一款游戏为;“走出迷宫”游戏即将开始");
        System.out.println("欢迎来到走出迷宫第一关");
        System.out.println("你目前的位置为0;0,你需要走到10;10,你可以选择上下左右");
        int x = 0;
        int y = 0;
        try {
            GameIn(x, y);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void GameIn(int x,int y) throws ZlException{
        while (x!=10||y!=10) {
            Scanner zl = new Scanner(System.in);
            System.out.println("请选择 1 right 2 left 3 down 4 up");
            String zl1 = zl.next();
            String A = "1";
            String B = "2";
            String C = "3";
            String D = "4";
            if (zl1.equals(A)) {
                y++;
                System.out.println("目前位置为" + x + ":" + y);
                SetMaze setMaze=new SetMaze();
                setMaze.SetMaze(x, y);

            } else if (zl1.equals(B)) {

                y = y - 1;
                System.out.println("目前位置为" + x + ":" + y);
                SetMaze setMaze=new SetMaze();
                setMaze.SetMaze(x, y);

            } else if ((zl1.equals(C))) {

                x++;
                System.out.println("目前位置为" + x + ":" + y);
                SetMaze setMaze=new SetMaze();
                setMaze.SetMaze(x, y);

            } else if ((zl1.equals(D))) {

                x = x - 1;
                System.out.println("目前位置为" + x + ":" + y);
                SetMaze setMaze=new SetMaze();
                setMaze.SetMaze(x, y);

            }else {
                throw new ZlException("输入异常");

            }

        }
    }

}

2.打印迷宫

public class SetMaze {
    public void SetMaze(int x,int y){
        int[][] ch=new int[10][10];

        for (int i=0;i<10;i++){
        for (int j=0;j<10;j++){
            if (i==x&&j==y){
                ch[i][j]=1;
            }else {
                ch[i][j] = 0;
            }
            System.out.print(ch[i][j]+" ");
        }
        System.out.println();
        }
    }
}

3.输入异常

public class ZlException extends Exception{
    public ZlException() {
    }

    public ZlException(String message) {
        super(message);
        System.out.println("指令发生异常");

    }
}

      以下为输入5时的测试案例
请选择 1 right 2 left 3 down 4 up
5
指令发生异常
ZlException: 输入异常
    at UserPlayGame.GameIn(UserPlayGame.java:54)
    at UserPlayGame.UserPlayGame(UserPlayGame.java:12)
    at User.User(User.java:22)
    at LoginInterface.LoginInterface(LoginInterface.java:29)
    at Main.main(Main.java:4)

Process finished with exit code 0
        以下的测试案例打印出了迷宫
请选择 1 right 2 left 3 down 4 up
1
目前位置为0:1
0 1 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 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 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 0 0 0 0 0 0 0 0 
请选择 1 right 2 left 3 down 4 up

       接下来我们可以对游戏进行拓展
1.随机生成一个迷宫 0表示可以移动 1表示当前位置 2表示有障碍
2.用户可以传入参数移动任意步数
       第四版代码如下

import java.util.Random;
import java.util.Scanner;

public class SetMap2 {
    private int[][] ch=new int[10][10];
    public void SetMape2() throws ZlException{

        Random random=new Random();
        for (int i=0;i<10;i++){
            for (int j=0;j<10;j++){

                ch[i][j] = random.nextInt(2);

                System.out.print(ch[i][j]+" ");
                }
            System.out.println();
            }






            int x=0;
            int y=0;
            ch[0][0]=2;
            Scanner zl = new Scanner(System.in);
            while (x!=10||y!=10) {
                System.out.println("请选择 1 right 2 left 3 down 4 up");
                int dirction = zl.nextInt();
                System.out.println("请输入要移动的步数:");
                int step = zl.nextInt();
                if (1 == dirction) {
                    if (ch[x][y+step]==1){
                        System.out.println("有障碍物");
                    }
                    else {
                        y = y + step;
                        ch[x][y-step]=0;
                        ch[x][y]=2;
                    }
                    for (int i=0;i<10;i++){
                        for (int j=0;j<10;j++) {
                            System.out.print(ch[i][j]+" ");
                        }
                        System.out.println();
                    }


                } else  if (2 == dirction) {
                    if (ch[x][y-step]==1){
                        System.out.println("有障碍物");
                    }
                    else {
                        y = y - step;
                        ch[x][y+step]=0;
                        ch[x][y]=2;
                    }
                    for (int i=0;i<10;i++){
                        for (int j=0;j<10;j++) {
                            System.out.print(ch[i][j]+" ");
                        }
                        System.out.println();
                    }
                } else  if (3 == dirction) {
                    if (ch[x+step][y]==1){
                        System.out.println("有障碍物");
                    }
                    else {
                        x = x + step;
                        ch[x-step][y]=0;
                        ch[x][y]=2;
                    }
                    for (int i=0;i<10;i++){
                        for (int j=0;j<10;j++) {
                            System.out.print(ch[i][j]+" ");
                        }
                        System.out.println();
                    }
                } else  if (4 == dirction) {
                    if (ch[x-step][y]==1){
                        System.out.println("有障碍物");
                    }
                    else {
                        x = x - step;
                        ch[x+step][y]=0;
                        ch[x][y]=2;
                    }
                    for (int i=0;i<10;i++){
                        for (int j=0;j<10;j++) {
                            System.out.print(ch[i][j]+" ");
                        }
                        System.out.println();
                    }
                } else {
                    throw new ZlException("输入异常");
                }
            }

        }
    }

该代码可以随机生成10*10的迷宫0代表可以走1代表障碍物2代表当前位置
        用户可以选择方向和步数
        参数如下
1 1 0 1 1 1 1 0 1 1 
0 0 1 1 0 0 0 0 0 1 
0 0 0 1 0 1 1 0 0 1 
1 1 1 0 0 0 0 0 1 0 
0 0 0 1 1 0 1 1 0 0 
0 1 0 0 1 1 1 0 0 1 
1 1 0 0 0 1 1 1 0 1 
0 1 0 0 0 0 1 0 1 1 
1 0 0 0 0 1 0 0 0 1 
1 0 0 0 1 0 1 1 0 0 
请选择 1 right 2 left 3 down 4 up
1
请输入要移动的步数:
1
有障碍物
2 1 0 1 1 1 1 0 1 1 
0 0 1 1 0 0 0 0 0 1 
0 0 0 1 0 1 1 0 0 1 
1 1 1 0 0 0 0 0 1 0 
0 0 0 1 1 0 1 1 0 0 
0 1 0 0 1 1 1 0 0 1 
1 1 0 0 0 1 1 1 0 1 
0 1 0 0 0 0 1 0 1 1 
1 0 0 0 0 1 0 0 0 1 
1 0 0 0 1 0 1 1 0 0 
请选择 1 right 2 left 3 down 4 up
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值