[英]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
似乎是printBoard
和main
方法中的局部变量。 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.