Skip to content

Instantly share code, notes, and snippets.

@PackmanDude
Last active April 24, 2025 18:15
Show Gist options
  • Save PackmanDude/42746b1db03e014631cacb020db9c715 to your computer and use it in GitHub Desktop.
Save PackmanDude/42746b1db03e014631cacb020db9c715 to your computer and use it in GitHub Desktop.
Magic 8 game in C.
/* Magic 8 game in C
Copyright (C) 2025 PackmanDude
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, char *argv[])
{
if (!argc) return EXIT_FAILURE;
if (argc < 2) goto fail;
{
const size_t last_argument_length = strlen(argv[argc - 1]);
// Check if the last character is not a question mark
if (!last_argument_length || argv[argc - 1][last_argument_length - 1] != '?')
goto fail;
}
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
{
perror("clock_gettime");
return EXIT_FAILURE;
}
srandom(ts.tv_nsec);
}
{
const char * const answers[20] =
{
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy, try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
};
printf("%s.\n", answers[((uint64_t)random() * 20) >> 31]);
}
return EXIT_SUCCESS;
fail:
fprintf(stderr, "You need to ask first! (add a question mark at the end)\n");
return EXIT_FAILURE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment