亲宝软件园·资讯

展开

基于C语言实现钻石棋游戏的示例代码

编程小鱼六六六 人气:0

游戏规则

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

胜利的条件是在棋盘上连成一条长度大于或等于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;
}

加载全部内容

相关教程
猜你喜欢
用户评论