Browse Source

Merge branch 'dev'

main
KaffeeMaus 11 months ago
parent
commit
5a0e5f2894
  1. 128
      src/main/c/Georg/tictactoe.c
  2. 9
      src/main/c/Georg/tictactoe.h
  3. 250
      src/test/c/Georg/test_tictactoe.c

128
src/main/c/Georg/tictactoe.c

@ -4,6 +4,8 @@
#include "tictactoe.h"
void printBoard();
struct ticTacToe GAME;
struct command COMMANDS[MAX_COMMANDS] = {
@ -12,6 +14,7 @@ struct command COMMANDS[MAX_COMMANDS] = {
};
void startTicTacToe(){
setbuf(stdout, 0);
printf( "%s\n", getWelcomeMessageTicTacToe() );
printf( "%s\n\n", getRulesMessageTicTacToe() );
@ -85,6 +88,22 @@ char* getUserInput(){
return userInput;
}
void initializeBoard( bool board[BORAD_SIZE][BORAD_SIZE] ){
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
board[i][j] = 0;
}
}
}
int handleGameInput( char* input ){
if( strstr(input, "set") != NULL ){
return 1;
}else{
return -1;
}
}
int startMenu( int code ){
if( code == -1 ){ // command test
return 1;
@ -105,21 +124,116 @@ int startMenu( int code ){
return 0;
}
void printBoard(){
for( int i = 0; i < BORAD_SIZE*4+1; i++ ){
printf("-");
}
printf("\n");
for (int i = 0; i < 3; ++i) {
printf("| ");
for (int j = 0; j < 3; ++j) {
if( GAME.board[i][j] == true )
printf( "X" );
else
printf ( " " );
printf(" | ");
}
printf( "\n" );
}
for( int i = 0; i < BORAD_SIZE*4+1; i++ ){
printf("-");
}
printf("\n");
}
int* getMarkerParameters( char* input ){
int* array = (int*)malloc(2 * sizeof(int));
int index = strchr(input, ',') - input;
int firstArgument = input[index-1] - '0';
int secondArgument = input[index+1] - '0';
array[0] = firstArgument-1;
array[1] = secondArgument-1;
return array;
}
void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params ){
board[params[0]][params[1]] = 1;
}
void handleGame(){
char* input = getUserInput();
int nextState = handleCommand( input );
// check commands, if no command found return for new Input
// gameCommands are saved and processed after this block
int gameCommand = -1;
if( nextState == -1 ){ // no stateCommand
gameCommand = handleGameInput( input );
if( gameCommand == -1 ){
printf("command not found!");
return;
}
}else{
GAME.currentState = nextState;
return;
}
// gameCommand processing
if( gameCommand == 1 ) { // set marker in field
int* params = getMarkerParameters( input );
setBoardMarker( GAME.board, params );
free(params);
printBoard();
if( playerHasWon( GAME.board ) ){
printf("\n\nDu hast gewonnwn!\n\n");
// start menu
GAME.currentState=0;
}
}
}
bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE]){
bool player = 1;
// Überprüfe Zeilen und Spalten
for (int i = 0; i < 3; i++) {
// Überprüfe Zeilen
if ((board[i][0] == player && board[i][1] == player && board[i][2] == player) ||
// Überprüfe Spalten
(board[0][i] == player && board[1][i] == player && board[2][i] == player)) {
return true; // Spieler hat gewonnen
}
}
// Überprüfe Diagonalen
if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||
(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return true; // Spieler hat gewonnen
}
return false;
}
int startGame( int code ){
if( code == -1 ){ // command test
return 1;
}
printf("Welcome to the game!\n");
bool board[BORAD_SIZE][BORAD_SIZE];
initializeBoard( board );
while( GAME.currentState == 1 ){
int nextState = handleCommand( getUserInput() );
printBoard();
if( nextState == -1 ){
printf("command not found!");
}else{
GAME.currentState = nextState;
}
while( GAME.currentState == 1 ){
handleGame();
}
return 0;

9
src/main/c/Georg/tictactoe.h

@ -1,11 +1,15 @@
#ifndef TICTACTOE_H
#define TICTACTOE_H
#include <stdbool.h>
#define MAX_INPUT_LENGTH 20
#define MAX_COMMANDS 3
#define BORAD_SIZE 3
struct ticTacToe{
int currentState;
bool board[BORAD_SIZE][BORAD_SIZE];
};
// Typdefinition für einen Funktionszeiger
@ -26,6 +30,11 @@ char* getWelcomeMessageTicTacToe(void);
char* getRulesMessageTicTacToe(void);
struct ticTacToe createTicTacToe();
int handleCommand( char* input );
void initializeBoard( bool board[3][3] );
int handleGameInput( char* input );
int* getMarkerParameters();
void setBoardMarker( bool board[BORAD_SIZE][BORAD_SIZE], int* params );
bool playerHasWon( bool board[BORAD_SIZE][BORAD_SIZE] );
/* commands */
commandFunction getCommandById(int id);

250
src/test/c/Georg/test_tictactoe.c

@ -105,4 +105,254 @@ void test_callCommandById_startGame(void){
// assert
TEST_ASSERT_EQUAL_PTR( startGame, actualCommand );
}
void test_initializeBoard(void){
// arrange
bool expectedBoard[BORAD_SIZE][BORAD_SIZE]={
{0,0,0},
{0,0,0},
{0,0,0}
};
// act
bool actualBoard[BORAD_SIZE][BORAD_SIZE];
initializeBoard(actualBoard);
// assert
for (size_t i = 0; i < BORAD_SIZE; i++) {
for (size_t ii = 0; ii < BORAD_SIZE; ii++) {
TEST_ASSERT_EQUAL_INT(expectedBoard[i][ii], actualBoard[i][ii]);
}
}
}
void test_handleGameInput(void){
// arrange
char* teststring = "set 3,4";
int expectedCommand = 1;
// act
int actualState = handleGameInput( teststring );
// assert
TEST_ASSERT_EQUAL_INT( expectedCommand, actualState );
}
void test_getMarkerParameters(void){
// arrange
char* teststring = "set 3,3";
int expectedParams[2] = {2, 2};
// act
int* actualParams = getMarkerParameters( teststring );
// assert
for( int i = 0; i < 2; i++ ){
TEST_ASSERT_EQUAL_INT( expectedParams[i], actualParams[i] );
}
}
void test_setBoardFields(){
// arrange
// arrange
bool board[3][3]={
{0,0,0},
{0,0,0},
{0,0,0}
};
int params[2] = {2,2};
// act
setBoardMarker( board, params );
// assert
TEST_ASSERT_EQUAL_INT( 1, board[2][2] );
}
void test_checkOnWin_vertically(void){
// arrange
bool board1[3][3]={
{0,0,1},
{0,0,1},
{0,0,1}
};
bool board2[3][3]={
{0,1,0},
{0,1,0},
{0,1,0}
};
bool board3[3][3]={
{1,0,0},
{1,0,0},
{1,0,0}
};
// act
bool result = playerHasWon( board1 );
bool result2 = playerHasWon( board2 );
bool result3 = playerHasWon( board3 );
// assert
TEST_ASSERT_EQUAL_INT( 1, result );
TEST_ASSERT_EQUAL_INT( 1, result2 );
TEST_ASSERT_EQUAL_INT( 1, result3 );
}
void test_negativ_checkOnWin_horizontally(void){
// arrange
bool board1[3][3]={
{1,0,0},
{0,0,0},
{0,0,0}
};
bool board2[3][3]={
{0,0,0},
{0,1,0},
{0,0,0}
};
bool board3[3][3]={
{0,0,0},
{0,0,0},
{0,0,1}
};
// act
bool result = playerHasWon( board1 );
bool result2 = playerHasWon( board2 );
bool result3 = playerHasWon( board3 );
// assert
TEST_ASSERT_EQUAL_INT( 0, result );
TEST_ASSERT_EQUAL_INT( 0, result2 );
TEST_ASSERT_EQUAL_INT( 0, result3 );
}
void test_negativ_checkOnWin_horizontally2(void){
// arrange
bool board1[3][3]={
{1,1,0},
{0,0,0},
{0,0,0}
};
bool board2[3][3]={
{0,0,0},
{0,1,1},
{0,0,0}
};
bool board3[3][3]={
{0,0,0},
{0,0,0},
{1,0,1}
};
// act
bool result = playerHasWon( board1 );
bool result2 = playerHasWon( board2 );
bool result3 = playerHasWon( board3 );
// assert
TEST_ASSERT_EQUAL_INT( 0, result );
TEST_ASSERT_EQUAL_INT( 0, result2 );
TEST_ASSERT_EQUAL_INT( 0, result3 );
}
void test_negativ_checkOnWin_vertically(void){
// arrange
bool board1[3][3]={
{1,0,0},
{0,0,0},
{0,0,0}
};
bool board2[3][3]={
{0,0,0},
{1,0,0},
{0,0,0}
};
bool board3[3][3]={
{0,0,0},
{0,0,0},
{1,0,0}
};
// act
bool result = playerHasWon( board1 );
bool result2 = playerHasWon( board2 );
bool result3 = playerHasWon( board3 );
// assert
TEST_ASSERT_EQUAL_INT( 0, result );
TEST_ASSERT_EQUAL_INT( 0, result2 );
TEST_ASSERT_EQUAL_INT( 0, result3 );
}
void test_negativ_checkOnWin_vertically2(void){
// arrange
bool board1[3][3]={
{0,1,0},
{0,0,0},
{0,0,0}
};
bool board2[3][3]={
{0,0,0},
{0,1,0},
{0,0,0}
};
bool board3[3][3]={
{0,0,0},
{0,0,0},
{0,1,0}
};
// act
bool result = playerHasWon( board1 );
bool result2 = playerHasWon( board2 );
bool result3 = playerHasWon( board3 );
// assert
TEST_ASSERT_EQUAL_INT( 0, result );
TEST_ASSERT_EQUAL_INT( 0, result2 );
TEST_ASSERT_EQUAL_INT( 0, result3 );
}
void test_negativ_checkOnWin_vertically3(void){
// arrange
bool board1[3][3]={
{0,0,1},
{0,0,0},
{0,0,0}
};
bool board2[3][3]={
{0,0,0},
{0,0,1},
{0,0,0}
};
bool board3[3][3]={
{0,0,0},
{0,0,0},
{0,0,1}
};
// act
bool result = playerHasWon( board1 );
bool result2 = playerHasWon( board2 );
bool result3 = playerHasWon( board3 );
// assert
TEST_ASSERT_EQUAL_INT( 0, result );
TEST_ASSERT_EQUAL_INT( 0, result2 );
TEST_ASSERT_EQUAL_INT( 0, result3 );
}
Loading…
Cancel
Save