Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Possible moves #12

Closed Starlight-30036225 closed 9 months ago

Starlight-30036225 commented 9 months ago

When the player selects a piece, I want the server to return the possible moves that piece could make. These moves should then be displayed to the screen.

Starlight-30036225 commented 9 months ago

Selecting a piece has already been created by the 'mouse pressed' event listener here: `public void mousePressed(MouseEvent e) { //Used for dragging

            //gets current mouse pos
            int MouseX = Board.MouseX = e.getX();
            int MouseY = Board.MouseY = e.getY();

            Board.MouseX -= 32;
            Board.MouseY -= 64; //tells the display where the mouse is, so it can display the selected piece over the mouse

            //Force Mouse to be in range
            if (MouseY > Board.BOARD_HEIGHT + Board.MARGIN) {MouseY = Board.BOARD_HEIGHT + Board.MARGIN;} else if (MouseY < 0) { MouseY =0;}
            if (MouseX > Board.BOARD_WIDTH + Board.MARGIN) {MouseX = Board.BOARD_WIDTH + Board.MARGIN;} else if (MouseX < 0) { MouseX =0;}

            //Convert mouse coords to board coords (plus 3 just seems to work)
            MouseX -= Board.MARGIN + 3;
            MouseY -= Board.MARGIN + 3;

            MouseX /= Board.SQUARE_SIZE;
            MouseY /= Board.SQUARE_SIZE;

            if (MouseX >= 8 || MouseY >= 8) {return;}   //gatekeeping if mouse is out of range of array
            Board.SelectedPiece = Board.Board[MouseX][MouseY];
            if (Board.SelectedPiece == null) {return;}      //not clicking a piece

            //Sends piece to server
            client.sendMessage(PacketHeader.SELECT_PIECE, Board.SelectedPiece.getLocation());
        }`

Translating the mouse position into a board location is a bit messy, I might need to come back later with and look into why 3 helps but isnt quite right.

Now onto the server:

` private void HandleSelect_Piece(ConnectionHandler Handler) { String Location = Handler.readNextString();

    //seperate location into x and y components
    int x = Character.getNumericValue(Location.charAt(0));
    int y = Character.getNumericValue(Location.charAt(1));

    String CompositeMoveString = "";    //will hold all possible tiles the piece can move too
    for (String Move :game.getPossibleMoves(x,y)){  //loops through all moves found from pieces function
        CompositeMoveString += Move;

    }
    //sends all moves back to client
    Handler.sendMessage(PacketHeader.POSSIBLE_MOVES, CompositeMoveString);
}`

The getPossibleMoves function is handled inside the piece class and will return an list of 2 character strings. (Not completely implemented yet. This should be returned straight back to the client.

Starlight-30036225 commented 9 months ago

As the move List is send to the client as a string instead of a a list, the first thing i need to do is extract it. So after receiving the Possible_Moves packet header, the client calls this function:

` private void SeperateMoveList() {

    //move list is recieved as one long string
    List<String> MoveList = new ArrayList<>();
    String PossibleMoves = readNextString();

    for (int i = 0; i < PossibleMoves.length() - 1; i += 2){
        MoveList.add(PossibleMoves.substring(i, i+2));  //takes every set of 2 characters and adds them to the list
    }
    Master.receivePossibleMoves(MoveList);  //sends resulting list to master object
}`

Then the master object has a very simple job, just set the possible moves list to the received list: public void receivePossibleMoves(List<String> possibleMoves) { Board.possibleMoves = possibleMoves; Board.frame.repaint(); }

It also takes the opportunity to redraw the board, this doesnt matter until I implement highlighting the possible moves. Which I will open as a new issue.