基於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語言鉆石棋遊戲內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: