Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson 2. Practice solution #10

Open
wants to merge 4 commits into
base: Vladimir.Svistelnikov
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 02-Git/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
out

23 changes: 23 additions & 0 deletions 02-Git/Makefile
Original file line number Diff line number Diff line change
@@ -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

6 changes: 6 additions & 0 deletions 02-Git/README.md
Original file line number Diff line number Diff line change
@@ -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.
71 changes: 71 additions & 0 deletions 02-Git/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#include <readline/readline.h>

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" );
}
}