三子棋
三子棋介紹
三子棋又叫井字棋、圈圈叉叉、一條龍等等。將正方形對角線連起來,相對兩邊依次擺上三個雙方棋子,只要將本身的三個棋子走成一條線,對方就算輸了。可是,有不少時候會出現和棋的狀況。windows
描述
用c語言編寫一個三子棋。數組
基本流程
1.建立一個用戶交互菜單;
2.建立一個棋盤並初始化;
3.打印一個棋盤;
4.玩家落子(玩家經過輸入行列座標落子);
5.斷定勝負關係;
6.電腦落子(電腦採用隨機落子的方式落子);
7.斷定勝負關係;
8.遊戲結束。
函數
代碼實現
用戶交互菜單
int menu(){ printf("=====================\n"); printf("1.開始遊戲\n"); printf("0.結束遊戲\n"); printf("=====================\n"); printf("請輸入您的選擇:"); int choice = 0; scanf("%d", &choice); return choice; }
咱們首先建立一個用戶交互菜單來實現與用戶的交互。spa
打印棋盤
void PrintChessBoard(char ChessBoard[MAX_ROW][MAX_COL]){ //打印棋盤 printf("+---+---+---+\n"); for (int row = 0; row < MAX_ROW; row++){ printf("| %c| %c| %c|\n", ChessBoard[row][0], ChessBoard[row][1], ChessBoard[row][2]); printf("+---+---+---+\n"); } }
咱們經過一個函數來打印棋盤,棋盤的邊框用+號和-號來完成,再經過一個for循環來實現每行每一個格子的邊框實現。這裏的%c是輸入一個字符。指針
棋盤初始化
void Init(char ChessBoard[MAX_ROW][MAX_COL]){ // 把數組中的每一個元素都設爲 空格 for (int row = 0; row < MAX_ROW; row++){ for (int col = 0; col < MAX_COL; col++){ ChessBoard[row][col] = ' '; } } }
初始化過程當中,咱們經過採用兩重循環使棋盤初始化,讓棋盤一開始的每一個格子中的元素都是空格。code
玩家落子
void playerMove(char ChessBoard[MAX_ROW][MAX_COL]){ //玩家落子 while (1){ printf("請玩家輸入座標(row,col):"); int row = 0; int col = 0; scanf("%d %d", &row, &col); if (row<0 || row>=MAX_ROW || col<0 || col>=MAX_COL){ printf("您的輸入有誤,請從新輸入!\n"); continue; } if (ChessBoard[row][col] != ' '){ printf("此處已經落子!\n"); continue; } ChessBoard[row][col] = 'x'; break; } }
玩家採用輸入行列的方式座標的方式來落子,同時咱們須要排除玩家出入的座標是否正確的問題,若是玩家輸入的座標超出了範圍,則提示玩家輸入錯誤,從新輸入,若是玩家輸入的座標的格子已經有一個元素了,則提醒玩家此處已經落子。當玩家正確落子時,咱們規定落子的元素爲‘X’。遊戲
電腦落子
void computerMove(char ChessBoard[MAX_ROW][MAX_COL]){ //電腦落子 while (1){ int row = rand() % MAX_ROW; int col = rand() % MAX_COL; if (ChessBoard[row][col] != ' '){ continue; } ChessBoard[row][col] = 'o'; break; } }
電腦落子時,咱們採用隨機數的方式來讓電腦落子,在後面的main()函數中使用srand((unsigned int)time(0)),unsigned int的做用是強制轉換類型,這裏是轉換成int型。it
斷定勝負關係
char iswin(char ChessBoard[MAX_ROW][MAX_COL]){ //斷定勝負 for (int row = 0; row < MAX_ROW; row++){ //斷定行 if (ChessBoard[row][0] != ' ' &&ChessBoard[row][0] == ChessBoard[row][1] && ChessBoard[row][0] == ChessBoard[row][2]){ return ChessBoard[row][0]; } } for (int col = 0; col < MAX_COL; col++){ //斷定列 if (ChessBoard[col][0] != ' ' &&ChessBoard[col][0] == ChessBoard[col][1] && ChessBoard[col][0] == ChessBoard[col][2]){ return ChessBoard[col][0]; } } if (ChessBoard[0][0] !=' ' && ChessBoard[0][0] == ChessBoard[1][1] && ChessBoard[0][0] == ChessBoard[2][2]){ return ChessBoard[0][0]; } if (ChessBoard[2][0] != ' ' && ChessBoard[2][0] == ChessBoard[1][1] && ChessBoard[2][0] == ChessBoard[0][2]){ return ChessBoard[2][0]; } if (isfull(ChessBoard)){ return 'q'; } return ' '; }
在遊戲介紹中咱們知道三子棋獲勝的方式就是在一條直線上有3個相同的元素,這樣則斷定爲勝利。io
斷定和棋狀況
int isfull(char ChessBoard[MAX_ROW][MAX_COL]){ //判斷和棋狀況 for (int row = 0; row < MAX_ROW; row++){ for (int col = 0; col < MAX_COL; col++){ if (ChessBoard[row][col] == ' '){ return 0; } } } return 1; }
和棋狀況的描述就是在棋盤中再沒有空格,且玩家與電腦雙方都沒有獲勝的狀況。for循環
game()函數
void game(){ char ChessBoard[MAX_ROW][MAX_COL] = { 0 }; Init(ChessBoard); char winner = ' '; while (1){ PrintChessBoard(ChessBoard);//打印棋盤 playerMove(ChessBoard);//玩家落子 winner = iswin(ChessBoard);//勝負斷定 if (winner != ' '){ break; } computerMove(ChessBoard); winner = iswin(ChessBoard); if (winner != ' '){ break; } } PrintChessBoard(ChessBoard); if (winner == 'x'){ printf("恭喜您,打敗了人工智障!\n"); } if (winner == 'o'){ printf("很遺憾,您連人工智障都不如!\n"); } if (winner == 'q'){ printf("您和人工智障五五開!\n"); } }
在game()函數中,咱們就要引用咱們剛纔所寫的棋盤初始化,打印棋盤,斷定勝負關係等函數。這裏要注意的是在玩家和電腦落子時,咱們都要對其進行一一斷定。
總代碼
#include<stdio.h>//三子棋 #include<windows.h> #pragma warning(disable:4996) #include<time.h> #define MAX_ROW 3 #define MAX_COL 3 //1.首先打印棋盤(並讓棋盤初始化) //2.玩家落子 //3.斷定勝負 //4.電腦落子 //5.斷定勝負 void Init(char ChessBoard[MAX_ROW][MAX_COL]){ // 把數組中的每一個元素都設爲 空格 for (int row = 0; row < MAX_ROW; row++){ for (int col = 0; col < MAX_COL; col++){ ChessBoard[row][col] = ' '; } } } void PrintChessBoard(char ChessBoard[MAX_ROW][MAX_COL]){ //打印棋盤 printf("+---+---+---+\n"); for (int row = 0; row < MAX_ROW; row++){ printf("| %c| %c| %c|\n", ChessBoard[row][0], ChessBoard[row][1], ChessBoard[row][2]); printf("+---+---+---+\n"); } } void playerMove(char ChessBoard[MAX_ROW][MAX_COL]){ //玩家落子 while (1){ printf("請玩家輸入座標(row,col):"); int row = 0; int col = 0; scanf("%d %d", &row, &col); if (row<0 || row>=MAX_ROW || col<0 || col>=MAX_COL){ printf("您的輸入有誤,請從新輸入!\n"); continue; } if (ChessBoard[row][col] != ' '){ printf("此處已經落子!\n"); continue; } ChessBoard[row][col] = 'x'; break; } } void computerMove(char ChessBoard[MAX_ROW][MAX_COL]){ //電腦落子 while (1){ int row = rand() % MAX_ROW; int col = rand() % MAX_COL; if (ChessBoard[row][col] != ' '){ continue; } ChessBoard[row][col] = 'o'; break; } } int isfull(char ChessBoard[MAX_ROW][MAX_COL]){ //判斷和棋狀況 for (int row = 0; row < MAX_ROW; row++){ for (int col = 0; col < MAX_COL; col++){ if (ChessBoard[row][col] == ' '){ return 0; } } } return 1; } char iswin(char ChessBoard[MAX_ROW][MAX_COL]){ //斷定勝負 for (int row = 0; row < MAX_ROW; row++){ //斷定行 if (ChessBoard[row][0] != ' ' &&ChessBoard[row][0] == ChessBoard[row][1] && ChessBoard[row][0] == ChessBoard[row][2]){ return ChessBoard[row][0]; } } for (int col = 0; col < MAX_COL; col++){ //斷定列 if (ChessBoard[col][0] != ' ' &&ChessBoard[col][0] == ChessBoard[col][1] && ChessBoard[col][0] == ChessBoard[col][2]){ return ChessBoard[col][0]; } } if (ChessBoard[0][0] !=' ' && ChessBoard[0][0] == ChessBoard[1][1] && ChessBoard[0][0] == ChessBoard[2][2]){ return ChessBoard[0][0]; } if (ChessBoard[2][0] != ' ' && ChessBoard[2][0] == ChessBoard[1][1] && ChessBoard[2][0] == ChessBoard[0][2]){ return ChessBoard[2][0]; } if (isfull(ChessBoard)){ return 'q'; } return ' '; } void game(){ char ChessBoard[MAX_ROW][MAX_COL] = { 0 }; Init(ChessBoard); char winner = ' '; while (1){ PrintChessBoard(ChessBoard);//打印棋盤 playerMove(ChessBoard);//玩家落子 winner = iswin(ChessBoard);//勝負斷定 if (winner != ' '){ break; } computerMove(ChessBoard); winner = iswin(ChessBoard); if (winner != ' '){ break; } } PrintChessBoard(ChessBoard); if (winner == 'x'){ printf("恭喜您,打敗了人工智障!\n"); } if (winner == 'o'){ printf("很遺憾,您連人工智障都不如!\n"); } if (winner == 'q'){ printf("您和人工智障五五開!\n"); } } int menu(){ printf("=====================\n"); printf("1.開始遊戲\n"); printf("0.結束遊戲\n"); printf("=====================\n"); printf("請輸入您的選擇:"); int choice = 0; scanf("%d", &choice); return choice; } int main(){ srand((unsigned int)time(0)); while (1){ int choice = menu();//建立一個簡單的用戶交互菜單 if (choice == 1){ game(); } else if (choice == 0){ printf("Goodbye!\n"); break; } else{ printf("您的輸入有誤,請從新輸入!\n"); } } system("pause"); return 0; }
ok ,文章就先到這裏了,但願這篇文章可以幫助到你對指針的認識,如有不足或者不正之處,但願諒解並歡迎批評指正!
若是本文章對你有幫助,哪怕一點點,那就請點一個讚唄,謝謝~~