繁体   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

我的程序将打印出电路板并要求用户输入,但输入不会改变电路板打印出来的内容。 我已将 userInput 分配到相应的位置,但似乎无法使碎片移动。 我只是在学习如何使用不同的方法,我认为这就是我的大部分问题的来源。 电路板打印输出是一种方法,目前运动是主要方法的一部分。

...

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();

 }
    
...
    
    
    
    

}   

变量String[][] board似乎是printBoardmain方法中的局部变量。 一种方法不能影响其范围之外的局部变量。 换句话说,即使变量在两种方法中命名相同,它们实际上是不同的变量。 最简单的解决方案是使String[][] board变量全局化。

暂无
暂无

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

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