From cef1efacb03d03cc8a4d3c307f6bcfc57869c0eb Mon Sep 17 00:00:00 2001 From: fdai7492 Date: Tue, 6 Feb 2024 04:59:56 +0000 Subject: [PATCH 1/4] =?UTF-8?q?K=C3=B6nig=20Hilfsfunktion,=20Anton=20Lohre?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Koenig.c | 18 ++++++++++++++++++ src/Koenig.h | 1 + 2 files changed, 19 insertions(+) diff --git a/src/Koenig.c b/src/Koenig.c index 19b1c0e..c51ad4a 100644 --- a/src/Koenig.c +++ b/src/Koenig.c @@ -333,3 +333,21 @@ void rochiere(char** Brett, Player player, bool kingside, Spielstatus* status) { } } } +bool KannKoenigEntkommen(char** Brett, int koenigX, int koenigY, Player player) { + int directions[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}; + for (int i = 0; i < 8; i++) { + int neuX = koenigX + directions[i][0]; + int neuY = koenigY + directions[i][1]; + + if (neuX >= 0 && neuX < 8 && neuY >= 0 && neuY < 8) { + char zielFeld = Brett[neuY][neuX]; + if ((player == PLAYER_WHITE && (zielFeld == ' ' || islower(zielFeld))) || + (player == PLAYER_BLACK && (zielFeld == ' ' || isupper(zielFeld)))) { + if (!istFeldUnsicher(Brett, neuX, neuY, player)) { + return true; + } + } + } + } + return false; +} \ No newline at end of file diff --git a/src/Koenig.h b/src/Koenig.h index 8bd6b03..cd99f44 100644 --- a/src/Koenig.h +++ b/src/Koenig.h @@ -14,4 +14,5 @@ bool istzugerlaubt_Koenig(char** Brett, int startX, int startY, int endX, int en void bewegeKoenig(char** Brett, int startX, int startY, int endX, int endY, Player player, Spielstatus* status); bool kannRochieren(char** Brett, int startX, int startY, Player player, Spielstatus* status, bool kingside); void rochiere(char** Brett, Player player, bool kingside, Spielstatus* status); +bool KannKoenigEntkommen(char** Brett, int koenigX, int koenigY, Player player); #endif // Koenig From e5d5b7240aa92eb186aa01baadeda6bb9abc724a Mon Sep 17 00:00:00 2001 From: fdai7492 Date: Tue, 6 Feb 2024 05:04:13 +0000 Subject: [PATCH 2/4] Test KannKoenigEntkommen, Anton Lohrey --- test/test_Koenig.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_Koenig.c b/test/test_Koenig.c index b36ecdb..6696839 100644 --- a/test/test_Koenig.c +++ b/test/test_Koenig.c @@ -95,4 +95,12 @@ void test_Rochieren(void) { // Clean up Schachbrettspeicher_freigeben(testBrett); } +void test_KoenigKannEntkommen(void) { + char** testBrett = Schachbrett_erstellen(); + testBrett[4][4] = 'k'; + print_Schachfeld(testBrett); + + TEST_ASSERT_TRUE(KannKoenigEntkommen(testBrett, 4, 4, PLAYER_BLACK)); + TEST_ASSERT_FALSE(KannKoenigEntkommen(testBrett, 4, 7, PLAYER_BLACK)); +} #endif // TEST From c09f13a3efa6258eec9f5f34c0643055239df91c Mon Sep 17 00:00:00 2001 From: fdai7492 Date: Tue, 6 Feb 2024 05:33:29 +0000 Subject: [PATCH 3/4] =?UTF-8?q?rudiment=C3=A4r=20istSchachmatt,=20Anton=20?= =?UTF-8?q?Lohrey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Koenig.c | 11 ++++++++++- src/Koenig.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Koenig.c b/src/Koenig.c index c51ad4a..ecd0dce 100644 --- a/src/Koenig.c +++ b/src/Koenig.c @@ -350,4 +350,13 @@ bool KannKoenigEntkommen(char** Brett, int koenigX, int koenigY, Player player) } } return false; -} \ No newline at end of file +} +bool istSchachmatt(char** Brett, int koenigX, int koenigY, Player player) { + // Überprüfen, ob der König im Schach steht + if (istKoenigImSchach(Brett, player)) { + return true; // Nicht Schachmatt, wenn der König nicht im Schach steht. + } + + // Überprüfen, ob der König entkommen kann + return !KannKoenigEntkommen(Brett, koenigX, koenigY, player); +} diff --git a/src/Koenig.h b/src/Koenig.h index cd99f44..f2c2637 100644 --- a/src/Koenig.h +++ b/src/Koenig.h @@ -15,4 +15,5 @@ void bewegeKoenig(char** Brett, int startX, int startY, int endX, int endY, Play bool kannRochieren(char** Brett, int startX, int startY, Player player, Spielstatus* status, bool kingside); void rochiere(char** Brett, Player player, bool kingside, Spielstatus* status); bool KannKoenigEntkommen(char** Brett, int koenigX, int koenigY, Player player); +bool istSchachmatt(char** Brett, int koenigX, int koenigY, Player player); #endif // Koenig From 5e43b25604433988fff84f7670e38fd07ce05b60 Mon Sep 17 00:00:00 2001 From: fdai7492 Date: Tue, 6 Feb 2024 05:35:20 +0000 Subject: [PATCH 4/4] istschachmatt Test, Anton Lohrey --- test/test_Koenig.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_Koenig.c b/test/test_Koenig.c index 6696839..a0ae758 100644 --- a/test/test_Koenig.c +++ b/test/test_Koenig.c @@ -103,4 +103,20 @@ void test_KoenigKannEntkommen(void) { TEST_ASSERT_TRUE(KannKoenigEntkommen(testBrett, 4, 4, PLAYER_BLACK)); TEST_ASSERT_FALSE(KannKoenigEntkommen(testBrett, 4, 7, PLAYER_BLACK)); } +void test_KingInCheckmate(void) { + char** testBrett = Schachbrett_erstellen(); + testBrett[4][4] = 'K'; + + TEST_ASSERT_FALSE(istSchachmatt(testBrett, 4, 4 ,PLAYER_WHITE)); + testBrett[4][0] = 'q'; + testBrett[5][0] = 'q'; + TEST_ASSERT_FALSE(istSchachmatt(testBrett, 4, 4 ,PLAYER_WHITE)); + testBrett[3][0] = 'q'; + testBrett[3][7] = 'q'; + testBrett[4][7] = 'q'; + testBrett[5][7] = 'q'; + testBrett[7][4] = ' '; + print_Schachfeld(testBrett); + TEST_ASSERT_TRUE(istSchachmatt(testBrett, 4, 4 ,PLAYER_WHITE)); +} #endif // TEST