我正试图在游戏中对玩家进行排名,并确定获胜者。你可以在下面阅读我的代码,因为我认为我已经在整个过程中记录了很好的逻辑。
我现在需要做的是确定哪个玩家获胜,因为有两个玩家。我已经制定了改组,处理和确定哪种类型的手牌的功能。
//
// 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个小时都在努力了。我担心的是我设置我的扑克手型函数只采取二维数组的一个维度。有没有办法有效地为玩家建立一个排名系统?
以下是我在其他地方做过的一些注释,这些注释也可能有用:determineingPlayerHand只接受二维数组的一个下标。我需要playerRanking来获取两个下标。我怎样才能实现这一目标,以便我的排名能够适当地回归给每个玩家?我认为排名返回值应该没问题。我最初认为它只是真或假,1或0,但后来意识到它不够具体
如果我需要更明确或更相关,请告诉我我可以提供的其他信息。
在O Ohhm的评论之后,我想更新这个以反映我认为有用的其他一些信息。
我解决的问题的部分参数要求某些事情就像它们一样。如果我有更改它们的选择,我认为还有更好的方法可以做到这一点。然而,甲板是原样的,混洗算法和交易算法是为了解决问题而构建的。至于评估他们应该做什么的手,到目前为止他们做了,我写的是基于ace需要在某一点上升高的想法,这是它为什么如此复杂的一部分。带有[PLAYERS] [HAND]的二维数组是为了让卡片以交替的顺序从牌组的顶部发给每个玩家,就像在现实生活中一样。
答案 0 :(得分:2)
在选择如何代表你的套牌和双手时,你已经开始了一个糟糕的开始。
您可以在牌组的工厂顺序中将牌表示为0到51之间的整数,从Ace of Hearts到King of Clubs。没关系。您可以从卡号确定等级和套装:
rank = card % 13;
suit = card / 13;
这不是您在determineFace
/ Suit
函数中执行的操作。接下来,牌组是一维卡片阵列:
int deck[52];
你可以用一种众所周知的改组算法来洗牌这个数组,例如:费雪耶茨。你将牌组表示为二维阵列,它不会使洗牌变得更容易。它还不清楚甲板的两个维度是什么。 (好吧,他们是适合和排名,但在洗牌后,这不再有意义。)
在一维阵列中,第一个玩家的手是前五张牌,第二个玩家的牌是5到9张牌。
您确定指针的算法过于复杂,并且存在一些复制和粘贴错误。评估扑克手可以归结为三个标准:
根据这些数据,您可以确定您的手。您还需要辅助数据来区分两个相等的手,例如一对国王和一对Nines。
我建议你重新开始,打开编译器警告并逐步实施改组,处理和三手标准,并在步骤之间进行验证。 (如果您愿意,可以将这些步骤称为“#34;里程碑&#34;”。