鱼C论坛

 找回密码
 立即注册
查看: 1048|回复: 3

[作品展示] 一个下棋小游戏【规则自编】【C语言】【简易】

[复制链接]
发表于 2023-9-6 17:21:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
因为是规则自编,说好了不好玩不要骂我哈

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <math.h>
  4. #include <stdlib.h> // 用于随机数生成
  5. #include <time.h>   // 用于种子初始化

  6. #define BOARD_SIZE 5
  7. #define COLOR_NONE "\033[0m" //表示清除前面设置的格式
  8. #define RED "\033[1;31;40m" //40表示背景色为黑色, 1 表示高亮
  9. #define YELLOW "\033[1;33;40m"

  10. // 定义棋盘
  11. char board[BOARD_SIZE][BOARD_SIZE];

  12. // 初始化棋盘
  13. void initializeBoard() {
  14.         int i,j;
  15.     for (i = 0; i < BOARD_SIZE; i++) {
  16.         for (j = 0; j < BOARD_SIZE; j++) {
  17.             board[i][j] = '*'; // 初始化为*
  18.         }
  19.     }
  20.     // 设置玩家初始棋子位置
  21.     board[0][0] = board[0][1] = board[0][3] = board[0][4] = '1'; // 玩家1的兵
  22.     board[0][2] = '0'; // 玩家1的王
  23.     board[4][0] = board[4][4] = board[4][1] = board[4][3] = 's'; // 玩家0的兵
  24.     board[4][2] = 'k'; // 玩家0的王
  25. }

  26. // 打印棋盘
  27. void printBoard() {
  28.         int i,j;
  29.         printf(RED"  0 1 2 3 4\n"COLOR_NONE);
  30.     for (i = 0; i < BOARD_SIZE; i++) {
  31.             printf(RED"%d "COLOR_NONE,i);
  32.         for (j = 0; j < BOARD_SIZE; j++) {
  33.             printf("%c ", board[i][j]);
  34.         }
  35.         printf("\n");
  36.     }
  37. }

  38. // 判断是否游戏结束
  39. bool isGameOver() {
  40.     // 遍历棋盘,查找王的位置
  41.     int king0_row = -1, king0_col = -1;
  42.     int king1_row = -1, king1_col = -1;
  43.         int i,j;
  44.     for (i = 0; i < BOARD_SIZE; i++) {
  45.         for (j = 0; j < BOARD_SIZE; j++) {
  46.             if (board[i][j] == '0') {
  47.                 king0_row = i;
  48.                 king0_col = j;
  49.             } else if (board[i][j] == 'k') {
  50.                 king1_row = i;
  51.                 king1_col = j;
  52.             }
  53.         }
  54.     }

  55.     // 检查游戏结束条件
  56.     if ((king0_row == -1 && king0_col == -1) || (king1_row == -1 && king1_col == -1)) {
  57.         // 王相遇,游戏结束
  58.         return true;
  59.     } else if ((king0_row == 4 && king0_row == 2) ||
  60.                (king1_row == 0 && king1_row == 2)) {
  61.         // 玩家0或玩家1的王到达对方的初始位置,游戏结束
  62.         return true;
  63.     }

  64.     return false;
  65. }


  66. int main() {
  67.     initializeBoard();
  68.     int currentPlayer,count_0 = 0,count_1 = 0; // 0表示玩家0,1表示玩家1
  69.     int row, col,x,y;
  70.    
  71.     printf("游戏规则:\n棋盘:5*5\n棋子:兵(玩家0:1 or 玩家1:s)和 王(玩家0:0 or 玩家1:k)\n移动条件:根据你的回合数决定,奇数只能直行,偶数只能斜行\n胜利条件:你的王击杀对面的王或者你的王走到对面王的初始位置\n");
  72.         printf(RED"!!!\n请输入整数谢谢\n!!!\n"COLOR_NONE);
  73.        
  74.         srand(time(NULL));
  75.         currentPlayer = rand() % 2;//随机决定先手
  76.        
  77.     while (!isGameOver()) {
  78.             if (currentPlayer)
  79.                 {
  80.                         count_1 ++;
  81.                         printf(YELLOW"玩家1的回合数:%d\n"COLOR_NONE,count_1);
  82.                 }
  83.                 else
  84.                 {
  85.                         count_0++;
  86.                         printf(YELLOW"玩家0的回合数:%d\n"COLOR_NONE,count_0);
  87.                 }//更新个人回合数
  88.         printf("当前棋盘状态:\n");
  89.         printBoard();

  90.                 printf("玩家 %d,请输入您要移动的棋子的坐标 (row col): ", currentPlayer);
  91.         scanf("%d %d", &row, &col);

  92.         // 在这里实现棋子移动的逻辑,根据规则更新棋盘状态。
  93.         //检查输入
  94.         if (row >= BOARD_SIZE || col >= BOARD_SIZE || board[row][col] == '*')
  95.         {
  96.                         printf("您的输入有误!\n");
  97.                         if (currentPlayer)
  98.                         {
  99.                                 count_1--;
  100.                         }
  101.                         else
  102.                         {
  103.                                 count_0--;
  104.                         }
  105.                         continue;
  106.                 }
  107.                 if ((currentPlayer == 0 && (board[row][col] == 's' || board[row][col] == 'k')) || currentPlayer == 1 && (board[row][col] == '1' || board[row][col] == '0'))
  108.                 {
  109.                         printf("您的输入有误!\n");
  110.                         if (currentPlayer)
  111.                         {
  112.                                 count_1--;
  113.                         }
  114.                         else
  115.                         {
  116.                                 count_0--;
  117.                         }
  118.                         continue;
  119.                 }
  120.         
  121.         printf("玩家 %d,请输入您要移动到的位置的坐标 (row col):",currentPlayer);
  122.         scanf("%d %d",&x,&y);
  123.         if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE) {
  124.                     printf("无效的坐标!请重新输入:");
  125.                         if (currentPlayer)
  126.                         {
  127.                                 count_1--;
  128.                         }
  129.                         else
  130.                         {
  131.                                 count_0--;
  132.                         }
  133.                     continue; // 输入无效,重新循环
  134.                 }

  135.                 // 检查移动是否符合规则
  136.                 int dx = x - row;
  137.                 int dy = y - col;
  138.                
  139.                 if (currentPlayer)
  140.                 {
  141.                         if (count_1 % 2)
  142.                         {
  143.                                 if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1) || (dx == 1 && dy == 0) || (dx == -1 && dy == 0))
  144.                                 {
  145.                                         ;
  146.                                 }
  147.                                 else
  148.                                 {

  149.                             printf("移动不符合规则!请重新输入:");
  150.                             if (currentPlayer)
  151.                                 {
  152.                                         count_1--;
  153.                                 }
  154.                                 else
  155.                                 {
  156.                                         count_0--;
  157.                                 }
  158.                             continue; // 移动不符合规则,重新循环
  159.                            
  160.                             }
  161.                         }
  162.                         else
  163.                         {
  164.                                 if ((dx == -1 && dy == -1) || (dx == 1 && dy == 1) || (dx == -1 && dy == 1) || (dx == 1 && dy == -1))
  165.                                 {
  166.                                         ;
  167.                                 }
  168.                                 else
  169.                                 {

  170.                             printf("移动不符合规则!请重新输入:");
  171.                             if (currentPlayer)
  172.                                 {
  173.                                         count_1--;
  174.                                 }
  175.                                 else
  176.                                 {
  177.                                         count_0--;
  178.                                 }
  179.                             continue; // 移动不符合规则,重新循环
  180.                            
  181.                             }
  182.                         }
  183.                 }
  184.                 else
  185.                 {
  186.                         if (count_0 % 2)
  187.                         {
  188.                                 if ((dx == 0 && dy == -1) || (dx == 0 && dy == 1) || (dx == 1 && dy == 0) || (dx == -1 && dy == 0))
  189.                                 {
  190.                                         ;
  191.                                 }
  192.                                 else
  193.                                 {

  194.                             printf("移动不符合规则!请重新输入:");
  195.                             if (currentPlayer)
  196.                                 {
  197.                                         count_1--;
  198.                                 }
  199.                                 else
  200.                                 {
  201.                                         count_0--;
  202.                                 }
  203.                             continue; // 移动不符合规则,重新循环
  204.                            
  205.                             }
  206.                         }
  207.                         else
  208.                         {
  209.                                 if ((dx == -1 && dy == -1) || (dx == 1 && dy == 1) || (dx == -1 && dy == 1) || (dx == 1 && dy == -1))
  210.                                 {
  211.                                         ;
  212.                                 }
  213.                                 else
  214.                                 {

  215.                             printf("移动不符合规则!请重新输入:");
  216.                             if (currentPlayer)
  217.                                 {
  218.                                         count_1--;
  219.                                 }
  220.                                 else
  221.                                 {
  222.                                         count_0--;
  223.                                 }
  224.                             continue; // 移动不符合规则,重新循环
  225.                            
  226.                             }
  227.                         }
  228.                 }
  229.                
  230.                 // 检查是否有自己的棋子或对方的王
  231.                 if ((currentPlayer == 0 && (board[x][y] == '1' || board[x][y] == '0')) ||
  232.             (currentPlayer == 1 && (board[x][y] == 's' || board[x][y] == 'k'))) {
  233.                     printf("该位置有自己的棋子!请重新输入\n");
  234.                     if (currentPlayer)
  235.                         {
  236.                                 count_1--;
  237.                         }
  238.                         else
  239.                         {
  240.                                 count_0--;
  241.                         }
  242.                     continue; // 位置上有自己的棋子,重新循环
  243.                 }
  244.         
  245.         //棋子的移动
  246.         if (board[x][y] == '*')
  247.         {
  248.                 board[x][y] = board[row][col];
  249.                 board[row][col] = '*';
  250.                
  251.                 //切换玩家
  252.                         currentPlayer = 1 - currentPlayer;
  253.                         continue;
  254.                 }
  255.                
  256.                 //判断兵之间的击杀,如果不能击杀,就返回输入错误
  257.                 if ((currentPlayer == 0 && board[row][col] == '1') || (currentPlayer == 1 && board[row][col] == 's'))
  258.         {
  259.                 if (currentPlayer == 0 && board[x][y] == 's')//可以击杀
  260.                         {
  261.                                 board[x][y] = '1';
  262.                                 board[row][col] = '*';
  263.                                
  264.                                 currentPlayer = 1 - currentPlayer;
  265.                                 continue;
  266.                         }
  267.                         if (currentPlayer == 1 && board[x][y] == '1')
  268.                         {
  269.                                 board[x][y] = 's';
  270.                                 board[row][col] = '*';
  271.                                
  272.                                 currentPlayer = 1 - currentPlayer;
  273.                                 continue;
  274.                         }
  275.                        
  276.                         if (currentPlayer == 0 && board[x][y] == 'k')
  277.                         {
  278.                                 printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
  279.                                 if (currentPlayer)
  280.                                 {
  281.                                         count_1--;
  282.                                 }
  283.                                 else
  284.                                 {
  285.                                         count_0--;
  286.                                 }
  287.                             continue;
  288.                         }
  289.                         if (currentPlayer == 1 && board[x][y] == '0')
  290.                         {
  291.                                 printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
  292.                                 if (currentPlayer)
  293.                                 {
  294.                                         count_1--;
  295.                                 }
  296.                                 else
  297.                                 {
  298.                                         count_0--;
  299.                                 }
  300.                             continue;
  301.                         }
  302.                 }
  303.                
  304.                 //判断王之间的击杀
  305.                 if ((currentPlayer == 0 && board[row][col] == '0') || (currentPlayer == 1 && board[row][col] == 'k'))
  306.         {
  307.                 if (currentPlayer == 0 && board[x][y] == 'k')//可以击杀
  308.                         {
  309.                                 board[x][y] = board[row][col];
  310.                                 board[row][col] = '*';
  311.                                
  312.                                 currentPlayer = 1 - currentPlayer;
  313.                         }
  314.                         if (currentPlayer == 1 && board[x][y] == '0')
  315.                         {
  316.                                 board[x][y] = board[row][col];
  317.                                 board[row][col] = '*';
  318.                                
  319.                                 currentPlayer = 1 - currentPlayer;
  320.                         }
  321.                        
  322.                         if (currentPlayer == 0 && board[x][y] == 's')
  323.                         {
  324.                                 printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
  325.                                 if (currentPlayer)
  326.                                 {
  327.                                         count_1--;
  328.                                 }
  329.                                 else
  330.                                 {
  331.                                         count_0--;
  332.                                 }
  333.                             continue;
  334.                         }
  335.                         if (currentPlayer == 1 && board[x][y] == '1')
  336.                         {
  337.                                 printf("抱歉,根据规则你不能击杀这枚棋子,请重新输入!\n");
  338.                                 if (currentPlayer)
  339.                                 {
  340.                                         count_1--;
  341.                                 }
  342.                                 else
  343.                                 {
  344.                                         count_0--;
  345.                                 }
  346.                             continue;
  347.                         }
  348.                 }
  349.     }

  350.     printf("游戏结束!玩家 %d 获胜!\n", 1-currentPlayer);

  351.     return 0;
  352. }
复制代码


评分

参与人数 2荣誉 +10 贡献 +6 收起 理由
python爱好者. + 5 + 3 鱼C有你更精彩^_^
歌者文明清理员 + 5 + 3 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 17:22:50 | 显示全部楼层
qpzc
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-6 17:24:34 | 显示全部楼层
hp
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-9-6 17:24:44 | 显示全部楼层
有bug请反馈,除了输入字符会死循环这个,都说了请输入整数了啊喂!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 13:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表