简体   繁体   English

Java 二维阵列棋盘游戏机 output 问题

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

MY CODE我的代码

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 turns. 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转身。

The player or 'P' is supposed to traverse the array, with the move only validated if it would not cause collision with a wall 'w'.玩家或“P”应该遍历数组,只有在不会与墙“w”发生碰撞时才验证移动。

The code is in a runnable state to test or if anyone could point me in the right direction as to where I am going wrong I would appreciate it.该代码位于可运行的 state 中进行测试,或者如果有人能指出我哪里出错了,我将不胜感激。

Delete continue;删除continue; in the switch block and move it to: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