当前位置:   article > 正文

C++五子棋代码

c++五子棋代码
  1. #include<iomanip>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<string>
  5. #include<cstring>
  6. #include<algorithm>
  7. #include<cstdio>
  8. #include<vector>
  9. #include<stack>
  10. #include<map>
  11. #include<queue>
  12. #define ll long long
  13. using namespace std;
  14. ll n,a[1000][1000],x,y,b[1000][1000];
  15. void init(){
  16. for(int i=0;i<1000;i++) for(int j=0;j<1000;j++) a[i][j]=2;
  17. }
  18. bool find(){
  19. //竖着
  20. ll sum=1,i,j;
  21. i=x,j=y;
  22. b[i][j]=3;
  23. while(1){
  24. if(a[i+1][j]==1){
  25. i++;
  26. sum++;
  27. b[i][j]=3;
  28. }
  29. else break;
  30. }
  31. i=x,j=y;
  32. while(1){
  33. if(a[i-1][j]==1){
  34. i--;
  35. sum++;
  36. b[i][j]=3;
  37. }
  38. else break;
  39. }
  40. if(sum>=5) return true;
  41. //横着
  42. sum=1;
  43. i=x,j=y;
  44. while(1){
  45. if(a[i][j+1]==1){
  46. j++;
  47. sum++;
  48. b[i][j]=3;
  49. }
  50. else break;
  51. }
  52. i=x,j=y;
  53. while(1){
  54. if(a[i][j-1]==1){
  55. j--;
  56. sum++;
  57. b[i][j]=3;
  58. }
  59. else break;
  60. }
  61. if(sum>=5) return true;
  62. //斜着 1
  63. sum=1;
  64. i=x,j=y;
  65. while(1){
  66. if(a[i-1][j-1]==1){
  67. i--;
  68. j--;
  69. sum++;
  70. b[i][j]=3;
  71. }
  72. else break;
  73. }
  74. i=x,j=y;
  75. while(1){
  76. if(a[i+1][j+1]==1){
  77. i++;
  78. j++;
  79. sum++;
  80. b[i][j]=3;
  81. }
  82. else break;
  83. }
  84. if(sum>=5) return true;
  85. //斜着 2
  86. sum=1;
  87. i=x,j=y;
  88. while(1){
  89. if(a[i-1][j+1]==1){
  90. i--;
  91. j++;
  92. sum++;
  93. b[i][j]=3;
  94. }
  95. else break;
  96. }
  97. i=x,j=y;
  98. while(1){
  99. if(a[i+1][j-1]==1){
  100. i++;
  101. j--;
  102. sum++;
  103. b[i][j]=3;
  104. }
  105. else break;
  106. }
  107. if(sum>=5) return true;
  108. return false;
  109. }
  110. bool find2(){
  111. //竖着
  112. ll sum=1,i,j;
  113. i=x,j=y;
  114. b[i][j]=3;
  115. while(1){
  116. if(a[i+1][j]==0){
  117. i++;
  118. sum++;
  119. b[i][j]=3;
  120. }
  121. else break;
  122. }
  123. i=x,j=y;
  124. while(1){
  125. if(a[i-1][j]==0){
  126. i--;
  127. sum++;
  128. b[i][j]=3;
  129. }
  130. else break;
  131. }
  132. if(sum>=5) return true;
  133. //横着
  134. sum=1;
  135. i=x,j=y;
  136. while(1){
  137. if(a[i][j+1]==0){
  138. j++;
  139. sum++;
  140. b[i][j]=3;
  141. }
  142. else break;
  143. }
  144. i=x,j=y;
  145. while(1){
  146. if(a[i][j-1]==0){
  147. j--;
  148. sum++;
  149. b[i][j]=3;
  150. }
  151. else break;
  152. }
  153. if(sum>=5) return true;
  154. //斜着 1
  155. sum=1;
  156. i=x,j=y;
  157. while(1){
  158. if(a[i-1][j-1]==0){
  159. i--;
  160. j--;
  161. sum++;
  162. b[i][j]=3;
  163. }
  164. else break;
  165. }
  166. i=x,j=y;
  167. while(1){
  168. if(a[i+1][j+1]==0){
  169. i++;
  170. j++;
  171. sum++;
  172. b[i][j]=3;
  173. }
  174. else break;
  175. }
  176. if(sum>=5) return true;
  177. //斜着 2
  178. sum=1;
  179. i=x,j=y;
  180. while(1){
  181. if(a[i-1][j+1]==0){
  182. i--;
  183. j++;
  184. sum++;
  185. b[i][j]=3;
  186. }
  187. else break;
  188. }
  189. i=x,j=y;
  190. while(1){
  191. if(a[i+1][j-1]==0){
  192. i++;
  193. j--;
  194. sum++;
  195. b[i][j]=3;
  196. }
  197. else break;
  198. }
  199. if(sum>=5) return true;
  200. return false;
  201. }
  202. bool find3(){
  203. for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(a[i][j]==2) return false;
  204. return true;
  205. }
  206. int main(){
  207. init();
  208. cout<<"棋盘大小:";
  209. cin>>n;
  210. cout<<"白子为0,黑子为1,2为空棋位\n下棋时请输入棋的坐标(格式:x y 坐标范围是从1,1到n,n)\n如果一方赢了那他赢得那一行棋都会变成3\n";
  211. while(1){
  212. cout<<" ";
  213. for(int i=1;i<=n;i++) printf("%3d",i);
  214. cout<<"\n\n";
  215. for(int i=1;i<=n;i++){
  216. printf("%3d",i);
  217. for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
  218. cout<<"\n";
  219. }
  220. if(find3()){
  221. cout<<"平局,游戏结束";
  222. break;
  223. }
  224. //黑子
  225. cout<<"该黑子了\n";
  226. cin>>x>>y;
  227. while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
  228. if(a[x][y]!=2)cout<<"此坐标已有棋子\n";
  229. else if(x>n||y>n||x<1||y<1) cout<<"此坐标不正确\n";
  230. cout<<"该黑子了\n";
  231. cin>>x>>y;
  232. }
  233. a[x][y]=1;
  234. for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
  235. if(find()){
  236. cout<<" ";
  237. for(int i=1;i<=n;i++) printf("%3d",i);
  238. cout<<"\n\n";
  239. for(int i=1;i<=n;i++){
  240. printf("%3d",i);
  241. for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
  242. cout<<"\n";
  243. }
  244. cout<<"黑子赢了,游戏结束";
  245. break;
  246. }
  247. cout<<" ";
  248. for(int i=1;i<=n;i++) printf("%3d",i);
  249. cout<<"\n\n";
  250. for(int i=1;i<=n;i++){
  251. printf("%3d",i);
  252. for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
  253. cout<<"\n";
  254. }
  255. if(find3()){
  256. cout<<"平局,游戏结束";
  257. break;
  258. }
  259. //白子
  260. cout<<"该白子了\n";
  261. cin>>x>>y;
  262. while((a[x][y]!=2)||(x>n||y>n||x<1||y<1)){
  263. if(a[x][y]!=2)cout<<"此坐标已有棋子\n";
  264. else if(x>n||y>n||x<1||y<1) cout<<"此坐标不正确\n";
  265. cout<<"该白子了\n";
  266. cin>>x>>y;
  267. }
  268. a[x][y]=0;
  269. for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) b[i][j]=a[i][j];
  270. if(find2()){
  271. cout<<" ";
  272. for(int i=1;i<=n;i++) printf("%3d",i);
  273. cout<<"\n\n";
  274. for(int i=1;i<=n;i++){
  275. printf("%3d",i);
  276. for(int j=1;j<=n;j++) printf("%3d",a[i][j]);
  277. cout<<"\n";
  278. }
  279. cout<<"白子赢了,游戏结束";
  280. break;
  281. }
  282. }
  283. cout<<"\n ";
  284. for(int i=1;i<=n;i++) printf("%3d",i);
  285. cout<<"\n\n";
  286. for(int i=1;i<=n;i++){
  287. printf("%3d",i);
  288. for(int j=1;j<=n;j++) printf("%3d",b[i][j]);
  289. cout<<"\n";
  290. }
  291. return 0;
  292. }

用数字代表棋子

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/243719?site
推荐阅读
相关标签
  

闽ICP备14008679号