上海家装博览会2023年时间,海口seo网站推广,山西建设厅网站查不了,有哪些可以做包装袋的网站回宿舍路上#xff0c;同学问起我能不能用C写个五子棋游戏#xff0c;我说应该挺简单的#xff0c;但是我不会写#xff0c;然后他说不用写五子棋#xff0c;就写井字棋吧#xff01;#xff01;#xff01;我说试试吧#xff01;#xff01;#xff01; (不过说实话…回宿舍路上同学问起我能不能用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;i3;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;i3;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 || sonIngD){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 ;
}