1
\$\begingroup\$

I wanted conditions that could determine which direction (north, south, east, west, northwest, northeast, southeast, southwest) the player is following, what I got was just north, south, east and west, as shown in the code below:

var playerX = player.x, playerY = player.y;

var destX = 8, destY = -15;

    if (playerX < destX) {
      playerX += 0.5; // east
    } else if (playerX > destX) {
      playerX -= 0.5; // west
    } else {
      playerX = destX; // maintains direction
    }
    if (playerY < destY) {
      playerY += 0.5; // north
    } else if (playerY > destY) {
      playerY -= 0.5; // south
    } else {
      playerY = destY; //maintains direction
    }

image example: enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ What are destX and destY, playerX and player, and player.x and player.y? What do they represent? Why do you hardcode 0.5, what is this value representing? \$\endgroup\$ – Vaillancourt Mar 23 '17 at 2:11
  • \$\begingroup\$ what do you need exactly ! what is your game engine? can you share picture that show your problem? \$\endgroup\$ – Seyed Morteza Kamali Mar 23 '17 at 2:53
  • \$\begingroup\$ I'm a bit confused. It looks like this code already computes movement correctly for northwest, northeast, etc. like you say you want. To take an example, if playerX < destX and playerY < destY then the player will move positively in both the x and y axes (ie. northeast). Have I misunderstood your code or the question you're asking? \$\endgroup\$ – DMGregory Mar 23 '17 at 2:56
  • \$\begingroup\$ Currently I have 4 directions (north, south, east and west), and I need to work like 4 others (northwest, northeast, southeast, southwest). TOTALLY 8 DIRECTIONS, BUT I JUST HAVE 4! I updated the post with the improved code, and also a representative image. \$\endgroup\$ – Lucas Mar 23 '17 at 3:03
  • \$\begingroup\$ I'm using three.js, but I do not think it matters much. Since this is a basic Cartesian problem, but I'm not in the mood. I'M SORRY, I'M USING A TRANSLATOR, I'M A BRAZILIAN. \$\endgroup\$ – Lucas Mar 23 '17 at 3:05
1
\$\begingroup\$

I solved this code:

var lastX, lastY;
function geraDirecao(player) {
    var playerX = player.position.x,
        playerY = player.position.y;


    if(playerX < lastX) {
        if (playerY < lastY) {
            // top left 
        } else if (playerY == lastY) {
             // left
        } else { // playerY > lastY
            // bottom left
        }
    } else
    if (playerX == lastX) {
        if (playerY < lastY) {
            // top
        } else if (playerY == lastY) {
            // Not allowed
        } else { // playerY > lastY
            // bottom
        }
    } else { // playerX > lastX
        if (playerY < lastY) {
            // top right
        } else if (playerY == lastY) {
            // right
        } else {// playerY > lastY
            // bottom right
        }
    }

    lastX = playerX;
    lastY = playerY;
}
\$\endgroup\$
1
1
\$\begingroup\$

king,bishop,rook

Imagine your player is a chess piece on a chessboard then look at the picture how every chess piece are moving and which field are valid fields for each piece, How ever your problem is more look like a Queen on a chess board because can move on every direction and you can adapt this algorithm to your player. you need array to loop through every row and column.

Here is the code for how a Queen move on a chessboard.

 public class Queen extends Chesspiece {

    protected Queen(char color, char name) { 
        super(color, name);
    }

    @Override 
    public void markReachableFields() { 
        char rw = row;
        byte col = column;

        for (char i = 'a'; i <='h'; i++) {
            rw = (char)i;       
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].mark();
            }
        }
        col = column;
        rw = row;
        for (byte j = 0; j < 8; j++) {
            col = (byte)j;
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].mark();
            }
        }
        rw = row;
        col = column;
        for (int i = 0; i < 8; i++) {
            rw = (char)(rw - 1);
            col = (byte)(col + 1);

            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].mark();
            }
        }
        rw = row;
        col = column;

        for (int i = 1; i <= 8; i++) {
            rw = (char)(rw + 1);
            col = (byte)(col + 1);
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].mark();
            }
        }
         rw = row;
         col = column;

        for (int i = 1; i <= 8; i++) {
            rw = (char)(rw + 1);
            col = (byte)(col - 1);
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].mark();
            }
        }

         rw = row;
         col = column;
        for (int i = 1; i <= 8; i++) {
            rw = (char)(rw - 1);
            col = (byte)(col - 1);
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].mark();

            }
        }
    }
    @Override 
    public void unmarkReachableFields() {
        char rw = row;
        byte col = column;

        for (char i = 'a'; i <='h'; i++) {
            rw = (char)i;       
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].unmark();
            }
        }
        col = column;
        rw = row;
        for (byte j = 0; j < 8; j++) {
            col = (byte)j;
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].unmark();
            }
        }
        rw = row;
        col = column;
        for (int i = 0; i < 8; i++) {
            rw = (char)(rw - 1);
            col = (byte)(col + 1);

            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].unmark();
            }
        }
        rw = row;
        col = column;

        for (int i = 1; i <= 8; i++) {
            rw = (char)(rw + 1);
            col = (byte)(col + 1);
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].unmark();
            }
        }
         rw = row;
         col = column;

        for (int i = 1; i <= 8; i++) {
            rw = (char)(rw + 1);
            col = (byte)(col - 1);
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].unmark();
            }
        }

         rw = row;
         col = column;
        for (int i = 1; i <= 8; i++) {
            rw = (char)(rw - 1);
            col = (byte)(col - 1);
            if ((rw >= 'a' && rw <= 'h') && (col >= 1 && col <= 8)
                    &&Chessboard.this.isValidField(rw, col)){
                int r = rw - FIRST_ROW;
                int c = col - FIRST_COLUMN;
                Chessboard.this.fields[r][c].unmark();

            }
        }
    }
 }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.