Skip to content

Commit

Permalink
Implement main program
Browse files Browse the repository at this point in the history
Fix prompt range types causing comparison issues

Fix linking in CMakeLists.txt
  • Loading branch information
speelbarrow committed Jun 20, 2023
1 parent 969eedd commit 0f344f3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ set(CMAKE_C_STANDARD 17)
# Set global include directory
include_directories(include)

# Create object libraries
# For creating object libraries
function(object_from_source_header name)
add_library(${name} OBJECT src/${name}.c)
list(APPEND OBJECT_LIBRARIES $<TARGET_OBJECTS:${name}>)
endfunction()
set(SOURCE_HEADERS prompt)
foreach(source_header ${SOURCE_HEADERS})
object_from_source_header(${source_header})
endforeach()

# Create main executable
add_executable(main main.c)
target_link_libraries(main ${OBJECT_LIBRARIES})

# Generate and link object libraries
foreach(source_header ${SOURCE_HEADERS})
object_from_source_header(${source_header})
target_link_libraries(main $<TARGET_OBJECTS:${source_header}>)
endforeach()

install(TARGETS main DESTINATION bin)
install(DIRECTORY include/amadeus TYPE INCLUDE)
34 changes: 33 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
int main() {}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
#include <amadeus/prompt.h>

int main() {
printf("The A.M.a.D.E.U.S. Project\n");
printf("Created by Noah Friedman\n");

// Get the maximum limit for the random number from the user
int max = prompt_min("Enter a maximum", 1);

// Initialize random number generator and generate the number the user will guess
srand(time(NULL));
int number = rand() % max + 1;

// Main loop
while (true) {
// Get the user's guess
int guess = prompt_range("Enter a guess", 1, max);

// Check if the guess is correct
if (guess == number) {
printf("Correct!\n");
break;
} else if (guess < number) {
printf("Higher!\n");
} else {
printf("Lower!\n");
}
}
}
18 changes: 9 additions & 9 deletions src/prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#include <math.h>
#include <string.h>

int prompt_counted(const char *message, int min, int max, int count) {
int prompt_counted(const char *message, double min, double max, int count) {
char range[25];

if (min != -INFINITY && max != INFINITY) {
sprintf(range, "(%d-%d) ", min, max);
sprintf(range, " (%d-%d)", (int)min, (int)max);
} else if (min != -INFINITY) {
sprintf(range, "(>%d) ", min);
sprintf(range, " (>%d)", (int)min);
} else if (max != INFINITY) {
sprintf(range, "(<%d) ", max);
sprintf(range, " (<%d)", (int)max);
} else {
range[0] = '\0';
}
Expand All @@ -35,9 +36,8 @@ int prompt_counted(const char *message, int min, int max, int count) {
exit(1);
}
}
int prompt_counted(const char *message, int min, int max, int count);

int prompt(const char *message) { return prompt_range(message, -INFINITY, INFINITY); }
int prompt_min(const char *message, int min) { return prompt_range(message, min, INFINITY); }
int prompt_max(const char *message, int max) { return prompt_range(message, -INFINITY, max); }
int prompt_range(const char *message, int min, int max) { return prompt_counted(message, min, max, 0); }
int prompt(const char *message) { return prompt_counted(message, -INFINITY, INFINITY, 0); }
int prompt_min(const char *message, int min) { return prompt_counted(message, (double)min, INFINITY, 0); }
int prompt_max(const char *message, int max) { return prompt_counted(message, -INFINITY, (double)max, 0); }
int prompt_range(const char *message, int min, int max) { return prompt_counted(message, (double)min, (double)max, 0); }

0 comments on commit 0f344f3

Please sign in to comment.