Browse Source
Merge branch 'fdai7492' into 'main'
Merge branch 'fdai7492' into 'main'
Implementation von Springer und Anfang König See merge request fdai7834/taktikmeister!2remotes/origin/Läufer-fertig,-Hendrik-Voß
fdai7492
11 months ago
12 changed files with 648 additions and 7 deletions
-
236src/Koenig.c
-
12src/Koenig.h
-
6src/Schachbrett.c
-
2src/Schachbrett.h
-
59src/Springer.c
-
7src/Springer.h
-
2src/main.c
-
2test/test_Bauer.c
-
195test/test_Feldsicherheit.c
-
48test/test_Koenig.c
-
2test/test_Moving.c
-
84test/test_Springer.c
@ -0,0 +1,236 @@ |
|||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdbool.h> |
||||
|
#include <ctype.h> |
||||
|
#include "spieler.h" |
||||
|
#include "Moving.h" |
||||
|
#include "Koenig.h" |
||||
|
|
||||
|
bool GreiftBauerAn(char** Brett, int x, int y, Player player) { |
||||
|
// Checke für weißen Bauer |
||||
|
if (player == PLAYER_BLACK) { |
||||
|
if (y > 0 && x > 0 && Brett[y - 1][x - 1] == 'P') { // Linke Diagonale |
||||
|
return true; |
||||
|
} |
||||
|
if (y > 0 && x < 7 && Brett[y - 1][x + 1] == 'P') { // Rechte Diagonale |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
// Checke für schwarzen Bauer |
||||
|
if (player == PLAYER_WHITE) { |
||||
|
if (y < 7 && x > 0 && Brett[y + 1][x - 1] == 'p') { // Linke Diagonale |
||||
|
return true; |
||||
|
} |
||||
|
if (y < 7 && x < 7 && Brett[y + 1][x + 1] == 'p') { // Rechte Diagonale |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool GreiftTurmAn(char** Brett, int x, int y, Player player) { |
||||
|
char Turm = player == PLAYER_WHITE ? 'r' : 'R'; |
||||
|
|
||||
|
// Check Vertikal Hoch |
||||
|
for (int i = y - 1; i >= 0; i--) { |
||||
|
if (Brett[i][x] != ' ') { |
||||
|
if (Brett[i][x] == Turm) { |
||||
|
return true; |
||||
|
} |
||||
|
break; // Stoppt wenn irgendeine Figur gefunden wird |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Vertikal Runter |
||||
|
for (int i = y + 1; i < 8; i++) { |
||||
|
if (Brett[i][x] != ' ') { |
||||
|
if (Brett[i][x] == Turm) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Horizontal Links |
||||
|
for (int j = x - 1; j >= 0; j--) { |
||||
|
if (Brett[y][j] != ' ') { |
||||
|
if (Brett[y][j] == Turm) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Horizontal Rechts |
||||
|
for (int j = x + 1; j < 8; j++) { |
||||
|
if (Brett[y][j] != ' ') { |
||||
|
if (Brett[y][j] == Turm) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool GreiftSpringerAn(char** Brett, int x, int y, Player player) { |
||||
|
int Springerzüge[8][2] = { |
||||
|
{2, 1}, {1, 2}, {-1, 2}, {-2, 1}, |
||||
|
{-2, -1}, {-1, -2}, {1, -2}, {2, -1} |
||||
|
}; |
||||
|
|
||||
|
for (int i = 0; i < 8; i++) { |
||||
|
int neuX = x + Springerzüge[i][0]; |
||||
|
int neuY = y + Springerzüge[i][1]; |
||||
|
if (neuX >= 0 && neuX < 8 && neuY >= 0 && neuY < 8) { |
||||
|
char Figur = Brett[neuY][neuX]; |
||||
|
if (player == PLAYER_WHITE && Figur == 'n') { |
||||
|
return true; |
||||
|
} else if (player == PLAYER_BLACK && Figur == 'N') { |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool GreiftLaeuferAn(char** Brett, int x, int y, Player player) { |
||||
|
char Laeufer = player == PLAYER_WHITE ? 'b' : 'B'; |
||||
|
|
||||
|
// Check oben Rechts |
||||
|
for (int i = x + 1, j = y - 1; i < 8 && j >= 0; i++, j--) { |
||||
|
if (Brett[j][i] != ' ') { |
||||
|
if (Brett[j][i] == Laeufer) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Check oben Links |
||||
|
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { |
||||
|
if (Brett[j][i] != ' ') { |
||||
|
if (Brett[j][i] == Laeufer) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Check unten Rechts |
||||
|
for (int i = x + 1, j = y + 1; i < 8 && j < 8; i++, j++) { |
||||
|
if (Brett[j][i] != ' ') { |
||||
|
if (Brett[j][i] == Laeufer) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Check unten Links |
||||
|
for (int i = x - 1, j = y + 1; i >= 0 && j < 8; i--, j++) { |
||||
|
if (Brett[j][i] != ' ') { |
||||
|
if (Brett[j][i] == Laeufer) { |
||||
|
return true; |
||||
|
} |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
bool GreiftDameAn(char** Brett, int x, int y, Player player) { |
||||
|
char Dame = player == PLAYER_WHITE ? 'q' : 'Q'; |
||||
|
|
||||
|
//Schaut in alle Richtungen |
||||
|
int Richtungen[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; |
||||
|
for (int i = 0; i < 8; i++) { |
||||
|
int dx = Richtungen[i][0]; |
||||
|
int dy = Richtungen[i][1]; |
||||
|
int nx = x + dx; |
||||
|
int ny = y + dy; |
||||
|
while (nx >= 0 && nx < 8 && ny >= 0 && ny < 8) { |
||||
|
if (Brett[ny][nx] != ' ') { |
||||
|
if (Brett[ny][nx] == Dame) return true; |
||||
|
break; |
||||
|
} |
||||
|
nx += dx; |
||||
|
ny += dy; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
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 |
||||
|
if (endX < 0 || endX >= 8 || endY < 0 || endY >= 8) |
||||
|
return false; |
||||
|
|
||||
|
// Berechnung des unterschieds der start und endpositionen |
||||
|
int dx = abs(endX - startX); |
||||
|
int dy = abs(endY - startY); |
||||
|
|
||||
|
// Schauen ob sich nur ein feld bewegt wird |
||||
|
if (dx > 1 || dy > 1) return false; |
||||
|
|
||||
|
// Nicht auf befreundete Figuren bewegen |
||||
|
char endPosition = Brett[endY][endX]; |
||||
|
if (player == PLAYER_WHITE && isupper(endPosition)) |
||||
|
return false; |
||||
|
if (player == PLAYER_BLACK && islower(endPosition)) |
||||
|
return false; |
||||
|
|
||||
|
if (istFeldUnsicher(Brett, endX, endY, player)) |
||||
|
return false; |
||||
|
|
||||
|
return true; |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
#ifndef Koenig |
||||
|
#define Koenig |
||||
|
#include "Spieler.h" |
||||
|
bool GreiftBauerAn(char** Brett, int x, int y, Player player); |
||||
|
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 |
@ -0,0 +1,59 @@ |
|||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdbool.h> |
||||
|
#include <ctype.h> |
||||
|
#include "spieler.h" |
||||
|
#include "Moving.h" |
||||
|
#include "Springer.h" |
||||
|
|
||||
|
bool istzugerlaubt_Springer(char** Brett, int startX, int startY, int endX, int endY, Player player) { |
||||
|
// Schauen ob der zug auf dem Spielbrett passiert |
||||
|
if (endX < 0 || endX >= 8 || endY < 0 || endY >= 8) { |
||||
|
return false; |
||||
|
} |
||||
|
// Berechnung des unterschieds der start und endpositionen |
||||
|
int dx = abs(endX - startX); |
||||
|
int dy = abs(endY - startY); |
||||
|
|
||||
|
|
||||
|
|
||||
|
// Schaut ob die bewegung eine L-form hat |
||||
|
if ((dx == 2 && dy == 1) || (dx == 1 && dy == 2)) { |
||||
|
|
||||
|
char EndPosition = Brett[endY][endX]; |
||||
|
//schaut ob die Endposition von einer eigenen Figur belegt ist |
||||
|
if (player == PLAYER_WHITE) { |
||||
|
if (isupper(EndPosition) && EndPosition != ' ') { |
||||
|
return false; |
||||
|
} |
||||
|
} else { // PLAYER_BLACK |
||||
|
if (islower(EndPosition) && EndPosition != ' ') { |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (player == PLAYER_WHITE && islower(EndPosition)) { |
||||
|
return true; // Erlaubter zug weil die Position leer ist oder von einer gegnerischen Figur belegt ist |
||||
|
} else if (player == PLAYER_BLACK && isupper(EndPosition)) { |
||||
|
return true; // Erlaubter zug weil die Position leer ist oder von einer gegnerischen Figur belegt ist |
||||
|
} else if (EndPosition == ' ') { |
||||
|
return true; // Erlaubter zug weil die Zielposition leer ist |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false; // kein erlaubter zug |
||||
|
} |
||||
|
bool bewegeSpringer(char** Brett, int startX, int startY, int endX, int endY, Player player) { |
||||
|
// Überprüfen ob der Zug legal ist |
||||
|
if (!istzugerlaubt_Springer(Brett, startX, startY, endX, endY, player)) { |
||||
|
return false; // Zug ist nicht erlaubt |
||||
|
} |
||||
|
|
||||
|
char springer = Brett[startY][startX]; |
||||
|
|
||||
|
// Aktualisiere das Brett. |
||||
|
Brett[startY][startX] = ' '; |
||||
|
Brett[endY][endX] = springer; |
||||
|
|
||||
|
return true; |
||||
|
} |
@ -0,0 +1,7 @@ |
|||||
|
#ifndef Springer |
||||
|
#define Springer |
||||
|
#include "spieler.h" |
||||
|
|
||||
|
bool istzugerlaubt_Springer(char** Brett, int startX, int startY, int endX, int endY, Player player); |
||||
|
bool bewegeSpringer(char** Brett, int startX, int startY, int endX, int endY, Player player); |
||||
|
#endif // Springer |
@ -0,0 +1,195 @@ |
|||||
|
#ifdef TEST |
||||
|
#include "unity.h" |
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdbool.h> |
||||
|
#include "Spieler.h" |
||||
|
#include "Koenig.h" |
||||
|
|
||||
|
char** ErstelleTestBrett() { |
||||
|
char** Brett = malloc(8 * sizeof(char*)); |
||||
|
for (int i = 0; i < 8; i++) { |
||||
|
Brett[i] = malloc(8 * sizeof(char)); |
||||
|
for (int j = 0; j < 8; j++) { |
||||
|
Brett[i][j] = ' '; //Erstelle komplett leeres Brett |
||||
|
} |
||||
|
} |
||||
|
return Brett; |
||||
|
} |
||||
|
|
||||
|
void freeTestBrett(char** Brett) { |
||||
|
for (int i = 0; i < 8; i++) { |
||||
|
free(Brett[i]); |
||||
|
} |
||||
|
free(Brett); |
||||
|
} |
||||
|
|
||||
|
void test_GreiftBauerAn(void) { |
||||
|
char** Brett = ErstelleTestBrett(); |
||||
|
|
||||
|
// Plaziert weißen Bauer auf d5 (3,4) und testet 'angriff' auf c6 (2,5) und e6 (4,5) |
||||
|
Brett[4][3] = 'P'; |
||||
|
Player player = PLAYER_BLACK; |
||||
|
TEST_ASSERT_TRUE(GreiftBauerAn(Brett, 2, 5, player)); |
||||
|
TEST_ASSERT_TRUE(GreiftBauerAn(Brett, 4, 5, player)); |
||||
|
|
||||
|
// Plaziert schwarzen Bauer auf e4 (4,3) und testet 'angriff' auf d3 (3,2) und f3 (5,2) |
||||
|
Brett[3][4] = 'p'; |
||||
|
player = PLAYER_WHITE; |
||||
|
TEST_ASSERT_TRUE(GreiftBauerAn(Brett, 3, 2, player)); |
||||
|
TEST_ASSERT_TRUE(GreiftBauerAn(Brett, 5, 2, player)); |
||||
|
|
||||
|
// Test ein Feld ohne Bedrohung |
||||
|
TEST_ASSERT_FALSE(GreiftBauerAn(Brett, 0, 0, player)); |
||||
|
|
||||
|
freeTestBrett(Brett); |
||||
|
} |
||||
|
|
||||
|
void test_GreiftTurmAn(void) { |
||||
|
char** Brett = ErstelleTestBrett(); |
||||
|
|
||||
|
// Plaziert weißen Turm auf d4 (3,3) und testet für Angriffe |
||||
|
Brett[3][3] = 'R'; |
||||
|
Player player = PLAYER_BLACK; |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 3, 0, player)); // Checkt vertikalen Angriff auf d1 |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 3, 7, player)); // Checkt vertikalen Angriff auf d8 |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 0, 3, player)); // Checkt horizontalen Angriff auf a4 |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 7, 3, player)); // Checkt horizontalen Angriff auf h4 |
||||
|
|
||||
|
// Plaziert schwarzen Turm auf e5 (4,4) und Tauscht Spieler zu weiß |
||||
|
Brett[3][3] = ' '; // Alten turm wegmachen |
||||
|
Brett[4][4] = 'r'; |
||||
|
player = PLAYER_WHITE; |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 4, 0, player)); // Checkt vertikalen Angriff auf e1 |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 4, 7, player)); // Checkt vertikalen Angriff auf e8 |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 0, 4, player)); // Checkt horizontalen Angriff auf a5 |
||||
|
TEST_ASSERT_TRUE(GreiftTurmAn(Brett, 7, 4, player)); // Checkt horizontalen Angriff auf h5 |
||||
|
|
||||
|
// Checkt sicheres Feld |
||||
|
TEST_ASSERT_FALSE(GreiftTurmAn(Brett, 0, 0, player)); |
||||
|
|
||||
|
freeTestBrett(Brett); |
||||
|
} |
||||
|
|
||||
|
void test_GreiftSpringerAn(void) { |
||||
|
char** Brett = ErstelleTestBrett(); |
||||
|
|
||||
|
// Plaziert ein schwarzen Springer (3,4) und testet 'Angriff' auf e3 (4,2) |
||||
|
Brett[4][3] = 'n'; |
||||
|
Player player = PLAYER_WHITE; |
||||
|
TEST_ASSERT_TRUE(GreiftSpringerAn(Brett, 4, 2, player)); |
||||
|
|
||||
|
// Plaziert ein weißen Springer (5,2) und testet 'Angriff' auf e5 (4,4) |
||||
|
Brett[2][5] = 'N'; |
||||
|
player = PLAYER_BLACK; |
||||
|
TEST_ASSERT_TRUE(GreiftSpringerAn(Brett, 4, 4, player)); |
||||
|
|
||||
|
// Testet Feld ohne Bedrohung |
||||
|
TEST_ASSERT_FALSE(GreiftSpringerAn(Brett, 0, 0, player)); |
||||
|
|
||||
|
freeTestBrett(Brett); |
||||
|
} |
||||
|
void test_GreiftLaeuferAn(void) { |
||||
|
char** Brett = ErstelleTestBrett(); |
||||
|
|
||||
|
// Plaziert einen weißen Läufer auf c4 (2,3) und testet Angriffe |
||||
|
Brett[3][2] = 'B'; |
||||
|
|
||||
|
Player player = PLAYER_BLACK; |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 0, 1, player)); // Check unten Links |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 5, 0, player)); // Check oben Links |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 4, 5, player)); // Check unten Rechts |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 3, 2, player)); // Check oben Rechts |
||||
|
|
||||
|
// Plaziert einen schwarzen Läufer auf e5 (4,4) und testet Angriffe vom anderen spieler |
||||
|
Brett[2][3] = ' '; |
||||
|
Brett[4][4] = 'b'; |
||||
|
player = PLAYER_WHITE; |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 2, 2, player)); // Check unten Rechts |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 2, 6, player)); // Check oben Links |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 6, 6, player)); // Check unten Rechts |
||||
|
TEST_ASSERT_TRUE(GreiftLaeuferAn(Brett, 6, 2, player)); // Check oben Rechts |
||||
|
|
||||
|
// Check sicheres Feld |
||||
|
TEST_ASSERT_FALSE(GreiftLaeuferAn(Brett, 0, 1, player)); |
||||
|
|
||||
|
freeTestBrett(Brett); |
||||
|
} |
||||
|
void test_GreiftDameAn(void) { |
||||
|
char** Brett = ErstelleTestBrett(); |
||||
|
|
||||
|
// Plaziert weiße dame auf d4 |
||||
|
Brett[3][3] = 'Q'; |
||||
|
Player player = PLAYER_BLACK; |
||||
|
|
||||
|
// Test Oben/Seitlich |
||||
|
TEST_ASSERT_TRUE(GreiftDameAn(Brett, 3, 7, player)); // d8 |
||||
|
TEST_ASSERT_TRUE(GreiftDameAn(Brett, 0, 3, player)); // a4 |
||||
|
|
||||
|
// Test Diagonal |
||||
|
TEST_ASSERT_TRUE(GreiftDameAn(Brett, 1, 5, player)); // b6 |
||||
|
|
||||
|
// Plaziert eigene Figur zum blocken |
||||
|
Brett[4][3] = 'P'; |
||||
|
// Test blockiert von eigener Figur |
||||
|
TEST_ASSERT_FALSE(GreiftDameAn(Brett, 3, 7, player)); // d8 blocked |
||||
|
|
||||
|
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); |
||||
|
} |
||||
|
void test_istFeldUnsicher(void) { |
||||
|
char** Brett = ErstelleTestBrett(); |
||||
|
Brett[4][4] = 'p'; |
||||
|
print_Schachfeld(Brett); |
||||
|
TEST_ASSERT_TRUE(istFeldUnsicher(Brett, 5, 3, player)); |
||||
|
TEST_ASSERT_FALSE(istFeldUnsicher(Brett, 0, 0, player)); |
||||
|
|
||||
|
Brett[4][4] = ' '; |
||||
|
Brett[0][1] = 'N'; |
||||
|
player = PLAYER_BLACK; |
||||
|
|
||||
|
TEST_ASSERT_TRUE(istFeldUnsicher(Brett, 2, 2, player)); |
||||
|
TEST_ASSERT_FALSE(istFeldUnsicher(Brett, 0, 0, player)); |
||||
|
|
||||
|
Brett[0][1] = ' '; |
||||
|
Brett[7][0] = 'r'; |
||||
|
player = PLAYER_WHITE; |
||||
|
|
||||
|
TEST_ASSERT_TRUE(istFeldUnsicher(Brett, 0, 0, player)); |
||||
|
TEST_ASSERT_FALSE(istFeldUnsicher(Brett, 4, 4, player)); |
||||
|
|
||||
|
Brett[7][0] = ' '; |
||||
|
Brett[3][3] = 'Q'; |
||||
|
player = PLAYER_BLACK; |
||||
|
TEST_ASSERT_TRUE(istFeldUnsicher(Brett, 7, 7, player)); |
||||
|
TEST_ASSERT_TRUE(istFeldUnsicher(Brett, 3, 0, player)); |
||||
|
|
||||
|
freeTestBrett(Brett); |
||||
|
} |
||||
|
#endif // TEST |
||||
|
|
@ -0,0 +1,48 @@ |
|||||
|
#ifdef TEST |
||||
|
#include "unity.h" |
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdbool.h> |
||||
|
#include "Spieler.h" |
||||
|
#include "Schachbrett.h" |
||||
|
#include "Koenig.h" |
||||
|
|
||||
|
|
||||
|
void test_istzugerlaubt_Koenig_LegalMoves(void) { |
||||
|
char **Brett = Schachbrett_erstellen(); |
||||
|
Player player = PLAYER_WHITE; |
||||
|
//Vorbereitung für Test |
||||
|
Brett[1][4] = ' '; |
||||
|
Brett[2][4] = 'p'; |
||||
|
|
||||
|
// Test bewegung |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Koenig(Brett, 4, 0, 4, 1, player)); // e1 zu e2 |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Koenig(Brett, 4, 1, 4, 2, player)); // e2 zu e3 (Einnehmen von Figur) |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
Brett = Schachbrett_erstellen(); |
||||
|
player = PLAYER_BLACK; |
||||
|
//Vorbereitung für Test |
||||
|
Brett[6][4] = ' '; |
||||
|
Brett[5][4] = 'P'; |
||||
|
|
||||
|
// Test bewegung |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Koenig(Brett, 4, 7, 4, 6, player)); // e8 zu e7 |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Koenig(Brett, 4, 6, 4, 5, player)); // e7 zu e6 (Einnehmen von Figur) |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
} |
||||
|
|
||||
|
void test_istzugerlaubt_Koenig_IllegalMoves(void) { |
||||
|
char **Brett = Schachbrett_erstellen(); |
||||
|
Player player = PLAYER_WHITE; |
||||
|
|
||||
|
// Illegaler zug: König versucht sich 2 Felder zu bewegen |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Koenig(Brett, 4, 0, 4, 2, player)); |
||||
|
|
||||
|
// Illegaler zug: König versucht sich auf besetztes Feld zu bewegen |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Koenig(Brett, 4, 0, 3, 1, player)); |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
} |
||||
|
#endif // TEST |
@ -0,0 +1,84 @@ |
|||||
|
#ifdef TEST |
||||
|
#include "unity.h" |
||||
|
#include <stdio.h> |
||||
|
#include <stdlib.h> |
||||
|
#include <stdbool.h> |
||||
|
#include "Springer.h" |
||||
|
#include "Schachbrett.h" |
||||
|
#include "Spieler.h" |
||||
|
|
||||
|
void test_istzugerlaubt_Springer_LegalMove(void) { |
||||
|
char **Brett = Schachbrett_erstellen(); |
||||
|
|
||||
|
// Viele Test um legale Züge zu prüfen |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 1, 0, 0, 2, PLAYER_WHITE)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 1, 0, 2, 2, PLAYER_WHITE)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 4, 3, 3, 5, PLAYER_WHITE)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 4, 3, 2, 2, PLAYER_WHITE)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 4, 3, 6, 2, PLAYER_BLACK)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 6, 7, 5, 5, PLAYER_BLACK)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 6, 7, 7, 5, PLAYER_BLACK)); |
||||
|
TEST_ASSERT_TRUE(istzugerlaubt_Springer(Brett, 4, 3, 5, 5, PLAYER_BLACK)); |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
} |
||||
|
void test_istzugerlaubt_Springer_IllegalMove(void) { |
||||
|
char **Brett = Schachbrett_erstellen(); |
||||
|
|
||||
|
// Landet auf eigener Figur |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 6, 7, 4, 6, PLAYER_BLACK)); |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 1, 7, 3, 6, PLAYER_BLACK)); |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 6, 0, 4, 1, PLAYER_WHITE)); |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 1, 0, 3, 1, PLAYER_WHITE)); |
||||
|
|
||||
|
// versucht sich geradeaus zu bewegen |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 1, 0, 1, 3, PLAYER_WHITE)); |
||||
|
|
||||
|
// Versucht Diagonal zu bewegen |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 7, 0, 6, 1, PLAYER_WHITE)); |
||||
|
|
||||
|
// Vom Brett springen |
||||
|
TEST_ASSERT_FALSE(istzugerlaubt_Springer(Brett, 7, 0, 8, 2, PLAYER_WHITE)); |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
} |
||||
|
void test_bewegeSpringer(void) { |
||||
|
char **Brett = Schachbrett_erstellen(); |
||||
|
bool Ergebnis; |
||||
|
|
||||
|
// Scenario 1: Legaler Zug, weißer Springer von b1 zu c3 |
||||
|
Ergebnis = bewegeSpringer(Brett, 1, 0, 2, 2, PLAYER_WHITE); |
||||
|
TEST_ASSERT_TRUE(Ergebnis); |
||||
|
TEST_ASSERT_EQUAL_CHAR('N', Brett[2][2]); |
||||
|
TEST_ASSERT_EQUAL_CHAR(' ', Brett[0][1]); |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
Brett = Schachbrett_erstellen(); |
||||
|
|
||||
|
// Scenario 2: Illegaler Zug , vom Brett |
||||
|
Ergebnis = bewegeSpringer(Brett, 7, 6, 8, 8, PLAYER_WHITE); |
||||
|
TEST_ASSERT_FALSE(Ergebnis); |
||||
|
|
||||
|
// Scenario 3: Legaler Zug, schwarzer Springer von g8 zu f6 |
||||
|
Ergebnis = bewegeSpringer(Brett, 6, 7, 5, 5, PLAYER_BLACK); |
||||
|
TEST_ASSERT_TRUE(Ergebnis); |
||||
|
TEST_ASSERT_EQUAL_CHAR('n', Brett[5][5]); |
||||
|
TEST_ASSERT_EQUAL_CHAR(' ', Brett[7][6]); |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
Brett = Schachbrett_erstellen(); |
||||
|
|
||||
|
// Scenario 4: Illegaler Zug, schwarzer Springer bewegt sich auf andere schwarze Figur |
||||
|
Brett[5][5] = 'n'; // ein anderer Springer als Hindernis |
||||
|
Ergebnis = bewegeSpringer(Brett, 6, 7, 5, 5, PLAYER_BLACK); |
||||
|
TEST_ASSERT_FALSE(Ergebnis); |
||||
|
|
||||
|
// Scenario 5: Legaler Zug zum einnehmen feindlicher Figur |
||||
|
Brett[5][5] = 'P'; // Weißer Bauer auf F6 |
||||
|
Ergebnis = bewegeSpringer(Brett, 6, 7, 5, 5, PLAYER_BLACK); |
||||
|
TEST_ASSERT_TRUE(Ergebnis); |
||||
|
TEST_ASSERT_EQUAL_CHAR('n', Brett[5][5]); |
||||
|
|
||||
|
Schachbrettspeicher_freigeben(Brett); |
||||
|
} |
||||
|
#endif // TEST |
Write
Preview
Loading…
Cancel
Save
Reference in new issue