diff --git a/src/Koenig.c b/src/Koenig.c index 16aa2c2..ae21665 100644 --- a/src/Koenig.c +++ b/src/Koenig.c @@ -140,23 +140,6 @@ bool GreiftLaeuferAn(char** Brett, int x, int y, Player player) { return false; } -bool istFeldUnsicher(char** Brett, int x, int y, Player player) { - - if (GreiftSpringerAn(Brett, x, y, player)) { - return true; - } - if (GreiftBauerAn(Brett, x, y, player)) { - return true; - } - if (GreiftTurmAn(Brett, x, y, player)) { - return true; - } - if (GreiftLaeuferAn(Brett, x, y, player)) { - return true; - } - - return false; -} bool GreiftDameAn(char** Brett, int x, int y, Player player) { char Dame = player == PLAYER_WHITE ? 'q' : 'Q'; @@ -179,6 +162,53 @@ bool GreiftDameAn(char** Brett, int x, int y, Player player) { return false; } +bool GreiftKoenigAn(char** Brett, int x, int y, Player player) { + // Mögliche Züge eines Königs + int koenigZuege[8][2] = { + {1, 0}, {-1, 0}, + {0, 1}, {0, -1}, + {1, 1}, {1, -1}, + {-1, 1}, {-1, -1} + }; + char gegnerischerKoenig = (player == PLAYER_WHITE) ? 'k' : 'K'; + + for (int i = 0; i < 8; i++) { + int neuX = x + koenigZuege[i][0]; + int neuY = y + koenigZuege[i][1]; + if (neuX >= 0 && neuX < 8 && neuY >= 0 && neuY < 8) { + if (Brett[neuY][neuX] == gegnerischerKoenig) { + return true; + } + } + } + + return false; +} + +bool istFeldUnsicher(char** Brett, int x, int y, Player player) { + + if (GreiftSpringerAn(Brett, x, y, player)) { + return true; + } + if (GreiftBauerAn(Brett, x, y, player)) { + return true; + } + if (GreiftTurmAn(Brett, x, y, player)) { + return true; + } + if (GreiftLaeuferAn(Brett, x, y, player)) { + return true; + } + if (GreiftDameAn(Brett, x, y, player)) { + return true; + } + if (GreiftKoenigAn(Brett, x, y, player)) { + return true; + } + + return false; +} + bool istzugerlaubt_Koenig(char** Brett, int startX, int startY, int endX, int endY, Player player) { // Schauen ob der zug auf dem Spielbrett passiert diff --git a/src/Koenig.h b/src/Koenig.h index b66f9d1..7dd9444 100644 --- a/src/Koenig.h +++ b/src/Koenig.h @@ -6,6 +6,7 @@ bool GreiftTurmAn(char** Brett, int x, int y, Player player); bool GreiftSpringerAn(char** Brett, int x, int y, Player player); bool GreiftLaeuferAn(char** Brett, int x, int y, Player player); bool GreiftDameAn(char** Brett, int x, int y, Player player); +bool GreiftKoenigAn(char** Brett, int x, int y, Player player); bool istFeldUnsicher(char** Brett, int x, int y, Player player); bool istzugerlaubt_Koenig(char** Brett, int startX, int startY, int endX, int endY, Player player); #endif // Koenig diff --git a/test/test_Feldsicherheit.c b/test/test_Feldsicherheit.c index 15639e2..e5b1326 100644 --- a/test/test_Feldsicherheit.c +++ b/test/test_Feldsicherheit.c @@ -136,5 +136,31 @@ void test_GreiftDameAn(void) { freeTestBrett(Brett); } +void test_GreiftKoenigAn(void) { + char** Brett = ErstelleTestBrett(); + + // Plaziert schwarzen König auf e5 (4,4) und einen weißen auf d4 (3,3) + Brett[4][4] = 'k'; + Brett[3][3] = 'K'; + print_Schachfeld(Brett); + // Test ob der schwarze könig Felder angreifen kann + TEST_ASSERT_TRUE(GreiftKoenigAn(Brett, 3, 4, PLAYER_WHITE)); // d5 + TEST_ASSERT_TRUE(GreiftKoenigAn(Brett, 5, 4, PLAYER_WHITE)); // f5 + TEST_ASSERT_TRUE(GreiftKoenigAn(Brett, 4, 3, PLAYER_WHITE)); // e4 + + // Test ob der weiße könig Felder angreifen kann + TEST_ASSERT_TRUE(GreiftKoenigAn(Brett, 2, 3, PLAYER_BLACK)); // c4 + TEST_ASSERT_TRUE(GreiftKoenigAn(Brett, 3, 2, PLAYER_BLACK)); // d3 + TEST_ASSERT_TRUE(GreiftKoenigAn(Brett, 4, 3, PLAYER_BLACK)); // e4 + + // Tests für nicht angegriffene Felder + TEST_ASSERT_FALSE(GreiftKoenigAn(Brett, 2, 4, PLAYER_WHITE)); + TEST_ASSERT_FALSE(GreiftKoenigAn(Brett, 6, 4, PLAYER_WHITE)); + TEST_ASSERT_FALSE(GreiftKoenigAn(Brett, 1, 3, PLAYER_BLACK)); + TEST_ASSERT_FALSE(GreiftKoenigAn(Brett, 5, 3, PLAYER_BLACK)); + TEST_ASSERT_FALSE(GreiftKoenigAn(Brett, 3, 1, PLAYER_BLACK)); + + freeTestBrett(Brett); +} #endif // TEST