[英]I am working on a checkers game in Java and can't seem to draw pieces on the board
[英]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
似乎是printBoard
和main
方法中的局部變量。 一種方法不能影響其范圍之外的局部變量。 換句話說,即使變量在兩種方法中命名相同,它們實際上是不同的變量。 最簡單的解決方案是使String[][] board
變量全局化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.