簡體   English   中英

Java 二維陣列棋盤游戲機 output 問題

[英]Java 2D array board game console output issues

我的代碼

import java.util.Scanner;

/**
*
* @author jwgau
*/
public class mazeTask {

    public static void main(String[] args) {

        printMaze();
    }

    public static void printMaze() {
        int py = 8;
        int px = 1;
        Scanner scan = new Scanner(System.in);
        char choice;

        char[][] maze = {
            {'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'}, // Row 0
            {'W', ' ', ' ', ' ', ' ', 'W', 'G', ' ', 'W'}, // Row 1
            {'W', ' ', 'W', 'W', 'W', 'W', 'W', ' ', 'W'}, // Row 2
            {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' ', 'W'}, // Row 3
            {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' ', 'W'}, // Row 4
            {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' ', 'W'}, // Row 5
            {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' ', 'W'}, // Row 6
            {'W', ' ', 'W', ' ', 'W', ' ', 'W', ' ', 'W'}, // Row 7
            {'W', 'P', ' ', ' ', ' ', ' ', ' ', ' ', 'W'}, // Row 8
            {'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'} // Row 9
        };
        

        while (true) {
            
            //printing 2d array using nested for loops
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 9; j++) {
                    System.out.print(maze[i][j]);
                    maze[py][px] = 'P';
                    
                }
                System.out.println("");
            }                    

            //maze[py][px] = 'P';

            System.out.println("North, south, east or west?");
            choice = scan.next().charAt(0);
            switch (choice) {
            case 'N':
                if(validMove(choice, py, px, maze) == true){
                    py = py-1;    
                    System.out.println(py);
                    maze[py][px] = 'P';
                    
                }
                continue;
            case 'S':
                if(validMove(choice, py, px, maze) == true){
                    py = py+1;
                    maze[py][px] = 'P';
                    
                }
                continue;
            case 'E':
                if(validMove(choice, py, px, maze) == true){
                    px = px+1;    
                    maze[py][px] = 'P';                    
                    
                }
                continue;
            case 'W':
                if(validMove(choice, py, px, maze) == true){
                    px = px - 1;
                    maze[py][px] = 'P';
                    
                }                    

                continue;

            }

            System.out.println("px = " + px);
            System.out.println("py = " + py);

        }
        
    }

    public static boolean validMove(char choice, int py, int px, char maze[][]){
        switch(choice){
        case 'N':
            if(maze[px][py-1] == 'W'){
                return false;
            }
            else{
                maze[py] = maze[py-1];
            }                  
            break;
        case 'S':
            if(maze[px][py+1] == 'W'){
                return false;
            }
            else{
                maze[py] = maze[py+1];
            }                  
            break;
        case 'E':
            if(maze[px+1][py] == 'W'){
                return false;
            }
            else{
                maze[px] = maze[px+1];
            }                
            break;
        case 'W':
            if(maze[px-1][py] == 'W'){
                System.out.println("Invalid move, try again");
                return false;
            }
            else{
                maze[px] = maze[px-1];
            }               
            break;                

        }
        return true;
    }

}

I am currently working on a 2D board game project using arrays in java and am having issues with incorrect output for the screen, the first couple of iterations work fine and my py/px variables are reduced correctly, then nothing is output after the first few轉身。

玩家或“P”應該遍歷數組,只有在不會與牆“w”發生碰撞時才驗證移動。

該代碼位於可運行的 state 中進行測試,或者如果有人能指出我哪里出錯了,我將不勝感激。

刪除continue; switch塊中並將其移動到:

            System.out.println("px = " + px);
            System.out.println("py = " + py);
            continue;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM