Browse Source

changed the command structure and make it global

remotes/origin/georg
KaffeeMaus 11 months ago
parent
commit
be2cb960ce
  1. 48
      src/main/c/Georg/tictactoe.c
  2. 6
      src/main/c/Georg/tictactoe.h
  3. 23
      src/test/c/Georg/test_tictactoe.c

48
src/main/c/Georg/tictactoe.c

@ -5,7 +5,11 @@
#include "tictactoe.h" #include "tictactoe.h"
struct ticTacToe GAME; struct ticTacToe GAME;
struct command* COMMANDS;
struct command COMMANDS[MAX_COMMANDS] = {
{ 1, "\"start menu\" - startet das menu", startMenu},
{ 2, "\"start game\" - startet das spiel", startGame}
};
char* getWelcomeMessage(){ char* getWelcomeMessage(){
@ -36,31 +40,13 @@ int handleCommand( char* input ){
} }
} }
struct command* createCommands(){
struct command commands[] = {
{ 1, "\"start menu\" - startet das menu", startMenu},
{ 2, "\"start game\" - startet das spiel", startGame}
};
size_t numCommands = sizeof(commands) / sizeof(commands[0]);
// Dynamischen Speicher für das Array von commands reservieren
struct command* ptrCommands = (struct command*)malloc(numCommands * sizeof(struct command));
// Über das lokale Array iterieren und Daten kopieren
for (size_t i = 0; i < numCommands; i++) {
ptrCommands[i] = commands[i];
}
return ptrCommands;
}
commandFunction getCommandById( struct command* commands, int id ){
commandFunction getCommandById( int id ){
commandFunction result = NULL; commandFunction result = NULL;
size_t arraySize = sizeof(*commands) / sizeof(commands[0]);
size_t arraySize = sizeof(COMMANDS) / sizeof(COMMANDS[0]);
for (size_t i = 0; i < arraySize; i++) { for (size_t i = 0; i < arraySize; i++) {
if( commands[i].id == id ){
result = commands[i].fun;
//printf( "%s", COMMANDS[i].description );
if( COMMANDS[i].id == id ){
result = COMMANDS[i].fun;
break; break;
} }
} }
@ -85,6 +71,8 @@ int startMenu( int code ){
return 1; return 1;
} }
printf("Welcome to the menu!\n");
while( GAME.currentState == 0 ){ while( GAME.currentState == 0 ){
int nextState = handleCommand( getUserInput() ); int nextState = handleCommand( getUserInput() );
@ -103,5 +91,17 @@ int startGame( int code ){
return 1; return 1;
} }
printf("Welcome to the game!\n");
while( GAME.currentState == 1 ){
int nextState = handleCommand( getUserInput() );
if( nextState == -1 ){
printf("command not found!");
}else{
GAME.currentState = nextState;
}
}
return 0; return 0;
} }

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

@ -2,6 +2,7 @@
#define TICTACTOE_H #define TICTACTOE_H
#define MAX_INPUT_LENGTH 20 #define MAX_INPUT_LENGTH 20
#define MAX_COMMANDS 3
struct ticTacToe{ struct ticTacToe{
int currentState; int currentState;
@ -17,16 +18,15 @@ struct command{
}; };
extern struct ticTacToe GAME; extern struct ticTacToe GAME;
extern struct command* COMMANDS;
extern struct command COMMANDS[MAX_COMMANDS];
char* getWelcomeMessage(); char* getWelcomeMessage();
char* getRulesMessage(); char* getRulesMessage();
struct ticTacToe createTicTacToe(); struct ticTacToe createTicTacToe();
int handleCommand( char* input ); int handleCommand( char* input );
struct command* createCommands();
/* commands */ /* commands */
commandFunction getCommandById(struct command* commands, int id);
commandFunction getCommandById(int id);
int startMenu( int code ); int startMenu( int code );
int startGame( int code ); int startGame( int code );

23
src/test/c/Georg/test_tictactoe.c

@ -75,27 +75,34 @@ void test_command_startMenu(void){
TEST_ASSERT_EQUAL_INT( expectedState, actualState ); TEST_ASSERT_EQUAL_INT( expectedState, actualState );
} }
void test_createCommandlist(void){
void test_checkCommandlist(void){
// arrange // arrange
struct command* commands = NULL;
// act // act
commands = createCommands();
size_t arraySize = sizeof(*commands) / sizeof(commands[0]);
size_t arraySize = sizeof(*COMMANDS) / sizeof(COMMANDS[0]);
for (size_t i = 0; i < arraySize; i++) { for (size_t i = 0; i < arraySize; i++) {
TEST_ASSERT_EQUAL_INT( 1, commands[i].fun(-1) );
TEST_ASSERT_EQUAL_INT( 1, COMMANDS[i].fun(-1) );
} }
} }
void test_callCommandById(void){ void test_callCommandById(void){
// arrange // arrange
struct command* commands = NULL;
// act // act
commands = createCommands();
commandFunction actualCommand = getCommandById( commands, 1);
commandFunction actualCommand = getCommandById( 1 );
// assert // assert
TEST_ASSERT_EQUAL_PTR( startMenu, actualCommand ); TEST_ASSERT_EQUAL_PTR( startMenu, actualCommand );
}
void test_callCommandById_startGame(void){
// arrange
// act
commandFunction actualCommand = getCommandById( 2);
// assert
TEST_ASSERT_EQUAL_PTR( startGame, actualCommand );
} }
Loading…
Cancel
Save