commit d891eb52370740a532158d273a83d6902338dd3c Author: klein panic Date: Sun Sep 29 02:35:03 2024 -0400 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9f0023 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# coinflipper Project + +This is a C project generated with the setup tool. diff --git a/bin/coin_flip_random_number b/bin/coin_flip_random_number new file mode 100755 index 0000000..e7414d4 Binary files /dev/null and b/bin/coin_flip_random_number differ diff --git a/build/Makefile b/build/Makefile new file mode 100644 index 0000000..f9f5fc4 --- /dev/null +++ b/build/Makefile @@ -0,0 +1,21 @@ +CC = gcc +CFLAGS = -I../include +OBJDIR = ../obj +SRCDIR = ../src +BINDIR = ../bin +TARGET = $(BINDIR)/coin_flip_random_number + +OBJS = $(OBJDIR)/main.o + +all: $(TARGET) + +$(TARGET): $(OBJS) + @mkdir -p $(BINDIR) + $(CC) $(OBJS) -lncurses -o $(TARGET) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + @mkdir -p $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -rf $(OBJDIR) $(BINDIR) diff --git a/include/functions.h b/include/functions.h new file mode 100644 index 0000000..92be284 --- /dev/null +++ b/include/functions.h @@ -0,0 +1,7 @@ +#ifndef FUNCTIONS_H +#define FUNCTIONS_H + +void flip_coin(); +void generate_random_number(int lower, int upper); + +#endif // FUNCTIONS_H diff --git a/obj/main.o b/obj/main.o new file mode 100644 index 0000000..e383caa Binary files /dev/null and b/obj/main.o differ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..db85544 --- /dev/null +++ b/src/main.c @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#include // For usleep() + +#define DELAY 300000 // 300 ms delay for animation + +// Function prototypes +void flip_coin(); +void generate_random_number(int lower, int upper); +void coin_animation(int result); +void random_number_animation(int num); +void handle_exit(); + +int main() { + int choice, lower, upper; + + srand(time(NULL)); // Seed the random number generator + + // Initialize ncurses + initscr(); + noecho(); + cbreak(); + curs_set(0); + keypad(stdscr, TRUE); // Enable arrow keys and special keys + + int term_height, term_width; + getmaxyx(stdscr, term_height, term_width); // Get terminal size + + while (1) { + clear(); + mvprintw(term_height / 2 - 3, (term_width - 35) / 2, "Choose an option (Press 'q' or 'Esc' to quit):"); + mvprintw(term_height / 2 - 1, (term_width - 20) / 2, "1. Flip a coin"); + mvprintw(term_height / 2, (term_width - 20) / 2, "2. Generate a random number"); + mvprintw(term_height / 2 + 1, (term_width - 20) / 2, "3. Exit"); + mvprintw(term_height / 2 + 3, (term_width - 20) / 2, "Enter your choice: "); + refresh(); + + char input = getch(); + choice = input - '0'; + + if (input == 'q' || input == 27) { // 'q' or 'Esc' + handle_exit(); + } + + switch (choice) { + case 1: + clear(); + flip_coin(); + break; + case 2: + clear(); + mvprintw(4, 4, "Enter the lower bound: "); + echo(); + scanw("%d", &lower); + mvprintw(5, 4, "Enter the upper bound: "); + scanw("%d", &upper); + noecho(); + clear(); + generate_random_number(lower, upper); + break; + case 3: + handle_exit(); + break; + default: + mvprintw(10, 4, "Invalid choice, please try again."); + refresh(); + usleep(1000000); // Pause for 1 second before clearing + } + } + + endwin(); // End ncurses mode + return 0; +} + +void flip_coin() { + int result = rand() % 2; + coin_animation(result); + if (result == 0) + mvprintw(LINES - 2, (COLS - 7) / 2, "Heads!"); // Align below the animation + else + mvprintw(LINES - 2, (COLS - 7) / 2, "Tails!"); + refresh(); + usleep(2000000); // Display result for 2 seconds +} + +void generate_random_number(int lower, int upper) { + if (lower > upper) { + mvprintw(8, 4, "Invalid range! Lower bound must be less than or equal to upper bound."); + refresh(); + usleep(2000000); // Pause for 2 seconds + return; + } + int num = (rand() % (upper - lower + 1)) + lower; + random_number_animation(num); + mvprintw(10, 4, "Random number: %d", num); + refresh(); + usleep(2000000); // Display result for 2 seconds +} + +void coin_animation(int result) { + const char *flat_coin = + " _______ \n" + " | | \n" + " |_______| \n"; + + const char *first_circle_coin = + " ______ \n" + " / \\ \n" + " | | \n" + " \\______/ \n"; + + const char *second_circle_coin = + " ____ \n" + " // \\ \n" + " | | \n" + " \\____// \n"; + + const char *side_coin_vertical = + " |=| \n" + " |=| \n" + " |=| \n" + " |=| \n"; + + const char *side_coin_horizontal = + " _______ \n" + " ||||||| \n" + " ¯¯¯¯¯¯¯ \n"; + + int height = 5; // Height the coin will "move" up + + // Center coin horizontally + int x_center = (COLS - 11) / 2; + + // Animating the coin going up and down with better alignment + for (int i = 0; i < height; i++) { + clear(); + if (i == 2) { + mvprintw(4 + i, x_center, "%s", first_circle_coin); // Coin facing the user at the peak + } else if (i == 1 || i == 3) { + mvprintw(4 + i, x_center, "%s", side_coin_vertical); // Vertical side coin + } else { + mvprintw(4 + i, x_center, "%s", flat_coin); // Flat coin at start and end + } + refresh(); + usleep(DELAY); + } + + // Display the final result as heads or tails, with proper alignment + clear(); + mvprintw(4 + height, x_center, "%s", second_circle_coin); // Show circle coin + if (result == 0) { + mvprintw(6 + height, x_center + 4, "O"); // Center the "O" inside the coin + } else { + mvprintw(6 + height, x_center + 4, "X"); // Center the "X" inside the coin + } + refresh(); +} + +void random_number_animation(int num) { + char *box_top = "+--------+\n"; + char *box_bottom = "+--------+\n"; + char number_display[20]; + + // Handle different number lengths + if (num < 10) { + sprintf(number_display, "| %d |\n", num); + } else if (num < 100) { + sprintf(number_display, "| %d |\n", num); + } else { + sprintf(number_display, "| %d |\n", num); + } + + int x_center = (COLS - 10) / 2; + + for (int i = 0; i < 5; i++) { + clear(); + mvprintw(4, x_center, "%s", box_top); + mvprintw(5, x_center, "%s", number_display); + mvprintw(6, x_center, "%s", box_bottom); + refresh(); + usleep(DELAY); + } +} + +void handle_exit() { + endwin(); // End ncurses mode + exit(0); +} diff --git a/src/main.c.bak b/src/main.c.bak new file mode 100644 index 0000000..aea365a --- /dev/null +++ b/src/main.c.bak @@ -0,0 +1,58 @@ +#include +#include +#include + +// Function prototypes +void flip_coin(); +void generate_random_number(int lower, int upper); + +int main() { + int choice, lower, upper; + + srand(time(NULL)); // Seed the random number generator + + while (1) { + printf("\nChoose an option:\n"); + printf("1. Flip a coin\n"); + printf("2. Generate a random number\n"); + printf("3. Exit\n"); + printf("Enter your choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + flip_coin(); + break; + case 2: + printf("Enter the lower bound: "); + scanf("%d", &lower); + printf("Enter the upper bound: "); + scanf("%d", &upper); + generate_random_number(lower, upper); + break; + case 3: + exit(0); + default: + printf("Invalid choice, please try again.\n"); + } + } + + return 0; +} + +void flip_coin() { + int result = rand() % 2; + if (result == 0) + printf("Heads!\n"); + else + printf("Tails!\n"); +} + +void generate_random_number(int lower, int upper) { + if (lower > upper) { + printf("Invalid range! Lower bound must be less than or equal to upper bound.\n"); + return; + } + int num = (rand() % (upper - lower + 1)) + lower; + printf("Random number: %d\n", num); +} diff --git a/tags b/tags new file mode 100644 index 0000000..48bf7aa --- /dev/null +++ b/tags @@ -0,0 +1,31 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_OUTPUT_EXCMD mixed /number, pattern, mixed, or combineV2/ +!_TAG_OUTPUT_FILESEP slash /slash or backslash/ +!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/ +!_TAG_PATTERN_LENGTH_LIMIT 96 /0 for no limit/ +!_TAG_PROC_CWD /home/klein/codeWS/C/coinflipper/ // +!_TAG_PROGRAM_AUTHOR Universal Ctags Team // +!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/ +!_TAG_PROGRAM_URL https://ctags.io/ /official site/ +!_TAG_PROGRAM_VERSION 5.9.0 // +$(OBJDIR)/%.o build/Makefile /^$(OBJDIR)\/%.o: $(SRCDIR)\/%.c$/;" t +$(TARGET) build/Makefile /^$(TARGET): $(OBJS)$/;" t +BINDIR build/Makefile /^BINDIR = ..\/bin$/;" m +CC build/Makefile /^CC = gcc$/;" m +CFLAGS build/Makefile /^CFLAGS = -I..\/include$/;" m +DELAY src/main.c /^#define DELAY /;" d file: +FUNCTIONS_H include/functions.h /^#define FUNCTIONS_H$/;" d +OBJDIR build/Makefile /^OBJDIR = ..\/obj$/;" m +OBJS build/Makefile /^OBJS = $(OBJDIR)\/main.o$/;" m +SRCDIR build/Makefile /^SRCDIR = ..\/src$/;" m +TARGET build/Makefile /^TARGET = $(BINDIR)\/coin_flip_random_number$/;" m +all build/Makefile /^all: $(TARGET)$/;" t +clean build/Makefile /^clean:$/;" t +coin_animation src/main.c /^void coin_animation(int result) {$/;" f typeref:typename:void +coinflipper Project README.md /^# coinflipper Project$/;" c +flip_coin src/main.c /^void flip_coin() {$/;" f typeref:typename:void +generate_random_number src/main.c /^void generate_random_number(int lower, int upper) {$/;" f typeref:typename:void +handle_exit src/main.c /^void handle_exit() {$/;" f typeref:typename:void +main src/main.c /^int main() {$/;" f typeref:typename:int +random_number_animation src/main.c /^void random_number_animation(int num) {$/;" f typeref:typename:void