0

I have homework to make a custom version of chess which is 6x6 and the pieces move in a specific way. But for now I am trying to figure out how to select a piece and then place it in another location inside the 2d array I have set up and swap the places of the characters. For now I have:

public class Chess {

    static final int rows = 7;
    static final int columns = 7;

    public static void main(String[] args) {
        String[][] board = new String[rows][columns];
        fillBoard(board);
        enterMove();
    }
    public static void fillBoard(String[][] board){
        for (int row = 0; row < 7; row++) {
            for (int col = 0; col < 7; col++) {
                board[row][col] = " X ";
                outerShell(board);
                pawns(board);
                System.out.print(board[row][col]);
            }
            System.out.println();
        }
    }
    public static void outerShell(String[][] board){
        board[0][0] = "   ";
        board[0][1] = " A ";
        board[0][2] = " B ";
        board[0][3] = " C ";
        board[0][4] = " D ";
        board[0][5] = " E ";
        board[0][6] = " F ";
        board[1][0] = " 6 ";
        board[2][0] = " 5 ";
        board[3][0] = " 4 ";
        board[4][0] = " 3 ";
        board[5][0] = " 2 ";
        board[6][0] = " 1 ";
    }
    public static void pawns(String[][] board){
        //whitePawns
        board[1][1] = "wDw";
        board[1][2] = " wD";
        board[1][3] = " wQ";
        board[1][4] = " wK";
        board[1][5] = " wM";
        board[1][6] = " wDw ";
        //blackPawns
        board[6][1] = "bDw";
        board[6][2] = " bM";
        board[6][3] = " bK";
        board[6][4] = " bQ";
        board[6][5] = " bD";
        board[6][6] = " bDw";
    }
    public static void enterMove(){
        System.out.println("");
        System.out.println("Select the piece you want to move(Example a1) if you want to quit enter: q");
        Scanner gameInput  = new Scanner(System.in);
        String pieceSelect = gameInput.nextLine();
        if(pieceSelect.equals("q")){
            System.exit(0);
        }
        switch (pieceSelect){
            case "a1": 

        }
        System.out.println(" ");
        System.out.println("Now select a finishing position for the piece to move to");
        String piecePlace = gameInput.nextLine();
        switch (piecePlace){
            case "a2": 
        }
    }
}

In the enterMove() section, I am trying to do it with switch but have no idea how to swap two positions in the array. I am not dead set on using switch because I am not sure if it is possible at all. If someone can make a version of this code where by entering the specific coordinates(like a1) selects bDw then entering another coordinate and swapping bDw with the X i am using as an indicator for an empty space and if possible to explain it i'd be grateful.

5
  • The thing is you need to keep track of the current positions of the pawns. For that, You might need to use a key value data structure like Map in Java. Commented May 2, 2020 at 14:32
  • Alternatively, you can take the initial position (x, y) and final position(x,y) as an input. And when you are done, you can say: "Moved xx from aa to bb". Commented May 2, 2020 at 14:33
  • Yes I am trying to do the second thing suggested but have no idea how to imploment it in code. For now I want with imputs to swap the piece inputed with the position of the X inputed which indicates in my program empty space. Later I will probably need to change things such as when a piece is selected over an enemy piece to delete it as in taking it and if a piece is inputed to go on a friendly piece to not be a valid move. Commented May 2, 2020 at 14:41
  • You can take an integer input twice from the user for initial position. i.e. One for x co-ordinate and one for y co-ordinate. Later you can use this value as board[x][y] to access or change the value of the board. You can copy the value of the pawn into another variable and set board[x][y] to empty string. and set the value of board[a][b] with this var. where a,b are the final coordinates. Commented May 2, 2020 at 14:49
  • I am not sure I am really following you. I forgot to mention that I am not very experienced in programming so if you are so kind to dumb it down a bit and explain more in detail thanks... and sorry. Commented May 2, 2020 at 14:59

1 Answer 1

1

I have made a chess program before and I used to store all the pieces in an array(here in 6x6 array) and I used the index values as board coordinates. So when a piece is selected a function is called to show the valid moves of the selected piece.

The valid moves can be stored in some array and can be checked. Then just swap the values within the array itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.