寻宝Java二维数组

时间:2021-04-03 22:34:19

标签: java arrays string 2d

我正在尝试制作一个寻宝板程序,在该程序中,用户将探索一个 10 x 10 的板,其中将包含宝藏、海盗和离岛逃生。目标是带着尽可能多的宝藏逃离岛屿。宝藏(T) - 随着发现更多宝藏而增加(即,第一个宝藏是 +100 金达布隆,第二个是 +200 达布隆,依此类推) 海盗 (P) - 您遇到的海盗越多,他们可以拿走的金币就越多。 (即第一组是-100金达布隆,第二组是-200金达布隆,依此类推)但是,如果玩家没有宝藏,就没有什么可失去的。 两者 (B)- 如果有宝藏和海盗,玩家将获得一半的可用宝藏。 逃生 (X) - 当远处看到一艘船时,用户可以离开岛屿。如何在二维数组中显示金达布隆的值?

 public static void main(String[] args) throws FileNotFoundException {

        Scanner s = new Scanner(System.in);
        
        // create 10x10 arrays
        final int ROWS = 10, COLS = 10;
        char[][] displayMap = new char[ROWS][COLS]; //stores the map
        char[][] board = new char[ROWS][COLS];// this will display the board 

        //variales for how many pirates 
        char pirate = 'P';

        //sets the location of the pirates on the board
        int upperBound = 5, lowerBound = 0;
        int pirateRow = (int) ((Math.random() * (upperBound - lowerBound + 1))
                + lowerArea); // the pirates location
        int pirateColumn = (int) ((Math.random() * (upperBound - lowerBound + 1))
                + lowerBound); // pirates location
        int treasures = 5;

        //call loadMap to read in the map and return it
        displayMap = loadMap();

        //this will take the board and resets the board
        clearBoard(board);

        //passes board and pirate informations to displayBoard then display it
        displayBoard(board, pirate, pirateRow, pirateColumn, treasures);

        int selection = 0;
        int playAgain = 0;

        while (selection != 4) {

            // displays the menu 
            System.out.println("WELCOME TO TREASURE HUNT");
            System.out.println("1. Play Game ");
            System.out.println("2. How to Play");
            System.out.println("3. Legend");
            System.out.println("4. Exit");

            // take the input from menu 
            System.out.println("Enter your selection.");
            selection = s.nextInt();

            switch (selection) {

                case 1:
                    PlayGame();
                    break;
                case 2:
                    HowToPlay();
                    break;
                case 100:
                    Legend();
                    break;
                case 4:
                    System.out.println("See you again next time!");
                default:
                    System.out.println("ERORR");
                    break;

            }
            // when user input = 1
            if (selection == 1) {

                // test for valid input
                int rowNum = 0, columnNum = 0;
                boolean isValid = false;
                
                while ( !isValid) {
                    //prompt user
                }
                System.out.println("Pick a position to explore or enter 100 to"
                        + "see the legend. ");
                char row;

                // check the row input
                Scanner sc = new Scanner(System.in); // decalres scanner
                row = sc.next().charAt(0); // this will store the character input

                if (row >= 0&& row <= 99) { // this has to be a range of 0-99
                    rowNum = testRowInput(row); // this will test and return what is in the meethod

                    if (rowNum >= 0 && rowNum <= 9) { // tests if rowNum is within the range

                        // prompt user for column number / position
                        System.out.println("Pick a position to explore or enter 100 to"
                                + "see the legend. ");
                        columnNum = getIntRange(1, 10);
                        columnNum --; // this will convert column range to 0-9
                        
                        /**
                         * check if the spot has not yet been revealed, otherwise
                         * it has been explored already
                         */
                        if ( testRevealed (board, rowNum, columnNum)) {
                            sc.nextLine(); // empty row input from buffer
                            System.out.println("You have already explored there.");
                        } else {
                            isValid = true;
                        }
                    } else {
                        sc.nextLine(); // empty row input 
                        System.out.println("Nothing to see here.");
                    }
                }
            } // finished checking user input
            
            // caught by pirate / losses gold doubloons
            if ( rowNum == pirateRow ) && ( columnNum == pirateColumn)){ 
            
            board[pirateRow][[pirateColumn]] = pirate;
            
        
                  }                   
                }
            }
        
    

    /**
     *
     */
    public static void PlayGame() {
        Scanner sc = new Scanner(System.in);
        int pirates = 0;
        int treasures = 0;
        System.out.println("You chose option 1. Enter the number of pirates");
        pirates = sc.nextInt();
        System.out.println("Enter the number of treasures.");
        treasures = sc.nextInt();
        System.out.println(displayBoard);

    }

    /**
     *
     */
    public static void HowToPlay() {
        System.out.println("You chose option 2");
        System.out.println("For this game, the user will be going on a treasure"
                + " hunt. They will explore a 10 x 10 board that will contain "
                + "treasure, pirates and an escape off the island. The goal is "
                + "to escape the island with the most treasure possible. \n"
                + "Treasure(T) - increases as more treasure is found (ie, the first treasure is "
                + "+100 gold doubloons, the second is +200 gold doubloons and so on)\n"
                + "Pirates (P) - the more pirates you encounter the more gold doubloons they can take."
                + " (ie. the first crew is -100 gold doubloons, the second crew "
                + "is -200 gold doubloons and so on)  BUT, if the player has no"
                + " treasure, there is nothing to be lost.\n"
                + "Both (B)- If there is treasure AND pirates, the player gets half the treasure "
                + "available.\n"
                + "Escape (X) - When a ship is seen in the distance, the user can leave the island.");
    }

    /**
     *
     */
    public static void Legend() {
        System.out.println("****************************************");
        System.out.println("%20s " + "LEGEND");
        System.out.println("****************************************");
        System.out.println("0 - indicates that spot has not been explored yet\n"
                + "T - indicates treasure, 100 gold doubloon\n"
                + "P - indicates pirates, lose 100 gold doubloon\n"
                + "B - indicates both treasure and pirates, 50 gold doubloon\n"
                + "X - escape the island");

    }
These codes are not yet finished and I am still in process in doing it. There are 
 more methods to work on but what I am trying to work on right now, is how I can 
display how much gold doubloons the user has every time a treasure is found and 
every time they encounter a pirate or both. 

The user's position entry must be between 0 and 99, inclusive. Should look like 
this.

Positions [00 - 09]: 0 0 0 0 0 0 0 0 0 0 
Positions [10 - 19]: 0 0 0 0 0 0 0 0 0 0
Positions [20 - 29]: 0 0 0 0 0 0 0 0 0 0 
Positions [30 - 39]: 0 0 0 0 0 0 0 0 0 0
Positions [40 - 49]: 0 0 0 0 0 0 0 0 0 0 
Positions [50 - 59]: 0 0 0 0 0 0 0 0 0 0 
Positions [60 - 69]: 0 0 0 0 0 0 0 0 0 0
Positions [70 - 79]: 0 0 0 0 0 0 0 0 0 0
Positions [80 - 89]: 0 0 0 0 0 0 0 0 0 0
Positions [90 - 99]: 0 0 0 0 0 0 0 0 0 0

1 个答案:

答案 0 :(得分:0)

在本例中,让 x 代表所选单元格的 x 坐标,y 代表所选单元格的 y 坐标。您的代码似乎相当不完整,因此让 doubloons 表示玩家在游戏期间持有的达布隆总数。

// We need two fields to track the number of treasures and pirates.
// These fields are effectively multipliers for the rewards and penalties
// for each treasure or pirate encountered as the game progresses

int doubloons = 0;
int treasureCount = 0;
int pirateCount = 0;

if(board[x][y] == 'T')
{
    treasureCount++;
    doubloons = doubloons + (treasureCount * 100);
}
else if(board[x][y] == 'P')
{
    pirateCount++;
    doubloons = doubloons - (pirateCount * 100);
}
else if(board[x][y] == 'B')
{
    treasureCount++;
    pirateCount++;
    doubloons = doubloons + (treasureCount * 50);
}
else if(board[x][y] == 'X')
{
    // whatever your end game scenario is supposed to be
}
else
{
    // whatever is supposed to happen when there's no treasure, no
    // pirates and no escape
}

请将任何后续问题限制在如何跟踪宝藏和海盗的具体问题上,因为这似乎是手头的问题。 Stack Overflow 试图坚持“一个问题,一个接受的答案”格式,因此如果您有更多不相关(甚至半相关)的问题,请将它们作为单独的问题发布,以免混淆演示文稿。