C++实现井字棋小游戏(写得不好,留作纪念!!!)
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                C++实现井字棋小游戏(写得不好,留作纪念!!!)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                回宿舍路上,同學問起我能不能用C++寫個五子棋游戲,我說應該挺簡單的,但是我不會寫,然后他說不用寫五子棋,就寫井字棋吧!!!我說試試吧!!!
 (不過說實話,寫得不是很好,留作紀念吧!!!)
代碼如下:
#include <iostream> using namespace std; const int N = 5; bool vis[N][N] = { false }; bool tttSon[N][N] = { false }; char tttDesk[N][N]; int dx[] = { 1,-1,1,-1,0,0,1,-1 }; int dy[] = { 0,0, 1,-1,1,-1,-1,1 }; bool isEnd = false;void printError() {cout << "無效輸入,按任意鍵后,重新輸入" << endl; }void initDesk() {for (int i = 1; i <= 3; i++)for (int j = 1; j <= 3; j++)tttDesk[i][j] = '@'; }void printInterface(char son) {int flagX = 0, flagY = 0;for (int i = 1; i <= 3; i++){for (int j = 1; j <= 3; j++){cout << vis[i][j] << " ";if (vis[i][j]){flagX = i;flagY = j;}}cout << endl;}cout << "當前要落的子為:" << son << endl;cout << "當前光標的位置在第" << flagX << "行," << "第" << flagY << "列" << endl;cout << "-------------------------------------" << endl;for (int i = 1; i <= 3; i++){for (int j = 1; j <= 3; j++){cout << tttDesk[i][j] << " ";}cout << endl;} }void checkPos(int &sonX,int &sonY) {for (int i =1;i<=3;i++)for (int j = 1; j <= 3; j++){if (!tttSon[i][j]){sonX = i;sonY = j;vis[sonX][sonY] = true;return;}} }bool checkWinOfDfs(int x, int y,int step,char me,int k) {if (step == 3) return true;int xx = x + dx[k];int yy = y + dy[k];if (xx < 1 || xx > 3 || yy < 1 || yy > 3 || tttDesk[xx][yy] != me || tttDesk[xx][yy]=='@') return false;/*cout << xx << " " << yy << endl;*/checkWinOfDfs(xx, yy, step + 1, me, k);}void operatorDesk(int &sonX,int &sonY,char &son) {char sonIng;bool flag = false;initDesk();int cnt = 0;while (true){bool winFlag = false;if (cnt > 4 && tttDesk[sonX][sonY]!='@'){/*cout << sonX << " " << sonY << endl;*/for (int k = 0; k < 8; k++){if (checkWinOfDfs(sonX, sonY, 1, tttDesk[sonX][sonY], k)){winFlag = true;}if (winFlag){if (tttDesk[sonX][sonY]=='O')cout << "Winer is O" << endl;else cout << "Winer is X" << endl;isEnd = true;return;}}int cnt1 = 0;for (int i = 1;i<=3;i++)for (int j = 1; j <= 3; j++){if (tttSon[i][j]) cnt1++;}if (cnt1 == 9) return;}if (!flag)checkPos(sonX,sonY);printInterface(son);cout << "請輸入指令" << endl;cin >> sonIng;if (sonIng == 'w' || sonIng == 'W'){if (sonX - 1 < 1 ){printError();system("pause");system("cls");continue;}flag = true;vis[sonX][sonY] = false;sonX -= 1;vis[sonX][sonY] = true;system("cls");}else if (sonIng == 's' ||sonIng == 'S'){if (sonX + 1 > 3 ){printError();system("pause");system("cls");continue;}flag = true;vis[sonX][sonY] = false;sonX += 1;vis[sonX][sonY] = true;system("cls");}else if (sonIng == 'a'|| sonIng == 'A'){if (sonY - 1 < 1 ){printError();system("pause");system("cls");continue;}flag = true;vis[sonX][sonY] = false;sonY -= 1;vis[sonX][sonY] = true;system("cls");}else if (sonIng == 'd' || sonIng=='D'){if (sonY + 1 > 3 ){printError();system("pause");system("cls");continue;}flag = true;vis[sonX][sonY] = false;sonY += 1;vis[sonX][sonY] = true;system("cls");}else if (sonIng == 'g' || sonIng == 'G'){if (tttSon[sonX][sonY]){printError();system("pause");system("cls");continue;}cnt++;tttSon[sonX][sonY] = true;vis[sonX][sonY] = false;flag = false;tttDesk[sonX][sonY] = son;if (son == 'O') son = 'X';else son = 'O';system("cls");}else{printError();system("pause");system("cls");continue;}} }void PrintGame() {cout << "----------------------" << endl;cout << "----簡略井字棋游戲----" << endl;cout << "----------------------" << endl;cout << "------游戲說明--------" << endl;cout << "---按下G鍵開始游戲----" << endl;cout << "--通過W鍵控制光標上移-" << endl;cout << "-通過S鍵控制光標下移--" << endl;cout << "--通過A鍵控制光標左移-" << endl;cout << "--通過D鍵控制光標右移-" << endl;cout << "------通過G鍵落子-----" << endl;cout << "----------------------" << endl;cout << "----------------------" << endl; }int main() {char son = 'O';int sonX = 1, sonY = 1;while (true){char gameStart;PrintGame();cin >> gameStart;if (gameStart == 'G' || gameStart == 'g'){system("cls");break;}else{cout << "輸入錯誤!!!,請重新輸入" << endl;system("pause");system("cls");}}operatorDesk(sonX, sonY,son);if (!isEnd){cout << "平局!!!" << endl;}return 0 ; }總結
以上是生活随笔為你收集整理的C++实现井字棋小游戏(写得不好,留作纪念!!!)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 买品牌电脑就是缴智商税买品牌电脑就是缴智
- 下一篇: [Java基础]Date类基础
