您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用C語言編寫一個洗牌與發(fā)牌游戲,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
普通版
#include<stdio.h> #include<stdlib.h> #include<time.h> #define SUITS 4 #define FACES 13 #define CARDS 52 void shuffle(int wDeck[][FACES]); void send(int wDeck[][FACES], const char* wSuit[], const char* wFace[]); void main() { int deck[SUITS][FACES]={0}; const char* suit[SUITS]={"Heart","Diamond","Club","Spade"}; const char* face[FACES]={"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; srand(time(NULL)); shuffle(deck); send(deck, suit, face); } void shuffle(int wDeck[][FACES]) { int row,column,card; for(card=1;card<=CARDS;card++) { row = rand() % SUITS; column = rand() % FACES; while(wDeck[row][column] != 0) { row = rand() % SUITS; column = rand() % FACES; } wDeck[row][column] = card; } } void send(int wDeck[][FACES], const char* wSuit[], const char* wFace[]) { int card,row,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'); } } } } }
高性能版
//先按順序填滿52個格子,再產(chǎn)生隨機格子相互交換 #include<stdio.h> #include<stdlib.h> #include<time.h> #define CARDS 52 #define FACES 13 struct card { const char* face; const char* suit; }; typedef struct card Card; void shuffle(Card *const wDeck); void send(const Card *const wDeck);//指針常量與數(shù)據(jù)常量均不可以改變,都用const限定,*前是限定指針,*后是限定數(shù)據(jù) void main() { const char* wSuit[]={"Heart","Diamond","Club","Spade"};//指針數(shù)組是一個數(shù)組 const char* wFace[]={"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"}; int i; Card deck[CARDS]; for(i=0;i<CARDS;i++) { deck[i].suit = wSuit[i / FACES]; deck[i].face = wFace[i % FACES]; } srand(time(NULL)); shuffle(deck); send(deck); } void shuffle(Card *const wDeck) { int i,j; Card temp; for(i=0;i<CARDS;i++) { j = rand() % CARDS; temp = wDeck[i]; wDeck[i] = wDeck[j]; wDeck[j] = temp; } } void send(const Card *const wDeck) { int i; for(i=0;i<CARDS;i++) { printf("%5s of %-8s%c",wDeck[i].face,wDeck[i].suit, (i+1) % 4 == 0 ? '\n' : '\t'); } }
上述內(nèi)容就是使用C語言編寫一個洗牌與發(fā)牌游戲,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。