|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
// 扑克程序
// 文件AppendixBpoker.c
/*这是一种名为draw poker的扑克游戏。
用户每次选择1至5下注。 然后徐处理5张牌,选择留下那张牌,替换那张牌。
程序会根据用户的处理结果在屏幕上显示最终持有的5张牌。
然后根据规则计算用户是否赢得彩金,病显示用户的资金。
最后询问用户是否继续游戏。 */
// 头文件
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
// 定义两个常量,判定是同花还是顺子
#define FALSE 0
#define TRUE 1
// 函数原型
void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank [], int cardSuit []);
void getFinalHand(int cardRank [], int cardSuit [] ,int finalRank [],
int finalSuit [], int ranksinHand [],
int suitsinHand []);
int analyzeHand(int ranksinHand [], int suitsinHand []);
mina()
{
int bet;
int bank = 100;
int i;
int cardRank[5]; //13个值的其中之一 (A--K)
int cardSuit[5]; //4个值的其中之一(梅、方、红、黑)
int finalRank[5];
int finalSuit[5];
int ranksinHand[13]; // 用于计算最终一手牌
int suitsinHand[4]; // 用于计算最终一手牌
int winnings;
time_t t;
char suit, rank, stillplay;
// 这个函数在do...while循环外部调用。
// 因为欢迎菜单只需要显示一次,而其他内容要根据用户要求可能运行多次。
printGreeting();
// 每次用户要再次玩该扑克游戏,运行循环。
do {
bet = getBet();
srand(time(&t));
getFirstHand(cardRank, cardSuit);
printf("Your five cards: \n");
for (i = 0; i < 5; i++)
{
suit = getSuit(cardSuit[i]);
rank = getRank(cardRank[i]);
printf("Card #%d: %c%c\n", i + 1, rank, suit);
}
// 这两个数组计算出用户手上牌的值。
// 然后,必须将其清零,以便用户玩多手牌。
for (i = 0; i < 4; i ++)
{
suitsinHand[i] = 0;
}
for (i = 0; i < 13; i++)
{
ranksinHand[i] = 0;
}
getFinalHand(cardRank, cardSuit, finalRank, finalSuit,
ranksinHand, suitsinHand);
printf("Your five final cards: \n");
for (i = 0; i < 5; i++)
{
suit = getSuit(finalSuit[i]);
rank = getRank(finalRank[i]);
printf("Card #%d: %c%c\n", i + 1, rank, suit);
}
winnings = analyzeHand(ranksinHand, suitsinHand);
printf("You won %d!\n", bet*winnings);
bank = bank - bet + (bet*winnings);
printf("\nYour bank is now %d.\n",bank);
printf("\nDo you want to play again? ");
scanf(" %c", &stillplay);
} while (toupper(stillPlay) == 'Y' );
return;
}
/******************************************************************/
// 打印欢迎菜单,并告诉用户游戏规则
void printGreeting()
{
printf("************************************************\n");
printf("\n\n\tWelcom to the Absolute Beginner's Casino\n\n");
printf("\tHome of Video Draw poker\n\n");
printf("**************************************************\n");
printf("Here are the rules:\n");
printf("You start with 100 credits, and you make a bet from ");
printf("1 to 5 credits.\n");
printf("You are dealt 5 cards, and you then choose which ");
printf("cards to keep ");
printf("or discard.\n");
printf("You want to make the best possible hand.\n");
printf("\nHere is the table for winnings (assuming a ");
printf("bet of 1 credit):");
printf("\nPair\t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight\t\t\t4 credits");
printf("\nFlush\t\t\t\t5 credits");
printf("\nFull House\t\t\t8 credits");
printf("\nFour of a Kind\t\t\t10 credits");
printf("\nStraight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n");
}
// 该函数处理最开始的5张牌
void getFirstHand(int cardRank [], int cardSuit [])
{
int i, j;
int cardDup;
for (i = 0; i < 5; i++)
{
cardDup = 0;
do {
// 13张牌中的一张(2-10,J, Q, K, A)
cardRank[i] = (rand() % 13);
// 4种花色之一(梅,方,红,黑)
cardSuit[i] = (rand() % 4);
// 循环确保每张独一无二
for (j = 0; j < i; j++)
{
if ((cardRank[i] == cardRank[j]) && (cardSuit[i] ==
cardSuit[j]))
{
cardDup = 1;
}
}
}while (cardDup == 1);
}
}
// 该函数将代表花色的整数值改为字符。
char getSuit(int suit)
{
switch (suit)
{
case 0:
return('c');
case 1:
return('d');
case 2:
return('h');
case 3:
return('s');
}
}
// 该函数将代表牌号的整数值改为字符。
char getRank(int rank)
{
switch (rank)
{
case 0:
return('A');
case 1:
return('2');
case 2:
return('3');
case 3:
return('4');
case 4:
return('5');
case 5:
return('6');
case 6:
return('7');
case 7:
return('8');
case 8:
return('9');
case 9:
return('T');
case 10:
return('J');
case 11:
return('Q');
case 12:
return('K');
}
}
// 该函数获取用户的下注(1~5)
int getBet()
{
int bet;
do // 保持运行,直至用户输入0~5的数字
{
printf("How much do you want to bet? (Enter a number ");
printf("1 to 5, or 0 to quit the game): ");
printf(" %d", &bet);
if (bet >=1 && bet <= 5)
{
return(bet);
}
else if (bet == 0)
{
exit(1);
}
else
{
printf("\n\nPlease enter a bet from 1-5 or ");
printf("0 to quit the game.\n");
}
} while ((bet < 0) || (bet > 5));
}
// 最后一个函数处理最后一手牌,并计算牌的值。
int analyzeHand(int ranksinHand [], int suitsinHand [])
{
int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
for (suit = 0; suit < 4; suit++)
if (suitsinHand[suit] == 5)
flush = TRUE;
rank = 0;
while (ranksinHand[rank] == 0)
rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
num_consec++
if (num_consec == 5)
{
straight = TRUE;
}
for (rank = 0; rank < 13; rank++)
{
if (ranksinHand[rank] == 4)
four = TRUE;
if (ranksinHand[rank] == 3)
three = TRUE;
if (ranksinHand[rank] == 2)
pairs++;
}
if(straight && flush)
{
printf("Straight flush\n\n");
return (20);
}
else if (four) {
printf("Four of a kind\n\n");
return (10);
}
else if (three && pairs == 1)
{
printf("Full house\n\n");
return (8);
}
else if (flush)
{
printf("Flush\n\n");
return (5);
}
else if (straight)
{
printf("Straight\n\n");
return (4);
}
else if (three)
{
printf("Three of a kind\n\n");
return (3);
}
else if (pairs == 2)
{
printf("Two pairs\n\n");
return (2);
}
else if (pairs == 1)
{
printf("Pair\n\n");
return (1);
}
else
{
printf("High Card\n\n");
return (0);
}
}
// 该函数查看第一手的5张牌,并询问用户是否留牌。
// 如果用户不留牌,则为其替换着张牌。
void getFinalHand(int cardRank [], int cardSuit [], int finalRank [],
int finalSuit [], int ranksinHand [],
int suitinHand [])
{
int i, j, cardDup;
char suit, rank, ans;
for (i = 0; i < 5; i++)
{
suit = getSuit(cardSuit[i]);
rank = getRank(cardRank[i]);
printf("Do you want to keep card #%d: %c%c?", i + 1, rank, suit);
printf("\nPlease answer (Y/N): ");
scanf(" %c", &ans);
if (toupper(ans) == 'Y')
{
finalRank[i] = cardRank[i];
finalSuit[i] = cardSuit[i];
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
continue;
}
else if (toupper(ans) == 'N')
{
cardDup = 0;
do {
cardDup = 0;
finalRank[i] = (rand() % 13);
finalSuit[i] = (rand() % 4);
// 首先检查新派,避免与原来5张牌重复
for (j = 0; j < 5; j++)
{
if ((finalRank[i] == cardRank[j]) &&
(finalSuit[i] == cardSuit[j]))
{
cardDup = 1;
}
}
// 接下来,检查是否有重复的新牌
for (j = 0; j < i; j++)
{
if ((finalRank[i] == finalRank[j]) &&
(finalSuit[i] == finalSuit[j]))
{
cardDup == 1;
}
}
}while (cardDup == 1);
ranksinHand[finalRank[i]]++;
suitsinHand[finalSuit[i]]++;
}
}
}
本帖最后由 superbe 于 2019-10-1 15:00 编辑
打错了很多地方。下面已经改过了,见红色注释标记。
// 扑克程序
// 文件AppendixBpoker.c
/*这是一种名为draw poker的扑克游戏。
用户每次选择1至5下注。 然后徐处理5张牌,选择留下那张牌,替换那张牌。
程序会根据用户的处理结果在屏幕上显示最终持有的5张牌。
然后根据规则计算用户是否赢得彩金,病显示用户的资金。
最后询问用户是否继续游戏。 */
// 头文件
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include <stdlib.h>
// 定义两个常量,判定是同花还是顺子
#define FALSE 0
#define TRUE 1
// 函数原型
void printGreeting();
int getBet();
char getSuit(int suit);
char getRank(int rank);
void getFirstHand(int cardRank [], int cardSuit []);
void getFinalHand(int cardRank [], int cardSuit [] ,int finalRank [],
int finalSuit [], int ranksinHand [],
int suitsinHand []);
int analyzeHand(int ranksinHand [], int suitsinHand []);
main() //原来是mina
{
int bet;
int bank = 100;
int i;
int cardRank[5]; //13个值的其中之一 (A--K)
int cardSuit[5]; //4个值的其中之一(梅、方、红、黑)
int finalRank[5];
int finalSuit[5];
int ranksinHand[13]; // 用于计算最终一手牌
int suitsinHand[4]; // 用于计算最终一手牌
int winnings;
time_t t;
char suit, rank, stillplay;
// 这个函数在do...while循环外部调用。
// 因为欢迎菜单只需要显示一次,而其他内容要根据用户要求可能运行多次。
printGreeting();
// 每次用户要再次玩该扑克游戏,运行循环。
do {
bet = getBet();
srand(time(&t));
getFirstHand(cardRank, cardSuit);
printf("Your five cards: \n");
for (i = 0; i < 5; i++)
{
suit = getSuit(cardSuit[ i ]);
rank = getRank(cardRank[ i ]);
printf("Card #%d: %c%c\n", i + 1, rank, suit);
}
// 这两个数组计算出用户手上牌的值。
// 然后,必须将其清零,以便用户玩多手牌。
for (i = 0; i < 4; i ++)
{
suitsinHand[ i ] = 0;
}
for (i = 0; i < 13; i++)
{
ranksinHand[ i ] = 0;
}
getFinalHand(cardRank, cardSuit, finalRank, finalSuit,
ranksinHand, suitsinHand);
printf("Your five final cards: \n");
for (i = 0; i < 5; i++)
{
suit = getSuit(finalSuit[ i ]);
rank = getRank(finalRank[ i ]);
printf("Card #%d: %c%c\n", i + 1, rank, suit);
}
winnings = analyzeHand(ranksinHand, suitsinHand);
printf("You won %d!\n", bet*winnings);
bank = bank - bet + (bet*winnings);
printf("\nYour bank is now %d.\n",bank);
printf("\nDo you want to play again? ");
scanf(" %c", &stillplay);
} while (toupper(stillplay) == 'Y' ); //原来是stillPlay
return;
}
/******************************************************************/
// 打印欢迎菜单,并告诉用户游戏规则
void printGreeting()
{
printf("************************************************\n");
printf("\n\n\tWelcom to the Absolute Beginner's Casino\n\n");
printf("\tHome of Video Draw poker\n\n");
printf("**************************************************\n");
printf("Here are the rules:\n");
printf("You start with 100 credits, and you make a bet from ");
printf("1 to 5 credits.\n");
printf("You are dealt 5 cards, and you then choose which ");
printf("cards to keep ");
printf("or discard.\n");
printf("You want to make the best possible hand.\n");
printf("\nHere is the table for winnings (assuming a ");
printf("bet of 1 credit):");
printf("\nPair\t\t\t\t1 credit");
printf("\nTwo pairs\t\t\t2 credits");
printf("\nThree of a kind\t\t\t3 credits");
printf("\nStraight\t\t\t4 credits");
printf("\nFlush\t\t\t\t5 credits");
printf("\nFull House\t\t\t8 credits");
printf("\nFour of a Kind\t\t\t10 credits");
printf("\nStraight Flush\t\t\t20 credits");
printf("\n\nHave fun!!\n\n");
}
// 该函数处理最开始的5张牌
void getFirstHand(int cardRank [], int cardSuit [])
{
int i, j;
int cardDup;
for (i = 0; i < 5; i++)
{
cardDup = 0;
do {
// 13张牌中的一张(2-10,J, Q, K, A)
cardRank[ i ] = (rand() % 13);
// 4种花色之一(梅,方,红,黑)
cardSuit[ i ] = (rand() % 4);
// 循环确保每张独一无二
for (j = 0; j < i; j++)
{
if ((cardRank[ i ] == cardRank[j]) && (cardSuit[ i ] == cardSuit[j]))
{
cardDup = 1;
}
}
}while (cardDup == 1);
}
}
// 该函数将代表花色的整数值改为字符。
char getSuit(int suit)
{
switch (suit)
{
case 0:
return('c');
case 1:
return('d');
case 2:
return('h');
case 3:
return('s');
}
}
// 该函数将代表牌号的整数值改为字符。
char getRank(int rank)
{
switch (rank)
{
case 0:
return('A');
case 1:
return('2');
case 2:
return('3');
case 3:
return('4');
case 4:
return('5');
case 5:
return('6');
case 6:
return('7');
case 7:
return('8');
case 8:
return('9');
case 9:
return('T');
case 10:
return('J');
case 11:
return('Q');
case 12:
return('K');
}
}
// 该函数获取用户的下注(1~5)
int getBet()
{
int bet;
do // 保持运行,直至用户输入0~5的数字
{
printf("How much do you want to bet? (Enter a number ");
printf("1 to 5, or 0 to quit the game): ");
scanf(" %d", &bet); //原来是printf
if (bet >=1 && bet <= 5)
{
return(bet);
}
else if (bet == 0)
{
exit(1);
}
else
{
printf("\n\nPlease enter a bet from 1-5 or ");
printf("0 to quit the game.\n");
}
} while ((bet < 0) || (bet > 5));
}
// 最后一个函数处理最后一手牌,并计算牌的值。
int analyzeHand(int ranksinHand [], int suitsinHand [])
{
int num_consec = 0;
int i, rank, suit;
int straight = FALSE;
int flush = FALSE;
int four = FALSE;
int three = FALSE;
int pairs = 0;
for (suit = 0; suit < 4; suit++)
if (suitsinHand[suit] == 5)
flush = TRUE;
rank = 0;
while (ranksinHand[rank] == 0)
rank++;
for (; rank < 13 && ranksinHand[rank]; rank++)
num_consec++; //原来少了个分号
if (num_consec == 5)
{
straight = TRUE;
}
for (rank = 0; rank < 13; rank++)
{
if (ranksinHand[rank] == 4)
four = TRUE;
if (ranksinHand[rank] == 3)
three = TRUE;
if (ranksinHand[rank] == 2)
pairs++;
}
if(straight && flush)
{
printf("Straight flush\n\n");
return (20);
}
else if (four) {
printf("Four of a kind\n\n");
return (10);
}
else if (three && pairs == 1)
{
printf("Full house\n\n");
return (8);
}
else if (flush)
{
printf("Flush\n\n");
return (5);
}
else if (straight)
{
printf("Straight\n\n");
return (4);
}
else if (three)
{
printf("Three of a kind\n\n");
return (3);
}
else if (pairs == 2)
{
printf("Two pairs\n\n");
return (2);
}
else if (pairs == 1)
{
printf("Pair\n\n");
return (1);
}
else
{
printf("High Card\n\n");
return (0);
}
}
// 该函数查看第一手的5张牌,并询问用户是否留牌。
// 如果用户不留牌,则为其替换着张牌。
void getFinalHand(int cardRank [], int cardSuit [], int finalRank [],
int finalSuit [], int ranksinHand [],
int suitinHand [])
{
int i, j, cardDup;
char suit, rank, ans;
for (i = 0; i < 5; i++)
{
suit = getSuit(cardSuit[ i ]);
rank = getRank(cardRank[ i ]);
printf("Do you want to keep card #%d: %c%c?", i + 1, rank, suit);
printf("\nPlease answer (Y/N): ");
scanf(" %c", &ans);
if (toupper(ans) == 'Y')
{
finalRank[ i ] = cardRank[ i ];
finalSuit[ i ] = cardSuit[ i ];
ranksinHand[finalRank[ i ]]++;
suitinHand[finalSuit[ i ]]++; //原来是suitsinHand
continue;
}
else if (toupper(ans) == 'N')
{
cardDup = 0;
do {
cardDup = 0;
finalRank[ i ] = (rand() % 13);
finalSuit[ i ] = (rand() % 4);
// 首先检查新派,避免与原来5张牌重复
for (j = 0; j < 5; j++)
{
if ((finalRank[ i ] == cardRank[j]) &&
(finalSuit[ i ] == cardSuit[j]))
{
cardDup = 1;
}
}
// 接下来,检查是否有重复的新牌
for (j = 0; j < i; j++)
{
if ((finalRank[ i ] == finalRank[j]) &&
(finalSuit[ i ] == finalSuit[j]))
{
cardDup = 1; //原来是==
}
}
}while (cardDup == 1);
ranksinHand[finalRank[ i ]]++;
suitinHand[finalSuit[ i ]]++; //原来是suitsinHand
}
}
}
|
|