我之前发过,但我会问一个更具体的问题。我现在正在设置我的游戏以打牌,但我无法弄清楚如何为两名玩家交易牌并确保他们没有相同的牌。谢谢你的意见!
打印功能的代码:
void print_hand( int hand[], int size )
{
int i ;
for( i = 0 ; i < 5 ; i ++ )
{
player = print_card( hand[i] ) ;
putchar( '\n' ) ;
}
}
我的所有代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DECK_SIZE 52
void init_deck( int deck[] , int size ) ;
void shuffle_deck( int deck[] , int size ) ;
void sort_hand( int hand[] , int size ) ;
void print_hand( int hand[] , int size ) ;
void print_card( int card ) ;
main()
{
int deck[DECK_SIZE] ;
char c;
printf("This game gives each user 5 cards and then compares the hands against eachother, and then determines who has the hand with the highest value\n");
printf(" If you would like to play, please enter yes, if not, enter no\n");
/* asks the user to play, if not then stops program*/
srand( time( NULL ) ) ;
// sets up the random number generator
init_deck( deck , DECK_SIZE ) ;
// calls the initialize deck function
shuffle_deck( deck , DECK_SIZE ) ;
// calls the shuffle deck function
print_hand( deck , DECK_SIZE ) ;
// prints out the users hand
putchar( '\n' ) ;
// prints each card on a seperate line.
}
void init_deck( int deck[] , int size )
// creates the deck function
{
int i ;
for( i = 0 ; i < size ; i ++ )
deck[i] = i ;
/* loops through 52 numbers and assigns a number to each card from the deck*/
}
void shuffle_deck( int deck[] , int size )
// creates the shuffle deck function
{
int i , j , temp ;
for( i = 0 ; i < size ; i ++ )
{
j = rand() % size ;
temp = deck[i] ;
deck[i] = deck[j] ;
deck[j] = temp ;
/* loops for all of the cards, and shuffles a card with another card from the deck randomly*/
}
}
void print_hand( int hand[], int size )
// function for displaying the users hand.
{
int i ;
for( i = 0 ; i < 5 ; i ++ )
{
print_card( hand[i] ) ;
putchar( '\n' ) ;
/* gives the player a random card for a total of five cards and displays each on a new line*/
}
}
void print_card( int card )
// function for printing the cards with their number and suit
{
char suit[4][9] =
{ "Spades" , "Hearts" , "Diamonds" , "Clubs" } ;
char rank[13][6] =
{ "Two" , "Three" , "Four" , "Five" , "Six" , "Seven" , "Eight" ,
"Nine" , "Ten" , "Jack" , "Queen" , "King" , "Ace" } ;
printf( "%s of %s" , rank[card%13] , suit[card/13] ) ;
/* sets up two 2 dimensional arrays for assigning the suit to each card, and assigning it's rank. It thens prints the cards.*/
}
答案 0 :(得分:0)
只是一个意见,但我会建议一些不同的东西。这是尝试实现堆栈的好程序: http://en.wikipedia.org/wiki/Stack_(abstract_data_type)
此外,如果您决定订购西装,则不一定需要打扰char数组。假设您选择黑桃,俱乐部,心形,钻石从最高到最低,那么您只需要4 * cards_per_suit字节来存储套牌。因此,您可以为卡组制作堆栈,然后您可以随机选择要添加到其中的哪些组(卡)。然后,当您从堆栈弹出时,您永远不必担心重复。