基于C语言如何实现钻石棋游戏

发布时间:2023-02-27 10:25:55 作者:iii
来源:亿速云 阅读:96

这篇文章主要介绍了基于C语言如何实现钻石棋游戏的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇基于C语言如何实现钻石棋游戏文章都会有所收获,下面我们一起来看看吧。

游戏规则

这是一个单人钻石棋游戏,游戏中有两种颜色的棋子:红色和绿色。每个玩家在游戏进行中轮流选择一个空格,并在该空格上放置自己的棋子。游戏的目的是尽可能地连成一条长的直线,使该直线的颜色与你的棋子颜色相同。如果所有格子都被填满,游戏将结束。最后,显示游戏结束的消息。注意:不能在已经被占用的格子上放置棋子。游戏胜利条件

胜利的条件是在棋盘上连成一条长度大于或等于5个格子的直线,且该直线上所有格子的颜色都相同。当一方玩家连成胜利直线后,游戏将结束并显示游戏结束的消息。

实现代码

#define _CRT_SECURE_NO_WARNINGS
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
 
#define ROWS 8
#define COLUMNS 8
#define CELL_SIZE 50
 
int board[ROWS][COLUMNS];
 
void init_board() {
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLUMNS; j++) {
      board[i][j] = rand() % 3;
    }
  }
}
 
void draw_board() {
  for (int i = 0; i <= ROWS; i++) {
    line(0, i * CELL_SIZE, COLUMNS * CELL_SIZE, i * CELL_SIZE);
  }
  for (int i = 0; i <= COLUMNS; i++) {
    line(i * CELL_SIZE, 0, i * CELL_SIZE, ROWS * CELL_SIZE);
  }
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLUMNS; j++) {
      if (board[i][j] == 1) {
        setfillcolor(RED);
        fillcircle(j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE / 2, CELL_SIZE / 2 - 5);
      }
      else if (board[i][j] == 2) {
        setfillcolor(GREEN);
        fillcircle(j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE / 2, CELL_SIZE / 2 - 5);
      }
    }
  }
}
 
bool check_valid_move(int row, int col) {
  return row >= 0 && row < ROWS && col >= 0 && col < COLUMNS && board[row][col] == 0;
}
 
bool make_move(int row, int col, int player) {
  if (check_valid_move(row, col)) {
    board[row][col] = player;
    return true;
  }
  return false;
}
 
bool check_game_over() {
  for (int i = 0; i < ROWS; i++) {
    for (int j = 0; j < COLUMNS; j++) {
      if (board[i][j] == 0) {
        return false;
      }
    }
  }
  return true;
}
int check_win(int row, int col) {
  int i, j, color = board[row][col];
  // 检查该点所在行是否有5个相连的棋子
  for (i = row - 4; i <= row; i++) {
    if (i >= 0 && i + 4 < ROWS) {
      int count = 0;
      for (j = i; j <= i + 4; j++) {
        if (board[j][col] == color) {
          count++;
        }
      }
      if (count == 5) {
        return 1;
      }
    }
  }
  // 检查该点所在列是否有5个相连的棋子
  for (i = col - 4; i <= col; i++) {
    if (i >= 0 && i + 4 < COLUMNS) {
      int count = 0;
      for (j = i; j <= i + 4; j++) {
        if (board[row][j] == color) {
          count++;
        }
      }
      if (count == 5) {
        return 1;
      }
    }
  }
  // 检查该点所在主对角线是否有5个相连的棋子
  for (i = row - 4, j = col - 4; i <= row && j <= col; i++, j++) {
    if (i >= 0 && i + 4 < ROWS && j >= 0 && j + 4 < COLUMNS) {
      int count = 0;
      int x, y;
      for (x = i, y = j; x <= i + 4 && y <= j + 4; x++, y++) {
        if (board[x][y] == color) {
          count++;
        }
      }
      if (count == 5) {
        return 1;
      }
    }
  }
  // 检查该点所在副对角线是否有5个相连的棋子
  for (i = row - 4, j = col + 4; i <= row && j >= 0; i++, j--) {
    if (i >= 0 && i + 4 < ROWS && j >= 0 && j - 4 < COLUMNS) {
      int count = 0;
      int x, y;
      for (x = i, y = j; x <= i + 4 && y >= j - 4; x++, y--) {
        if (board[x][y] == color) {
          count++;
        }
      }
      if (count == 5) {
        return 1;
      }
    }
  }
  return 0;
}
 
 
 
int main()
{
  srand(time(0));
  init_board();
  initgraph(COLUMNS * CELL_SIZE + 100, ROWS * CELL_SIZE + 100);
  draw_board();
  settextcolor(DARKGRAY);
  settextstyle(20,0,_T("宋体"));
  outtextxy(COLUMNS * CELL_SIZE - 200, ROWS * CELL_SIZE+20, "公众号:C语言研究");
  int player = 1;
  ExMessage m;
  while (!check_game_over()) {
    m = getmessage(EX_MOUSE | EX_KEY);
    if (m.message == WM_LBUTTONDOWN)
    {
      int x = m.x;
      int y = m.y;
      int row = y / CELL_SIZE;
      int col = x / CELL_SIZE;
      if (make_move(row, col, player))
      {
 
        draw_board();
        if (check_win(row, col))
        {
          settextstyle(64, 0, "黑体");
          const char *player_string;
          if (player == 1) {
            player_string = "红棋";
          }
          else {
            player_string = "绿棋";
          }
          char win_message[100];
          strcpy(win_message, "玩家");
          strcat(win_message, player_string);
          strcat(win_message, "获胜!");
          outtextxy(COLUMNS * CELL_SIZE / 2 - 100, ROWS * CELL_SIZE / 2 - 50, win_message);
          _getch();
          closegraph();
          return 0;
        }
        player = player == 1 ? 2 : 1;
      }
    }
  }
  settextstyle(64, 0, "黑体");
  outtextxy(COLUMNS * CELL_SIZE / 2 - 100, ROWS * CELL_SIZE / 2 - 100, "游戏结束");
  _getch();
  closegraph();
  return 0;
}

关于“基于C语言如何实现钻石棋游戏”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“基于C语言如何实现钻石棋游戏”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. C语言中经典的算法有哪些
  2. c语言内联汇编的概念是什么

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:怎么使用C语言实现音乐播放器

下一篇:C#中的WinForm框架如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》