我正在努力更好地了解以下程序的工作原理。该程序旨在以随机顺序洗牌。
有人可以解释一些函数的细节以及代码的确切运作方式吗?我理解二维数组的使用,但是一些shuffle函数让我和最后一个print语句混淆:
printf( "%5s of %-8s%c", wFace[ column ], wSuit[ row ],card % 2 == 0 ? '\n':'\t' );
以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
// prototypes
void shuffle( unsigned int wDeck[][ FACES ] ); // shuffling modifies wDeck
void deal( unsigned int wDeck[][ FACES ], const char *wFace[],
const char *wSuit[] ); // dealing doesn't modify the arrays
int main( void )
{
const char *suit[ SUITS ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
// initialize face array
const char *face[ FACES ] =
{ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
unsigned int deck[ SUITS ][ FACES ] = { 0 };
srand( time( NULL ) ); // seed random-number generator
shuffle( deck ); // shuffle the deck
deal( deck, face, suit ); // deal the deck
}
/*SHUFFLE FUNCTION*/
void shuffle( unsigned int wDeck[][ FACES ] ){
size_t row;
size_t column;
size_t card;
for ( card = 1; card <= CARDS; ++card ) {
do {
row = rand() % SUITS;
column = rand() % FACES;
} while( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;}}
void deal( unsigned int wDeck[][ FACES ], const char *wFace[],
const char *wSuit[] ){
size_t card;
size_t row;
size_t column;
for ( card = 1; card <= CARDS; ++card ) {
for ( row = 0; row < SUITS; ++row ) {
for ( column = 0; column < FACES; ++column ) {
if ( wDeck[ row ][ column ] == card ) {
printf( "%5s of %-8s%c", wFace[ column ], wSuit[ row ],
card % 2 == 0 ? '\n' : '\t' ); // 2-column format
}}}}}
答案 0 :(得分:0)
shuffle函数的核心是
for ( card = 1; card <= CARDS; ++card ) {
do {
row = rand() % SUITS;
column = rand() % FACES;
} while( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;}
它一直在猜测一张脸和一套西装,直到找到一个不在牌组中的组合,然后它将它分配给牌组中的下一张牌。
令人困惑的printf
功能只会打印两列中的所有卡片,如Ken解释的那样。