Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Straight and diagonal interface #10

Closed Starlight-30036225 closed 7 months ago

Starlight-30036225 commented 7 months ago

These interfaces will be used by the rook and bishop respectively, then combined to make the queens movement.

I have started work on the straight interface.

Starlight-30036225 commented 7 months ago

I have created the horizontal movement for the straight interface, but I am not very happy with the code. I am sure there is a way to simplify this and bring it together into one function, but for now this will do:

`List moves = new ArrayList<>(); //return val

    for (int i = piece.x + 1; i < 8; i++) { //To the right of the board

        Piece temp = board[i][piece.y];

        if (temp != null && temp.white == piece.white) {
            break;      //The piece here is the same colour, cant move.
        }

        moves.add((i) + "" + piece.y); //can move here

        if (temp != null) {
            break;      //opposite colour, take
        }
    }
    for (int i = piece.x - 1; i > -1; i--) {    //To the left of the board

        Piece temp = board[i][piece.y];

        if (temp != null && temp.white == piece.white) {
            break;      //The piece here is the same colour, cant move.
        }

        moves.add((i) + "" + piece.y); //can move here, its an empty space

        if (temp != null) {
            break;      //The piece here can be taken, but must stop here.
        }
    }
    return moves;`

I will just repeat the same with Y for vertical movement

Starlight-30036225 commented 7 months ago

Diagonal is an even bigger mess, I will NEED to come back to tidy this up later if for nothing else but my pride. But it works.

Diagonals work. (Somehow) `List moves = new ArrayList<>(); //return val { //right for (int i = 1; i < 8; i++) { //Up in both axis if(piece.x + i > 7 || piece.y + i > 7){break;} //Feels very illegal, gatekeeping

            Piece temp = board[piece.x + i][piece.y + i];

            if (temp != null && temp.white == piece.white) {
                break;      //The piece here is the same colour, cant move.
            }

            moves.add((piece.x + i) + "" + (piece.y + i)); //can move here

            if (temp != null) {
                break;      //opposite colour, take
            }
        }
        for (int i = 1; i < 8; i++) {    //up x , down Y
            if(piece.x + i > 7 || piece.y - i < 0) {break;}  //Feels very illegal, gatekeeping

            Piece temp = board[piece.x + i][piece.y - i];

            if (temp != null && temp.white == piece.white) {
                break;      //The piece here is the same colour, cant move.
            }

            moves.add((piece.x + i) + "" + (piece.y - i)); //can move here, its an empty space

            if (temp != null) {
                break;      //The piece here can be taken, but must stop here.
            }
        }
    }`

The same is repeated for moving left.

Starlight-30036225 commented 7 months ago

This must be able to be simplified. I am going to make another piece with this messy method, then find a way to simplify them both in one method.

Starlight-30036225 commented 7 months ago

Through my work here: #13 , I have made it so these functions can be greatly simplified. Through the use of the validate move function, i can check if a space is free or take-able easier

image

This is my new idea, It uses the validate move function to test if the space is empty or is taking a piece, then uses a secondary check to test if a piece is being taken, this should work?

I will use it for all diagonal, then report back.

Starlight-30036225 commented 7 months ago

Works perfectly, Could probably be tidied up even more

Starlight-30036225 commented 7 months ago

image This is a little nicer and easier to read, now the calculation is run once per loop instead of twice. Which means nothing but looks better.

Starlight-30036225 commented 7 months ago

It has been simplified even further to thus:

image I think calling the internal function Validate move to reflect the function inside the piece may be a bit confusing as they are not truly related, one just calls the other. So I Should really rename it slightly.

Tho now saying this, as i moved it to the new function and cut it down, It can probably just be moved back into the while condition as its only a little bit longer than whats currently in there, saving on a whole confucing function

Starlight-30036225 commented 7 months ago

I have chosen to keep it, as moving it back makes the x and y variables slightly messy, And now renamed it to 'CheckEndOfPath'

Starlight-30036225 commented 7 months ago

Here is a bishop using the diagonal interface: image

Here is a rook using the straight interface: image

And here is a queen using both: image