Skip to content

Instantly share code, notes, and snippets.

@ablwr
Last active January 31, 2018 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ablwr/7a7ef8fff1292f110cd70595fb32c7ee to your computer and use it in GitHub Desktop.
Save ablwr/7a7ef8fff1292f110cd70595fb32c7ee to your computer and use it in GitHub Desktop.
writing something in C from scratch for the first time
// make cat-game and ./cat-game to play, macOS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// adds ability to pause
void nsleep(long us)
{
struct timespec snooze;
snooze.tv_sec = us / (1000 * 1000);
snooze.tv_nsec = (us % (1000 * 1000)) * 1000;
nanosleep(&snooze, NULL);
}
// this part mostly defines different kinds of ASCII cats
// it might be easier to jump to the bottom and move up to read the code!
void draw_winner()
{
printf(" ________________ \n");
printf(" _|::|| Purrrr... | \n");
printf(" _|::| | You win! | \n");
printf(" |::| |________________| \n");
printf(" | /\\**/\\ \n");
printf(" | _( ^_^ ) \n");
printf(" | (_// u--u) \n");
printf(" \\==( ___||) \n");
printf(" ,dP /b/=( /P /b\\ \n");
printf(" |8 || 8\\=== || 8 \n");
printf(" 'b, ,P 'b, ,P \n");
}
void draw_loser()
{
printf(" ________________ \n");
printf(" _|::|| Grrrrr... | \n");
printf(" _|::| | Go away! | \n");
printf(" |::| |________________| \n");
printf(" | /\\**/\\ \n");
printf(" | _( >_< ) \n");
printf(" | (_// u--u) \n");
printf(" \\==( ___||) \n");
printf(" ,dP /b/=( /P /b\\ \n");
printf(" |8 || 8\\=== || 8 \n");
printf(" 'b, ,P 'b, ,P \n");
}
void clear_screen()
{
nsleep(800000);
printf("- - - - - - - - - -\n");
printf(" \\ | / \n");
printf(" '-.;;;.-' \n");
printf(" -==;;;;;==- \n");
printf(" .-';;;'-. \n");
printf(" / | \\ \n");
printf("- - - - - - - - - -\n\n");
nsleep(400000);
printf(". \n");
nsleep(400000);
printf(". . \n");
nsleep(400000);
printf(". . .\n");
nsleep(800000);
}
void fancy_cat()
{
printf(" /\\-/\\ \n");
printf("(=^Y^=)\n");
printf(" (>o<) \n");
}
void wary_cat()
{
printf(" |\\=/| \n");
printf(" /6 6\\ _ \n");
printf(" =\\_Y_/= (( \n");
printf(" / ^ \\ )) \n");
printf(" /| | |\\ // \n");
printf(" ( | | | )/ \n");
printf(" `"" ""` \n");
}
void tiny_cat()
{
printf(" \\ /\\ \n");
printf(" ) ( ') \n");
printf(" ( / ) \n");
printf(" X \\(__)| \n");
}
void long_cat()
{
printf(" /\\-/\\ \n");
printf(" /o o \\ _ \n");
printf(" =\\ Y =/-~~~~~~-,______________________________________________/ ) \n");
printf(" '^--' _______________________________________________/ \n");
printf(" \\ / \n");
printf(" || |---'\\ \\ \n");
printf(" (_(__| ((__| \n");
}
void curious_cat()
{
printf(" /\\___/\\ \n");
printf("( o o ) \n");
printf("( =^= ) \n");
printf("( ) \n");
printf("( ) \n");
printf("( ))))))))))) \n");
}
void hissing_cat()
{
printf(" _ \n ");
nsleep(100000);
printf(" \\`\\ \n ");
nsleep(100000);
printf(" /./././. | | \n ");
nsleep(100000);
printf(" / `/. | | \n ");
nsleep(100000);
printf(" / __ `/'/' \n ");
nsleep(100000);
printf("/\\__/\\ /' `\\ / \n ");
nsleep(100000);
printf("| O O | `.,.| \n ");
nsleep(100000);
printf(" \\vvvv/ |||| \n ");
nsleep(100000);
printf(" |||| |||| \n ");
nsleep(100000);
printf(" |||| |||| \n ");
nsleep(100000);
printf(" `'`' `'`' \n ");
}
void game_intro()
{
printf("Welcome to Cat Game\n");
printf("The goal of this game is to pet a cat.\n");
printf("Every day, you eat lunch outside and encounter many cats.\n");
printf("But they run away when you try to pet them.\n");
printf("Can you win the affection of a cat?\n");
printf("If you want to exit the game at any time, type 0\n");
printf("You have one week (7 days) to win.\n\n");
printf("OK, let's not waste time! \n");
}
void game_options()
{
printf("1. Pick up.\n");
printf("2. Hold treat.\n");
printf("3. Do nothing.\n");
printf("4. Pet! On the head.\n");
printf("5. Pet! On the tail.\n");
printf("6. Pet! On the back.\n");
}
// bulk of operations begins
void final_day(choice)
{
clear_screen();
if (choice == 4){
draw_winner();
} else {
draw_loser();
}
}
void perform_action(choice,count)
{
if (count == 7) {
final_day(choice);
} else {
switch(choice) {
case 1:
printf("You picked... 1. Pick up.\n");
break;
case 2:
printf("You picked... 2. Hold treat.\n");
break;
case 3:
printf("You picked... 3. Do nothing.\n");
break;
case 4:
printf("You picked... 4. Pet! On the head.\n");
printf("\n\n HISSSSSSSSSSSSSSSS!!!");
break;
case 5:
printf("You picked... 5. Pet! On the tail.\n");
printf("\n\n HISSSSSSSSSSSSSSSS!!!");
break;
case 6:
printf("You picked... 6. Pet! On the back.\n");
printf("\n\n HISSSSSSSSSSSSSSSS!!!");
break;
default:
printf("You picked... ???. Do nothing.\n");
break;
}
hissing_cat();
printf("\n The cat ran away! Better luck tomorrow.\n");
}
}
void get_cat()
{
srand(time(NULL));
int randomnumber;
randomnumber = rand() % 6;
switch(randomnumber) {
case 1:
fancy_cat();
break;
case 2:
tiny_cat();
break;
case 3:
curious_cat();
break;
case 4:
long_cat();
break;
case 5:
wary_cat();
break;
default:
wary_cat();
}
printf("\n");
}
void check_choice(count)
{
char buffer[10];
while (1) {
fgets(buffer, 10, stdin);
int choice = buffer[0] - '0';
if (choice == 0){
exit(0);
}
if (choice <= 6 && choice >= 0){
printf("\n");
perform_action(choice,count);
break;
}
else
printf("Must be a number between 1-6 to continue. To quit, type 0.\nYour choice: ");
}
}
const char* game(int user)
{
game_intro();
int count = 0;
do
{
count++;
clear_screen();
printf("- - - - - - - - - -\nDay %d out of 7",count);
printf("\n- - - - - - - - - -\nA new day! Here comes a cat...\n\n");
get_cat();
game_options();
printf("\nWhatcha gonna do? Pick a number.\n");
if (count == 7){
printf("\n\n\nLast chance!!!! Choose wisely. It may very well be that nothing has mattered up until this point...\n\n\n");
}
printf("Your choice: ");
check_choice(count);
} while (count < 7);
exit(0);
}
// start it up!!!
int main()
{
int begin = 0;
while (1) printf("%s", game(begin = !begin));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment