#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// ÇؽøÊÀÇ °¢ ¿£Æ®¸®¸¦ ³ªÅ¸³»´Â ±¸Á¶Ã¼
struct HashEntry {
char playerName[20];
double balance;
};
// ÇØ½Ã¸Ê ±¸Á¶Ã¼
struct HashMap {
struct HashEntry entries[100];
int size;
};
// Á߸³°èÁÂ
struct HashEntry neutralAccount;
// ³ª¸¸ÀÇ Çؽà ÇÔ¼ö(murmur3 Âü°í)
unsigned int myHash(const char *key, unsigned int len) {
const unsigned int c1 = 0xdeadbeef;
const unsigned int c2 = 0xfaceb00c;
const unsigned int seed = 0xabcdef01;
unsigned int hash = seed;
const int nblocks = len / 4;
const unsigned int *blocks = (const unsigned int *)(key);
int i;
unsigned int k;
for (i = 0; i < nblocks; i++) {
k = blocks[i];
k *= c1;
k = (k << 15) | (k >> (32 - 15));
k *= c2;
hash ^= k;
hash = (hash << 13) | (hash >> (32 - 13));
hash = hash * 5 + 0xe6546b64;
}
const unsigned char *tail = (const unsigned char *)(key + nblocks * 4);
unsigned int k1 = 0;
switch (len & 3) {
case 3:
k1 ^= tail[2] << 16;
case 2:
k1 ^= tail[1] << 8;
case 1:
k1 ^= tail[0];
k1 *= c1;
k1 = (k1 << 15) | (k1 >> (32 - 15));
k1 *= c2;
hash ^= k1;
}
hash ^= len;
hash ^= (hash >> 16);
hash *= 0x85ebca6b;
hash ^= (hash >> 13);
hash *= 0xc2b2ae35;
hash ^= (hash >> 16);
return hash % 100;
}
// °èÁ¸¦ Çؽøʿ¡ Ãß°¡ÇÏ´Â ÇÔ¼ö
void addAccount(struct HashMap *hashMap, const char *playerName, double balance) {
if (hashMap->size < 100) {
unsigned int index = myHash(playerName, (unsigned int)strlen(playerName)) % 100;
struct HashEntry *entry = &(hashMap->entries[index]);
// Áߺ¹µÈ Ç÷¹À̾î À̸§ÀÎ °æ¿ì, ÇØ´ç Ç÷¹À̾îÀÇ °èÁ¸¦ ¾÷µ¥ÀÌÆ®
while (entry->balance != 0 && strcmp(entry->playerName, playerName) != 0) {
index = (index + 1) % 100;
entry = &(hashMap->entries[index]);
}
if (entry->balance != 0) {
// ÀÌ¹Ì Á¸ÀçÇÏ´Â Ç÷¹À̾îÀÇ °èÁ¸¦ ¾÷µ¥ÀÌÆ®
entry->balance = balance;
} else {
// »õ·Î¿î Ç÷¹À̾îÀÇ °èÁ¸¦ Ãß°¡
strncpy(entry->playerName, playerName, 19);
entry->playerName[19] = '\0';
entry->balance = balance;
hashMap->size++;
}
}
}
void addBetToNeutralAccount(double betAmount) {
neutralAccount.balance += betAmount;
}
void endGame(struct HashMap *hashMap) {
if (neutralAccount.balance <= 0) {
printf("Á߸³ °èÁ Àܾ×ÀÌ ºÎÁ·ÇÕ´Ï´Ù.\n");
return;
}
// °ÔÀÓ °á°ú °è»êÇÏ°í À̱ä Ç÷¹À̾îÀÇ À̸§À» winningPlayer¿¡ ¼³Á¤
char winningPlayer[20];
// °ÔÀÓ °á°ú °è»ê ¹× À̱ä Ç÷¹ÀÌ¾î ¼³Á¤ ÄÚµå Ãß°¡
// À̱ä Ç÷¹À̾°Ô ¹èÆà ÀÌü
unsigned int winningIndex = myHash(winningPlayer, (unsigned int)strlen(winningPlayer));
if (winningIndex >= 100) {
fprintf(stderr, "Error: Index out of bounds.\n");
exit(EXIT_FAILURE);
}
struct HashEntry *winningEntry = &(hashMap->entries[winningIndex]);
winningEntry->balance += neutralAccount.balance;
printf("%sÀÌ(°¡) À̱ä Ç÷¹À̾î·Î ¼±Á¤µÇ¾ú½À´Ï´Ù. %.2f°¡ ÀÌüµÇ¾ú½À´Ï´Ù.\n", winningPlayer, neutralAccount.balance);
// Á߸³ °èÁ ÃʱâÈ
printf("Á߸³ °èÁ¿¡ ÀúÀåµÈ ÃÑ ¹èÆà ±Ý¾×: %.2f\n", neutralAccount.balance);
addBetToNeutralAccount(0);
}
double getBalance(struct HashMap *hashMap, const char *playerName) {
if (hashMap == NULL) {
fprintf(stderr, "Error: hashMap is NULL.\n");
exit(EXIT_FAILURE);
}
unsigned int index = myHash(playerName, (unsigned int)strlen(playerName));
if (index >= 100) {
fprintf(stderr, "Error: Index out of bounds.\n");
exit(EXIT_FAILURE);
}
struct HashEntry *entry = &(hashMap->entries[index]);
int i = 1;
// ¼±Çü Ž»ç¸¦ ÅëÇØ Ãæµ¹ ó¸®
while (entry->balance != 0 && strcmp(entry->playerName, playerName) != 0) {
index = (index + i) % 100;
entry = &(hashMap->entries[index]);
i++;
// ¸ðµç ¿£Æ®¸®¸¦ È®ÀÎÇÑ °æ¿ì
if (i > 100) {
fprintf(stderr, "Error: Player not found.\n");
return -1; // Ç÷¹À̾ ãÁö ¸øÇÑ °æ¿ì -1 ¹Ýȯ
}
}
return entry->balance;
}
void makeBet(struct HashMap *hashMap, const char *playerName, double betAmount) {
if (hashMap == NULL) {
fprintf(stderr, "Error: hashMap is NULL.\n");
exit(EXIT_FAILURE);
}
double playerBalance = getBalance(hashMap, playerName);
if (playerBalance == -1) {
// Á¸ÀçÇÏÁö ¾Ê´Â Ç÷¹À̾îÀÇ °æ¿ì ¿À·ù ¸Þ½ÃÁö Ãâ·Â
printf("Ç÷¹À̾ ãÀ» ¼ö ¾ø½À´Ï´Ù.\n");
} else if (betAmount > 0 && betAmount <= playerBalance) {
unsigned int index = myHash(playerName, (unsigned int)strlen(playerName));
if (index >= 100) {
fprintf(stderr, "Error: Index out of bounds.\n");
exit(EXIT_FAILURE);
}
struct HashEntry *entry = &(hashMap->entries[index]);
// ÀÌ¹Ì Á¸ÀçÇÏ´Â Ç÷¹À̾îÀÇ °èÁ¸¦ ã¾Æ ¾÷µ¥ÀÌÆ®
while (entry->balance != 0 && strcmp(entry->playerName, playerName) != 0) {
index = (index + 1) % 100;
entry = &(hashMap->entries[index]);
}
entry->balance -= betAmount; // ÀÌ¹Ì Á¸ÀçÇÏ´Â Ç÷¹À̾îÀÇ °èÁ¸¦ ¾÷µ¥ÀÌÆ®
printf("%sÀÌ(°¡) %.2f¸¦ ¹èÆÃÇß½À´Ï´Ù.\n", playerName, betAmount);
addBetToNeutralAccount(betAmount);
} else {
printf("¹èÆÃÇÒ ±Ý¾×ÀÌ À¯È¿ÇÏÁö ¾Ê°Å³ª Àܾ×ÀÌ ºÎÁ·ÇÕ´Ï´Ù. ´Ù½Ã ½ÃµµÇϼ¼¿ä.\n");
}
}
int main(void) {
struct HashMap hashMap;
hashMap.size = 0;
addAccount(&hashMap, "Player1", 1000.0);
addAccount(&hashMap, "Player2", 1000.0);
while (1) {
// ÀÜ¾× Ç¥½Ã
printf("Ç÷¹À̾î 1 Àܾ×: %.2f\n", getBalance(&hashMap, "Player1"));
printf("Ç÷¹À̾î 2 Àܾ×: %.2f\n", getBalance(&hashMap, "Player2"));
char playerName[20];
// Ãʱ⠺£ÆÃ
makeBet(&hashMap, "Player1", 100.0);
makeBet(&hashMap, "Player2", 100.0);
printf("ÇöÀç±îÁöÀÇ ÃÑ ¹èÆà ±Ý¾×: %.2f\n", neutralAccount.balance);
// playerNameÀ» ÀÔ·ÂÀ¸·Î ¹Þ½À´Ï´Ù.
printf("Ç÷¹À̾îÀÇ À̸§À» ÀÔ·ÂÇϼ¼¿ä: ");
if (scanf("%s", playerName) != 1) {
fprintf(stderr, "Error: Failed to read playerName.\n");
exit(EXIT_FAILURE);
}
// ¼öÁ¤µÈ ºÎºÐ ½ÃÀÛ
double playerBalance = getBalance(&hashMap, playerName);
if (playerBalance == -1) {
printf("Ç÷¹À̾ ãÀ» ¼ö ¾ø½À´Ï´Ù.\n");
} else {
printf("Ç÷¹À̾î %s Àܾ×: %.2f\n", playerName, playerBalance);
}
// ¼öÁ¤µÈ ºÎºÐ ³¡
// ÄÉÀ̽º ¼±ÅÃ
printf("1. ÄÝ\n2. µû´ç\n3. ÄõÅÍ\n4. ÇÏÇÁ\n5. Ç®\n6. ¸Æ½º\n");
int betOption;
printf("¹èÆà ¿É¼ÇÀ» ¼±ÅÃÇϼ¼¿ä (1-6): ");
if (scanf("%d", &betOption) != 1) {
fprintf(stderr, "Error: Failed to read betOption.\n");
exit(EXIT_FAILURE);
}
// Ãß°¡ º£ÆÃ
switch (betOption) {
case 1: // ÄÝ
makeBet(&hashMap, playerName, 100.0);
break;
case 2: // µû´ç
makeBet(&hashMap, playerName, 200.0); // 100¿ø * 2
break;
case 3: // ÄõÅÍ
makeBet(&hashMap, playerName, neutralAccount.balance / 4);
break;
case 4: // ÇÏÇÁ
makeBet(&hashMap, playerName, neutralAccount.balance / 2);
break;
case 5: // Ç®
makeBet(&hashMap, playerName, neutralAccount.balance);
break;
case 6: // ¸Æ½º
makeBet(&hashMap, playerName, getBalance(&hashMap, playerName));
break;
default:
printf("À߸øµÈ ¼±ÅÃÀÔ´Ï´Ù. ´Ù½Ã ½ÃµµÇϼ¼¿ä.\n");
continue;
}
}
return 0;
}