回宿舍路上,同学问起我能不能用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++实现井字棋小游戏(写得不好,留作纪念!!!)相关推荐

  1. python井字棋_python实现井字棋小游戏

    本文为大家分享了python实现井字棋小游戏,供大家参考,具体内容如下 周五晚上上了python的选修课,本来以为老师是从python的基础语法开始的,没想到是从turtle画图开始,正好补上了我以前 ...

  2. python如何在前面加井_python入门之井字棋小游戏

    引言: 刚学python好几天了,从java到python,基础学起来确实比较容易,语法掌握,基本概念上都比较容易入脑. 唯一比较郁闷的是老想着用java的语法去学python代码,这点还需要后面慢慢 ...

  3. python井字棋小游戏代码_python实现井字棋小游戏

    本文为大家分享了python实现井字棋小游戏,供大家参考,具体内容如下 周五晚上上了python的选修课,本来以为老师是从python的基础语法开始的,没想到是从turtle画图开始,正好补上了我以前 ...

  4. C语言初学——井字棋小游戏

    hello呀!小伙伴们,今天小刘同学要带着大家学习的内容是--井字棋小游戏!!! 在本次学习中,我们将会运用到的主要内容是数组,函数,循环和判断. 当然,本次学习的内容并不困难,希望本次教学结束大家都 ...

  5. python井字棋_用Python做一个井字棋小游戏

    井字棋是一个经典的小游戏,在九宫格上玩家轮流画OXO,当每列或每行或是两个对角成一线时便是获胜. 今天就用Python编写一个井字棋小游戏,与电脑对战. 程序执行画面如下图所示: 程序提供了两种人工智 ...

  6. 用Unity3D实现简单的井字棋小游戏

    用Unity3D实现简单的井字棋小游戏 项目地址 井字棋小游戏 完成效果图 实现思路 首先定义游戏的数据部分: /* 井字棋中每一个棋格中的逻辑控制常量,代表这个棋格的状态 */ private co ...

  7. python井字棋游戏人机对战_用Python做一个井字棋小游戏

    井字棋是一个经典的小游戏,在九宫格上玩家轮流画OXO,当每列或每行或是两个对角成一线时便是获胜. 今天就用Python编写一个井字棋小游戏,与电脑对战. 程序执行画面如下图所示: 程序提供了两种人工智 ...

  8. 井字棋小游戏代码(Visual Studio)

    #include<graphics.h> #include<conio.h> #include<iostream> #include "ImagePng. ...

  9. 基于Unity设计的井字棋小游戏

    资源下载地址:https://download.csdn.net/download/sheziqiong/86871335 资源下载地址:https://download.csdn.net/downl ...

最新文章

  1. 【风之语】至贱城市之苏州
  2. hibernate对象关系实现(二)一对一
  3. html中autocomplete无效,OnChange和AutoComplete都不能使用HTML.TextBox
  4. keil C 51 strlen库函数使用
  5. open a BP will trigger text load - COM_TEXT_MAINTAIN - READ_TEXT
  6. 谈谈对于技术面试的心得体验
  7. copy linux file to mac,Mac Linux互相传递文件
  8. android开发歌词滑动效果_一些Flutter开发中的“坑”
  9. java之classloader的对象层次关系
  10. 实对称矩阵的特征值求法_“绝境之下”,如何求解矩阵的特征值?
  11. Excel如何生成11位随机数,包含大小写字母和数字
  12. android 多媒体封装格式详解---MKV
  13. 计算机编程教育现状,中小学生编程教育现状调查研究
  14. nnUNet原创团队全新力作!MedNeXt:医学图像分割新SOTA
  15. 洛谷 P1144 最短路计数 dijkstra
  16. 设计模式 | 中介者模式(详解)
  17. c++ memcpy内存拷贝
  18. 记一次nginx配置自定义错误页面的麻瓜经历
  19. Java 9 - JShell介绍
  20. 能跟你聊DOTA的神经对话模型:MeenaDialoGPT

热门文章

  1. 【MATLAB统计分析与应用100】案例001:matlab使用Importdata函数导入文本txt数据
  2. C++之explicit关键字使用总结
  3. 这是我第一次使用代码创建出一个窗口【python 游戏实战 01】
  4. [小白进] 大佬们学习为什么简单?小白该如何学习?学历不高如何找工作?副业很好赚?了解后少走弯路
  5. jmeter对乱码如何处理_JMeter读取 Excel 表中用例数据实现接口压测
  6. 柠檬汁制成的电池可以开动超100千克的车子吗?
  7. 分享一个理工男必学的撩妹姿势
  8. 华人AI界痛失“一代宗师”,计算机视觉之父黄煦涛教授去世
  9. Nature封面:大团队日趋中庸,小团队更容易出颠覆性创新
  10. 深度学习与机器学习到底什么关系?