Skip to content

Instantly share code, notes, and snippets.

@NguyenTatNhac
Created January 13, 2023 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NguyenTatNhac/dbe2c32878553c183a16c801c4ca4d58 to your computer and use it in GitHub Desktop.
Save NguyenTatNhac/dbe2c32878553c183a16c801c4ca4d58 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdbool.h>
struct chess {
int row;
char column;
};
bool isValid(struct chess position) {
return position.column >= 'a' && position.column <= 'h' && position.row >=1 && position.row <= 8;
}
bool knight(struct chess from, struct chess to) {
if (!isValid(from) || !isValid(to)) {
return false;
}
int rowMove = from.row - to.row;
int colMove = from.column - to.column;
if (rowMove == 1 || rowMove == -1) {
return colMove == 2 || colMove == -2;
} else if (rowMove == 2 || rowMove == -2) {
return colMove == 1 || colMove == -1;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment