用C语言写一个扑克游戏
时间: 2023-02-25 13:45:54 浏览: 40
基于C语言的扑克牌小游戏
C语言写一个扑克游戏需要具备一定的编程基础。首先,你需要了解扑克牌的规则。其次,需要熟悉C语言的语法和常用函数。
下面是一个简单的代码示例:
```
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define CARDS 52
#define FACES 13
#define SUITS 4
#define HAND 5
//函数声明
void shuffle(int wDeck[][FACES]);
void deal(const int wDeck[][FACES], const char *wFace[], const char *wSuit[], int hand[][2]);
void sort(int hand[][2]);
void printArray(const int wDeck[][FACES], const char *wFace[], const char *wSuit[]);
int main(void)
{
//定义扑克牌数组
int deck[SUITS][FACES] = {0};
int hand[HAND][2] = {0};
const char *face[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
srand(time(NULL));
shuffle(deck);
deal(deck, face, suit, hand);
sort(hand);
printArray(deck, face, suit);
return 0;
}
//洗牌函数
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 deal(const int wDeck[][FACES], const char *wFace[], const char *wSuit[], int hand[][2])
{
int card, row, column;
for(card = 1; card <= HAND; ++card)
{
for(row = 0; row <= SUITS - 1; ++row)
{
for(column = 0; column <= FACES - 1; ++column)
{
if(wDeck[row][column] == card)
阅读全文