diff --git a/src/main/c/Template/Battleship_game.c b/src/main/c/Template/Battleship_game.c new file mode 100644 index 0000000..e0b97fa --- /dev/null +++ b/src/main/c/Template/Battleship_game.c @@ -0,0 +1,1392 @@ +#include +#include +#include + + +int feld(char prifield[]) { + + int a = 0; + int numcoor[10]; + char letcoor[10] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J' }; + + printf(" "); + + for (int k = 0; k <= 9; k++) { + numcoor[k] = k + 1; + printf(" %d ", numcoor[k]); + } + + printf("\n"); + + + for (int i = 0; i <= 9; i++) { + + printf("%c-", letcoor[i]); + + for (int j = 0; j <= 9; j++) { + printf("[%c]", prifield[a]); + a++; + } + + printf("\n"); + } + + + return 0; +} + +int convertcoor(char a, int b) { + + int co = 0; + + switch (a) { + case 'A': co = 0; break; + case 'a': co = 0; break; + case 'B': co = 10; break; + case 'b': co = 10; break; + case 'C': co = 20; break; + case 'c': co = 20; break; + case 'D': co = 30; break; + case 'd': co = 30; break; + case 'E': co = 40; break; + case 'e': co = 40; break; + case 'F': co = 50; break; + case 'f': co = 50; break; + case 'G': co = 60; break; + case 'g': co = 60; break; + case 'H': co = 70; break; + case 'h': co = 70; break; + case 'I': co = 80; break; + case 'i': co = 80; break; + case 'J': co = 90; break; + case 'j': co = 90; break; + default: co = 0; + } + + co += b; + co -= 1; + + return co; +} + +int* coorgenerator() { + + int randomcoor[100] = {44, 45, 55, 54, 53, 43, 33, 34, 35, 36, 46, 56, 66, 65, 64, 63, 62, 52, 42, 32, 22, 23, 24, 25, 26, 27, 37, 47, 57, 67, 77, 76, 75, 74, 73, 72, 71, 61, 51, 41, 31, 21, 11, 12, 13, 14, 15, 16, 17, 18, 28, 38, 48, 58, 68, 78, 88, 87, 86, 85, 84, 83, 82, 81, 80, 70, 60, 50, 40, 30, 20, 10, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 19, 29, 39, 49, 59, 69, 79, 89, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90}; + int shipcoor[17]; + int* shipcoorptr[17]; + int co, vers, direct; + int a = 0; + + int* returnarr = (int*)malloc(17* sizeof(int)); + + if (returnarr == NULL) { + perror("Memory allocation failed"); + exit(1); + } + + srand(time(NULL)); + + //(5)-er + + int ranco = rand() % 4; + + co = randomcoor[ranco]; + + int ranvers = rand()%5; + int randirect = rand()%4; + + if (randirect == 0) { + co = co + ranvers; + } + if (randirect == 1) { + co = co - ranvers; + } + if (randirect == 2) { + co = co + 10*ranvers; + } + if (randirect == 3) { + co = co - 10*ranvers; + } + + ranvers = rand() % 5; + int randirect2 = rand() % 2; + + if (randirect == 0) { + if (randirect2 == 0) { + co = co + 10 * ranvers; + a = 1; + } + if (randirect2 == 1) { + co = co - 10 * ranvers; + a = 2; + } + } + if (randirect == 1) { + if (randirect == 0) { + co = co + 10 * ranvers; + a = 3; + } + if (randirect == 0) { + co = co - 10 * ranvers; + a = 4; + } + } + if (randirect == 2) { + if (randirect == 0) { + co = co + ranvers; + a = 1; + } + if (randirect == 0) { + co = co - ranvers; + a = 3; + } + } + if (randirect == 3) { + if (randirect == 0) { + co = co + ranvers; + a = 2; + } + if (randirect == 0) { + co = co - ranvers; + a = 4; + } + } + + shipcoor[0] = co; + + int shipdirect = rand() % 2; + + if (a == 1) { + if (shipdirect == 0) { + for (int i = 1; i < 5; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 1; i < 5; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 2) { + if (shipdirect == 0) { + for (int i = 1; i < 5; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 1; i < 5; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + if (a == 3) { + if (shipdirect == 0) { + for (int i = 1; i < 5; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 1; i < 5; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 4) { + if (shipdirect == 0) { + for (int i = 1; i < 5; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 1; i < 5; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + + //(4)-er + + ranco = rand() % 4; + + co = randomcoor[ranco]; + + ranvers = rand() % 5; + randirect = rand() % 4; + + if (randirect == 0) { + co = co + ranvers; + } + if (randirect == 1) { + co = co - ranvers; + } + if (randirect == 2) { + co = co + 10 * ranvers; + } + if (randirect == 3) { + co = co - 10 * ranvers; + } + + ranvers = rand() % 5; + randirect2 = rand() % 2; + + if (randirect == 0) { + if (randirect2 == 0) { + co = co + 10 * ranvers; + a = 1; + } + if (randirect2 == 1) { + co = co - 10 * ranvers; + a = 2; + } + } + if (randirect == 1) { + if (randirect == 0) { + co = co + 10 * ranvers; + a = 3; + } + if (randirect == 0) { + co = co - 10 * ranvers; + a = 4; + } + } + if (randirect == 2) { + if (randirect == 0) { + co = co + ranvers; + a = 1; + } + if (randirect == 0) { + co = co - ranvers; + a = 3; + } + } + if (randirect == 3) { + if (randirect == 0) { + co = co + ranvers; + a = 2; + } + if (randirect == 0) { + co = co - ranvers; + a = 4; + } + } + + shipcoor[5] = co; + + shipdirect = rand() % 2; + + if (a == 1) { + if (shipdirect == 0) { + for (int i = 6; i < 9; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 6; i < 9; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 2) { + if (shipdirect == 0) { + for (int i = 6; i < 9; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 6; i < 9; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + if (a == 3) { + if (shipdirect == 0) { + for (int i = 6; i < 9; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 6; i < 9; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 4) { + if (shipdirect == 0) { + for (int i = 6; i < 9; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 6; i < 9; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + + //(3)-er 1 + + ranco = rand() % 4; + + co = randomcoor[ranco]; + + ranvers = rand() % 5; + randirect = rand() % 4; + + if (randirect == 0) { + co = co + ranvers; + } + if (randirect == 1) { + co = co - ranvers; + } + if (randirect == 2) { + co = co + 10 * ranvers; + } + if (randirect == 3) { + co = co - 10 * ranvers; + } + + ranvers = rand() % 5; + randirect2 = rand() % 2; + + if (randirect == 0) { + if (randirect2 == 0) { + co = co + 10 * ranvers; + a = 1; + } + if (randirect2 == 1) { + co = co - 10 * ranvers; + a = 2; + } + } + if (randirect == 1) { + if (randirect == 0) { + co = co + 10 * ranvers; + a = 3; + } + if (randirect == 0) { + co = co - 10 * ranvers; + a = 4; + } + } + if (randirect == 2) { + if (randirect == 0) { + co = co + ranvers; + a = 1; + } + if (randirect == 0) { + co = co - ranvers; + a = 3; + } + } + if (randirect == 3) { + if (randirect == 0) { + co = co + ranvers; + a = 2; + } + if (randirect == 0) { + co = co - ranvers; + a = 4; + } + } + + shipcoor[9] = co; + + shipdirect = rand() % 2; + + if (a == 1) { + if (shipdirect == 0) { + for (int i = 10; i < 12; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 10; i < 12; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 2) { + if (shipdirect == 0) { + for (int i = 10; i < 12; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 10; i < 12; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + if (a == 3) { + if (shipdirect == 0) { + for (int i = 10; i < 12; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 10; i < 12; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 4) { + if (shipdirect == 0) { + for (int i = 10; i < 12; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 10; i < 12; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + + //(3)-er 2 + + ranco = rand() % 4; + + co = randomcoor[ranco]; + + ranvers = rand() % 5; + randirect = rand() % 4; + + if (randirect == 0) { + co = co + ranvers; + } + if (randirect == 1) { + co = co - ranvers; + } + if (randirect == 2) { + co = co + 10 * ranvers; + } + if (randirect == 3) { + co = co - 10 * ranvers; + } + + ranvers = rand() % 5; + randirect2 = rand() % 2; + + if (randirect == 0) { + if (randirect2 == 0) { + co = co + 10 * ranvers; + a = 1; + } + if (randirect2 == 1) { + co = co - 10 * ranvers; + a = 2; + } + } + if (randirect == 1) { + if (randirect == 0) { + co = co + 10 * ranvers; + a = 3; + } + if (randirect == 0) { + co = co - 10 * ranvers; + a = 4; + } + } + if (randirect == 2) { + if (randirect == 0) { + co = co + ranvers; + a = 1; + } + if (randirect == 0) { + co = co - ranvers; + a = 3; + } + } + if (randirect == 3) { + if (randirect == 0) { + co = co + ranvers; + a = 2; + } + if (randirect == 0) { + co = co - ranvers; + a = 4; + } + } + + shipcoor[12] = co; + + shipdirect = rand() % 2; + + if (a == 1) { + if (shipdirect == 0) { + for (int i = 13; i < 15; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 13; i < 15; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 2) { + if (shipdirect == 0) { + for (int i = 13; i < 15; i++) { + co -= 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 13; i < 15; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + if (a == 3) { + if (shipdirect == 0) { + for (int i = 13; i < 15; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 13; i < 15; i++) { + co -= 10; + shipcoor[i] = co; + } + } + } + if (a == 4) { + if (shipdirect == 0) { + for (int i = 13; i < 15; i++) { + co += 1; + shipcoor[i] = co; + } + } + if (shipdirect == 1) { + for (int i = 13; i < 15; i++) { + co += 10; + shipcoor[i] = co; + } + } + } + + //(2)-er + + ranco = rand() % 4; + + co = randomcoor[ranco]; + + ranvers = rand() % 5; + randirect = rand() % 4; + + if (randirect == 0) { + co = co + ranvers; + } + if (randirect == 1) { + co = co - ranvers; + } + if (randirect == 2) { + co = co + 10 * ranvers; + } + if (randirect == 3) { + co = co - 10 * ranvers; + } + + ranvers = rand() % 5; + randirect2 = rand() % 2; + + if (randirect == 0) { + if (randirect2 == 0) { + co = co + 10 * ranvers; + a = 1; + } + if (randirect2 == 1) { + co = co - 10 * ranvers; + a = 2; + } + } + if (randirect == 1) { + if (randirect == 0) { + co = co + 10 * ranvers; + a = 3; + } + if (randirect == 0) { + co = co - 10 * ranvers; + a = 4; + } + } + if (randirect == 2) { + if (randirect == 0) { + co = co + ranvers; + a = 1; + } + if (randirect == 0) { + co = co - ranvers; + a = 3; + } + } + if (randirect == 3) { + if (randirect == 0) { + co = co + ranvers; + a = 2; + } + if (randirect == 0) { + co = co - ranvers; + a = 4; + } + } + + shipcoor[15] = co; + + shipdirect = rand() % 2; + + if (a == 1) { + if (shipdirect == 0) { + co -= 1; + shipcoor[16] = co; + } + if (shipdirect == 1) { + co -= 10; + shipcoor[16] = co; + } + } + if (a == 2) { + if (shipdirect == 0) { + co -= 1; + shipcoor[16] = co; + } + if (shipdirect == 1) { + co += 10; + shipcoor[16] = co; + } + } + if (a == 3) { + if (shipdirect == 0) { + co += 1; + shipcoor[16] = co; + } + if (shipdirect == 1) { + co -= 10; + shipcoor[16] = co; + } + } + if (a == 4) { + if (shipdirect == 0) { + co += 1; + shipcoor[16] = co; + } + if (shipdirect == 1) { + co += 10; + shipcoor[16] = co; + } + } + + for (int i = 0; i < 17; i++) { + + returnarr[i] = shipcoor[i]; + } + + + return returnarr; +} + +int hitgenerator() { + + int hit; + + hit = rand() % 100; + + return hit; +} + +int singleplayer() { + + char player[100]; + char progplayer[100]; + char ships1[100]; + char ships2[100]; + int* gencoor = coorgenerator(); + + char letco; + int numco, ranco; + int coor, coor1, coor2; + int score1 = 0; + int score2 = 0; + + + for (int i = 0; i <= 99; i++) { + + player[i] = '0'; + + } + + for (int i = 0; i <= 99; i++) { + + progplayer[i] = '0'; + + } + + for (int i = 0; i <= 99; i++) { + + ships1[i] = '0'; + + } + + for (int i = 0; i <= 99; i++) { + + ships2[i] = '0'; + + } + + for (int i = 0; i <= 18; i++) { + ranco = gencoor[i]; + ships2[ranco] = 'X'; + } + + !feld(ships1); + + printf("\nPlayer, platziere dein Schlachtschiff (5).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 4; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + + !feld(ships1); + + printf("\nPlayer, platziere deinen Kreuzer (4).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 3; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + + for (int i = 0; i < 2; i++) { + + !feld(ships1); + + printf("\nPlayer, platziere deine Zerstoerer 2x(3).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 2; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + } + + !feld(ships1); + + printf("\nPlayer, platziere dein U-Boot (2).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 1; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + } + + system("cls"); + + int runde = 1; + int higherscore = 0; + + while (higherscore < 17) { + + !feld(player); + printf("Runde %d\n\nDein Score: %d\n\nGegnerischer Score: %d\n\nPlayer, gib eine Zielkoordinate ein: ", runde, score1, score2); + scanf(" %c%d", &letco, &numco); + + coor = convertcoor(letco, numco); + + if (ships2[coor] == 'X') { + player[coor] = 'X'; + score1 += 1; + system("cls"); + printf("Treffer!!\n\n"); + } + else { + player[coor] = '-'; + system("cls"); + printf("Kein Treffer.\n\n"); + } + + coor = hitgenerator(); + + int f = 0; + + while (f != 1) { + + if (progplayer[coor] == '0') { + f = 1; + } + } + + if (ships1[coor] == 'X') { + progplayer[coor] = 'X'; + score2 += 1; + system("cls"); + printf("Treffer!!\n\n"); + } + else { + progplayer[coor] = '-'; + system("cls"); + printf("Kein Treffer.\n\n"); + } + + if (score1 >= score2) { + higherscore = score1; + } + else { + higherscore = score2; + } + + runde++; + } + + system("cls"); + + if (score1 = 17) { + printf("Player 1 hat gewonnen."); + } + if (score2 = 17) { + printf("Player 2 hat gewonnen."); + } + + return 0; +} + +int multiplayer() { + + char player1[100]; + char player2[100]; + char ships1[100]; + char ships2[100]; + + char letco; + int numco, coor, coor1, coor2; + int score1 = 0; + int score2 = 0; + + + for (int i = 0; i <= 99; i++) { + + player1[i] = '0'; + + } + + for (int i = 0; i <= 99; i++) { + + player2[i] = '0'; + + } + + for (int i = 0; i <= 99; i++) { + + ships1[i] = '0'; + + } + + for (int i = 0; i <= 99; i++) { + + ships2[i] = '0'; + + } + + system("cls"); + + !feld(ships1); + + + printf("\nPlayer 1, platziere dein Schlachtschiff (5).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 4; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + + !feld(ships1); + + printf("\nPlayer 1, platziere deinen Kreuzer (4).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 3; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + + for (int i = 0; i < 2; i++) { + + !feld(ships1); + + printf("\nPlayer 1, platziere deine Zerstoerer 2x(3).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 2; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + } + + !feld(ships1); + + printf("\nPlayer 1, platziere dein U-Boot (2).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships1[coor1] = 'X'; + + for (int i = 0; i < 1; i++) { + !feld(ships1); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships1[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + } + + !feld(ships1); + + system("cls"); + + !feld(ships2); + + printf("\nPlayer 2, platziere dein Schlachtschiff (5).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships2[coor1] = 'X'; + + for (int i = 0; i < 4; i++) { + !feld(ships2); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + + !feld(ships2); + + printf("\nPlayer 2, platziere deinen Kreuzer (4).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships2[coor1] = 'X'; + + for (int i = 0; i < 3; i++) { + !feld(ships2); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + + for (int i = 0; i < 2; i++) { + + !feld(ships2); + + printf("\nPlayer 2, platziere deine Zerstoerer 2x(3).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships2[coor1] = 'X'; + + for (int i = 0; i < 2; i++) { + !feld(ships2); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + + } + } + + !feld(ships2); + + printf("\nPlayer 2, platziere dein U-Boot (2).\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + system("cls"); + + coor1 = convertcoor(letco, numco); + + ships2[coor1] = 'X'; + + for (int i = 0; i < 1; i++) { + !feld(ships2); + printf("\nKoordinate: "); + scanf(" %c%d", &letco, &numco); + + coor2 = convertcoor(letco, numco); + + if (coor2 == coor1 + 1 || coor2 == coor1 - 1) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else if (coor2 == coor1 + 10 || coor2 == coor1 - 10) { + ships2[coor2] = 'X'; + coor1 = coor2; + system("cls"); + } + else { + system("cls"); + printf("Ungueltige Eingabe.\n"); + i--; + } + } + + system("cls"); + + int runde = 1; + int higherscore = 0; + + while (higherscore < 17) { + + !feld(player1); + printf("Runde %d\n\nScore: %d\n\nPlayer 1, gib eine Zielkoordinate ein: ", runde, score1); + scanf(" %c%d", &letco, &numco); + + coor = convertcoor(letco, numco); + + if (ships2[coor] == 'X') { + player1[coor] = 'X'; + score1 += 1; + system("cls"); + printf("Treffer!!\n\n"); + } + else { + player1[coor] = '-'; + system("cls"); + printf("Kein Treffer.\n"); + } + + !feld(player2); + printf("Runde %d\n\nScore: %d\n\nPlayer 2, gib eine Zielkoordinate ein: ", runde, score2); + scanf(" %c%d", &letco, &numco); + + coor = convertcoor(letco, numco); + + if (ships1[coor] == 'X') { + player2[coor] = 'X'; + score2 += 1; + system("cls"); + printf("Treffer!!\n\n"); + } + else { + player2[coor] = '-'; + system("cls"); + printf("Kein Treffer.\n"); + } + + if (score1 >= score2) { + higherscore = score1; + } + else { + higherscore = score2; + } + + runde++; + } + + system("cls"); + + if (score1 = 17) { + printf("Player 1 hat gewonnen."); + } + if (score2 = 17) { + printf("Player 2 hat gewonnen."); + } + + return 0; +} + + +int battleship() { + + int player; + + printf("*Schiffeversenken*\n\n Single player: Drueck <1>\n Multiplayer: Drueck <2>\n"); + scanf("%d", &player); + + if (player == 1) { + !singleplayer(); + } + else if (player == 2) { + !multiplayer(); + } + + return 0; +} + diff --git a/src/main/c/Template/battleship_game.h b/src/main/c/Template/battleship_game.h new file mode 100644 index 0000000..129d762 --- /dev/null +++ b/src/main/c/Template/battleship_game.h @@ -0,0 +1,6 @@ +#ifndef BATTLESHIP_GAME.H +#define BATTLESHIP_GAME.H + +int battleship(); + +#endif diff --git a/test/test_battleship.c b/test/test_battleship.c new file mode 100644 index 0000000..56d6aa3 --- /dev/null +++ b/test/test_battleship.c @@ -0,0 +1,43 @@ +#ifdef TEST +#include "unity.h" +#include "battleship_game.h" + + +void setUp(void){ + +} +void tearDown(void){ +} + + +void test_convertcoor(void) { + + int output; + + + output = convertcoor('b', 5); + + + TEST_ASSERT_EQUAL_INT(14, output); +} + +void setUp(void){ + +} +void tearDown(void){ +} + + +void test_convertcoor(void) { + + int output; + + + output = convertcoor('F', 6); + + + TEST_ASSERT_EQUAL_INT(55, output); +} + +#endif +