java - 如何在我的马棋子去过的每个位置放置一个数字(在 2D "board"数组中)

标签 java arrays

该程序基本上提示用户选择显示的走法之一并移动棋盘中的马棋子。通过这种方式,我们尝试看看这匹马可以移动到棋盘多少次而不出界,甚至处于同一位置。

问题是我还没有找到一种方法来为马去过的位置放置一个数字(从1开始)。更具体地说,我希望它在马的当前位置打印 H,在过去的位置打印 1 to ...。

import java.util.Scanner;

public class Ex7_22 {
    private static Scanner scanner = new Scanner(System.in);
    private static String[][] board = new String[8][8];
    private static int[] horizontal = new int[8];
    private static int[] vertical = new int[8];
    private static boolean[][] boardPositions = new boolean[8][8];
    private static int currentRow = 3;
    private static int currentColumn = 4;
    public static void main(String[] args){
        fillArrays();
        boolean outOfBoundsV;
        boolean outOfBoundsH;
        boolean positionAvailability = false;
        int VerticalMove,HorizontalMove;
        fillBoard();
        board[3][4] = "H";
        boardPositions[3][4] = true;
        displayBoard();
        int pickMove;
        for(int i=1;i<=64;i++){
            displayBoardPositions();
            displayPossibleMoves();
            do {
                System.out.println("\nPick one of the displayed moves to do (0-7): ");
                pickMove = scanner.nextInt();
                VerticalMove = moveHorse_Vertical(pickMove);
                HorizontalMove = moveHorse_Horizontal(pickMove);
                outOfBoundsV = checkForOutOfBounds(VerticalMove);
                outOfBoundsH = checkForOutOfBounds(HorizontalMove);
                if ((outOfBoundsV)||(outOfBoundsH)){
                    reverse(pickMove);
                }
                if((!outOfBoundsV)&&(!outOfBoundsH)){
                    positionAvailability = checkForPositionAvailability(VerticalMove,HorizontalMove);
                    if((!positionAvailability)){
                        reverse(pickMove); 
                    }
                }
            }while (((outOfBoundsV)||(outOfBoundsH))||(!positionAvailability));
            board[VerticalMove][HorizontalMove] = "H";
           //placeNumber(VerticalMove,HorizontalMove,pickMove);
            boardPositions[VerticalMove][HorizontalMove] = true;
            displayBoard();
        }
    }

    /* private static void placeNumber(int VerticalMove, int HorizontalMove, int pickMove) {
        //have to place number in every position the horse has been to. Haven't managed to get it working.
        reverse(pickMove);
        int i=0;
        i++;
        String iStr = String.valueOf(i);
        board[VerticalMove][HorizontalMove] = iStr;
        currentRow += vertical[pickMove];
        currentColumn += horizontal[pickMove];
    } */

    private static void reverse(int move){
        currentRow -= vertical[move];
        currentColumn -= horizontal[move];
    }

    private static void fillBoard(){
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[i].length;j++){
                board[i][j] = "0";
            }
        }
    }

    private static boolean checkForPositionAvailability(int vertical, int 
    horizontal) {
        if(!boardPositions[vertical][horizontal]){
            return true;
        }
        else{
            System.out.println("You've already been in this position.");
            return false;
        }
    }

    private static boolean checkForOutOfBounds(int position) {
        if((position<0)||(position>=8)){
            System.out.print("Position out of bounds\nPlease choose another move\n");
            return true;
        }
        else return false;
    }

    private static int moveHorse_Vertical(int move) {
        currentRow += vertical[move];
        return currentRow;
    }

    private static int moveHorse_Horizontal(int move){
        currentColumn += horizontal[move];
        return currentColumn;
    }

    private static void fillArrays() {
        //fill horizontal array
        horizontal[0] = 2;
        horizontal[1] = 1;
        horizontal[2] = -1;
        horizontal[3] = -2;
        horizontal[4] = -2;
        horizontal[5] = -1;
        horizontal[6] = 1;
        horizontal[7] = 2;
        //fill vertical array
        vertical[0] = -1;
        vertical[1] = -2;
        vertical[2] = -2;
        vertical[3] = -1;
        vertical[4] = 1;
        vertical[5] = 2;
        vertical[6] = 2;
        vertical[7] = 1;
    }

    private static void displayBoard(){
        System.out.print("\n");
        System.out.println("     0   1   2   3   4   5   6   7");
        System.out.println("     -----------------------------   ");
        for(int i=0;i<board.length;i++){
            System.out.print(i + "|\t");
            for(int j=0;j<board[i].length;j++){
                System.out.print(" "+board[i][j] + "\t");
            }
            System.out.println();
        }
    }

    private static void displayBoardPositions(){
        System.out.print("\n");
        System.out.println("     0   1   2   3   4   5   6   7");
        System.out.println("     -----------------------------   ");
        for(int i=0;i<boardPositions.length;i++){
            System.out.print(i + "|\t");
            for(int j=0;j<boardPositions[i].length;j++){
                System.out.print(" "+boardPositions[i][j] + "\t");
            }
            System.out.println();
        }
    }

    private static void displayPossibleMoves(){
        System.out.println("\n0 -> 1 move up // 2 moves right.\n" +
                "1 -> 2 moves up // 1 move right.\n" +
                "2 -> 2 moves up // 1 move left.\n" +
                "3 -> 1 move up // 2 moves left.\n" +
                "4 -> 1 move down // 2 moves left.\n" +
                "5 -> 1 move left // 2 moves down.\n" +
                "6 -> 1 move right // 2 moves down.\n" +
                "7 -> 1 move down // 2 moves right.");
    }
}

最佳答案

嘿,我认为你不需要做太多改变。您只需添加两个整数即可跟踪您之前的位置。我尽力将对代码的修改保持在最低限度:

int previousRow = 3;
int previousColumn = 4;

并将当前位置设置为“H”后更新它们:

这是 public static main(String[] args) 的修改版本:


public static void main(String[] args){

    fillArrays();
    boolean outOfBoundsV;
    boolean outOfBoundsH;
    boolean positionAvailability = false;
    int VerticalMove,HorizontalMove;
    fillBoard();
    board[3][4] = "H";
    boardPositions[3][4] = true;
    // add the two integers that keep track of you previous position:
    int previousRow = 3;
    int previousColumn = 4;
    displayBoard();
    int pickMove;
    for(int i=1;i<=64;i++){
      displayBoardPositions();
      displayPossibleMoves();
      do {
        System.out.println("\nPick one of the displayed moves to do (0-7): ");
        pickMove = scanner.nextInt();
        VerticalMove = moveHorse_Vertical(pickMove);
        HorizontalMove = moveHorse_Horizontal(pickMove);
        outOfBoundsV = checkForOutOfBounds(VerticalMove);
        outOfBoundsH = checkForOutOfBounds(HorizontalMove);
        if ((outOfBoundsV)||(outOfBoundsH)){
          reverse(pickMove);
        }
        if((!outOfBoundsV)&&(!outOfBoundsH)){
          positionAvailability = checkForPositionAvailability(VerticalMove,HorizontalMove);
          if((!positionAvailability)){
            reverse(pickMove);
          }
        }
      } while ((outOfBoundsV)||(outOfBoundsH)||(!positionAvailability));
      // set the previous position to your current step of i:
      board[previousRow][previousColumn] = String.valueOf(i);
      board[VerticalMove][HorizontalMove] = "H";

      //update your previous position, with your current position
      previousColumn  = currentColumn;
      previousRow   = currentRow;
      boardPositions[VerticalMove][HorizontalMove] = true;
      displayBoard();
    }
  }



关于java - 如何在我的马棋子去过的每个位置放置一个数字(在 2D "board"数组中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56629784/

相关文章:

Java - 通过 FTP 上传文件夹?

java - 如何在我的 Android 应用程序中使用内置语音识别功能?

arrays - 如何检查 Kotlin 函数中的泛型类型?

javascript - 所有数组值都相同时的拼接问题

python - 在 python numpy.savez 中使用变量值而不是关键字

arrays - Swift:通过引用传递数组?

Java Reflection - 将形式参数列表与实际参数列表匹配

java - 如何在不更改 URL 的情况下从 Controller 调用方法到 thymeleaf

java - ObjectMapper.writeValueAsString 输出中的重复值

java - lambda 数组有缺点吗?