Created
January 13, 2023 14:21
-
-
Save NguyenTatNhac/dbe2c32878553c183a16c801c4ca4d58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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