您好,登錄后才能下訂單哦!
本篇文章為大家展示了使用C語言實現一個三子棋游戲,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
game.h
#include<stdio.h> #include<stdlib.h> #include<time.h> #define ROW 3 #define COL 3 //聲明函數 void InitBoard(char board[ROW][COL], int row, int col); //打印棋盤 void PrintBoard(char board[ROW][COL], int row, int col); //玩家下棋 void PlayerMove(char board[ROW][COL], int row, int col); //電腦下棋 void ComputerMove(char board[ROW][COL], int row, int col); //檢測游戲輸贏 char CheckWin(char board[ROW][COL], int row, int col);
game.c
#include "game.h" //初始化棋盤 void InitBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < row; i++) { int j = 0; for (j = 0; j < col; j++) { board[i][j] = ' '; } } } //打印棋盤 void PrintBoard(char board[ROW][COL], int row, int col) { int i = 0; for (i = 0; i < col; i++) { //打印數據 int j = 0; for (j = 0; j < row; j++) { printf(" %c ", board[i][j]); if (j < row - 1) { printf("|"); } } printf("\n"); //打印分隔符 if (i < row - 1) { for (j = 0; j < col; j++) { printf("---"); if (j < col - 1) { printf("|"); } } printf("\n"); } } } //玩家走 void PlayerMove(char board[ROW][COL], int row, int col) { int x = 0; int y = 0; printf("玩家走:>\n"); while (1) { printf("請輸入坐標:>"); scanf("%d%d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (board[x - 1][y - 1] == ' ') { board[x - 1][y - 1] = '*';//落子 break; } else { printf("該坐標已被占用,請重新輸入\n"); } } else { printf("坐標輸入非法,請重新輸入\n"); } } } //電腦走 void ComputerMove(char board[ROW][COL], int row, int col) { printf("電腦走\n"); while (1) { int x = rand() % row;//0-2 int y = rand() % col;//0-2 if (board[x][y] == ' ') { board[x][y] = '#'; break; } } } static int IsFull(char board[ROW][COL], int row, int col) { int i = 0; int j = 0; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { if (board[i][j] == ' ') { return 0; } } } return 1;//沒空格 } //判斷輸贏 char CheckWin(char board[ROW][COL], int row, int col) { int i = 0; //行判斷 for (i = 0; i < row; i++) { if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') { return board[i][0]; } } //列判斷 for (i = 0; i < col; i++) { if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[1][i] != ' ') { return board[0][i]; } } //對角線判斷 if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ') { return board[1][1]; } if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') { return board[1][1]; } //判斷是否平局 //判斷期盼是否滿了 鍵盤是否有空格 if (IsFull(board, row, col) == 1) { return 'Q'; } else return 'C'; }
test.c
#include "game.h" void menu() { printf("************************************\n"); printf("********* 1. play ***********\n"); printf("********* 0. exit ***********\n"); printf("************************************\n"); } //設計三子棋游戲 void game() { char ret = 0; //存儲數據的 char board[ROW][COL] = { 0 }; //數組應該初始化為空格 InitBoard(board, ROW, COL); //打印棋盤 PrintBoard(board, ROW, COL); //分析發現在玩游戲的過程中會出現的情況 //1.玩家贏 - '*' //2.電腦贏 - '#' //3.平局了 - 'Q' //4.繼續 - 'C' while (1) { PlayerMove(board, ROW, COL); //判斷輸贏 ret = CheckWin(board, ROW, COL); if (ret != 'C') break; PrintBoard(board, ROW, COL); ComputerMove(board, ROW, COL); //判斷輸贏 ret = CheckWin(board, ROW, COL); if (ret != 'C') break; PrintBoard(board, ROW, COL); } if (ret == '*') printf("玩家贏\n"); else if (ret == '#') printf("電腦贏\n"); else if (ret == 'Q') printf("平局\n"); PrintBoard(board, ROW, COL); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("請選擇:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戲\n"); break; default: printf("選擇錯誤\n"); break; } } while (input); return 0; }
輸出結果:
上述內容就是使用C語言實現一個三子棋游戲,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。