简体   繁体   中英

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.

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'.

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.

Delete continue; in the switch block and move it to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM