C語(yǔ)言實(shí)現(xiàn)騎士飛行棋小游戲
本文實(shí)例為大家分享了C語(yǔ)言實(shí)現(xiàn)騎士飛行棋的具體代碼,供大家參考,具體內(nèi)容如下
需求分析
游戲規(guī)則和傳統(tǒng)的飛行棋一樣,支持兩人對(duì)戰(zhàn)
采用100格小型游戲棋盤
游戲規(guī)則:對(duì)戰(zhàn)雙方輪流擲骰子控制自己的騎兵前進(jìn)或后退,在游戲棋盤上設(shè)置有關(guān)卡
普通
地雷
暫停
時(shí)空隧道
幸運(yùn)輪盤(提供兩種運(yùn)氣:交換位置和轟炸)
棋盤上的關(guān)卡只在騎兵第一次移動(dòng)遇到時(shí)有效
#include<stdio.h>
#include<windows.h> //顏色
#include<string.h>
#include<conio.h> //通過(guò)控制臺(tái)進(jìn)行數(shù)據(jù)輸入和數(shù)據(jù)輸出的函數(shù)
#include<stdlib.h>
#include<time.h> //定義時(shí)間函數(shù)
struct node //定義四個(gè)人物名
{
char name[20];
} people[4];
int map[100]=
{
0,0,0,0,0,2,1,0,0,3,0,0,0,2,0,0,0,2,0,0,4,0,0,1,0,4,0,3,0,0,
0,0,0,2,0,
0,0,0,2,0,1,0,0,0,0,4,0,0,0,0,2,0,0,0,0,1,0,0,0,0,3,0,0,4,2,
0,0,0,0,1,
0,0,4,0,0,0,0,0,0,0,2,0,0,1,0,0,0,0,4,0,4,0,0,3,2,0,0,0,0,0
}; //地圖的數(shù)字代碼
int number1,number2; //玩家1的選擇人物序號(hào)
int numstep1,numstep2; //玩家1的選擇人物序號(hào)
int rand(void); //偽隨機(jī)數(shù)生成函數(shù)
void srand(unsigned int n); //種子函數(shù)
void showmap(int map[]) //打印游戲地圖
{
int i,j,k;
printf("\t\t\t騎 士 飛 行 棋 \n\n");
printf("'∷'是平地,'¤'是幸運(yùn)羅盤,'★'是地雷,'■'是暫停,'〓'是時(shí)空隧道\n\n");
for(i=0; i<31; i++)
{
if(map[i]==0)
printf("∷");
else if(map[i]==1)
printf("¤");
else if(map[i]==2)
printf("★");
else if(map[i]==3)
printf("■");
else if(map[i]==4)
printf("〓");
else if(map[i]==10)
printf("A");
else if(map[i]==20)
printf("B");
else if(map[i]==30)
printf("@@");
}
printf("\n");
for(k=0; k<4; k++)
{
for(j=0; j<30; j++)
{
printf(" ");
}
if(map[i]==0)
printf("∷");
else if(map[i]==1)
printf("¤");
else if(map[i]==2)
printf("★");
else if(map[i]==3)
printf("■");
else if(map[i]==4)
printf("〓");
else if(map[i]==10)
printf("A");
else if(map[i]==20)
printf("B");
else if(map[i]==30)
printf("@@");
i++;
printf("\n");
}
for(i=65; i>=35; i--)
{
if(map[i]==0)
printf("∷");
else if(map[i]==1)
printf("¤");
else if(map[i]==2)
printf("★");
else if(map[i]==3)
printf("■");
else if(map[i]==4)
printf("〓");
else if(map[i]==10)
printf("A");
else if(map[i]==20)
printf("B");
else if(map[i]==30)
printf("@@");
}
printf("\n");
i=66;
for(j=0; j<3; j++)
{
if(map[i]==0)
printf("∷");
else if(map[i]==1)
printf("¤");
else if(map[i]==2)
printf("★");
else if(map[i]==3)
printf("■");
else if(map[i]==4)
printf("〓");
else if(map[i]==10)
printf("A");
else if(map[i]==20)
printf("B");
else if(map[i]==30)
printf("@@");
i++;
printf("\n");
}
for(i=69; i<100; i++)
{
if(map[i]==0)
printf("∷");
else if(map[i]==1)
printf("¤");
else if(map[i]==2)
printf("★");
else if(map[i]==3)
printf("■");
else if(map[i]==4)
printf("〓");
else if(map[i]==10)
printf("A");
else if(map[i]==20)
printf("B");
else if(map[i]==30)
printf("@@");
}
printf("\n");
}
void cleana(int map[]) //清除地圖上的標(biāo)記A,并還原地圖
{
int i;
for(i=0; i<100; i++)
{
if(map[i]==10)
{
if(i==6||i==23||i==40||i==55||i==69||i==83) //‘¤'所對(duì)應(yīng)地圖上的位置
map[i]=1;
else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) //‘★'所對(duì)應(yīng)地圖上的位置
map[i]=2;
else if(i==9||i==27||i==60||i==93) //‘■'所對(duì)應(yīng)地圖上的位置
map[i]=3;
else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90) //‘〓'所對(duì)應(yīng)地圖上的位置
map[i]=4;
else
map[i]=0;
}
}
}
void cleanb(int map[]) //清除地圖上的標(biāo)記B,并還原地圖
{
int i;
for(i=0; i<100; i++)
{
if(map[i]==20)
{
if(i==6||i==23||i==40||i==55||i==69||i==83) //‘¤'所對(duì)應(yīng)地圖上的位置
map[i]=1;
else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) //‘★'所對(duì)應(yīng)地圖上的位置
map[i]=2;
else if(i==9||i==27||i==60||i==93) //‘■'所對(duì)應(yīng)地圖上的位置
map[i]=3;
else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90) //‘〓'所對(duì)應(yīng)地圖上的位置
map[i]=4;
else
map[i]=0;
}
}
}
void showprocess(int map[]) //游戲進(jìn)行的過(guò)程
{
int flag1=2,flag2=2; //控制游戲暫停的標(biāo)記變量
numstep1=0; //玩家1的初始位置
numstep2=0; //玩家2的初始位置
int numtou; //每回投擲的骰子數(shù)
int t; //作為幸運(yùn)羅盤交換位置時(shí)的中間變量
int number; //作為輸入幸運(yùn)羅盤選擇時(shí)的變量
system("cls"); //清屏
showmap(map); //調(diào)用函數(shù)showmap(map)用來(lái)輸出地圖
printf("\n");
while(numstep1<100&&numstep2<100) //游戲開(kāi)始
{
system("pause"); //運(yùn)行時(shí)會(huì)出現(xiàn)“請(qǐng)按任意鍵繼續(xù) . .
printf("\n");
cleana(map); //清除地圖上的標(biāo)記A
cleanb(map); //清除地圖上的標(biāo)記B
if(flag1==2) //判斷是否為暫停
{
if(flag2==0||flag2==1)
{
flag2++;
}
srand(time(NULL)); //是設(shè)置隨機(jī)數(shù)的種子,以當(dāng)前時(shí)間作為隨機(jī)數(shù)的種子
numtou=rand()%6+1; //產(chǎn)生隨機(jī)數(shù)
numstep1+=numtou;
Sleep(500); //暫停0.5秒
printf("\n玩家1擲出的點(diǎn)數(shù)為 %d\n\n",numtou);
if(numstep1>=100) //步數(shù)大于100時(shí)跳出循環(huán),游戲結(jié)束
{
map[99]=10; //使地圖上的最后一個(gè)位置為A
Sleep(1000); //暫停1秒
system("cls"); //清屏
showmap(map); //調(diào)用函數(shù)showmap(map)用來(lái)輸出地圖
printf("\n\n");
printf("游戲結(jié)束!\n");
break;
}
else
{
printf("玩家1%s,你當(dāng)前的位置為 %d\n",people[number1-1].name,numstep1);
Sleep(800);
if(map[numstep1-1]==0) //地圖位置上為'∷'
{
map[numstep1-1]=10;
if(numstep1==numstep2) //判斷玩家1,玩家2的位置是否相同
{
printf("\n玩家2%s被炸飛了!\n",people[number2-1].name);
numstep2=0;
printf("玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
}
if(numstep2!=0)
map[numstep2-1]=20;
Sleep(1000); //暫停1秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n");
}
else if(map[numstep1-1]==1) //地圖位置上為'¤',幸運(yùn)輪盤
{
printf("\n玩家1%s,恭喜你來(lái)到幸運(yùn)羅盤!\n",people[number1-1].name);
printf("請(qǐng)做出選擇:\n");
printf("1.和對(duì)方交換位置\n2.轟炸對(duì)方(炸退六步)\n");
scanf("%d",&number); //輸入選擇數(shù)
if(number==1) //交換玩家位置
{
t=numstep1;
numstep1=numstep2;
numstep2=t;
printf("玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);
printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
if(numstep1==0)
{
map[numstep2-1]=20;
}
else
{
map[numstep1-1]=10;
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800); //暫停1.8秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n");
}
else if(number==2) //對(duì)方退六步
{
map[numstep1-1]=10;
if(numstep2>=6)
{
numstep2-=6;
}
else numstep2=0;
printf("玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
if(numstep2!=0)
{
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800); //暫停1.8秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n");
}
}
else if(map[numstep1-1]==2) //地圖位置上為'★',地雷
{
printf("\nSORRY , 你踩到地雷了 要后退6步●﹏●\n");
if(numstep1>=6)
numstep1-=6;
else numstep1=0;
printf("\n玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);
if(numstep1==0&&numstep2!=0)
{
map[numstep2-1]=20;
}
else if(numstep1!=0&&numstep2==0)
{
map[numstep1-1]=10;
}
else if(numstep1!=0&&numstep2!=0)
{
map[numstep1-1]=10;
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800); //暫停1.8秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n");
}
else if(map[numstep1-1]==3) //地圖位置上為'■',暫停一次
{
flag1=0;
printf("\n~~>_<~~ 要停戰(zhàn)一局了!\n");
map[numstep1-1]=10;
if(numstep2!=0)
{
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800); //暫停1.8秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n");
}
else if(map[numstep1-1]==4) //地圖位置上為'〓',時(shí)空隧道
{
printf("\nOh My God ,是時(shí)空隧道??! 沖啊^_^\n");
numstep1+=10;
if(numstep1>=100)
{
map[99]=10;
Sleep(1000);
system("cls");
showmap(map);
printf("\n\n");
printf("游戲結(jié)束!\n");
break;
}
printf("\n玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);
map[numstep1-1]=10;
if(numstep2!=0)
{
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800); //暫停1.8秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n");
}
}
}
else if(flag1!=2) //當(dāng)玩家1為暫停狀態(tài)
{
flag1++;
}
system("pause"); //顯示"請(qǐng)按任意鍵繼續(xù)....."
printf("\n");
cleana(map); //清除地圖上的標(biāo)記A
cleanb(map); //清除地圖上的標(biāo)記B
if(flag2==2) //判斷玩家2是否為暫停狀態(tài)
{
if(flag1==0||flag1==1)
{
flag1++;
}
srand(time(NULL)); //是設(shè)置隨機(jī)數(shù)的種子,以當(dāng)前時(shí)間作為隨機(jī)數(shù)的種子
numtou=rand()%6+1; //產(chǎn)生隨機(jī)數(shù)
numstep2+=numtou;
Sleep(500); //暫停0.5秒
printf("\n玩家2擲出的點(diǎn)數(shù)為%d\n\n",numtou);
if(numstep2>=100) //步數(shù)大于100時(shí)跳出循環(huán),游戲結(jié)束
{
map[99]=20; //使地圖上最后一個(gè)位置為B
Sleep(1000); //暫停1秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n\n");
printf("游戲結(jié)束!\n");
break;
}
else
{
printf("玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
Sleep(1000); //暫停1秒
if(map[numstep2-1]==0) //地圖位置上為'∷'
{
map[numstep2-1]=20;
if(numstep1==numstep2)
{
printf("\n玩家1%s被炸飛了!\n",people[number1-1].name);
numstep1=0;
printf("玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);
}
if(numstep1!=0)
map[numstep1-1]=10;
Sleep(1000);
system("cls");
showmap(map);
printf("\n");
}
else if(map[numstep2-1]==1) //地圖上位置為'¤',幸運(yùn)輪盤
{
printf("\n玩家2%s,恭喜你來(lái)到幸運(yùn)羅盤!\n",people[number2-1].name);
printf("請(qǐng)做出選擇:\n");
printf("1.和對(duì)方交換位置\n2.轟炸對(duì)方(炸退六步)\n");
scanf("%d",&number);
if(number==1) //玩家雙方交換位置
{
t=numstep1;
numstep1=numstep2;
numstep2=t;
printf("\n玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);
printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
if(numstep2==0)
{
map[numstep1-1]=10;
}
else
{
map[numstep1-1]=10;
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800);
system("cls");
showmap(map);
printf("\n");
}
else if(number==2) //對(duì)方退六步
{
map[numstep2-1]=20;
if(numstep1>=6)
{
numstep1-=6;
}
else numstep1=0;
printf("玩家1%s,你當(dāng)前的位置為%d\n",people[number1-1].name,numstep1);
if(numstep1!=0)
{
map[numstep1-1]=10;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800);
system("cls");
showmap(map);
printf("\n");
}
}
else if(map[numstep2-1]==2) //地圖上位置為'★',地雷
{
printf("\nSORRY , 你踩到地雷了 要后退6步●﹏●\n");
if(numstep2>=6)
numstep2-=6;
else numstep2=0;
printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
if(numstep2==0&&numstep1!=0)
{
map[numstep1-1]=10;
}
else if(numstep2!=0&&numstep1==0)
{
map[numstep2-1]=20;
}
else if(numstep1!=0&&numstep2!=0)
{
map[numstep1-1]=10;
map[numstep2-1]=20;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800);
system("cls");
showmap(map);
printf("\n");
}
else if(map[numstep2-1]==3) //地圖位置上為'■',暫停一次
{
flag2=0;
printf("\n~~>_<~~ 要停戰(zhàn)一局了\n");
map[numstep2-1]=20;
if(numstep1!=0)
{
map[numstep1-1]=10;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800);
system("cls");
showmap(map);
printf("\n");
}
else if(map[numstep2-1]==4) //地圖位置上為'〓',時(shí)空隧道
{
printf("\nOh My God ,是時(shí)空隧道??! 沖啊^_^\n");
numstep2+=10;
if(numstep1>=100) //步數(shù)大于100,跳出循環(huán)
{
map[99]=10;
Sleep(1000);
system("cls");
showmap(map);
printf("\n\n");
printf("游戲結(jié)束!\n");
break;
}
printf("\n玩家2%s,你當(dāng)前的位置為%d\n",people[number2-1].name,numstep2);
map[numstep2-1]=20;
if(numstep1!=0)
{
map[numstep1-1]=10;
}
if(numstep1==numstep2&&numstep1!=0)
{
map[numstep1-1]=30;
}
Sleep(1800);
system("cls");
showmap(map);
printf("\n");
}
}
}
else if(flag2!=0)
{
flag2++;
}
}
if(numstep1>numstep2) //判斷玩家的輸贏
printf("\n恭喜玩家1%s,你贏了!!!!\n",people[number1-1].name);
else printf("\n恭喜玩家2%s,你贏了!!!!\n",people[number2-1].name);
}
void showready()
{
int i;
printf("地圖載入中——");
for(i=0; i<15; i++)
{
printf(".");
Sleep(100);
}
system("cls");
showmap(map);
printf("\n\n");
printf("玩家1%s,你當(dāng)前的位置為 0\n",people[number1-1].name);
printf("玩家2%s,你當(dāng)前的位置為 0\n\n",people[number2-1].name);
system("pause");
printf("\n游戲開(kāi)始!\n請(qǐng)玩家1先開(kāi)始擲骰子\n");
Sleep(1000);
showprocess(map);
}
void showstart() //展示游戲開(kāi)始界面
{
int i;
int choose;
system("color 71");
printf("**************************************************\n");
printf("http:// //\n");
printf("http:// //\n");
printf("http:// 騎 士 飛 行 棋 //\n");
printf("http:// //\n");
printf("http:// //\n");
printf("**************************************************\n");
for(i=0; i<5; i++)
{
printf("\n");
}
printf("~~~~~~~~~~~兩 人 對(duì) 戰(zhàn)~~~~~~~~~~~\n\n");
printf("請(qǐng)選擇角色:");
strcpy(people[0].name,"戴高樂(lè)");
strcpy(people[1].name,"艾森豪威爾");
strcpy(people[2].name,"麥克阿瑟");
strcpy(people[3].name,"巴頓");
for(i=1; i<=4; i++)
{
printf("%d.%s ",i,people[i-1].name);
}
printf("\n");
printf("請(qǐng)玩家1選擇角色(選擇序號(hào)):");
scanf("%d",&number1);
printf("請(qǐng)玩家2選擇角色(選擇序號(hào)):");
scanf("%d",&number2);
printf("\n\n");
printf("1.直接進(jìn)行游戲 2.閱讀游戲規(guī)則\n");
scanf("%d",&choose);
if(choose==1)
{
showready();
}
else if(choose==2) //展示游戲規(guī)則
{
system("cls");
printf("\n~~~~~~~~~~~~~~~~~~~游戲規(guī)則如下~~~~~~~~~~~~~~~~~~~~\n\n");
printf("1.兩個(gè)玩家輪流擲骰子,如果上輪走到暫停關(guān)卡,停擲一次\n\n");
printf("2.若玩家走到幸運(yùn)輪盤,則和對(duì)方交換位置或者對(duì)方后退6步\n\n");
printf("3.若玩家走到某格,而對(duì)方也在此格,則對(duì)方退回原點(diǎn)\n\n");
printf("4.若遇到地雷后退6步\n\n");
printf("5.若遇到暫停則此玩家下一回合停止擲骰子\n\n");
printf("6.若遇到時(shí)空隧道再前進(jìn)10步\n\n");
printf("注意:棋盤上的關(guān)卡只在騎兵第一次移動(dòng)遇到時(shí)有效\n\n");
Sleep(1800);
system("pause");
printf("\n");
showready();
}
}
int main()
{
char str[10];
showstart();
printf("\n是否再來(lái)一局?請(qǐng)選擇: (Yes/No)\n"); //判斷是否再來(lái)一局
scanf("%s",str);
if(strcmp(str,"Yes")==0)
{
system("cls");
cleana(map);
cleanb(map);
showstart();
}
if(strcmp(str,"No")==0)
return 0;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++11用兩個(gè)線程輪流打印整數(shù)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)內(nèi)核特征碼掃描PE代碼段
這篇文章主要為大家介紹了C語(yǔ)言驅(qū)動(dòng)開(kāi)發(fā)內(nèi)核特征碼掃描PE代碼段,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
NDK 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列與棧等的實(shí)現(xiàn)
這篇文章主要介紹了NDK 數(shù)據(jù)結(jié)構(gòu)之隊(duì)列與棧等的實(shí)現(xiàn)的相關(guān)資料,希望通過(guò)本文大家能理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-10-10
C語(yǔ)言超詳細(xì)講解函數(shù)指針的運(yùn)用
函數(shù)指針是一個(gè)指針變量,它可以存儲(chǔ)函數(shù)的地址,然后使用函數(shù)指針,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言進(jìn)階教程之函數(shù)指針的相關(guān)資料,需要的朋友可以參考下2022-06-06
C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳小球
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單彈跳小球,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
詳解C++編程中的sizeof運(yùn)算符與typeid運(yùn)算符
這篇文章主要介紹了C++編程中的sizeof運(yùn)算符與typeid運(yùn)算符,是C++入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01
基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲
這篇文章主要為大家詳細(xì)介紹了基于C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

