Browse Source

Feldsicherheit König + Tests, Anton Lohrey

remotes/origin/Läufer-fertig,-Hendrik-Voß
fdai7492 11 months ago
parent
commit
b44ece9116
  1. 64
      src/Koenig.c
  2. 1
      src/Koenig.h
  3. 26
      test/test_Feldsicherheit.c

64
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

1
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

26
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
Loading…
Cancel
Save