如何清理我的代码?编码新手

问题描述 投票:0回答:1

我是编码新手,这是我制作的第一个程序,但感觉非常笨重,我想知道如何更有效地编写代码。我正在使用 JDK SE 8 的 IntelliJ IDEA 上运行。

import java.util.Objects;
import java.util.Scanner;

public class Game {

    public static void main(String[] args) {

        String[][] OxO = {{"@","@","@","@","@","@","@","@"},
                          {"@","P","O"," "," "," "," ","@",""," "},
                          {"@"," "," "," ","O","O"," ","@",""," "},
                          {"@","O"," ","O","O"," "," ","@",""," "},
                          {"@"," "," ","O","X"," "," ","@",""," "},
                          {"@"," ","O","O","O","O"," ","@",""," "},
                          {"@"," "," "," "," "," "," ","@",""," "},
                          {"@","@","@","@","@","@","@","@"}};
        int x = 1;
        int y = 1;

        System.out.println("Enter 'w','a','s', or 'd' to play. You are 'P' and your objective is to get to the 'X'");

        while(OxO[4][4] != "P") {

            System.out.println(OxO[0][0]+OxO[0][1]+OxO[0][2]+OxO[0][3]+OxO[0][4]+OxO[0][5]+OxO[0][6]+OxO[0][7]);
            System.out.println(OxO[1][0]+OxO[1][1]+OxO[1][2]+OxO[1][3]+OxO[1][4]+OxO[1][5]+OxO[1][6]+OxO[1][7]);
            System.out.println(OxO[2][0]+OxO[2][1]+OxO[2][2]+OxO[2][3]+OxO[2][4]+OxO[2][5]+OxO[2][6]+OxO[2][7]);
            System.out.println(OxO[3][0]+OxO[3][1]+OxO[3][2]+OxO[3][3]+OxO[3][4]+OxO[3][5]+OxO[3][6]+OxO[3][7]);
            System.out.println(OxO[4][0]+OxO[4][1]+OxO[4][2]+OxO[4][3]+OxO[4][4]+OxO[4][5]+OxO[4][6]+OxO[4][7]);
            System.out.println(OxO[5][0]+OxO[5][1]+OxO[5][2]+OxO[5][3]+OxO[5][4]+OxO[5][5]+OxO[5][6]+OxO[5][7]);
            System.out.println(OxO[6][0]+OxO[6][1]+OxO[6][2]+OxO[6][3]+OxO[6][4]+OxO[6][5]+OxO[6][6]+OxO[6][7]);
            System.out.println(OxO[7][0]+OxO[7][1]+OxO[7][2]+OxO[7][3]+OxO[7][4]+OxO[7][5]+OxO[7][6]+OxO[7][7]);
            Scanner scanner = new Scanner(System.in);
            String movement = scanner.nextLine();
            if(Objects.equals(movement, "a")) {
                int old = x;
                x = x - 1;
                OxO[y][8] = OxO[y][old];
                if(Objects.equals(OxO[y][x], "O") || Objects.equals(OxO[y][x], "@")) {
                    x = x+1;
                } else {
                    OxO[y][x] = OxO[y][8];
                    OxO[y][old] = " ";
                }
            } else if (Objects.equals(movement, "d")) {
                int old = x;
                x = x + 1;
                OxO[y][8] = OxO[y][old];
                if(Objects.equals(OxO[y][x], "O") || Objects.equals(OxO[y][x], "@")) {
                    x = x-1;
                } else {
                    OxO[y][x] = OxO[y][8];
                    OxO[y][old] = " ";
                }
            } else if (Objects.equals(movement, "w")) {
                int old = y;
                y = y - 1;
                OxO[y][8] = OxO[old][x];
                if(Objects.equals(OxO[y][x], "O") || Objects.equals(OxO[y][x], "@")) {
                    y = y+1;
                } else {
                    OxO[y][x] = OxO[y][8];
                    OxO[old][x] = " ";
                }
            } else if (Objects.equals(movement, "s")) {
                int old = y;
                y = y + 1;
                OxO[y][8] = OxO[old][x];
                if(Objects.equals(OxO[y][x], "O") || Objects.equals(OxO[y][x], "@")) {
                    y = y-1;
                } else {
                    OxO[y][x] = OxO[y][8];
                    OxO[old][x] = " ";
                }
            }
            System.out.println("\n\n\n\n\n\n\n");

        }

        System.out.println(OxO[0][0]+OxO[0][1]+OxO[0][2]+OxO[0][3]+OxO[0][4]+OxO[0][5]+OxO[0][6]+OxO[0][7]);
        System.out.println(OxO[1][0]+OxO[1][1]+OxO[1][2]+OxO[1][3]+OxO[1][4]+OxO[1][5]+OxO[1][6]+OxO[1][7]);
        System.out.println(OxO[2][0]+OxO[2][1]+OxO[2][2]+OxO[2][3]+OxO[2][4]+OxO[2][5]+OxO[2][6]+OxO[2][7]);
        System.out.println(OxO[3][0]+OxO[3][1]+OxO[3][2]+OxO[3][3]+OxO[3][4]+OxO[3][5]+OxO[3][6]+OxO[3][7]);
        System.out.println(OxO[4][0]+OxO[4][1]+OxO[4][2]+OxO[4][3]+OxO[4][4]+OxO[4][5]+OxO[4][6]+OxO[4][7]);
        System.out.println(OxO[5][0]+OxO[5][1]+OxO[5][2]+OxO[5][3]+OxO[5][4]+OxO[5][5]+OxO[5][6]+OxO[5][7]);
        System.out.println(OxO[6][0]+OxO[6][1]+OxO[6][2]+OxO[6][3]+OxO[6][4]+OxO[6][5]+OxO[6][6]+OxO[6][7]);
        System.out.println(OxO[7][0]+OxO[7][1]+OxO[7][2]+OxO[7][3]+OxO[7][4]+OxO[7][5]+OxO[7][6]+OxO[7][7]);
        System.out.println("\nYou have done it!");

    }

}

我删除了一些不会影响任何内容的无用行,但这就是我能想到的全部。另外,如果您能为像我这样的初学者提供任何提示,我将不胜感激。谢谢!

java
1个回答
0
投票

您的程序是一个很好的起点,但是在某些方面您可以提高效率和可读性

使用的一些通用编码规范:

  • 使用 for 循环打印网格,而不是手动打印网格的每一行。
  • 不要使用像 4 这样的硬编码行和列索引值,而是使用常量或变量来使代码更具可读性和可维护性。
  • 您可以通过首先检查运动是否有效,然后更新位置(如果有效)来简化运动逻辑。
  • 使用
    equals()
    进行字符串比较 使用
    ==
    方法代替
    !=
    equals()
    进行字符串比较。这里
    ==
    比较的是内存地址,而不是字符串文字 intsself。
import java.util.*;

public class Game {

    private static final int GRID_SIZE = 8;
    private static final int PLAYER_ROW = 1;
    private static final int PLAYER_COL = 1;
    private static final char PLAYER_CHAR = 'P';
    private static final char EMPTY_CHAR = ' ';
    private static final char WALL_CHAR = '@';
    private static final char OBJECTIVE_CHAR = 'X';
    private static final char OBSTACLE_CHAR = 'O';

    public static void main(String[] args) {
        char[][] grid = initializeGrid();

        System.out.println("Enter 'w','a','s', or 'd' to play. You are 'P' and your objective is to get to the 'X'");

        int playerRow = PLAYER_ROW;
        int playerCol = PLAYER_COL;

        while (grid[playerRow][playerCol] != OBJECTIVE_CHAR) {
            printGrid(grid);
            Scanner scanner = new Scanner(System.in);
            String movement = scanner.nextLine();
            movePlayer(grid, movement, playerRow, playerCol);
        }

        printGrid(grid);
        System.out.println("\nYou have done it!");
    }

    private static char[][] initializeGrid() {
        return new char[][]{
                {'@', '@', '@', '@', '@', '@', '@', '@'},
                {'@', 'P', 'O', ' ', ' ', ' ', ' ', '@'},
                {'@', ' ', ' ', ' ', 'O', 'O', ' ', '@'},
                {'@', 'O', ' ', 'O', 'O', ' ', ' ', '@'},
                {'@', ' ', ' ', 'O', 'X', ' ', ' ', '@'},
                {'@', ' ', 'O', 'O', 'O', 'O', ' ', '@'},
                {'@', ' ', ' ', ' ', ' ', ' ', ' ', '@'},
                {'@', '@', '@', '@', '@', '@', '@', '@'}
        };
    }

    private static void printGrid(char[][] grid) {
        for (char[] row : grid) {
            for (char cell : row) {
                System.out.print(cell);
            }
            System.out.println();
        }
    }

    private static void movePlayer(char[][] grid, String movement, int playerRow, int playerCol) {
        switch (movement) {
            case "a":
                move(grid, playerRow, playerCol, 0, -1);
                break;
            case "d":
                move(grid, playerRow, playerCol, 0, 1);
                break;
            case "w":
                move(grid, playerRow, playerCol, -1, 0);
                break;
            case "s":
                move(grid, playerRow, playerCol, 1, 0);
                break;
            default:
                System.out.println("Invalid movement. Please enter 'w', 'a', 's', or 'd'.");
        }
    }

    private static void move(char[][] grid, int playerRow, int playerCol, int rowChange, int colChange) {
        int newRow = playerRow + rowChange;
        int newCol = playerCol + colChange;
        if (isValidMove(grid, newRow, newCol)) {
            grid[playerRow][playerCol] = EMPTY_CHAR;
            grid[newRow][newCol] = PLAYER_CHAR;
        } else {
            System.out.println("Invalid move! You can't go there.");
        }
    }

    private static boolean isValidMove(char[][] grid, int row, int col) {
        return row >= 0 && row < GRID_SIZE && col >= 0 && col < GRID_SIZE && grid[row][col] != WALL_CHAR && grid[row][col] != OBSTACLE_CHAR;
    }
}

此版本的代码使用方法来提高可读性和可维护性。它还简化了移动逻辑并利用常量来避免幻数。并且它使用嵌套循环打印网格,使代码更加简洁。

© www.soinside.com 2019 - 2024. All rights reserved.