diff --git a/.gitlab/.gitlab-webide.yml b/.gitlab/.gitlab-webide.yml new file mode 100644 index 0000000..faf8c97 --- /dev/null +++ b/.gitlab/.gitlab-webide.yml @@ -0,0 +1,9 @@ +terminal: + # This can be any image that has the necessary runtime environment for your project. + image: node:10-alpine + before_script: + - apk update + script: sleep 60 + variables: + RAILS_ENV: "test" + NODE_ENV: "test" diff --git a/src/main/c/GameTic_Tac_Toe/tictactoe.c b/src/main/c/GameTic_Tac_Toe/tictactoe.c index 62b99bc..0e5a452 100644 --- a/src/main/c/GameTic_Tac_Toe/tictactoe.c +++ b/src/main/c/GameTic_Tac_Toe/tictactoe.c @@ -1,6 +1,6 @@ #include +#include "tictactoe.h" -const int BOARD_SIZE = 3; char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}}; @@ -8,6 +8,14 @@ char currentPlayer = 'X'; const char PLAYER_X = 'X'; const char PLAYER_O = 'O'; +void reset_board(){ + for(int i = 0; i < BOARD_SIZE; i++){ + for(int j = 0; j < BOARD_SIZE; j++){ + board[i][j] = i * BOARD_SIZE + j + 49; + } + } +} + // Funktionen zur Anzeige des Spielbretts void displayBoard() { printf("Tic-Tac-Toe\n"); @@ -26,6 +34,13 @@ void displayBoard() { } } +int get_move(){ + int choice; + printf("Spieler %c, waehle eine Zahl (1-9): ", (currentPlayer == PLAYER_X) ? 'X' : 'O'); + scanf("%d", &choice); + return choice; +} + char checkLine(char a, char b, char c) { if (a == b && b == c) return a; return ' '; @@ -76,21 +91,18 @@ int isValidMove(int choice) { return (choice >= 1 && choice <= BOARD_SIZE * BOARD_SIZE && board[row][col] != 'X' && board[row][col] != 'O'); } -void makeMove() { - int choice; - printf("Spieler %c, waehle eine Zahl (1-9): ", (currentPlayer == PLAYER_X) ? 'X' : 'O'); - scanf("%d", &choice); +void makeMove(int choice) { -// Konvertiere die Zahl in Zeilen- und Spaltenindex -if (isValidMove(choice)) { - int row = (choice - 1) / BOARD_SIZE; - int col = (choice - 1) % BOARD_SIZE; - board[row][col] = (currentPlayer == PLAYER_X) ? 'X' : 'O'; - currentPlayer = switchPlayer(currentPlayer); + // Konvertiere die Zahl in Zeilen- und Spaltenindex + if (isValidMove(choice)) { + int row = (choice - 1) / BOARD_SIZE; + int col = (choice - 1) % BOARD_SIZE; + board[row][col] = (currentPlayer == PLAYER_X) ? 'X' : 'O'; + currentPlayer = switchPlayer(currentPlayer); -} else { - printf("Ungueltiger Zug! Bitte waehle erneut.\n"); - makeMove(); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird + } else { + printf("Ungueltiger Zug! Bitte waehle erneut.\n"); + makeMove(get_move()); // Rekursiver Aufruf, bis ein gültiger Zug gemacht wird } } @@ -99,6 +111,7 @@ int isGameOver() { return (winner == PLAYER_X || winner == PLAYER_O || isBoardFull()); } + void printGameResult(char winner) { if (winner == PLAYER_X || winner == PLAYER_O) { printf("Spieler %c gewinnt!\n", (winner == PLAYER_X) ? 'X' : 'O'); @@ -110,7 +123,7 @@ void printGameResult(char winner) { void playGame() { while (!isGameOver()) { displayBoard(); - makeMove(); + makeMove(get_move()); } displayBoard(); @@ -118,7 +131,6 @@ void playGame() { printGameResult(winner); } -int main(){ +void start_tictactoe(){ playGame(); - return 0; } diff --git a/src/main/c/GameTic_Tac_Toe/tictactoe.h b/src/main/c/GameTic_Tac_Toe/tictactoe.h index 8b13789..d007eb4 100644 --- a/src/main/c/GameTic_Tac_Toe/tictactoe.h +++ b/src/main/c/GameTic_Tac_Toe/tictactoe.h @@ -1 +1,14 @@ +#ifndef TICTACTOE_H +#define TICTACTOE_H +#define BOARD_SIZE 3 + +int isValidMove(int choice); +char checkLine(char a, char b, char c); +void start_tictactoe(); +char checkWinner(); +void makeMove(int choice); +void displayBoard(); +void reset_board(); + +#endif diff --git a/src/main/c/main.c b/src/main/c/main.c index c858693..f0e5b90 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -2,6 +2,8 @@ #include #include +#include "Template/game100.h" +#include "GameTic_Tac_Toe/tictactoe.h" #include "Snake/snake_start.h" #include "Minesweeper/minesweeper_start.h" #include "Pong/pong.h" @@ -18,7 +20,7 @@ int main(){ printf("\t1.Spiel1 starten\n"); printf("\t2.Pong starten\n"); printf("\t3.Snake starten\n"); - printf("\t4.Spiel4 starten\n"); + printf("\t4.tic_tac_toe starten\n"); printf("\t7.Minesweeper starten\n"); printf("\t10.Exit\n"); @@ -36,7 +38,7 @@ int main(){ snake_start(); break; case 4: - //start_game4(); + start_tictactoe(); break; case 7: minesweeper_start(); diff --git a/test/test_tictactoe.c b/test/test_tictactoe.c index 8b13789..887aad7 100644 --- a/test/test_tictactoe.c +++ b/test/test_tictactoe.c @@ -1 +1,88 @@ +#ifdef TEST +#include "unity.h" +#include "tictactoe.h" + + +void setUp(void){ + //Wenn Funktion Vorraussetzungen braucht +} +void tearDown(void){ + reset_board(); + displayBoard(); +} + + +void test_if_not_identical(void){ + /* arrange */ + char result; + char a = 'a', b = 'b', c = 'c'; + + result = checkLine(a, b, c); + + /* assert */ + TEST_ASSERT_EQUAL_CHAR(' ', result); +} + + +void test_if_identical(void){ + /* arrange */ + char result; + char a = 'a', b = 'a', c = 'a'; + + result = checkLine(a, b, c); + + /* assert */ + TEST_ASSERT_EQUAL_CHAR(a, result); +} + +void test_isValidMove_gueltigerZug(void) { + /* arrangieren */ + int result; + int choice = 5; + + /* handeln */ + result = isValidMove(choice); + + /* überprüfen */ + TEST_ASSERT_EQUAL_INT(1, result); +} + + +void test_checkWinner_vertikalerGewinner(void) { + /* arrangieren */ + char result; + + // Setze die Daten für einen vertikalen Gewinne + makeMove(1); + makeMove(2); + makeMove(4); + makeMove(3); + makeMove(7); + + /* handeln */ + result = checkWinner(); + + /* überprüfen */ + TEST_ASSERT_EQUAL_CHAR('O', result); +} + +void test_checkWinner_horizontalerGewinner(void) { + /* arrangieren */ + char result; + + //makeMove(1); + //makeMove(4); + //makeMove(2); + //makeMove(5); + //makeMove(3); + + /* handeln */ + result = checkWinner(); + + /* überprüfen */ + TEST_ASSERT_EQUAL_CHAR('X', result); + reset_board(); +} + +#endif // TEST