简体   繁体   English

我正在尝试制作一个在命令行上播放的跳棋游戏,我不知道如何根据用户输入移动棋子

[英]I am trying to make a checkers game that plays on the command line, I can't figure out how to move the piece based on the users Input

My program will print out the board and will ask the user for input, but the input doesn't change what the board prints out.我的程序将打印出电路板并要求用户输入,但输入不会改变电路板打印出来的内容。 I have assigned the userInput to the corresponding locations but can't seem to make the pieces move.我已将 userInput 分配到相应的位置,但似乎无法使碎片移动。 I am just learning how to use different methods and I think that's where the majority of my problem comes from.我只是在学习如何使用不同的方法,我认为这就是我的大部分问题的来源。 the board print out is in one method and currently the movement is part of the main method.电路板打印输出是一种方法,目前运动是主要方法的一部分。

... ...

import java.util.Scanner;
public class Checkers {
public static void printBoard () {
    String[][] board = new String [9][9];
    for (int i=0; i < board.length; i++) {
        for (int j=0; j < board[i].length; j++) {
            board[i][j] = "";
        }
    } 
     // Numbers for rows.
    board[1][0] = "2";
    board[2][0] = "3";
    board[3][0] = "4";
    board[4][0] = "5";
    board[5][0] = "6";
    board[6][0] = "7";
    board[7][0] = "8"; 
    
    board[0][1] = "1";// Numbers for columns.
    board[0][2] = "2";
    board[0][3] = "3";
    board[0][4] = "4";
    board[0][5] = "5";
    board[0][6] = "6";
    board[0][7] = "7";
    board[0][8] = "8";
    
    board[8][1] = "WP";
    board[8][3] = "WP";
    board[8][5] = "WP";
    board[8][7] = "WP";
    board[7][2] = "WP";
    board[7][4] = "WP";
    board[7][6] = "WP";
    board[7][8] = "WP";
    board[6][1] = "WP";
    board[6][3] = "WP";
    board[6][5] = "WP";
    board[6][7] = "WP";
    
    board[1][2] = "BP";
    board[1][4] = "BP";
    board[1][6] = "BP";
    board[1][8] = "BP";
    board[2][1] = "BP";
    board[2][3] = "BP";
    board[2][5] = "BP";
    board[2][7] = "BP";
    board[3][2] = "BP";
    board[3][4] = "BP";
    board[3][6] = "BP";
    board[3][8] = "BP";
    
    for (int i=0; i < 1; i++) {
        for (int j=0; j < 9; j++) {
            System.out.print(board[i][j] + "     " );
        }
    }   
    System.out.println();
    
    boolean iswhite = true;
    String emptyBlack = "      ";
    String emptyWhite = "******";
    
    
    for (int i= 1; i< board.length; i++) {
        for (int j= 1; j< board[i].length; j++) {
            if (iswhite) {
                System.out.print(emptyWhite);
            } else {
                System.out.print(emptyBlack);
            }
            iswhite = !iswhite;
        }   
        System.out.println();
        
        for (int j=1; j< board[i].length; j++) {
            if (iswhite) {
                if (board[i][j].equals("")) {
                    System.out.print(emptyWhite);
                }   else {  
                System.out.print("**" + board[i][j] + "**");
                }
            }   else {
                    if (board[i][j].equals("")) {
                        System.out.print(emptyBlack);
                    } else {    
                    System.out.print("  " + board[i][j] + "  ");
                    }
                }   
                iswhite = !iswhite;
        }
        
        System.out.println();
        
        for (int j=1; j< board[i].length; j++) {
            if (iswhite) {
                System.out.print(emptyWhite);
            } else {
                System.out.print(emptyBlack);
            }
            iswhite = !iswhite;
        }   
        System.out.println();
        
        iswhite=!iswhite;
    }
  }

public static void main (String [] args) {
    Scanner scnr = new Scanner(System.in);
    
    System.out.println("Please select a piece to move by entering it's location.");
    
    int playerHz = scnr.nextInt();
    int playerVt = scnr.nextInt();
    System.out.println("Enter a new location.");
    
    int newHz = scnr.nextInt();
    int newVt = scnr.nextInt();
    String[][] board = new String [9][9];
    
    for (int i = 1; i < board.length; i++) {
        for (int j = 1; j <board[i].length; j++) {
            if (board[i][j] == (board[playerHz][playerVt]) ) {
             board[i][j] = "";
            }
            board[newHz][newVt] = "WP";
        }
    }
    printBoard();

 }
    
...
    
    
    
    

}   

The variable String[][] board seems to be local variables in both printBoard and main methods.变量String[][] board似乎是printBoardmain方法中的局部变量。 One method cannot affect a local variable outside it's scope.一种方法不能影响其范围之外的局部变量。 In other words, even though the variable is named same in both methods,they are in fact different variables.换句话说,即使变量在两种方法中命名相同,它们实际上是不同的变量。 Easiest solution is make String[][] board variable global.最简单的解决方案是使String[][] board变量全局化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在用Java开发跳棋游戏,似乎无法在板上画图 - I am working on a checkers game in Java and can't seem to draw pieces on the board 我将如何创建一种方法来检查棋子游戏的移动是否合法? - How would I create a method that checks if a move is legal for a checkers game? 我正在netbeans上运行基于Java的游戏。 如何计算其中的数据流? - I am running a java based game on netbeans . How do I figure out the data flow in it? 我不知道如何从命令行调用Java oscar3类方法stringToString - I can't figure out how to call Java oscar3 class method stringToString from the command line 我无法弄清楚这个线程有什么问题,我试图理解多线程 - I can't figure out what is wrong with this threads, I am trying to understand multithreading 我试图弄清楚如何正确循环鼠标事件 - I am trying to figure out how to properly loops mouse events 我想弄清楚如何在方法中创建用户 - I am trying to figure out how to create a user within a method 我正在尝试找出Collat​​zSequence - I am trying to figure out CollatzSequence 在第 16 行从二维数组输入中获取 InputMismatchException 并且我似乎无法弄清楚为什么 - Getting An InputMismatchException On Line 16 From A 2D Array Input And I Can't Seem To Figure Out Why 无法弄清楚为什么我得到空值 - Can't figure out why I am getting null values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM