# How to model chess knight movement in GameMaker?

I am making a chess game in GameMaker, and I have the basic movement down for all of the other pieces except for the knight. I am using a system where I have an if statement that checks if the mouse is in a location that it can move too, then if it moves the piece there. For example, the rook code is (mouse_y div 64) *64 == y || (mouse_x div 64) * 64 == x. What would be an expression that would work for knights? Would this strategy even work for knights?

I imagine you have to do 8 expressions. All expressions using the x and y axes:

if (mouse_x div 64) * 64 == x+64 && (mouse_y div 64) * 64 == y-128 ||
(mouse_x div 64) * 64 == x+128 && (mouse_y div 64) * 64 == y-64 ||
(mouse_x div 64) * 64 == x+128 && (mouse_y div 64) * 64 == y+64 ||
(mouse_x div 64) * 64 == x+64 && (mouse_y div 64) * 64 == y+128 ||

(mouse_x div 64) * 64 == x-64 && (mouse_y div 64) * 64 == y+128 ||
(mouse_x div 64) * 64 == x-128 && (mouse_y div 64) * 64 == y+64 ||
(mouse_x div 64) * 64 == x-128 && (mouse_y div 64) * 64 == y-64 ||
(mouse_x div 64) * 64 == x-64 && (mouse_y div 64) * 64 == y-128 {


This way you check every possible point.

Borrowing the diagram from disc_code22's answer, let's overlay it with some numbers. For each square, we're going to count how many squares it is left or right from the knight, and square it; then do the same for up/down; then add the two numbers together:

You might recognize this as Pythagorean theorem, just without the square root (we don't need it if we just want to tell the squares apart).

Knight moves that travel 2 out and 1 over are the only moves on the chess board with squared length exactly 5.

So, we can do something like this:

travelX = (mouse_x div 64) - (x div 64)

travelY = (mouse_y div 64) - (y div 64)


With these variables in hand, now we can write a variety of piece moves as one-liners:

• Knight: travelX * travelX + travelY * travelY == 5

• Rook: travelX == 0 || travelY == 0

• Bishop: travelX == travelY || travelX == -travelY

• King: travelX * travelX + travelY * travelY < 3

There are quite a few approaches you could take to validating this kind of movement, so you should choose whatever is cohesive with the style of your movement validation so far, but I'll suggest a simple approach and perhaps you and other answerers can build on it.

If your knight is at a position, lets say (x,y) on the chess board/grid, lets also imagine a second version of this grid that is relative to the knights position. To tie this in with the example image, if we drew these relative grid labels on the example image, the knight's position would be labeled (0,0), and the grid would range through (+/-2, +/-2).

Using these relative positions can make it a lot simpler to determine which knight moves are valid. Lets consider the movement where the knight heads north and then east. In the example image's grid this is the move that ends up on (3,0), but in our imaginary relative grid this would end up at (1, 2).

Since there are only 8 valid moves maximum for a knight, the simplest approach is likely to hard code these valid relative moves, then check the players attempted move against the list of valid relative positions representing knight's possible moves. The valid positions could most concisely be stated as (+/-1, +/-2) and (+/-2, +/-1)

So in conclusion, this approach simplifies stating which moves are valid for the knight, but adds in some trickiness about transforming between the absolute and relative grid so you can compare the players absolute grid mouse clicks with the relative grid moveset of the knight.

There are certainly other approaches to this problem but I hope this one is enough to get you started on tackling the issue.

If you are looking for more reading including some different approaches to this issue as well as some code samples, check out this Geeks4Geeks article on coding chess knight movement