gpt4 book ai didi

c - 两名玩家之间的扑克游戏结果

转载 作者:行者123 更新时间:2023-11-30 20:55:39 25 4
gpt4 key购买 nike

我正试图对游戏中的玩家进行排名,并确定获胜者。您可以阅读下面的代码,因为我认为我已经很好地记录了整个逻辑。

鉴于有两名玩家,我现在需要做的是确定哪位玩家获胜。我已经列出了洗牌、发牌和确定牌型的函数。

    //
// main.c
// Created by gixx88 on 7/22/15.
// Copyright (c) 2015 gixx88. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define SUITS 4
#define FACES 13
#define CARDS 52

#define HAND 5
#define PLAYERS 2

//prototypes for shuffle and deal
void shuffle( size_t wDeck[][FACES] ); //shuffling modifies wDeck
void deal( size_t wDeck[][FACES], const char *wFace[],
const char *wSuit[] ); //dealing doesn't modify the arrays
void dealHand(size_t wHands[][HAND], size_t wDeck[][FACES]);

//prototypes for determing suit and face
size_t determineSuit(size_t wCard);
size_t determineFace(size_t wCard);


//prototypes for determing poker hand
unsigned int findAPair(size_t wHand[HAND]);
unsigned int findTwoPairs(size_t wHand[HAND]);
unsigned int findThreeOfAKind(size_t wHand[HAND]);
unsigned int findFourOfAKind(size_t wHand[HAND]);
unsigned int findFlush(size_t wHand[HAND]);
unsigned int findFullHouse(size_t wHand[HAND]);
unsigned int findStraightFlush(size_t wHand[HAND]);
unsigned int findStraight (size_t wHand[HAND]);

//prototypes for determing player outcomes
unsigned int determingPlayerHand(size_t wHand[HAND]);
void playerRank( size_t wHands[PLAYERS][HAND]);



int main(void) {

//initialize suit array
const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades" };

//initilize face array
const char *face[ FACES ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King" };

//initilize deck array
size_t deck[ SUITS ][FACES] = {{0}};

size_t hands[PLAYERS][HAND] = {{0}};

srand(time(NULL)); //random seed generator

shuffle(deck); //shuffle the deck
dealHand(hands, deck);

playerRank(hands);



} //end main

//determine suit
size_t determineSuit(size_t wCard){
return wCard / SUITS;
} // end determine suit

//determine face
size_t determineFace(size_t wCard){
return wCard % FACES;
}//end determine face

//if four of a kind

//shuffle cards in deck
void shuffle( size_t wDeck[][FACES] ){
size_t row; //row number
size_t column; //column number
size_t card; //counter

for (card = 0; card <= CARDS; ++card){

//choose a random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
} //end do
while (wDeck[row][column] != 0); //end do while

//place card number in chosen slot of deck
wDeck[row][column] = card;
}// end for
} //end shuffle function

//deal cards in deck
void deal( size_t wDeck[][FACES], const char *wFace[],
const char *wSuit[] ){
size_t card; //card counter
size_t row; //row counter
size_t column; //column counter

//deal each of the cards
for (card = 0; card <= CARDS; ++card){
//loop through rows of wDeck
for (row = 0; row < SUITS; ++row){
//loop through columns in wDeck for current row
for(column = 0; column < FACES; ++column){
//if slot contians current card, display card
if (wDeck[row][column] == card){
printf("%5s of %-8s%c", wFace[column], wSuit[row],
card % 2 == 0 ? '\n' : '\t'); //two column format
} //end if
} //end column for
} //end row for
} //end card for
} //end function deal

void dealHand(size_t wHands[][HAND], size_t wDeck[][FACES]){

size_t cards;
size_t players;
unsigned int current;
unsigned int current_suit;
unsigned int current_face;

for (cards = 0; cards <= HAND; cards ++){

for (players = 0; players < PLAYERS; players++){
current = cards * PLAYERS + players;
current_suit = current / SUITS;
current_face = current % FACES;
wHands[players][cards] = wDeck[current_suit][current_face];
} // end players for
} // end cards for

} // end deal hand


//PRINT THE CARD
void printCard(unsigned int wCard, const char wFaces[FACES], const char wSuit[SUITS]){
//find suit and face
size_t suit;
size_t face;

suit = determineSuit(wCard);
face = determineFace(wCard);

printf("%s or %s", wFaces[face], wSuit[suit]);


} //end print card

//******************************
//BEGIN HAND TYPE DETERMINATION
//******************************


//find A PAIR
unsigned int findAPair(size_t wHand[HAND]){

size_t countOfFaces[FACES] = { 0 };
unsigned int foundAPair = 1;
size_t card;


for (card = 0; card < HAND; card++)
{

size_t face = determineFace(wHand[card]);

countOfFaces[face]++;

if (countOfFaces[face] == 3)
{

return 1;
}
else if (countOfFaces[face] == 2)
{

if (foundAPair)
{

return 1;
}

foundAPair = 0;
}
}
return foundAPair;

} // end find a pair


//find TWO PAIRS
unsigned int findTwoPairs(size_t wHand[HAND]){

size_t countOfFaces[FACES] = { 0 };

unsigned int numberOfPairs = 0;

size_t card;
for (card = 0; card < HAND; card++){

size_t face = determineFace(wHand[card]);
countOfFaces[face]++;

if (countOfFaces[face] == 3)
{

return 1;
}
else if (countOfFaces[face] == 2)
{

numberOfPairs++;
}
}
return numberOfPairs == 2;

} //end find two pairs


//find THREE OF A KIND
unsigned int findThreeOfAKind(size_t wHand[HAND]){

size_t countOfFaces[FACES] = {0};
size_t card;
unsigned int foundThree = 1;

for (card = 0; card < HAND; card++){
size_t face = determineFace(wHand[card]);
countOfFaces[face]++;

if (countOfFaces[face] == 3){
foundThree = 0;
} //end if

else if (countOfFaces[face] == 4){
return 1;
} //end else if

else if (countOfFaces[face] == 2 && foundThree){
return 1;
} //end else if
} //end card for loop

return 1;

} //end find three of a kind



//find FOUR OF A KIND
unsigned int findFourOfAKind(size_t wHand[HAND]){

size_t countOfFaces[FACES] = {0};
size_t card;

for (card = 0; card < HAND; card++){
size_t face = determineFace(wHand[card]);
countOfFaces[face]++;

if (countOfFaces[face] == 4){
return 0;
} //end if
} //end card for loop

return 1;

} //end for of a kind

unsigned int findStraight (size_t wHand[HAND]){
size_t card;

size_t lowFace;
size_t highFace;

size_t firstSuit;
unsigned int foundSecondSuit = 1;

for (card = 0; card < HAND; card++)
{
size_t suit = determineSuit(wHand[card]);
size_t face = determineFace(wHand[card]);

// first card
if (card == 0)
{
lowFace = face;
highFace = face;

firstSuit = suit;
}
// all other cards
// check for two equal faces first
else if (face == lowFace || face == highFace)
{

return 1;
}
// update low and high face, if necessary
else
{

if (suit != firstSuit)
{

foundSecondSuit = 0;
}

// an Ace can only go low if the low face is at most a Five
if (face == 0 && lowFace > HAND - 1)
{

face = 13;
}
if (face < lowFace)
{

lowFace = face;
}

if (face > highFace)
{

highFace = face;
}

if ((highFace - lowFace + 1) != HAND)
{

return 1;
}
}
}

return foundSecondSuit;
}


//find FLUSH
unsigned int findFlush(size_t wHand[HAND]){

size_t card;
size_t firstSuit = 5;
for (card = 0; card < HAND; card++){
size_t suit = determineSuit(wHand[card]);
//determing if all cards are the same suit
if (card == 0){
firstSuit = suit;
} //end if
else if (suit != firstSuit){
return 1;
} //end else if
} //end card for loop

return 0;

} //end find flush



//find FULL HOUSE
unsigned int findFullHouse(size_t wHand[HAND]){
if (findAPair(wHand) && findThreeOfAKind(wHand)){
return 0;
} //end if

return 1;
} //end full house


//find STRAIGHT FLUSH
unsigned int findStraightFlush(size_t wHand[HAND]){

size_t lowFace;
size_t highFace;
size_t card;
size_t firstSuit;

for (card = 0; card < HAND; card++){
size_t suit = determineSuit(wHand[card]);
size_t face = determineFace(wHand[card]);
//determing if all cards are the same suit
if (card == 0){
firstSuit = suit;
lowFace = face;
highFace = face;
} //end if
else if (suit != firstSuit || face == lowFace || face == highFace){
return 1;
} //end else if

//determination for ace being high or low, which depends on hand
else
{
// an Ace can only go low if the low face is at most a Five
if (face == 0 && lowFace > HAND - 1)
{

face = 13;
}
if (face < lowFace)
{

lowFace = face;
}

if (face > highFace)
{

highFace = face;
}

if ((highFace - lowFace + 1) != HAND)
{

return 1;
}
}
}

return 0;

} //end straight flush

//****************************
//DETERMINING PLAYER OUTCOMES
//****************************

//determing PLAYER HAND
unsigned int determingPlayerHand(size_t wHand[HAND]){

if (findStraightFlush(wHand) == 0){
return 8;
} //end if

else if (findFourOfAKind(wHand) == 0){
return 7;
} //end else if

else if (findFullHouse(wHand) == 0){
return 6;
} //end else if

else if (findFlush(wHand) == 0){
return 5;
} //end else if

else if (findStraight(wHand) == 0){
return 4;
} //end else if

else if (findThreeOfAKind(wHand) == 0){
return 3;
} //end else if

else if (findTwoPairs(wHand) == 0){
return 2;
} //end else if

else if (findAPair(wHand) == 0){
return 1;
}


return 0;

} //end determing player hand



//determing PLAYER RANK
void playerRank( size_t wHands[PLAYERS][HAND]){


unsigned int player1 = determingPlayerHand(wHands);
unsigned int player2 = determingPlayerHand(wHands);

if (player1 > player2){
printf("%s", "Player 1 Wins!");
}//end if

else if (player2 > player1){
printf("%s", "Player 2 Wins!");
}

} //end player rank

我很不愿意发布这篇文章,但遗憾的是,我必须在明天之前完成这篇文章,而且我已经连续工作了 12 个小时。我担心的是,我将扑克牌类型函数设置为仅采用二维数组的一维。有没有办法以我现有的能力,有效地为玩家建立一个排名系统?

以下是我在其他地方给自己做的一些注释,可能也有用: definegPlayerHand 仅采用二维数组的一个下标。我需要playerRanking 来获取两个下标。我怎样才能做到这一点,以便我的排名能够正确返回给每个玩家?我认为排名返回值应该没问题。我最初只是将其视为 true 或 false、1 或 0,但后来意识到这不够具体

如果我需要使这一点更加清晰或相关,请告诉我我可以提供哪些其他信息。

在 M Oehm 发表评论后,我想更新此内容以反射(reflect)我认为有用的其他一些信息。

我正在解决的问题的部分参数要求某些事情保持原来的样子。如果我可以选择改变它们,我认为也有更好的方法来做到这一点。然而,牌组本身、洗牌算法和发牌算法都是根据问题的具体情况构建的。至于手牌评估他们应该做什么,到目前为止他们是这样做的,我写它是基于 A 需要在某个点走高的想法,这也是它如此复杂的部分原因。带有 [PLAYERS][HAND] 的二维数组旨在使纸牌从牌堆顶部以交替的顺序发给每个玩家,就像现实生活中所做的那样。

最佳答案

在选择如何代表你的牌组和手牌时,你的开局很糟糕。

您将牌表示为从红心 A 到梅花 K 的牌组出厂顺序中从 0 到 51 的整数。没关系。您可以根据卡号确定等级和花色:

rank = card % 13;
suit = card / 13;

这不是您在 defineFace/Suit 函数中所做的事情。接下来,这副牌是一维卡牌数组:

int deck[52];

您可以使用一种众所周知的洗牌算法来洗牌该数组,例如费舍尔-耶茨。您将牌组表示为二维数组,这并不会使洗牌变得更容易。目前还不清楚甲板的两个尺寸是多少。 (好吧,它们是花色和等级,但洗牌后就不再有意义了。)

在一维数组中,第一个玩家的手牌是前五张牌,第二个玩家的手牌是第 5 到 9 张牌。

您确定手牌的算法太复杂,并且存在一些复制粘贴错误。评估扑克手牌可归结为三个标准:

  • 同花:一手牌的所有牌都是同花色吗?
  • :所有排名都是连续的吗? (有特殊规则,A 可以根据需要打高或低)
  • 倍数:大多数牌都有对牌、三连牌或四连牌或其组合,因此您需要一个按出现次数排序的排名“直方图”。

利用这些数据,您可以确定您的手牌。您还需要辅助数据来区分两只相同的手,例如一对 K 和一对 N。

我建议你重新开始,打开编译器警告并逐步实现洗牌、发牌和三手标准,并在步骤之间进行验证。 (如果您愿意,您可以将这些步骤称为“里程碑”。)

关于c - 两名玩家之间的扑克游戏结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31581533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com