摘要:如果上述兩種情況均不存在,當發現,位置是空時,電腦先下這一步。其他情況均隨意落子。
朋友們,我們還記得以前上課經常和同桌玩起井字棋,那么我們就當我們回憶童年,現在也用C語言來實現井字棋,本次代碼相對于初階的井字棋,在電腦下棋代碼部分做了優化,使得電腦更加具有威脅,但是由于博主水平有限,代碼不夠精簡優化,或許朋友們有更好的算法,還希望能夠與博主分享
井字棋棋盤我們總體可以當成一個二維數組來操作,我們分別需要實現初始化二維數組,打印棋盤,玩家下棋,電腦下棋,判斷輸贏等代碼
我們創建了頭文件用于放函數的聲明,game.c文件放置函數的實現,test.c文件用于測試。
電腦獲勝:
玩家獲勝:
平局:
我們在game.c文件中實現函數功能的代碼
void InitBoard(char Board[ROW][COL], int row, int col){ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { Board[i][j] = " "; } }}
void DisplayBoard(char Board[ROW][COL], int row, int col){ for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { printf(" %c ", Board[i][j]); if (j < col - 1) printf("|"); } printf("/n"); if (i < row - 1) { for (int j = 0; j < col; j++) { printf("---"); if (j < col - 1) printf("|"); } } printf("/n"); }}
這里的棋盤我們只打印了井字的形狀,如果為了美觀,還可以進行封邊,這里就留給朋友們自行實現啦。
void PlayMove(char Board[ROW][COL], int row, int col){ while (1) { int x = 0, y = 0; printf("請輸入坐標:/n"); 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"); } }}
在電腦落子之前,先檢測有沒有下一步可以直接贏的,若有,則落子在此處,電腦獲勝,若沒有,再檢測玩家下一步是否有機會獲勝,若玩家下一步可以直接獲勝,那么電腦將搶占這一步。如果上述兩種情況均不存在,當發現(1,1)位置是空時,電腦先下這一步。其他情況均隨意落子。
void ComputerMove(char Board[ROW][COL], int row, int col){ int x = 0, y = 0; while (1) { if (JudgeComputer(Board, ROW, COL)) { for (int i = 0; i < row; i++) { if (Board[i][0] == Board[i][1] && Board[i][0] == "*" && Board[i][2] == " ") { Board[i][2] = "*"; return; } else if (Board[i][0] == Board[i][2] && Board[i][0] == "*" && Board[i][1] == " ") { Board[i][1] = "*"; return; } else if (Board[i][1] == Board[i][2] && Board[i][1] == "*" && Board[i][0] == " ") { Board[i][0] = "*"; return; } } for (int j = 0; j < col; j++) { if (Board[0][j] == Board[1][j] && Board[0][j] == "*" && Board[2][j] == " ") { Board[2][j] = "*"; return; } else if (Board[0][j] == Board[2][j] && Board[0][j] == "*" && Board[1][j] == " ") { Board[1][j] = "*"; return; } else if (Board[1][j] == Board[2][j] && Board[1][j] == "*" && Board[0][j] == " ") { Board[0][j] = "*"; return; } } if (Board[0][0] == Board[1][1] && Board[0][0] == "*" && Board[2][2] == " ") { Board[2][2] = "*"; return; } else if (Board[0][0] == Board[2][2] && Board[0][0] == "*" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[1][1] == Board[2][2] && Board[1][1] == "*" && Board[0][0] == " ") { Board[0][0] = "*"; return; } else if (Board[0][2] == Board[1][1] && Board[0][2] == "*" && Board[2][0] == " ") { Board[2][0] = "*"; return; } else if (Board[0][2] == Board[2][0] && Board[0][2] == "*" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[2][0] == Board[1][1] && Board[2][0] == "*" && Board[0][2] == " ") { Board[0][2] = "*"; return; } } else if (JudgePlayer(Board, ROW, COL)) { for (int i = 0; i < row; i++) { if (Board[i][0] == Board[i][1] && Board[i][0] == "#" && Board[i][2] == " ") { Board[i][2] = "*"; return; } else if (Board[i][0] == Board[i][2] && Board[i][0] == "#" && Board[i][1] == " ") { Board[i][1] = "*"; return; } else if (Board[i][1] == Board[i][2] && Board[i][1] == "#" && Board[i][0] == " ") { Board[i][0] = "*"; return; } } for (int j = 0; j < col; j++) { if (Board[0][j] == Board[1][j] && Board[0][j] == "#" && Board[2][j] == " ") { Board[2][j] = "*"; return; } else if (Board[0][j] == Board[2][j] && Board[0][j] == "#" && Board[1][j] == " ") { Board[1][j] = "*"; return; } else if (Board[1][j] == Board[2][j] && Board[1][j] == "#" && Board[0][j] == " ") { Board[0][j] = "*"; return; } } if (Board[0][0] == Board[1][1] && Board[0][0] == "#" && Board[2][2] == " ") { Board[2][2] = "*"; return; } else if (Board[0][0] == Board[2][2] && Board[0][0] == "#" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[1][1] == Board[2][2] && Board[1][1] == "#" && Board[0][0] == " ") { Board[0][0] = "*"; return; } else if (Board[0][2] == Board[1][1] && Board[0][2] == "#" && Board[2][0] == " ") { Board[2][0] = "*"; return; } else if (Board[0][2] == Board[2][0] && Board[0][2] == "#" && Board[1][1] == " ") { Board[1][1] = "*"; return; } else if (Board[2][0] == Board[1][1] && Board[2][0] == "#" && Board[0][2] == " ") { Board[0][2] = "*"; return; } } else { if (Board[1][1] == " ") { Board[1][1] = "*"; return; } else { x
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/123066.html
摘要:三子棋三子棋游戲介紹規則游戲設計思路代碼分析文件代碼分析。玩家下棋實現。三子棋是一種民間傳統游戲,又叫九宮棋圈圈叉叉一條龍井字棋等。落子后打印棋盤以顯示。假設平局時判定輸贏的函數返回后面代碼實現時詳細講。 ...
摘要:三子棋目錄一問題介紹三子棋,在民間又叫井字棋。因為人們在游玩時常常不畫棋盤的邊框,正如漢字中的井字,多稱為井字棋。 三子棋 目錄 一、問題介紹 ? ? ? ? 三子棋,在民間又叫井字棋。因為人們在游玩時常常不畫棋盤的邊框,正如漢字中的井字,多稱為井字棋。 三子棋的游戲規則十分的簡單: ...
目前為止學了分支,循環,數組和函數,現在利用這些知識來寫一個三子棋小游戲來鞏固加深所學哈哈哈 目錄 1、測試結果 2、 整體思路 3、代碼模塊解讀 (1)、打印菜單 ?(2)、初始化棋盤 (3)、打印棋盤 (4)、玩家下棋 (5)、電腦下棋 ?(6)、判斷輸贏 4、代碼整體實現 game.h ?game.c test.c 1、測試結果 這就是游戲的整個過程,接下來我將慢慢講解整個工程是如何實現的...
摘要:文章目錄前言正文一,游戲實現基本流程二游戲實現步驟創建工程及分配功能給頭文件上內容當當當,主函數出場游戲函數里面打印游戲菜單棋盤初始化打印棋盤玩家下棋電腦下棋判斷結果三游戲結果演示恭喜友友獲勝啦電腦獲勝,很遺憾,再接再厲 ...
摘要:說起井字游戲,真是滿滿的童年味道,還記得最瘋狂的時候是小時候跟同桌拿著一張草稿紙就能玩一節課,回到家跟弟弟也能繼續玩,對于沒有太多娛樂節目的童年來說,真是一款玩不厭的小游戲。 99% of information we read, we forget anyway. The best way to remember is to DO. 體驗地址:http://www.hoohack.m...
閱讀 3915·2021-11-16 11:50
閱讀 926·2021-11-11 16:55
閱讀 3657·2021-10-26 09:51
閱讀 856·2021-09-22 15:03
閱讀 3409·2019-08-30 15:54
閱讀 3260·2019-08-30 15:54
閱讀 2468·2019-08-30 14:04
閱讀 919·2019-08-30 13:53