每次命令输入后停止板随机化

发布于 2025-01-04 02:03:55 字数 2096 浏览 0 评论 0原文

我的程序在我输入每个命令后都会生成一个新的游戏板。我只希望“P”更新移动,而棋盘和障碍物保持不变,直到游戏终止。感谢您提前的帮助,这是我的代码:

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

public class Adventure {

public static void main(String[] args) {
    // Create 2D array for game board.
    char grid[][]= new char[10][10];
    Scanner move = new Scanner(System.in);
    System.out.println("Here is the current game board:");
    System.out.println("-------------------------------");

    do{
        for(int i=0; i<grid.length; i++) {          
            for(int j=0; j<grid.length; j++) {
                double Random = Math.random();
                if(Random <=.05) {
                    grid[i][j]='*';
                }
                else if(Random > .06 && Random <= .15) {
                    grid[i][j]='X';
                }           
                else {
                    grid[i][j]='.';
                }               
                grid[0][0]='P';
                grid[9][9]='T';
                System.out.print(grid[i][j]);
            }
            System.out.println("");
        }               
        System.out.print("Enter your move (U/D/L/R)>");     
        String movePlayer = move.next();

        int x=0, y=0;

        if(movePlayer.equals("R")) {
            grid[y][x]='.';
            x++;
        }
        else if(movePlayer.equals("L")) {
            grid[y][x]='.';
            x--;
        }
        else if(movePlayer.equals("U")) {
            grid[y][x]='.';
            y++;
        }
        else if(movePlayer.equals("D")) {
            grid[y][x]='.';
            y--;
        }
        else if(grid[y][x]=='*') {
            System.out.println("You fell in a pit. Game Over.");
        }
        else if(grid[y][x]=='X') {
            System.out.println("That spot is blocked. Please enter another move.");
        }
        else if(grid[y][x]=='T')
            System.out.println("Congratulations! You've found the treasure!");
        else {

            System.out.print(grid[y][x]);
        }
    }while('P' != 'T');
}

}

My program generates a new game board after every command I input. I only want the "P" to update the move while the board and obstacles stay the same until the game is terminated. Thanks for the help in advance, here is my code:

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

public class Adventure {

public static void main(String[] args) {
    // Create 2D array for game board.
    char grid[][]= new char[10][10];
    Scanner move = new Scanner(System.in);
    System.out.println("Here is the current game board:");
    System.out.println("-------------------------------");

    do{
        for(int i=0; i<grid.length; i++) {          
            for(int j=0; j<grid.length; j++) {
                double Random = Math.random();
                if(Random <=.05) {
                    grid[i][j]='*';
                }
                else if(Random > .06 && Random <= .15) {
                    grid[i][j]='X';
                }           
                else {
                    grid[i][j]='.';
                }               
                grid[0][0]='P';
                grid[9][9]='T';
                System.out.print(grid[i][j]);
            }
            System.out.println("");
        }               
        System.out.print("Enter your move (U/D/L/R)>");     
        String movePlayer = move.next();

        int x=0, y=0;

        if(movePlayer.equals("R")) {
            grid[y][x]='.';
            x++;
        }
        else if(movePlayer.equals("L")) {
            grid[y][x]='.';
            x--;
        }
        else if(movePlayer.equals("U")) {
            grid[y][x]='.';
            y++;
        }
        else if(movePlayer.equals("D")) {
            grid[y][x]='.';
            y--;
        }
        else if(grid[y][x]=='*') {
            System.out.println("You fell in a pit. Game Over.");
        }
        else if(grid[y][x]=='X') {
            System.out.println("That spot is blocked. Please enter another move.");
        }
        else if(grid[y][x]=='T')
            System.out.println("Congratulations! You've found the treasure!");
        else {

            System.out.print(grid[y][x]);
        }
    }while('P' != 'T');
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独行侠 2025-01-11 02:03:55

将生成棋盘的代码移到 do ... while() 循环之外:

// generate the board once at the beginning of the program.
do
{
  // update moves repeatedly until game is over
} while (<game over perdicate>)

move the code that generates the board outside the do ... while() loop:

// generate the board once at the beginning of the program.
do
{
  // update moves repeatedly until game is over
} while (<game over perdicate>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文