diff --git a/02-Git/.gitignore b/02-Git/.gitignore new file mode 100644 index 0000000..f5e0883 --- /dev/null +++ b/02-Git/.gitignore @@ -0,0 +1,3 @@ +.idea +out + diff --git a/02-Git/Makefile b/02-Git/Makefile new file mode 100644 index 0000000..78d519f --- /dev/null +++ b/02-Git/Makefile @@ -0,0 +1,23 @@ +OBJ_DIR=out + +CC=gcc +CFLAGS=-I. -I/usr/local/include -Wall -Werror +DEPS = + +CSRC = ${wildcard *.c} +OBJECTS = ${addprefix ${OBJ_DIR}/, ${patsubst %.c, %.o, ${CSRC}}} + +${OBJ_DIR}/%.o: %.c ${DEPS} + ${CC} -c $< ${CFLAGS} -o $@ + +all: folders ${OBJECTS} + ${CC} ${OBJECTS} -L/usr/local/lib -lreadline -o ${OBJ_DIR}/guess + +folders: + mkdir -p ${OBJ_DIR} + +clean: folders + rm -rf ${OBJ_DIR}/* + +.PHONY: clean all folders + diff --git a/02-Git/README.md b/02-Git/README.md new file mode 100644 index 0000000..7a0a818 --- /dev/null +++ b/02-Git/README.md @@ -0,0 +1,6 @@ +# Homework +Create a question-answer game (e.g. guess a number, paper-rock-scissors etc). + +1. Main goal is not the game itself, but the proper commit structure. +2. Minimum 3 commits. +3. Follow all the rules. diff --git a/02-Git/main.c b/02-Git/main.c new file mode 100644 index 0000000..45c3fcf --- /dev/null +++ b/02-Git/main.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include + +static const int max_number = 10; +static const int min_number = 1; + +void promt( char** out_result ); +int parser( char* user_input ); +void checker( const int number ); + +int main(int argc, char *argv[] ) { + char* result; + int number; + + promt( &result ); + number = parser( result ); + checker( number ); + return EXIT_SUCCESS; +} + +void promt( char** out_result ) { + printf("Guess the number from %d to %d, dude !\n", min_number, max_number); + printf("Type it and press enter or type exit to exit:\n"); + *out_result = readline("> "); +} + +int parser( char* user_input ) { + int user_number; + char* pend; + + if( strcmp(user_input, "exit" ) == 0 ) { + printf("Exiting...Bye !\n"); + exit( EXIT_SUCCESS ); + } + + errno = 0; + user_number = strtol( user_input, &pend, 10 ); + if( *pend != '\0' ) { + printf( "Invalid input: not number\n" ); + exit( EXIT_FAILURE); + } + /* Check for various possible errors */ + if( ( errno == ERANGE && ( user_number == LONG_MAX || user_number == LONG_MIN ) ) + || ( errno != 0 && user_number == 0 ) || ( user_number > max_number ) || ( user_number < min_number ) ) { + printf( "Invalid input: out of range\n" ); + exit( EXIT_FAILURE); + } + printf( "Nice try from you: %d\n", user_number ); + return user_number; +} + +void checker( const int number ) { + int random_number; + + srand( time( NULL ) ); + do { + random_number = rand() % ( max_number + 1 ); + } while( random_number < min_number || random_number > max_number ); + printf( "Random is: %d\n", random_number ); + if( number == random_number ) { + printf( "Congratulations ! ! ! You've guessed it ! ! !\n" ); + } + else { + printf( "Ahhh... You've missed - try nex time !\n" ); + } +} +