C語言實(shí)現(xiàn)簡(jiǎn)易撲克牌游戲
將一副撲克牌平均分成兩份,每人拿一份。a先拿出手中的第一張撲克牌放在桌上,然后b也拿出手中的第一張撲克牌,并放在a剛打出的撲克牌的上面,就像這樣兩人交替出牌。出牌時(shí),如果某人打出的牌與桌上某張牌的牌面相同,即可將兩張相同的牌及其中間所夾的牌全部取走,并依次放到自己手中牌的末尾。當(dāng)任意一人手中的牌全部出完時(shí),游戲結(jié)束,對(duì)手獲勝。
以下是代碼的實(shí)現(xiàn):
#define _crt_secure_no_deprecate
#include<stdio.h>
#include<stdlib.h>
struct queue//定義隊(duì)列的結(jié)構(gòu)體
{
int data[1000];
int head;
int tail;
};
struct stack//定義棧的結(jié)構(gòu)體
{
int data[10];
int top;
};
void poker()
{
struct queue q1;
struct queue q2;
struct stack s;
int arr[10];
int i, t;
q1.head = 1; q1.tail = 1;
q2.head = 1; q2.tail = 1;
s.top = 0;
for (i = 1; i <= 9; i++)
{
arr[i] = 0;//對(duì)數(shù)組進(jìn)行初始化,全部為0
}
for (i = 1; i <= 6; i++)
{
scanf("%d", &q1.data[q1.tail]);
q1.tail++;
}
for (i = 1; i <= 6; i++)
{
scanf("%d", &q2.data[q2.tail]);
q2.tail++;
}
while (q1.head < q1.tail&&q2.head < q2.tail)
{
t = q1.data[q1.head];
if (arr[t] == 0)
{
q1.head++;
s.top++;
s.data[s.top] = t;
arr[t] = 1;
}
else
{
q1.head++;
q1.data[q1.tail] = t;
q1.tail++;
while (s.data[s.top] != t)
{
arr[s.data[s.top]] = 0;
q1.data[q1.tail] = s.data[s.top];
q1.tail++;
s.top--;
}
}
t = q2.data[q2.head];
if (arr[t] == 0)
{
q2.head++;
s.top++;
s.data[s.top] = t;
arr[t] = 1;
}
else
{
q2.head++;
q2.data[q2.tail] = t;
q2.tail++;
while (s.data[s.top] != t)
{
arr[s.data[s.top]] = 0;
q2.data[q2.tail] = s.data[s.top];
q2.tail++;
s.top--;
}
}
}
if (q2.head == q2.tail)
{
printf("a贏\n");
printf("a當(dāng)前手中的牌是:");
for (i = q1.head; i <= q1.tail - 1; i++)
{
printf(" %d", q1.data[i]);
}
if (s.top > 0)
{
printf("\n桌上的牌是:");
for (i = 1; i <= s.top; i++)
{
printf(" %d", s.data[i]);
}
printf("\n");
}
else
{
printf("\n桌上已經(jīng)沒有牌了");
}
}
else
{
printf("b贏\n");
printf("b當(dāng)前手中的牌是:");
for (i = q2.head; i <= q2.tail - 1; i++)
{
printf(" %d", q2.data[i]);
}
if (s.top > 0)
{
printf("\n桌上的牌是:");
for (i = 1; i <= s.top; i++)
{
printf(" %d", s.data[i]);
}
printf("\n");
}
else
{
printf("\n桌上已經(jīng)沒有牌了");
}
}
}
int main()
{
poker();
system("pause");
return 0;
}
運(yùn)行的結(jié)果:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言嵌入式實(shí)現(xiàn)支持浮點(diǎn)輸出的printf示例詳解
這篇文章主要為大家介紹了C語言嵌入式實(shí)現(xiàn)支持浮點(diǎn)輸出的printf示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
C語言 棧的表示和實(shí)現(xiàn)詳細(xì)介紹
這篇文章主要介紹了C語言 棧的表示和實(shí)現(xiàn)詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12
C++?OpenCV裁剪圖片時(shí)發(fā)生報(bào)錯(cuò)的解決方式
在圖像處理中,我們經(jīng)常根據(jù)需要截取圖像中某一區(qū)域做處理,下面這篇文章主要給大家介紹了關(guān)于C++?OpenCV裁剪圖片時(shí)發(fā)生報(bào)錯(cuò)的解決方式,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
opencv利用霍夫變換檢測(cè)直線進(jìn)行圖片校正
這篇文章主要為大家詳細(xì)介紹了opencv利用霍夫變換檢測(cè)直線對(duì)圖片進(jìn)行校正,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
C++使用GDAL庫實(shí)現(xiàn)Tiff文件的讀取
這篇文章主要為大家詳細(xì)介紹了C++使用GDAL庫實(shí)現(xiàn)Tiff文件的讀取的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03

