Created
February 2, 2025 07:08
-
-
Save briandowns/2658c8e36b26a566098492b00cc5f9da to your computer and use it in GitHub Desktop.
Bim Bum Bam
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cc -o bim_bum_bam bim_bum_bam.c | |
#include <stdbool.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <time.h> | |
#include <unistd.h> | |
#define ERR_EVEN_ODD_CHOICE "errore: solo pari o dispari" | |
void | |
clear_screen() | |
{ | |
const char *CLEAR_SCREEN_ANSI = "\e[1;1H\e[2J"; | |
write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 11); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
if (argc > 2) { | |
fprintf(stderr, "%s\n", ERR_EVEN_ODD_CHOICE); | |
return 1; | |
} | |
if (strcmp(argv[1], "pari") != 0 && strcmp(argv[1], "dispari") != 0) { | |
fprintf(stderr, "%s\n", ERR_EVEN_ODD_CHOICE); | |
return 1; | |
} | |
char player_choice[5]; | |
strncpy(player_choice, argv[1], 5); | |
srand(time(NULL)); | |
static unsigned int player_score = 0; | |
static unsigned int computer_score = 0; | |
static unsigned int player_number = 0; | |
static unsigned int computer_choice = 0; | |
while (true) { | |
clear_screen(); | |
printf("[+] Bim Bum Bam [+]\n\n"); | |
printf("Giocatore : %d\n", player_score); | |
printf("Computer : %d\n\n", computer_score); | |
printf("Scegli un numero 1-5: \n"); | |
scanf("%d", &player_number); | |
if (player_number > 5) { | |
printf("\nerrore: numero piu di 5\n"); | |
sleep(1); | |
continue; | |
} | |
computer_choice = rand() % (5 - 0 + 1) + 0; | |
if (player_number == 0 && computer_choice == 0) { | |
printf("Pareggio!\n"); | |
sleep(1); | |
continue; | |
} | |
printf("Computer sceglia: %d\n", computer_choice); | |
if ((strcmp(player_choice, "pari") == 0 && (player_number + computer_choice) % 2 == 0) || | |
(strcmp(player_choice, "dispari") == 0 && (player_number + computer_choice) % 2 != 0)) { | |
printf("Hai vinto!\n"); | |
player_score++; | |
sleep(1); | |
continue; | |
} else { | |
printf("Computer ha vinto!\n"); | |
computer_score++; | |
sleep(1); | |
continue; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment