Browse Source

Merge branch 'Ariana' into 'main'

Ariana

See merge request pmuw_projekt/pmuw_projekt_notebinder!35
remotes/origin/Saba
fdai8032 11 months ago
parent
commit
1975856864
  1. 9
      .gitlab/.gitlab-webide.yml
  2. 46
      src/main/c/GameTic_Tac_Toe/tictactoe.c
  3. 13
      src/main/c/GameTic_Tac_Toe/tictactoe.h
  4. 6
      src/main/c/main.c
  5. 87
      test/test_tictactoe.c

9
.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"

46
src/main/c/GameTic_Tac_Toe/tictactoe.c

@ -1,6 +1,6 @@
#include <stdio.h>
#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;
}

13
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

6
src/main/c/main.c

@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdbool.h>
#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();

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