22 if (commandName != NULL)
24 printf(
" '%c' -> %s\n", commandToken, commandName);
108 if (moveList != NULL && commandList != NULL)
111 size_t moveListSize = strlen(moveList);
112 for (
size_t index = 0; index < moveListSize; index++)
114 char commandChar = (char)toupper(moveList[index]);
140 if (moveCommand != NULL)
144 printf(
" Error! Out of memory condition adding command %c.\n", commandChar);
152 printf(
" Error! Out of memory condition creating MoveCommand for command %c.\n", commandChar);
176 if (commands != NULL)
181 if (moveCommand->
Execute != NULL)
200 if (commands != NULL)
277 printf(
"\nNullObject Exercise\n");
281 const char* moveString =
"ur#ld!lr";
282 printf(
" Showing the move commands:\n");
285 printf(
" Executing the move commands:\n");
286 printf(
" %s -> ", moveString);
void NullObject_Exercise(void)
Example of using the Null Object Pattern.
static void MoveCommandRight_Execute(void)
Represents the Move Right command.
static void MoveProcessor_ExecuteMoveList(const char *moveList)
Parse and execute the given list of move commands, where each command is represents by a single chara...
static void _MoveProcessor_ExecuteMoves(MoveCommandList *commands)
Helper method to execute all the given commands.
static void MoveCommandUp_Execute(void)
Represents the Move Up command.
static void MoveCommandDown_Execute(void)
Represents the Move Down command.
static void _MoveProcessor_ShowMoves(MoveCommandList *commands)
Show the command character and name of the command for all commands in the given list of commands.
static void MoveCommand_Show(char commandToken, const char *commandName)
Show the move command and its name followed by a newline.
static bool _MoveProcessor_ParseMoves(const char *moveList, MoveCommandList *commandList)
Represents the processor that translates the move list into a list of MoveCommand objects then either...
static void MoveProcessor_ShowMoveList(const char *moveList)
Parse and display the given list of move commands, where each command is represents by a single chara...
static void MoveCommandNone_Execute(void)
Represents the Do Nothing command. This is the Null "Object" for this exercise.
static void MoveCommandLeft_Execute(void)
Represents the Move Left command.
MoveCommand * MoveCommand_Create(char commandToken, const char *commandName, ExecuteFunction executeFunction)
Create a MoveCommand object and initialize it with the given arguments.
void MoveCommandList_Clear(MoveCommandList *commandList)
Clear the given MoveCommandList, freeing up any allocated resources, so the list can be reused.
void MoveCommandList_Initialize(MoveCommandList *commandList)
Initialize the given MoveCommandList object. This should be the first function called for an uninitia...
bool MoveCommandList_Add(MoveCommandList *commandList, MoveCommand *moveCommand)
Add a given MoveCommand object to the given MoveCommandList object. The MoveCommandList takes ownersh...
Declaration of the MoveCommandList structure along with the support functions, MoveCommandList_Initia...
Declaration of the NullObject_Exercise() function as used in the Null Object Pattern.
Represents a move command. A move command has a name and the command character that represents the co...
char commandToken
The character that represents this move command in a string.
const char * commandName
The name of this move command.
ExecuteFunction Execute
The function to call to execute the move command (this varies for each command)
Represents a list of MoveCommand objects.
MoveCommand ** commands
Array of pointers to MoveCommand objects.
size_t commands_count
Number of commands in the commands array.