Question

I just can't figure this thing out. I'm making a chess game and I need to get figures influences...

I started with a horse and got quite stuck, even after drawing it on a paper. I want that as an array:

class Horse extends Figure {
    public int[][] getRadius() {
        //Allocate a max possible count of fields
        int [][] return = new int[8][2];
        //Now what?
        ???
    }
}

This is what I made for myself on a paper, repainted in Ms-paint, in hope it will help somebody else since it left me helpless.
enter image description here
Horse is sitting in the square and I think what I've drawn are all his possible moves. Sounds like a combenation of {1,-1,2,-2} with 2 members and no repeat, doesn't it? But how to do this programatically? (the list in the array must be relative to horses actual position!) Thanks for any hint. If there is an article about other figures too, it would save me a lot of time!

If the image gives you silly ideas, please keep them for yourself, thanks.

Was it helpful?

Solution

What you want to get from getRadius() is every permutation of (x,y) with x, y ∈ {-2, -1, 1, 2} and |x| ≠ |y|.

int[][] ret = new int[8][2];
int[] set = {-2, -1, 1, 2};
int i = 0;
for(int x : set)
    for(int y : set)
        if(Math.abs(x) != Math.abs(y)){
            ret[i][0] = x;
            ret[i][1] = y;
            i++;
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top