Skip to content

Instantly share code, notes, and snippets.

@loxygenK
Last active October 17, 2020 10:22
Show Gist options
  • Save loxygenK/b80438bc67103fe633aff64401975753 to your computer and use it in GitHub Desktop.
Save loxygenK/b80438bc67103fe633aff64401975753 to your computer and use it in GitHub Desktop.
速攻で生やした指スマ
#include <stdio.h>
#include <stdlib.h>
typedef int bool;
#define true 1
#define false 0
#define min(a, b) (a < b ? a : b)
bool askyn(const char *prompt) {
printf("%s [yn]:", prompt);
while(true) {
char userInput;
scanf("%c", &userInput);
if(userInput == 'y' || userInput == 'n')
return userInput == 'y';
}
}
int inputClamp(const char *prompt, int min, int max) {
printf("%s [%d~%d]: ", prompt, min, max);
while(true) {
int userInput;
scanf("%d", &userInput);
if(userInput >= min && userInput <= max)
return userInput;
}
}
int main(int argc, char const* argv[])
{
printf("†FINGER SMASH†\n");
bool isUserTurn = askyn("Who goes first, you want?");
int userFingers = 2, computerFingers = 2;
while(userFingers > 0 && computerFingers > 0) {
printf("-----------------------------\n");
printf("† %s turn †\n", isUserTurn ? "Your" : "My");
printf(" USER: %d v.s. %d: COMPUTER\n", userFingers, computerFingers);
int expectedSum = isUserTurn
? inputClamp(" What number will you say?", 0, userFingers + computerFingers)
: rand() % (userFingers + computerFingers + 1);
int userRaise = inputClamp(
" How many finger will you raise?",
0,
isUserTurn ? min(userFingers, expectedSum) : userFingers
);
int cpRaise = rand() % (computerFingers + 1);
int sum = userRaise + cpRaise;
printf("\n");
printf(" Let's do this:\n");
printf(" Number: %d\n", expectedSum);
printf(" YOU: %d - %d: ME\n", userRaise, cpRaise);
if(sum == expectedSum) {
printf(" Ah yes, %s hit! What a genius!\n", isUserTurn ? "You" : "I");
if(isUserTurn) userFingers--; else computerFingers--;
} else if(abs(expectedSum - sum) <= 1) {
printf(" Hmm.. It was super close.\n");
}
printf("\n");
isUserTurn = !isUserTurn;
}
printf("† GAME SET †\n");
if(isUserTurn) {
printf(" You won! You did it!\n");
} else {
printf(" I won! You did a good job!\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment