這是一個(gè)非常簡單的三子棋小游戲,學(xué)過C語言數(shù)組語法的就可以寫出來。在這里博主實(shí)現(xiàn)的是人機(jī)對(duì)戰(zhàn),當(dāng)然你們也可以擴(kuò)展一下人人對(duì)戰(zhàn)。代碼的邏輯是盡量弄得初學(xué)者都容易理解,并且減少了很多復(fù)雜的代碼。比如,AI電腦選手需要分析棋局然后選擇合適的下棋位置,但是給簡略了。有失必有得,這里也新學(xué)到了生成隨機(jī)數(shù)(
rand()
和rand((unsigned)time(NULL))
)這個(gè)新的語法點(diǎn)。注意:這里要調(diào)用這兩個(gè)函數(shù)必須包含#include
和#include
這兩個(gè)頭文件。這在之后要做的猜數(shù)字游戲和其他游戲中會(huì)有很大的用處。話不多說,下來一睹風(fēng)采吧!
在這里的游戲界面框架沿用的上一章中彈跳小球的游戲界面框架,并在此基礎(chǔ)上改變了一點(diǎn)文字和顏色總體上是沒有多大改變的。在這里就能知道,做一些簡單的小游戲,知道一個(gè)小游戲如何從無到有是很重要的。框架也不需要刻意去背,只有理解了自己才能寫出屬于自己的框架。
void menu()//菜單
{system("color 71");//改變?nèi)诸伾? printf("|----------------------|\n");
printf("| 1.開始游戲 |\n");
printf("|----------------------|\n");
printf("| 2.游戲幫助 |\n");
printf("|----------------------|\n");
printf("| 3.更新日志 |\n");
printf("|----------------------|\n");
printf("| 0.退出游戲 |\n");
printf("|----------------------|\n");
printf("\n");
printf("(溫馨提醒:請(qǐng)按照菜單相應(yīng)的選項(xiàng)選擇)");
printf("\n");
printf("請(qǐng)輸入你的選擇:>");
}
運(yùn)行結(jié)果:
這里要詳解一下
system("color 71")
這個(gè)函數(shù)。要調(diào)用這個(gè)函數(shù)的話就要包含#include
這個(gè)頭文件。color
就是顏色的意思,但是后面的7
和1
又是什么呢?7是代表背景顏色,1是代表前景顏色。這個(gè)你也不需要特別去記憶。只需要win+R
打開windows系統(tǒng)的運(yùn)行窗口,然后輸入cmd
然后回車,就可實(shí)現(xiàn)用戶和操作系統(tǒng)的直接交流。這里你也不需要懂操作系統(tǒng)是什么,直接輸入color 空格+?
然后就可以自己選擇你寫的程序運(yùn)行窗口的前景和背景色了。
第一步:
第二步:
game.h頭文件這里新增加的只有生成隨機(jī)數(shù)需要的兩個(gè)頭文件,其他的東西跟彈跳小球的邏輯差不多。這里存放了一些函數(shù)的聲明。
#include//C語言輸入輸出流
#include//windows API
#include//生成隨機(jī)數(shù)頭文件
#include//隨機(jī)數(shù)種子頭文件
#include//_getch()頭文件
#define row 3//行
#define col 3//列
char is_win(char arr[row][col], int x, int y);//判斷輸贏
void conputer_move(char arr[row][col], int x, int y);//電腦走
void player_move(char arr[row][col], int x, int y);//玩家走
void init_chess(char arr[row][col], int x, int y);//初始化數(shù)組
void print_chess(char arr[row][col], int x, int y);//打印數(shù)組
void update();//更新日志
void game_help();//游戲幫助
void play_game();//開始玩游戲
void menu();//游戲界面
2.game.cgeme.c這個(gè)源文件之前在彈跳小球說過,這里主要是放一些函數(shù)的實(shí)現(xiàn),也就是函數(shù)的定義。博主比較喜歡把函數(shù)的聲明和定義分開寫,這樣比較簡潔,也容易讓別人易懂。這里是打算先解釋重要的部分,然后在附上源碼。
玩家下棋邏輯:
void player_move(char arr[row][col], int x, int y)
{int x1 = 0;
int y1 = 0;
printf("(玩家:@)\n");
printf("(電腦:#)\n");
printf("玩家走:>\n");
while (1)
{printf("請(qǐng)輸入坐標(biāo):>");
scanf_s("%d%d",&x1,&y1);
if ((x1>=1&&x1<=row)&&(y1>=1 && y1<=col))
{ if (arr[x1-1][y1-1]==' ')
{ arr[x1-1][y1-1] = '@';
break;
}
else
{ printf("坐標(biāo)已被占用,請(qǐng)重新輸入\n");
}
}
else
{ printf("坐標(biāo)非法,請(qǐng)重新輸入\n");
}
}
}
電腦下棋邏輯:
void conputer_move(char arr[row][col], int x, int y)
{ while (1)
{int x2 = rand()%x;//0~2
int y2 = rand()%y;//0~2
if (arr[x2][y2] == ' ')
{ arr[x2][y2] = '#';
break;
}
}
}
注意:假如這里沒有輸贏的實(shí)現(xiàn)邏輯,電腦這里下棋實(shí)會(huì)發(fā)生死循環(huán)的。
打印棋盤:
void print_chess(char arr[row][col],int x,int y)//打印棋盤
{for (int i=0;iprintf("----|");
}
printf("\n");
for (int i=0;ifor (int j=0;j printf(" %c |",arr[i][j]);
}
printf("\n");
for (int j=0;j printf("----|");
}
printf("\n");
}
}
源碼:
#include"game.h"
//'Q'代表游戲地圖滿了,也就是平局
//‘C’代表游戲地圖沒滿
//‘@’代表玩家落子
//‘#’代表電腦落子
//2表示棋盤沒滿--游戲繼續(xù)
//3表示棋盤滿了--平局
static int find(char arr[row][col], int x, int y)
{for (int i=0;ifor (int j=0;j if (arr[i][j]==' ')
{ return 2;
}
}
}
return 3;
}
char is_win(char arr[row][col], int x, int y)
{for (int i=0;ifor (int j=0;j if ((arr[i][0]==arr[i][1])&&(arr[i][1]==arr[i][2])&&arr[i][0]!=' ')
{ return arr[i][0];
}
if ((arr[0][j] == arr[1][j]) && (arr[1][j] == arr[2][j]) && arr[0][j] != ' ')
{ return arr[0][j];
}
}
}
if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && arr[0][0] != ' ')
{return arr[1][1];
}
if ((arr[2][0] == arr[1][1]) && (arr[1][1] == arr[0][2]) && arr[2][0] != ' ')
{return arr[2][0];
}
int n = find(arr, x, y);
if (3==n)
{return 'Q';
}
else
{return 'C';
}
}
void conputer_move(char arr[row][col], int x, int y)
{ while (1)
{int x2 = rand()%x;
int y2 = rand()%y;
if (arr[x2][y2] == ' ')
{ arr[x2][y2] = '#';
break;
}
}
}
void player_move(char arr[row][col], int x, int y)
{int x1 = 0;
int y1 = 0;
printf("(玩家:@)\n");
printf("(電腦:#)\n");
printf("玩家走:>\n");
while (1)
{printf("請(qǐng)輸入坐標(biāo):>");
scanf_s("%d%d",&x1,&y1);
if ((x1>=1&&x1<=row)&&(y1>=1 && y1<=col))
{ if (arr[x1-1][y1-1]==' ')
{ arr[x1-1][y1-1] = '@';
break;
}
else
{ printf("坐標(biāo)已被占用,請(qǐng)重新輸入\n");
}
}
else
{ printf("坐標(biāo)非法,請(qǐng)重新輸入\n");
}
}
}
void init_chess(char arr[row][col], int x, int y)//初始化棋盤為空格
{for (int i=0;ifor (int j=0;j arr[i][j] = ' ';
}
}
}
void print_chess(char arr[row][col],int x,int y)//打印棋盤
{for (int i=0;iprintf("----|");
}
printf("\n");
for (int i=0;ifor (int j=0;j printf(" %c |",arr[i][j]);
}
printf("\n");
for (int j=0;j printf("----|");
}
printf("\n");
}
}
static void update_menu()//更新日志菜單
{printf("(暫無)\n");
printf("\n");
printf("\n");
printf("\n");
printf("(按1返回,按任意鍵退出)\n");
printf("請(qǐng)選擇:>");
}
void update()//更新日志
{update_menu();
switch (_getch())
{case '1':
break;
default:
exit(0);
break;
}
}
static void game_help_menu()//游戲幫助菜單
{printf("\n");
printf("游戲操作:\n");
printf("\n");
printf("①:輸入相應(yīng)的坐標(biāo)點(diǎn)進(jìn)行下棋\n");
printf("\n");
printf(" 例如:2 3\n (中間空格or回車都行)\n");
printf("\n");
printf("!!!輸入坐標(biāo)后請(qǐng)按回車\n");
printf("\n");
printf(" (溫馨提醒:請(qǐng)按標(biāo)準(zhǔn)格式輸入)\n");
printf("\n");
printf("\n");
printf("\n");
printf("(按1返回,按任意鍵退出)\n");
printf("請(qǐng)選擇:>");
}
void game_help()//游戲幫助
{game_help_menu();
switch (_getch())
{case '1':
break;
default:
exit(0);
break;
}
}
void play_game()//開始游戲
{char chessboard[row][col] = {0};
init_chess(chessboard, row, col);
print_chess(chessboard,row,col);
char n = '0';
while (1)
{player_move(chessboard, row, col);
system("cls");
print_chess(chessboard, row, col);
n=is_win(chessboard,row,col);
if (n!='C')
{ break;
}
conputer_move(chessboard,row,col);
system("cls");
print_chess(chessboard, row, col);
n = is_win(chessboard, row, col);
if (n!='C')
{ break;
}
}
if (n == '@')
{printf("游戲結(jié)束?。。?);
MessageBox(NULL, "玩家贏", "提示", MB_OK);
}
else if (n=='#')
{printf("游戲結(jié)束?。?!");
MessageBox(NULL, "電腦贏", "提示", MB_OK);
}
else if(n=='Q')
{printf("游戲結(jié)束!?。?);
MessageBox(NULL, "平局", "提示", MB_OK);
}
}
void menu()//菜單
{system("color 71");
printf("|----------------------|\n");
printf("| 1.開始游戲 |\n");
printf("|----------------------|\n");
printf("| 2.游戲幫助 |\n");
printf("|----------------------|\n");
printf("| 3.更新日志 |\n");
printf("|----------------------|\n");
printf("| 0.退出游戲 |\n");
printf("|----------------------|\n");
printf("\n");
printf("(溫馨提醒:請(qǐng)按照菜單相應(yīng)的選項(xiàng)選擇)");
printf("\n");
printf("請(qǐng)輸入你的選擇:>");
}
3.test.c這里放的是一些函數(shù)的調(diào)用,需要講的就說隨機(jī)數(shù)的種子放在了這里,因?yàn)槲覀兙托枰{(diào)用一次種子就可以了。主函數(shù)里一個(gè)
do-while
循環(huán)就可以實(shí)現(xiàn)函數(shù)借口調(diào)用。
#include"game.h"http://在這里相當(dāng)于包含了game.h里面的全部頭文件
int main()
{srand((unsigned)time(NULL));//生成隨機(jī)數(shù)的種子
system("mode con cols=32 lines=16");//控制窗口大小
SetConsoleTitle("簡單三子棋游戲");
do
{system("cls");
menu();
switch (_getch())
{case '1':
system("cls");
play_game();//開始游戲
break;
case '2':
system("cls");
game_help();//游戲幫助
break;
case '3':
system("cls");
update();//更新日志
break;
case '0':
exit(0);//退出
break;
default:
//一個(gè)消息機(jī)制,用來實(shí)現(xiàn)用戶輸入其他然后提示
MessageBox(NULL, "輸入有誤,請(qǐng)重新輸入", "提示", MB_OK);
break;
}
} while ('0');
return 0;
}
四、總結(jié)學(xué)會(huì)怎樣在c或者其他任何程序語言中使用這些既有的功能也是學(xué)習(xí)編程的一門重頭戲。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
網(wǎng)頁題目:C語言小游戲快速入門--三子棋-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://www.rwnh.cn/article40/ceideo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、建站公司、企業(yè)網(wǎng)站制作、小程序開發(fā)、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容