練手小專案 1 洗牌發牌 C

2021-10-20 19:20:45 字數 2492 閱讀 4040

可以將整個實現過程分為3步

建立陣列存放牌面和花色以及每一張牌->定義shuffle函式洗牌->定義deal函式發牌
face陣列用於存放13種牌面,定義時直接將其初始化.const用於修飾陣列避免傳參時修改原始陣列.

const string face[13]

=;

suit陣列用於存放4種花色,定義時也將其初始化

const string suit[4]

=;

二維陣列deck陣列用於存放52張牌,用整數1-52作為標記在定義時進行初始化

int deck[4]

[13]=

;int deck_num =1;

for(

int counter =

0; counter <=

3; counter++

)}

shuffle函式宣告

void

shuffle

(int deck[

13],int size)

;

將deck陣列傳入,隨機打亂二維陣列傳參時必須直接傳入列寬

具體演算法為:進入乙個52次的迴圈,依次將1-52號牌與隨機一張牌交換.隨機牌的行列位置使用兩個隨機數算出.

void

shuffle

(int deck[

13],int size)

//洗牌函式

}}

deal函式宣告

void

deal

(int deck[

13],int size_deck,

const string face,

int size_face,

const string suit,

int size_suit)

;

傳入打亂後的deck陣列,face陣列,suit陣列,從1-52依次展示每張牌的牌面和花色.

具體演算法為:進入乙個52次的迴圈,依次尋找1-52號牌並列印牌面.

void

deal

(int deck[

13],int size_deck,

const string face,

int size_face,

const string suit,

int size_suit)

//發牌函式

//洗牌}}

void

deal

(int deck[

13],int size_deck,

const string face,

int size_face,

const string suit,

int size_suit)

//發牌

else

if(colunm >12)

}}

除去大小王 ↩︎

洗牌,發牌C程式

洗牌需要隨機函式 rand 如要產生 m,n 範圍內的隨機數num,可用 int num rand n m 1 m 在呼叫rand 函式之前,可以使用srand 函式設定隨機數種子,如果沒有設定隨機數種子,rand 函式在呼叫時,自動設計隨機數種子為1。隨機種子相同,每次產生的隨機數也會相同。所以要...

小專案 模擬鬥地主發牌

a 鬥地主的功能分析 a 具體規則 1.組裝54張撲克牌 2.將54張牌順序打亂 3.三個玩家參與遊戲,三人交替摸牌,每人17張牌,最後三張留作底牌。4.檢視三人各自手中的牌 按照牌的大小排序 底牌 b 分析 1.準備牌 完成數字與紙牌的對映關係 使用雙列map hashmap 集合,完成乙個數字與...

C語言洗牌與發牌遊戲

include include include define suits 4 define faces 13 define cards 52 void shuffle int wdeck faces void send int wdeck faces const char wsuit,const c...