简体   繁体   中英

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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