Skip to content

Instantly share code, notes, and snippets.

@strideynet
Created November 6, 2019 18:16
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 strideynet/b55e9fcc5c534925bb0643c85a10f38a to your computer and use it in GitHub Desktop.
Save strideynet/b55e9fcc5c534925bb0643c85a10f38a to your computer and use it in GitHub Desktop.
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
// get random number from 0 up to `max` [0 - `(max - 1)` inclusive]
int getRandom(int max);
// fill the array with emotes yay
void fillEmotes(wchar_t *arr, int len);
// fill the "drum" with values
void randomiseDrum(int *drum, int drumWidth, int emotesLength);
// change one column of the drum
int nudgeDrum(int *drum, int drumWidth, int emotesLength, int toNudge);
// print the value of the drum. if `showColNums != 0` then the column numbers will be printed below the drum
void printDrum(int *drum, int drumWidth, int *emotes, int showColNums);
// get an integer value from the user
int getNumFromUser(char *prompt);
// get a single character from the user
char getCharFromUser(char *prompt);
// 0 - loss, 1 - normal win, 2 - jackpot
int getVictoryLevel(int *drum, int drumWidth, int emotesLength);
// print out all the emotes in use this game
void printEmotes(wchar_t *emotes, int emotesLength);
// entrypoint
int main() {
setlocale(LC_CTYPE, "");
srand(time(NULL));
int difficulty = -1;
while (!(difficulty >= 3 && difficulty <= 6)) {
difficulty = getNumFromUser("how many symbols? [3 - 6] \u03BB ");
}
int emotesLength = difficulty;
wchar_t emotes[emotesLength];
fillEmotes(emotes, emotesLength);
printEmotes(emotes, emotesLength);
difficulty = -1;
while (!(difficulty >= 3)) {
difficulty = getNumFromUser("how wide should the drum be? \u03BB ");
}
wprintf(L"to get the jackpot, you will need %d of one symbol\n" \
L"to win normally, you will need at least %d of one symbol\n\n", difficulty, ( ( difficulty / 2 ) + 1 ));
int drumWidth = difficulty;
int drum[drumWidth];
randomiseDrum(drum, drumWidth, emotesLength);
for (int i = 0; i < 15; i++) {
printDrum(drum, drumWidth, emotes, 0);
randomiseDrum(drum, drumWidth, emotesLength);
}
printDrum(drum, drumWidth, emotes, 1);
char res = getCharFromUser("would you like to nudge a column? [y / n] \u03BB ");
if (res == 'y' || res == 'Y') {
int res2 = getNumFromUser("which column? enter a number not listed to cancel [0..n] \u03BB ");
if (nudgeDrum(drum, drumWidth, emotesLength, res2)) {
printDrum(drum, drumWidth, emotes, 1);
} else {
wprintf(L"nudge cancelled - calculating victory...\n");
}
}
switch (getVictoryLevel(drum, drumWidth, emotesLength)) {
wchar_t emote;
case 1: { // normal win
emote = 0x1F4B8;
wprintf(L" %lc you win! reckon you can get a jackpot next time?", emote);
break;
}
case 2: { // jackpot win
emote = 0x1F911;
wprintf(L" %lc JACKPOT!!! can you do it again though?", emote);
break;
}
default: { // loss
emote = 0x1F61E;
wprintf(L" %lc tough luck... maybe you'll do better in the next game", emote);
break;
}
}
return 0;
}
void fillEmotes(wchar_t *arr, int len) {
wchar_t storage[] = { // 6
0x1F514, // bell
0x1F34A, // orange
0x1F352, // cherry
0x1F4B0, // moneybag
0x1F34E, // apple
0x1F3E6 // bank
};
for (int i = 0; i < len; i++) {
arr[i] = storage[i];
}
}
int getRandom(int max) {
return rand() % max;
}
void randomiseDrum(int *drum, int drumWidth, int emotesLength) {
for (int i = 0; i < drumWidth; i++) {
drum[i] = getRandom(emotesLength);
}
}
int nudgeDrum(int *drum, int drumWidth, int emotesLength, int toNudge) {
if (toNudge >= drumWidth || toNudge < 0) {
return 0;
}
drum[toNudge] = getRandom(emotesLength);
return 1;
}
void printDrum(int *drum, int drumWidth, int *emotes, int showColNums) {
wprintf(L" | ");
for (int i = 0; i < drumWidth; i++) {
wprintf(L"%lc | ", emotes[drum[i]]);
}
if (showColNums) {
wprintf(L"\n | ");
for (int i = 0; i < drumWidth; i++) {
wprintf(L"%-*d | ", 2, i);
}
wprintf(L"\n");
} else {
wprintf(L"\n");
}
}
int getNumFromUser(char *prompt) {
int res;
wprintf(L"%s", prompt);
wscanf(L" %d", &res);
int c;
while ((c = getchar()) != '\n' && c != EOF)
continue;
return res;
}
char getCharFromUser(char *prompt) {
char res;
wprintf(L"%s", prompt);
wscanf(L" %lc", &res);
return res;
}
int getVictoryLevel(int *drum, int drumWidth, int emotesLength) { // this should work for all sizes of drum
int successLevel = 0;
int counts[emotesLength];
// wprintf(L"Debug: Counting %d emotes...\n", emotesLength); // DEBUG
for (int emote = 0; emote < emotesLength; emote++) { // count each emote
// wprintf(L" Counting emote #%d\n", emote); // DEBUG
counts[emote] = 0;
for (int drumCol = 0; drumCol < drumWidth; drumCol++) { // loop through drum
counts[emote] += drum[drumCol] == emote ? 1 : 0;
// wprintf(L" %d\n", counts[emote]); // DEBUG
}
if (counts[emote] == drumWidth) { // [ width ] - jackpot
successLevel = 2;
// wprintf(L" Jackpot counted\n"); // DEBUG
break; // short circuit
} else if (counts[emote] >= (( drumWidth / 2 ) + 1 )) { // [ ( width / 2 ) + 1 ] - not jackpot but no other emote will have more
successLevel = 1;
// wprintf(L" Normal victory counted\n"); // DEBUG
break; // short circuit
}
}
return successLevel;
}
void printEmotes(wchar_t *emotes, int emotesLength) {
wprintf(L"symbols in play: ");
for (int i = 0; i < emotesLength; i++) {
wprintf(L"%lc ", emotes[i]);
}
wprintf(L"\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment