当前位置:   article > 正文

井字棋(C语言实现,可运行玩耍,自行编写)_井字棋c语言代码

井字棋c语言代码

井字棋,英文名叫Tic-Tac-Toe,是一种在3*3格子上进行的连珠游戏,和五子棋类似,由于棋盘一般不画边框,格线排成井字故得名。游戏需要的工具仅为纸和笔,然后由分别代表O和X的两个游戏者轮流在格子里留下标记(一般来说先手者为X),任意三个标记形成一条直线,则为获胜。

很简单,就是在画一个井图形里面画符号,谁先到3个就赢了.

试想一下,我们第一步肯定是需要读入输入的符号,我们用 X  和  O  代替. 

那记录判断我们是否赢了的方法, 需要用到数组和遍历

然后就是判断结果.就可以了.

 

下面开始实施阶段:

按照我们理想化的步骤来实现

第一步

我们是想要如图的图案,那就输入回车和制表符,很简单,但是不定长的数字就不好控制了,所以我们用

for循环依次输出:

  1. printf("\t 0 \t 1 \t 2 \t\n\n");
  2. for(int i=0;i<3;i++){
  3. printf(" %d\t ___\t___\t___\t\n\n",i);
  4. }

第二步:实现输入字符

 需要我们用数组保存我们输入的坐标地址

定义数组

int board[size][size];

等玩家输入对应坐标后,对二维数组的值进行对应赋值,

  1. printf("玩家一请输入坐标\n");
  2. scanf("%d %d",&i,&j);
  3. board[i][j]=1;
  4. printf("请玩家二输入坐标\n");
  5. scanf("%d %d",&i,&j);
  6. board[i][j]=0;

我们会根据对应的数组的值进行遍历,是我们需要的就赋值对应图形的形状  X   O

遍历的话都是一样的,同样道理,这里列出来一个

  1. for(int i=0; i<size;i++){
  2. for(int j=0;j<size; j++ )
  3. {
  4. if(board[i][j]==1){
  5. printf("_X_");
  6. ope++;
  7. if(ope%size==0){
  8. if(ope/size==size){
  9. printf("\n\n\n");
  10. }else if(ope/size<size){
  11. printf("\n\n\n %d\t",ope/size);
  12. }
  13. }else{
  14. printf("\t");
  15. }
  16. }else if(board[i][j]==-1){
  17. printf("___");
  18. ope++;
  19. if(ope%size==0){
  20. if(ope/size==size){
  21. printf("\n\n\n");
  22. }else if(ope/size<size){
  23. printf("\n\n\n %d\t",ope/size);
  24. }
  25. }else{
  26. printf("\t");
  27. }
  28. }else if(board[i][j] == 0){
  29. printf("_O_");
  30. ope++;
  31. if(ope%size==0){
  32. if(ope/size==size){
  33. printf("\n\n\n");
  34. }else if(ope/size<size){
  35. printf("\n\n\n %d\t",ope/size);
  36. }
  37. }else{
  38. printf("\t");
  39. }
  40. }
  41. }
  42. }

根据不同的数,进行不同的赋值

然后我们可以根据输入对应的坐标进行游戏了

第三步:判断输赢家. 

那如何让电脑判定谁赢了呢?

接下来,就是遍历,计数每一行,每一列,正对角线,副对角线是否出现同样图案一条线的了,玩家一先划

,接着玩家二画图案.

我们可以根据玩家输入的数字进行对对应的二维数组赋值,然后分三次遍历计数,

第一次,先确定一行,遍历列,这样就把每一行便利判断完了

  1. //检查行
  2. for (int i=0; i<size ; i++ )
  3. {
  4. numofo=0;
  5. numofx=0;
  6. for( j=0; j<size; j++ ){
  7. if( board[i][j] == 1){
  8. numofx++;
  9. // printf("numofx的值是:%d\n",numofx);
  10. }else if(board[i][j]==0){
  11. numofo++;
  12. // printf("numofo的值是:%d\n",numofo);
  13. }else{
  14. }
  15. }
  16. if(numofo == size){
  17. result = 0;
  18. cnt_o++;
  19. } else if(numofx == size) {
  20. result =1;
  21. cnt_x++;
  22. } else{
  23. result =-1;
  24. }
  25. }

第二次,和第一次差不多,调换了以下顺序,先确定列,遍历行,逐渐加一,直到把所有的列遍历完

  1. //检查列
  2. for( j=0; j<size ; j++){
  3. numofo = numofx = 0;
  4. for( i=0; i<size; i++){
  5. if( board[i][j] == 1){
  6. numofx ++;
  7. }else if(board[i][j]==0) {
  8. numofo ++;
  9. }
  10. }
  11. if(numofo == size){
  12. result = 0;
  13. cnt_o++;
  14. }else if(numofx == size){
  15. result = 1;
  16. cnt_x++;
  17. }
  18. }

第三次,正对角线

每次的行号和列号是相等的

  1. //正对角线
  2. numofo = numofx = 0;
  3. for(i=0; i<size; i++){
  4. if( board[i][i] == 1){
  5. numofx ++;
  6. }else if(board[i][j]==0) {
  7. numofo ++;
  8. }
  9. }
  10. if( numofo == size){
  11. result = 0;
  12. cnt_o++;
  13. }else if( numofx == size){
  14. result = 1;
  15. cnt_x++;
  16. }
  17. // printf("3.检查的正对角线\n cnt_o值是:%d\n cnt_x值是:%d\n",cnt_o,cnt_x);
  18. //斜对角线
  19. numofo = numofx = 0;
  20. for( i=0; i<size; i++ ){
  21. if( board[i][size-i-1] == 1){
  22. numofx++;
  23. }else if( board[i][size-i-1] == 0){
  24. numofo++;
  25. }
  26. }
  27. if( numofo == size){
  28. result = 0;
  29. cnt_o++;
  30. }else if(numofx == size){
  31. result = 1;
  32. cnt_x++;
  33. }

第四次,斜对角线

和正对角线不同的是, 斜对角线对应数组行列不一样.

具体的话,验算一下,相对于正对角线,加了一个size,但是我们是从0计算坐标的,所以再减去1,对比一下, 就知道了:

board[i][size - i - 1]

  1. numofo = numofx = 0;
  2. for( i=0; i<size; i++ ){
  3. if( board[i][size-i-1] == 1){
  4. numofx++;
  5. }else if( board[i][size-i-1] == 0){
  6. numofo++;
  7. }
  8. }
  9. if( numofo == size){
  10. result = 0;
  11. cnt_o++;
  12. }else if(numofx == size){
  13. result = 1;
  14. cnt_x++;
  15. }

检查完了后,如何判断是否够一条线呢?  就是在每次判断同样图案后,加一个计数器

当等于一条线的长度时,就对对应的数加一,这样我们就可以判断, 游戏是否结束, 并且谁获胜,或者是平局了

if( numofo == size){
            result = 0;
            cnt_o++;
        }else if(numofx == size){
            result = 1;
            cnt_x++;
        }

第六步:就是判断游戏结束

当游戏的九个空格输入完前,  判断游戏是否结束,  我们的游戏是否继续,我们使用嵌套

do{

}

while(cnt_o==0 && cnt_x==0 &&count<(size*size-1)/2);

第七步:  根据具体计数器的结果, 判断谁胜谁负

if(cnt_x==1 && cnt_o==0&& count<=(size*size-1)/2){
        printf("恭喜玩家一,你赢啦!\n");
    }else if(cnt_o==1 && cnt_x==0){
        printf("恭喜玩家二,你赢啦!\n");
    }else if(cnt_o==0 &&cnt_x==0 && count==(size*size-1)/2){
        printf("恭喜双方打成平局,以和为贵!");
    }else if(cnt_o==1 &&cnt_x==1 ){
        printf("恭喜你们双赢啦!\n");
        printf("请按回车键继续:\n");
        getchar();getchar();
        printf("开个玩笑,其实是 玩家一 赢了!\n");
    }

第八步,其实现在大概已经实现了

但是还有几个小改进,需要注意一下

①如果我们想要让玩家每次输入对应坐标后,  就可以看见对应符号在数组中的位置,  就需要刚开始第一部输入的表格, 然后我们接着遍历输入对应的符号就可以了.

②因为我们判断游戏结束, 是当两个人都输入完后才判断的,  有一种情况是,  玩家一最后才输入完,

然后格子占满了,  玩家二不用输入了,仍然会出现让玩家二输入的情况.   

我们的解决办法是, 在玩家二输入后边 加了一个计数器,  当 

while(cnt_o==0 && cnt_x==0 &&count<(size*size-1)/2);

 当玩家二输入4次时候,  如果判断没有玩家赢的话,  需要玩家一接着输入一次

所以我们 在玩家二输入四次的时候,  就跳出循环, 给玩家一,  单独来一个套娃,  (这里作者能力有限,只能套娃)  ,就是把前面的重新拷贝一下,  把玩家二的删除救行了.   这里就解决了最后一次不能输入的问题

源代码奉上:

  1. #include <stdio.h>
  2. int main(int argc, char *argv[])
  3. {
  4. const int size = 3;
  5. int board[size][size];
  6. int i,j;
  7. int numofx=0;
  8. int numofo=0;
  9. int result=-1; //-1:没人赢,1:x赢, 0:0赢
  10. int cnt_o=0;
  11. int cnt_x=0;
  12. int count=0;
  13. for( i=0; i<size; i++){
  14. for( j=0; j<size; j++){
  15. board[i][j]=-1;
  16. }
  17. }
  18. // printf("%d",board[2][2]);
  19. //读入矩阵
  20. printf("\t 0 \t 1 \t 2 \t\n\n");
  21. for(int i=0;i<3;i++){
  22. printf(" %d\t ___\t___\t___\t\n\n",i);
  23. }
  24. do {
  25. //printf("游戏还未结束,请继续:\n");
  26. printf("玩家一请输入坐标\n");
  27. scanf("%d %d",&i,&j);
  28. printf("\n");
  29. printf("\t");
  30. for(int k=0;k<size;k++){
  31. printf("%d \t",k);
  32. }
  33. // printf("\t 0 \t 1 \t 2 \t");
  34. printf("\n\n");
  35. printf(" 0 \t");
  36. board[i][j]=1;
  37. int ope=0;
  38. for(int i=0; i<size;i++){
  39. for(int j=0;j<size; j++ )
  40. {
  41. if(board[i][j]==1){
  42. printf("_X_");
  43. ope++;
  44. if(ope%size==0){
  45. if(ope/size==size){
  46. printf("\n\n\n");
  47. }else if(ope/size<size){
  48. printf("\n\n\n %d\t",ope/size);
  49. }
  50. }else{
  51. printf("\t");
  52. }
  53. }else if(board[i][j]==-1){
  54. printf("___");
  55. ope++;
  56. if(ope%size==0){
  57. if(ope/size==size){
  58. printf("\n\n\n");
  59. }else if(ope/size<size){
  60. printf("\n\n\n %d\t",ope/size);
  61. }
  62. }else{
  63. printf("\t");
  64. }
  65. }else if(board[i][j] == 0){
  66. printf("_O_");
  67. ope++;
  68. if(ope%size==0){
  69. if(ope/size==size){
  70. printf("\n\n\n");
  71. }else if(ope/size<size){
  72. printf("\n\n\n %d\t",ope/size);
  73. }
  74. }else{
  75. printf("\t");
  76. }
  77. }
  78. }
  79. }
  80. // printf("board[%d][%d]=%d\n",i,j,board[i][j]);
  81. printf("请玩家二输入坐标\n");
  82. scanf("%d %d",&i,&j);
  83. count++;
  84. printf("\n");
  85. printf("\t");
  86. for(int k=0;k<size;k++){
  87. printf("%d \t",k);
  88. }
  89. // printf("\t 0 \t 1 \t 2 \t");
  90. printf("\n\n");
  91. printf(" 0 \t");
  92. board[i][j]=0;
  93. int opes=0;
  94. for(int i=0; i<size;i++){
  95. for(int j=0;j<size; j++ )
  96. {
  97. if(board[i][j]==1){
  98. printf("_X_");
  99. opes++;
  100. if(opes%size==0){
  101. if(opes/size==size){
  102. printf("\n\n\n");
  103. }else if(opes/size<size){
  104. printf("\n\n\n %d\t",opes/size);
  105. }
  106. }else{
  107. printf("\t");
  108. }
  109. }else if(board[i][j]==-1){
  110. printf("___");
  111. opes++;
  112. if(opes%size==0){
  113. if(opes/size==size){
  114. printf("\n\n\n");
  115. }else if(opes/size<size){
  116. printf("\n\n\n %d\t",opes/size);
  117. }
  118. }else{
  119. printf("\t");
  120. }
  121. }else if(board[i][j] == 0){
  122. printf("_O_");
  123. opes++;
  124. if(opes%size==0){
  125. if(opes/size==size){
  126. printf("\n\n\n");
  127. }else if(opes/size<size){
  128. printf("\n\n\n %d\t",opes/size);
  129. }
  130. }else{
  131. printf("\t");
  132. }
  133. }
  134. }
  135. }
  136. // printf("board[%d][%d]=%d\n",i,j,board[i][j]);
  137. // printf("%d",board[2][2]);
  138. //检查行
  139. for (int i=0; i<size ; i++ )
  140. {
  141. numofo=0;
  142. numofx=0;
  143. for( j=0; j<size; j++ ){
  144. if( board[i][j] == 1){
  145. numofx++;
  146. // printf("numofx的值是:%d\n",numofx);
  147. }else if(board[i][j]==0){
  148. numofo++;
  149. // printf("numofo的值是:%d\n",numofo);
  150. }else{
  151. }
  152. }
  153. if(numofo == size){
  154. result = 0;
  155. cnt_o++;
  156. } else if(numofx == size) {
  157. result =1;
  158. cnt_x++;
  159. } else{
  160. result =-1;
  161. }
  162. }
  163. // printf("1.检查的行\n cnt_o值是:%d\ncnt_x值是:%d\n",cnt_o,cnt_x);
  164. //检查列
  165. for( j=0; j<size ; j++){
  166. numofo = numofx = 0;
  167. for( i=0; i<size; i++){
  168. if( board[i][j] == 1){
  169. numofx ++;
  170. }else if(board[i][j]==0) {
  171. numofo ++;
  172. }
  173. }
  174. if(numofo == size){
  175. result = 0;
  176. cnt_o++;
  177. }else if(numofx == size){
  178. result = 1;
  179. cnt_x++;
  180. }
  181. }
  182. // printf("2.检查的列\n cnt_o值是:%d\ncnt_x值是:%d\n",cnt_o,cnt_x);
  183. //正对角线
  184. numofo = numofx = 0;
  185. for(i=0; i<size; i++){
  186. if( board[i][i] == 1){
  187. numofx ++;
  188. }else if(board[i][j]==0) {
  189. numofo ++;
  190. }
  191. }
  192. if( numofo == size){
  193. result = 0;
  194. cnt_o++;
  195. }else if( numofx == size){
  196. result = 1;
  197. cnt_x++;
  198. }
  199. // printf("3.检查的正对角线\n cnt_o值是:%d\n cnt_x值是:%d\n",cnt_o,cnt_x);
  200. //斜对角线
  201. numofo = numofx = 0;
  202. for( i=0; i<size; i++ ){
  203. if( board[i][size-i-1] == 1){
  204. numofx++;
  205. }else if( board[i][size-i-1] == 0){
  206. numofo++;
  207. }
  208. }
  209. if( numofo == size){
  210. result = 0;
  211. cnt_o++;
  212. }else if(numofx == size){
  213. result = 1;
  214. cnt_x++;
  215. }
  216. // printf("4.检查的斜对角线\n cnt_o值是:%d\ncnt_x值是:%d\n",cnt_o,cnt_x);
  217. }while(cnt_o==0 && cnt_x==0 &&count<(size*size-1)/2);
  218. //int count=9;
  219. if(cnt_x==0 && cnt_o==0 && count==(size*size-1)/2){
  220. do {
  221. //printf("游戏还未结束,请继续:\n");
  222. printf("玩家一请输入坐标\n");
  223. scanf("%d %d",&i,&j);
  224. printf("\n");
  225. printf("\t");
  226. for(int k=0;k<size;k++){
  227. printf("%d \t",k);
  228. }
  229. // printf("\t 0 \t 1 \t 2 \t");
  230. printf("\n\n");
  231. printf(" 0 \t");
  232. board[i][j]=1;
  233. int ope=0;
  234. for(int i=0; i<size;i++){
  235. for(int j=0;j<size; j++ )
  236. {
  237. if(board[i][j]==1){
  238. printf("_X_");
  239. ope++;
  240. if(ope%size==0){
  241. if(ope/size==size){
  242. printf("\n\n\n");
  243. }else if(ope/size<size){
  244. printf("\n\n\n %d\t",ope/size);
  245. }
  246. }else{
  247. printf("\t");
  248. }
  249. }else if(board[i][j]==-1){
  250. printf("___");
  251. ope++;
  252. if(ope%size==0){
  253. if(ope/size==size){
  254. printf("\n\n\n");
  255. }else if(ope/size<size){
  256. printf("\n\n\n %d\t",ope/size);
  257. }
  258. }else{
  259. printf("\t");
  260. }
  261. }else if(board[i][j] == 0){
  262. printf("_O_");
  263. ope++;
  264. if(ope%size==0){
  265. if(ope/size==size){
  266. printf("\n\n\n");
  267. }else if(ope/size<size){
  268. printf("\n\n\n %d\t",ope/size);
  269. }
  270. }else{
  271. printf("\t");
  272. }
  273. }
  274. }
  275. }
  276. //检查行
  277. for (int i=0; i<size ; i++ )
  278. {
  279. numofo=0;
  280. numofx=0;
  281. for( j=0; j<size; j++ ){
  282. if( board[i][j] == 1){
  283. numofx++;
  284. // printf("numofx的值是:%d\n",numofx);
  285. }else if(board[i][j]==0){
  286. numofo++;
  287. // printf("numofo的值是:%d\n",numofo);
  288. }else{
  289. }
  290. }
  291. if(numofo == size){
  292. result = 0;
  293. cnt_o++;
  294. } else if(numofx == size) {
  295. result =1;
  296. cnt_x++;
  297. } else{
  298. result =-1;
  299. }
  300. }
  301. // printf("1.检查的行\n cnt_o值是:%d\ncnt_x值是:%d\n",cnt_o,cnt_x);
  302. //检查列
  303. for( j=0; j<size ; j++){
  304. numofo = numofx = 0;
  305. for( i=0; i<size; i++){
  306. if( board[i][j] == 1){
  307. numofx ++;
  308. }else if(board[i][j]==0) {
  309. numofo ++;
  310. }
  311. }
  312. if(numofo == size){
  313. result = 0;
  314. cnt_o++;
  315. }else if(numofx == size){
  316. result = 1;
  317. cnt_x++;
  318. }
  319. }
  320. // printf("2.检查的列\n cnt_o值是:%d\ncnt_x值是:%d\n",cnt_o,cnt_x);
  321. //正对角线
  322. numofo = numofx = 0;
  323. for(i=0; i<size; i++){
  324. if( board[i][i] == 1){
  325. numofx ++;
  326. }else if(board[i][j]==0) {
  327. numofo ++;
  328. }
  329. }
  330. if( numofo == size){
  331. result = 0;
  332. cnt_o++;
  333. }else if( numofx == size){
  334. result = 1;
  335. cnt_x++;
  336. }
  337. // printf("3.检查的正对角线\n cnt_o值是:%d\n cnt_x值是:%d\n",cnt_o,cnt_x);
  338. //斜对角线
  339. numofo = numofx = 0;
  340. for( i=0; i<size; i++ ){
  341. if( board[i][size-i-1] == 1){
  342. numofx++;
  343. }else if( board[i][size-i-1] == 0){
  344. numofo++;
  345. }
  346. }
  347. if( numofo == size){
  348. result = 0;
  349. cnt_o++;
  350. }else if(numofx == size){
  351. result = 1;
  352. cnt_x++;
  353. }
  354. // printf("4.检查的斜对角线\n cnt_o值是:%d\ncnt_x值是:%d\n",cnt_o,cnt_x);
  355. }while(cnt_o==0 && cnt_x==0 &&count<(size*size-1)/2);
  356. }
  357. if(cnt_x==1 && cnt_o==0&& count<=(size*size-1)/2){
  358. printf("恭喜玩家一,你赢啦!\n");
  359. }else if(cnt_o==1 && cnt_x==0){
  360. printf("恭喜玩家二,你赢啦!\n");
  361. }else if(cnt_o==0 &&cnt_x==0 && count==(size*size-1)/2){
  362. printf("恭喜双方打成平局,以和为贵!");
  363. }else if(cnt_o==1 &&cnt_x==1 ){
  364. printf("恭喜你们双赢啦!\n");
  365. printf("请按回车键继续:\n");
  366. getchar();getchar();
  367. printf("开个玩笑,其实是 玩家一 赢了!\n");
  368. }
  369. return 0;
  370. }

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

闽ICP备14008679号