使用C語言實(shí)現(xiàn)五子棋游戲?很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)公司制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計(jì),成都做網(wǎng)站、網(wǎng)站設(shè)計(jì),網(wǎng)站設(shè)計(jì),企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為上千余家服務(wù),成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 5 #define COL 5 char g_broad[ROW][COL]; void menu(); void menu() { printf("-----------------------\n"); printf("------1.開始游戲-------\n"); printf("------0.結(jié)束游戲-------\n"); printf("-----------------------\n"); printf("請輸入您的選擇:"); } void Init(char broad[ROW][COL]) { for (int row = 0; row < ROW; ++row) { for (int col = 0; col < COL; ++col) { broad[row][col] = ' '; } } } void print(char broad[ROW][COL]) { for (int row = 0; row < ROW; ++row) { printf("| %c | %c | %c | %c | %c |\n", broad[row][0], broad[row][1], broad[row][2], broad[row][3], broad[row][4]); if (row != ROW - 1) { printf(" ---|---|---|---|--- \n"); } } } void playermove(char broad[ROW][COL]) { printf("玩家落子:\n"); while (1) { printf("玩家請輸入一組坐標(biāo):(row col)"); int row = 0; int col = 0; scanf("%d %d", &row, &col); if (row < 0 || row >= ROW || col < 0 || col >= COL) { printf("您輸入的坐標(biāo)非法,請重新輸入:\n"); continue; } else if (broad[row][col] != ' ') { printf("該位置已經(jīng)被占用,請重新輸入:\n"); continue; } else { broad[row][col] = 'x'; break; } } } void computermove(char broad[ROW][COL]) { printf("電腦落子:\n"); while (1) { int row = rand() % ROW; int col = rand() % COL; if (broad[row][col] != ' ') { continue; } else { broad[row][col] = 'o'; break; } } } int Isfull(char broad[ROW][COL]) { for (int row = 0; row < ROW; ++row) { for (int col = 0; col < COL; ++col) { if (broad[row][col] == ' ') return 0; } } return 1; } char checkwinner(char broad[ROW][COL]) { for (int row = 0; row < ROW; ++row) { if (broad[row][0] == broad[row][1] && broad[row][0] == broad[row][2] && broad[row][0] == broad[row][3] && broad[row][0] == broad[row][4] && broad[row][0] != ' ') { return broad[row][0]; } } for (int col = 0; col < COL; ++col) { if (broad[0][col] == broad[1][col] && broad[0][col] == broad[2][col] && broad[0][col] == broad[3][col] && broad[0][col] == broad[4][col] && broad[0][col] != ' ') { return broad[0][col]; } } if (broad[0][0] == broad[1][1] && broad[0][0] == broad[2][2] && broad[0][0] == broad[3][3] && broad[0][0] == broad[4][4] && broad[0][0] != ' ') { return broad[0][0]; } if (broad[0][4] == broad[1][3] && broad[0][4] == broad[2][2] && broad[0][4] == broad[3][1] && broad[0][4] == broad[4][0] && broad[4][0] != ' ') { return broad[4][0]; } if (Isfull(broad)) { return 'p'; } else return ' '; } int main() { srand((unsigned int)time(0)); int input = 0; menu(g_broad); //初始化棋盤,將棋盤初始化成' ' Init(g_broad); scanf("%d", &input); char winner = ' '; while (input) { //第一次打印棋盤 print(g_broad); //玩家輸入,提示玩家輸入一組坐標(biāo),檢查游戲是否結(jié)束 playermove(g_broad); winner = checkwinner(g_broad); if (winner != ' ') { break; } //電腦輸入,瞎下,檢查游戲是否結(jié)束 computermove(g_broad); winner = checkwinner(g_broad); if (winner != ' ') { break; } } if (winner == 'x') { printf("玩家勝利!\n"); print(g_broad); } if (winner == 'o') { printf("電腦勝利!\n"); print(g_broad); } if (winner == 'p') { printf("和棋!\n"); print(g_broad); } if (winner == ' ') { printf("游戲結(jié)束!goodbay~\n"); return 0; } return 0; }
當(dāng)前名稱:使用C語言實(shí)現(xiàn)五子棋游戲-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://aaarwkj.com/article18/gdgdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、全網(wǎng)營銷推廣、App開發(fā)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容