C語言實現(xiàn)騎士飛行棋小游戲
更新時間:2020年02月05日 14:32:32 作者:wfpc__
這篇文章主要為大家詳細介紹了C語言實現(xiàn)騎士飛行棋小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言實現(xiàn)騎士飛行棋的具體代碼,供大家參考,具體內(nèi)容如下
需求分析
游戲規(guī)則和傳統(tǒng)的飛行棋一樣,支持兩人對戰(zhàn)
采用100格小型游戲棋盤
游戲規(guī)則:對戰(zhàn)雙方輪流擲骰子控制自己的騎兵前進或后退,在游戲棋盤上設置有關卡
普通
地雷
暫停
時空隧道
幸運輪盤(提供兩種運氣:交換位置和轟炸)
棋盤上的關卡只在騎兵第一次移動遇到時有效
#include<stdio.h>
#include<windows.h> //顏色
#include<string.h>
#include<conio.h> //通過控制臺進行數(shù)據(jù)輸入和數(shù)據(jù)輸出的函數(shù)
#include<stdlib.h>
#include<time.h> //定義時間函數(shù)
struct node //定義四個人物名
{
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的選擇人物序號
int numstep1,numstep2; //玩家1的選擇人物序號
int rand(void); //偽隨機數(shù)生成函數(shù)
void srand(unsigned int n); //種子函數(shù)
void showmap(int map[]) //打印游戲地圖
{
int i,j,k;
printf("\t\t\t騎 士 飛 行 棋 \n\n");
printf("'∷'是平地,'¤'是幸運羅盤,'★'是地雷,'■'是暫停,'〓'是時空隧道\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[]) //清除地圖上的標記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) //‘¤'所對應地圖上的位置
map[i]=1;
else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) //‘★'所對應地圖上的位置
map[i]=2;
else if(i==9||i==27||i==60||i==93) //‘■'所對應地圖上的位置
map[i]=3;
else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90) //‘〓'所對應地圖上的位置
map[i]=4;
else
map[i]=0;
}
}
}
void cleanb(int map[]) //清除地圖上的標記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) //‘¤'所對應地圖上的位置
map[i]=1;
else if(i==5||i==13||i==17||i==33||i==38||i==50||i==64||i==80||i==94) //‘★'所對應地圖上的位置
map[i]=2;
else if(i==9||i==27||i==60||i==93) //‘■'所對應地圖上的位置
map[i]=3;
else if(i==20||i==25||i==45||i==63||i==72||i==88||i==90) //‘〓'所對應地圖上的位置
map[i]=4;
else
map[i]=0;
}
}
}
void showprocess(int map[]) //游戲進行的過程
{
int flag1=2,flag2=2; //控制游戲暫停的標記變量
numstep1=0; //玩家1的初始位置
numstep2=0; //玩家2的初始位置
int numtou; //每回投擲的骰子數(shù)
int t; //作為幸運羅盤交換位置時的中間變量
int number; //作為輸入幸運羅盤選擇時的變量
system("cls"); //清屏
showmap(map); //調(diào)用函數(shù)showmap(map)用來輸出地圖
printf("\n");
while(numstep1<100&&numstep2<100) //游戲開始
{
system("pause"); //運行時會出現(xiàn)“請按任意鍵繼續(xù) . .
printf("\n");
cleana(map); //清除地圖上的標記A
cleanb(map); //清除地圖上的標記B
if(flag1==2) //判斷是否為暫停
{
if(flag2==0||flag2==1)
{
flag2++;
}
srand(time(NULL)); //是設置隨機數(shù)的種子,以當前時間作為隨機數(shù)的種子
numtou=rand()%6+1; //產(chǎn)生隨機數(shù)
numstep1+=numtou;
Sleep(500); //暫停0.5秒
printf("\n玩家1擲出的點數(shù)為 %d\n\n",numtou);
if(numstep1>=100) //步數(shù)大于100時跳出循環(huán),游戲結(jié)束
{
map[99]=10; //使地圖上的最后一個位置為A
Sleep(1000); //暫停1秒
system("cls"); //清屏
showmap(map); //調(diào)用函數(shù)showmap(map)用來輸出地圖
printf("\n\n");
printf("游戲結(jié)束!\n");
break;
}
else
{
printf("玩家1%s,你當前的位置為 %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\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) //地圖位置上為'¤',幸運輪盤
{
printf("\n玩家1%s,恭喜你來到幸運羅盤!\n",people[number1-1].name);
printf("請做出選擇:\n");
printf("1.和對方交換位置\n2.轟炸對方(炸退六步)\n");
scanf("%d",&number); //輸入選擇數(shù)
if(number==1) //交換玩家位置
{
t=numstep1;
numstep1=numstep2;
numstep2=t;
printf("玩家1%s,你當前的位置為%d\n",people[number1-1].name,numstep1);
printf("\n玩家2%s,你當前的位置為%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) //對方退六步
{
map[numstep1-1]=10;
if(numstep2>=6)
{
numstep2-=6;
}
else numstep2=0;
printf("玩家2%s,你當前的位置為%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\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) //地圖位置上為'〓',時空隧道
{
printf("\nOh My God ,是時空隧道?。?沖啊^_^\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\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) //當玩家1為暫停狀態(tài)
{
flag1++;
}
system("pause"); //顯示"請按任意鍵繼續(xù)....."
printf("\n");
cleana(map); //清除地圖上的標記A
cleanb(map); //清除地圖上的標記B
if(flag2==2) //判斷玩家2是否為暫停狀態(tài)
{
if(flag1==0||flag1==1)
{
flag1++;
}
srand(time(NULL)); //是設置隨機數(shù)的種子,以當前時間作為隨機數(shù)的種子
numtou=rand()%6+1; //產(chǎn)生隨機數(shù)
numstep2+=numtou;
Sleep(500); //暫停0.5秒
printf("\n玩家2擲出的點數(shù)為%d\n\n",numtou);
if(numstep2>=100) //步數(shù)大于100時跳出循環(huán),游戲結(jié)束
{
map[99]=20; //使地圖上最后一個位置為B
Sleep(1000); //暫停1秒
system("cls"); //清屏
showmap(map); //打印地圖
printf("\n\n");
printf("游戲結(jié)束!\n");
break;
}
else
{
printf("玩家2%s,你當前的位置為%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\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) //地圖上位置為'¤',幸運輪盤
{
printf("\n玩家2%s,恭喜你來到幸運羅盤!\n",people[number2-1].name);
printf("請做出選擇:\n");
printf("1.和對方交換位置\n2.轟炸對方(炸退六步)\n");
scanf("%d",&number);
if(number==1) //玩家雙方交換位置
{
t=numstep1;
numstep1=numstep2;
numstep2=t;
printf("\n玩家1%s,你當前的位置為%d\n",people[number1-1].name,numstep1);
printf("\n玩家2%s,你當前的位置為%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) //對方退六步
{
map[numstep2-1]=20;
if(numstep1>=6)
{
numstep1-=6;
}
else numstep1=0;
printf("玩家1%s,你當前的位置為%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\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) //地圖位置上為'〓',時空隧道
{
printf("\nOh My God ,是時空隧道??! 沖啊^_^\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\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,你當前的位置為 0\n",people[number1-1].name);
printf("玩家2%s,你當前的位置為 0\n\n",people[number2-1].name);
system("pause");
printf("\n游戲開始!\n請玩家1先開始擲骰子\n");
Sleep(1000);
showprocess(map);
}
void showstart() //展示游戲開始界面
{
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("~~~~~~~~~~~兩 人 對 戰(zhàn)~~~~~~~~~~~\n\n");
printf("請選擇角色:");
strcpy(people[0].name,"戴高樂");
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("請玩家1選擇角色(選擇序號):");
scanf("%d",&number1);
printf("請玩家2選擇角色(選擇序號):");
scanf("%d",&number2);
printf("\n\n");
printf("1.直接進行游戲 2.閱讀游戲規(guī)則\n");
scanf("%d",&choose);
if(choose==1)
{
showready();
}
else if(choose==2) //展示游戲規(guī)則
{
system("cls");
printf("\n~~~~~~~~~~~~~~~~~~~游戲規(guī)則如下~~~~~~~~~~~~~~~~~~~~\n\n");
printf("1.兩個玩家輪流擲骰子,如果上輪走到暫停關卡,停擲一次\n\n");
printf("2.若玩家走到幸運輪盤,則和對方交換位置或者對方后退6步\n\n");
printf("3.若玩家走到某格,而對方也在此格,則對方退回原點\n\n");
printf("4.若遇到地雷后退6步\n\n");
printf("5.若遇到暫停則此玩家下一回合停止擲骰子\n\n");
printf("6.若遇到時空隧道再前進10步\n\n");
printf("注意:棋盤上的關卡只在騎兵第一次移動遇到時有效\n\n");
Sleep(1800);
system("pause");
printf("\n");
showready();
}
}
int main()
{
char str[10];
showstart();
printf("\n是否再來一局?請選擇: (Yes/No)\n"); //判斷是否再來一局
scanf("%s",str);
if(strcmp(str,"Yes")==0)
{
system("cls");
cleana(map);
cleanb(map);
showstart();
}
if(strcmp(str,"No")==0)
return 0;
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++ Qt開發(fā)之CheckBox多選框組件的用法詳解
Qt是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應用程序,在Qt中我們可以通過拖拽的方式將不同組件放到指定的位置,實現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點介紹CheckBox單行輸入框組件的使用方法,需要的朋友可以參考下2023-12-12

