#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>
void Register(char*, int*);
void DisplayFrame(char*, int, int,int, int, int, int, int*, int*, int);
void Shuffle(int*);
void SetOrder(int**, int*, int*, int);
void CompareTable(int*, int*);
bool betting(int*, int*,int*);
enum {PLAYER, COMPUTER};
void main()
{
// User information.
char name[20];
int age;
// Game information.
char askContinue;
int cntGame = 0;
int bettingAmount;
int firstGetter = PLAYER; // 0 - player, 1 - computer.
int shuffledCards[20];
int* order[2];
// Com. information.
int comMoney;
int comBetting = 0;
int comCards[2] = {0};
int comResult;
char comResultName[20];
// Player information.
int playerMoney;
int playerBetting = 0;
int playerCards[2] = {0};
int playerResult;
char playerResultName[20];
// Game setting.
int defaultMoney = 1000;
playerMoney = comMoney = defaultMoney;
srand(time(NULL));
// User Register.
Register(name, &age);
while(1)
{
// Initialize.
cntGame++;
bettingAmount = 0;
comCards[0] = 0;
comCards[1] = 0;
playerCards[0] = 0;
playerCards[1] = 0;
// Shuffle all card.
Shuffle(shuffledCards);
// Decide first turn.
SetOrder(order, playerCards, comCards, firstGetter);
// Show default information.
DisplayFrame(name, age, cntGame, firstGetter, bettingAmount, comMoney, playerMoney, comCards, playerCards, 0);
// Loop for dealing and betting.
bool die = false;
for(int i=0; i<4; i++)
{
// Dealing.
*(order[i%2]+i/2) = shuffledCards[i];
if(i%2==1)
{
printf("Ä«µå¸¦ ¹ÞÀ¸½Ã·Á¸é ¾Æ¹«Å°³ª ´©¸£¼¼¿ä.\n");
getch();
// Show dealing information.
DisplayFrame(name, age, cntGame, firstGetter, bettingAmount, comMoney, playerMoney, comCards, playerCards, 0);
if(!betting(&comMoney, &playerMoney, &bettingAmount))
{
die = true;
break;
}
}
}
if(die)
{
printf("ComputerÀÇ ½Â¸®ÀÔ´Ï´Ù.%d %d\n", comResult, playerResult);
comMoney += bettingAmount*2;
firstGetter = COMPUTER; // Set first getter to computer.
continue;
}
// Show final stats.
DisplayFrame(name, age, cntGame, firstGetter, bettingAmount, comMoney, playerMoney, comCards, playerCards, 1);
printf("½ÂÆÐ¸¦ È®ÀÎÇϽ÷Á¸é ¾Æ¹«Å°³ª ´·¯ÁÖ¼¼¿ä.\n");
getch();
// Show result.
DisplayFrame(name, age, cntGame, firstGetter, bettingAmount, comMoney, playerMoney, comCards, playerCards, 1);
// Compare to point table and get the point.
CompareTable(comCards, &comResult);
CompareTable(playerCards, &playerResult);
// Decide winner.
if(comResult>playerResult)
{
printf("ComputerÀÇ ½Â¸®ÀÔ´Ï´Ù.%d %d\n", comResult, playerResult);
comMoney += bettingAmount*2;
firstGetter = COMPUTER; // Set first getter to computer.
}
else if(comResult<playerResult)
{
printf("PlayerÀÇ ½Â¸®ÀÔ´Ï´Ù.%d %d\n", comResult, playerResult);
playerMoney += bettingAmount*2;
firstGetter = PLAYER; // Set first getter to player.
}
else
{
printf("¹«½ÂºÎÀÔ´Ï´Ù.%d %d\n", comResult, playerResult);
playerMoney += bettingAmount;
comMoney += bettingAmount;
}
// Notify Ending of the game.
if(comMoney<=0)
{
printf("Computer°¡ °ÅÁö°¡ µÇ¾î ´õ ÀÌ»ó °ÔÀÓÀ» ÇÒ ¼ö°¡ ¾ø½À´Ï´Ù. ´Ù½Ã ½ÇÇàÇØ ÁÖ¼¼¿ä.\n");
break;
}
if(playerMoney<=0)
{
printf("Player°¡ °ÅÁö°¡ µÇ¾î ´õ ÀÌ»ó °ÔÀÓÀ» ÇÒ ¼ö°¡ ¾ø½À´Ï´Ù. ´Ù½Ã ½ÇÇàÇØ ÁÖ¼¼¿ä.\n");
break;
}
// Ask continuation.
fflush(stdin);
printf("°è¼ÓÇϽðڽÀ´Ï±î?(y/n): ");
scanf("%c", &askContinue);
if(askContinue=='n')
break;
}
}
void Register(char* name, int* age)
{
printf("¼¸´ÙÀÔ´Ï´Ù. ¾î¼¿À¼¼¿ä.\n");
printf("À̸§À» ÀÔ·ÂÇØÁÖ¼¼¿ä: ");
scanf("%s", name);
printf("³ªÀ̸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä: ");
scanf("%d", age);
}
void DisplayFrame(char* name, int age, int cntGame, int firstGetter, int bettingAmount, int comMoney, int playerMoney, int* comCards, int* playerCards, int open)
{
char strFirst[10];
if(firstGetter==PLAYER)
strcpy(strFirst, "Player");
else if(firstGetter==COMPUTER)
strcpy(strFirst, "Computer");
system("cls");
printf(" À̸§: %s ³ªÀÌ: %d ¼¼\n", name, age);
printf(" [ %d ] ¹øÂ° °ÔÀÓ (¼±): %s, ¹èÆÃ±Ý¾×: %d¿ø, Computer: %d ¿ø, Player %d ¿ø\n", cntGame, strFirst, bettingAmount*2, comMoney, playerMoney);
if(comCards[0]!=0 && comCards[1]==0)
{
printf("%15s %15s\n", "Computer Ä«µå", "Player Ä«µå");
printf("####### #######\n");
printf("# # # #\n");
if(open==1)
printf("# %3d # # %3d #\n", comCards[0], playerCards[0]);
else
printf("# * # # %3d #\n", playerCards[0]);
printf("# # # #\n");
printf("####### #######\n");
}else if(comCards[0]!=0 && comCards[1]!=0)
{
printf("%15s %15s\n", "Computer Ä«µå", "Player Ä«µå");
printf("####### ####### ####### #######\n");
printf("# # # # # # # #\n");
if(open==1)
printf("# %3d # # %3d # # %3d # # %3d #\n", comCards[0], comCards[1], playerCards[0], playerCards[1]);
else
printf("# * # # * # # %3d # # %3d #\n", playerCards[0], playerCards[1]);
printf("# # # # # # # #\n");
printf("####### ####### ####### #######\n");
}
}
void Shuffle(int* shuffledCards)
{
printf("Ä«µå¸¦ ¼¯½À´Ï´Ù...\n");
bool swCards[20] = {0};
int i=0;
int r;
while(i<20)
{
r = rand()%20;
if(swCards[r] == false)
{
swCards[r] = true;
shuffledCards[i] = r>9 ? r-10+1: r+1;
i++;
}
}
}
void SetOrder(int* order[], int* playerCards, int* comCards, int player)
{
if(player==0)
{
order[0] = playerCards;
order[1] = comCards;
}else if(player==1)
{
order[0] = comCards;
order[1] = playerCards;
}
}
void CompareTable(int* cards, int* result)
{
int table[][3] = {
{3, 8, 26},
{8, 3, 26},
{10, 10, 25},
{9, 9, 24},
{8, 8, 23},
{7, 7, 22},
{6, 6, 21},
{5, 5, 20},
{4, 4, 19},
{3, 3, 18},
{2, 2, 17},
{1, 1, 16},
{1, 2, 15},
{2, 1, 15},
{1, 4, 14},
{4, 1, 14},
{1, 9, 13},
{9, 1, 13},
{1, 10, 12},
{10, 1, 12},
{4, 10, 11},
{10, 4, 11},
{4, 6, 10},
{6, 4, 10}
};
int plag = 0;
for(int j=0; j<22; j++)
{
if(cards[0]==table[j][0] && cards[1]==table[j][1])
{
*result = table[j][2];
plag = 1;
break;
}
}
if(plag==0)
*result = (cards[0]+cards[1])%10;
}
bool betting(int* comMoney, int* playerMoney, int* bettingAmount)
{
int bet;
printf("¹èÆÃ ±Ý¾×À» ÀÔ·ÂÇϼ¼¿ä(-1:Die, 0~%d): ", *playerMoney);
scanf("%d", &bet);
// For die.
if(bet==-1)
return false;
// For all-in.
if(bet>*playerMoney)
bet = *playerMoney;
else if(bet>*comMoney)
bet = *comMoney;
// Betting.
*bettingAmount += bet;
*comMoney -= bet;
*playerMoney -= bet;
return true;
}
|