ToB企服应用市场:ToB评测及商务社交产业平台

标题: c语言三子棋的解说 [打印本页]

作者: 王國慶    时间: 2025-1-23 05:14
标题: c语言三子棋的解说
  这里讲述的是关于三子棋的解说,由于才疏学浅,有讲得不对的地方希望各位大佬指教。
关于三子棋的代码,全都放在文章的末了了。
  首先,因为写三子棋这个游戏也需要较多的代码,为了可读性,我们应该采取模块式的誊写,而不是一股脑地写在一个源文件当中。
  所以在这里,我分成了三个来写,此中  test.c  这个文件负责游戏的测试部分, game.c负责游戏当中的函数实现,game.h负责游戏当中的函数声明。

在写代码之前我们应该先做好关于我们后续设计的规划,以方便后面誊写代码。
所以我们第一步要先打印一个菜单,作为游戏的开始界面。

    像我们菜单打印出来以后,我们肯定要进行选择,在此中的话,printf  里面的话就是起到了提示语句的作用,因为我们在多项的选择中 ,所有可以用  switch  语句来对这个多分支进行处置惩罚。
   这里  default   的作用的话就是怕有的人不小心输入错了,可以提醒,让人重新输入。
   因为在玩游戏不大概说我们只玩一次,所以为了可以重复的玩,我们可以在里面使用do .....while循环。

   现在的话我们就要开始游戏的内容,游戏的内容我们可以封装在game()这个自定义函数里。

 首先我们要定义一个二维数组,然后我们下棋需要一个棋盘,所以我们先定义一个人空的棋盘。

首先我们可以使用for循环初始化一个空白棋盘,i,j分别代表棋盘的行列。
 我们下棋的话,空白的棋盘肯定是不行的,

既然我们棋盘做完了,那肯定我们要来下棋了 

 在下棋以后,我们肯定是要判断胜负的,三子棋的话就三种环境,一种是行上面三个棋赢了,一种是列上面赢了,一种就是对角线赢了。
 

 末了附上全部的代码
这个是test.c的代码
  1. #include "game.h"
  2. void menu()
  3. {
  4.         printf("*****************************\n");
  5.         printf("*********  1. play   ********\n");
  6.         printf("*********  0. exit   ********\n");
  7.         printf("*****************************\n");
  8. }
  9. void game()
  10. {
  11.         char board[ROW][COL] = { 0 };
  12.         InitBoard(board, ROW, COL);//初始化一个空格棋盘
  13.         DisplayBoard(board, ROW, COL);//打印一个棋盘
  14.         char ret = 0;
  15.         while (1)
  16.         {
  17.                 PlayMove(board, ROW, COL);
  18.                 DisplayBoard(board, ROW, COL);
  19.                 ret = IsWin(board, ROW, COL);
  20.                 if (ret != 'C')
  21.                         break;
  22.                 ComputerMove(board, ROW, COL);
  23.                 DisplayBoard(board, ROW, COL);
  24.                 ret = IsWin(board, ROW, COL);
  25.                 if (ret != 'C')
  26.                         break;
  27.         }
  28.         if (ret == '*')
  29.                 printf("玩家赢\n");
  30.         else if (ret == '#')
  31.                 printf("电脑赢\n");
  32.         else
  33.                 printf("平局\n");
  34. }
  35. int main()
  36. {
  37.         int input = 0;
  38.         srand((unsigned int)time(NULL));
  39.         do
  40.         {
  41.                 menu();
  42.                 printf("请进行选择:>");
  43.                 scanf("%d", &input);
  44.                 switch (input)
  45.                 {
  46.                 case 1:
  47.                         game();
  48.                         break;
  49.                 case 0:
  50.                         printf("退出游戏\n");
  51.                         break;
  52.                 default:
  53.                         printf("请重新输入\n");
  54.                         break;
  55.                 }
  56.         } while (input);
  57.         return 0;
  58. }
复制代码
 这个是game.c的代码
  1. #define _CRT_SECURE_NO_WARNINGS 1
  2. #include "game.h"
  3. void InitBoard(char board[ROW][COL], int row, int col)
  4. {
  5.         int i = 0;
  6.         for (i = 0; i < row; i++)
  7.         {
  8.                 int j = 0;
  9.                 for (j = 0; j < col; j++)
  10.                 {
  11.                         board[i][j] = ' ';
  12.                 }
  13.         }
  14. }
  15. void DisplayBoard(char board[ROW][COL], int row, int col)
  16. {
  17.         int i = 0;
  18.         for (i = 0; i < row; i++)
  19.         {
  20.                 int j = 0;
  21.                 for (j = 0; j < col; j++)
  22.                 {
  23.                         printf(" %c ", board[i][j]);
  24.                         if (j < col - 1)
  25.                                 printf("|");
  26.                 }
  27.                 printf("\n");
  28.                 if (i < row - 1)
  29.                 {
  30.                         int j = 0;
  31.                         for (j = 0; j < col; j++)
  32.                         {
  33.                                 printf("---");
  34.                                 if (j < col - 1)
  35.                                         printf("|");
  36.                         }
  37.                         printf("\n");
  38.                 }
  39.         }
  40. }
  41. void PlayMove(char board[ROW][COL], int row, int col)
  42. {
  43.         int x = 0;
  44.         int y = 0;
  45.         printf("玩家下棋:>\n");
  46.         while (1)
  47.         {
  48.                 printf("请输入下棋坐标,中间用空格隔开:>");
  49.                 scanf("%d %d", &x, &y);
  50.                 if (x >= 1 && x <= row && y >= 1 && y <= col)
  51.                 {
  52.                         if (board[x - 1][y - 1] == ' ')
  53.                         {
  54.                                 board[x - 1][y - 1] = '*';
  55.                                 break;
  56.                         }
  57.                         else
  58.                                 printf("坐标被占有,请重新输入\n");
  59.                 }
  60.         }
  61. }
  62. void ComputerMove(char board[ROW][COL], int row, int col)
  63. {
  64.         int x = 0;
  65.         int y = 0;
  66.         printf("电脑下棋:>\n");
  67.         while (1)
  68.         {
  69.                 x = rand() % row;
  70.                 y = rand() % col;
  71.                 if (board[x][y] == ' ')
  72.                 {
  73.                         board[x][y] = '#';
  74.                         break;
  75.                 }
  76.         }
  77. }
  78. IsFull(char board[ROW][COL], int row, int col)
  79. {
  80.         int i = 0;
  81.         for (i = 0; i < row; i++)
  82.         {
  83.                 int j = 0;
  84.                 for (j = 0; j < col; j++)
  85.                 {
  86.                         if (board[i][j] == ' ')
  87.                                 return 0;
  88.                 }
  89.         }
  90.         return 1;
  91. }
  92. char IsWin(char board[ROW][COL], int row, int col)
  93. {
  94.         int i = 0;
  95.         for (i = 0; i < row; i++)
  96.         {
  97.                 if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ')
  98.                 {
  99.                         return board[i][0];
  100.                 }
  101.         }
  102.         for (i = 0; i < col; i++)
  103.         {
  104.                 if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ')
  105.                 {
  106.                         return board[0][i];
  107.                 }
  108.         }
  109.         if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ')
  110.                 return board[0][0];
  111.         if (board[0][3] == board[1][2] && board[1][2] == board[3][1] && board[0][3] != ' ')
  112.                 return board[0][3];
  113.         //平局情况
  114.         if (IsFull(board, row, col) == 1)
  115.         {
  116.                 return 'Q';//代表平局
  117.         }
  118.         return 'C';//C代表继续下
  119. }
复制代码
 这个是game.h的代码
  1. #define ROW 3
  2. #define COL 3
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include <stdlib.h>
  6. void InitBoard(char board[ROW][COL], int row, int col);
  7. void DisplayBoard(char board[ROW][COL], int row, int col);
  8. void PlayMove(char board[ROW][COL], int row, int col);
  9. void ComputerMove(char board[ROW][COL], int row, int col);
  10. char IsWin(char board[ROW][COL], int row, int col);
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4