initial commit

This commit is contained in:
klein panic
2024-09-29 02:35:03 -04:00
commit d891eb5237
8 changed files with 310 additions and 0 deletions

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# coinflipper Project
This is a C project generated with the setup tool.

BIN
bin/coin_flip_random_number Executable file

Binary file not shown.

21
build/Makefile Normal file
View File

@@ -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)

7
include/functions.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
void flip_coin();
void generate_random_number(int lower, int upper);
#endif // FUNCTIONS_H

BIN
obj/main.o Normal file

Binary file not shown.

190
src/main.c Normal file
View File

@@ -0,0 +1,190 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ncurses.h>
#include <unistd.h> // 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);
}

58
src/main.c.bak Normal file
View File

@@ -0,0 +1,58 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 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);
}

31
tags Normal file
View File

@@ -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