您好,登錄后才能下訂單哦!
使用C語言實現五子棋游戲?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
#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.結束游戲-------\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("玩家請輸入一組坐標:(row col)"); int row = 0; int col = 0; scanf("%d %d", &row, &col); if (row < 0 || row >= ROW || col < 0 || col >= COL) { printf("您輸入的坐標非法,請重新輸入:\n"); continue; } else if (broad[row][col] != ' ') { printf("該位置已經被占用,請重新輸入:\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); //玩家輸入,提示玩家輸入一組坐標,檢查游戲是否結束 playermove(g_broad); winner = checkwinner(g_broad); if (winner != ' ') { break; } //電腦輸入,瞎下,檢查游戲是否結束 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("游戲結束!goodbay~\n"); return 0; } return 0; }
游戲運行結果如下:
注:使用宏定義可以擴充棋盤
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。