This bit of code is a small game to see how long you play a game of FizzBuzz without messing up.
Code:
The top of the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef enum {false, true} boolean;
typedef enum {FIZZ, BUZZ, FIZZBUZZ} FB;
int n = 0; // the number that is being incremented
char input[256];
boolean user_turn(void);
FB n_fizz_buzz_fizzbuzz(void); // is n or fizz or buzz or fizzbuzz
The main
function:
int main(void) {
boolean should_loop = true;
while(should_loop) {
n++;
printf("Number: %d\n", n);
should_loop = user_turn();
}
return 0;
}
The user_turn
function:
/*
Prompts the user for the answer
*/
boolean user_turn(void) {
FB correct = n_fizz_buzz_fizzbuzz();
scanf("%s", input);
if(!((strcmp(input, "fizzbuzz") == 0 && correct == FIZZBUZZ) || (strcmp(input, "fizz") == 0 && correct == FIZZ) || (strcmp(input, "buzz") == 0 && correct == BUZZ) || (atoi(input) == n && correct == n))) {
printf("Sorry! That is incorrect.\nScore: %d\n", n - 1);
return false;
}
return true;
}
The n_fizz_buzz_fizzbuzz
function:
/*
Returns the correct answer either:
FIZZBUZZ(2)
BUZZ(1)
FIZZ(0)
n
*/
FB n_fizz_buzz_fizzbuzz(void) {
if(n % 15 == 0)
return FIZZBUZZ;
if(n % 5 == 0)
return BUZZ;
if(n % 3 == 0)
return FIZZ;
return n;
}
How it works:
- The number is logged to console.
- The user enters their answer, which is either FizzBuzz, Fizz, Buzz, or the number that was logged.
- If they are incorrect, the game ends.
- If they were correct, the number in incremented by 1 and this process goes back to step 1.
My main concerns:
- This might only apply to Java, but should the separate(non-main) functions use Input and Output?
- Are the enumerated types appropriate, or are they causing too much trouble?
- Is anything repetitive?
- Are there any inefficiencies?
- Are there any inconsistencies?
- Are there any bad practices or practices that are left out?
Any other recommendations are welcome and encouraged.
boolean
type, unless you prefer the verbose name, that is... \$\endgroup\$