基于C語言實(shí)現(xiàn)鉆石棋游戲的示例代碼
游戲規(guī)則
這是一個(gè)單人鉆石棋游戲,游戲中有兩種顏色的棋子:紅色和綠色。每個(gè)玩家在游戲進(jìn)行中輪流選擇一個(gè)空格,并在該空格上放置自己的棋子。游戲的目的是盡可能地連成一條長的直線,使該直線的顏色與你的棋子顏色相同。如果所有格子都被填滿,游戲?qū)⒔Y(jié)束。最后,顯示游戲結(jié)束的消息。注意:不能在已經(jīng)被占用的格子上放置棋子。游戲勝利條件
勝利的條件是在棋盤上連成一條長度大于或等于5個(gè)格子的直線,且該直線上所有格子的顏色都相同。當(dāng)一方玩家連成勝利直線后,游戲?qū)⒔Y(jié)束并顯示游戲結(jié)束的消息。


實(shí)現(xiàn)代碼
#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];
// 檢查該點(diǎn)所在行是否有5個(gè)相連的棋子
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;
}
}
}
// 檢查該點(diǎn)所在列是否有5個(gè)相連的棋子
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;
}
}
}
// 檢查該點(diǎn)所在主對(duì)角線是否有5個(gè)相連的棋子
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;
}
}
}
// 檢查該點(diǎn)所在副對(duì)角線是否有5個(gè)相連的棋子
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, "公眾號(hào):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, "游戲結(jié)束");
_getch();
closegraph();
return 0;
}到此這篇關(guān)于基于C語言實(shí)現(xiàn)鉆石棋游戲的示例代碼的文章就介紹到這了,更多相關(guān)C語言鉆石棋游戲內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Matlab利用隨機(jī)森林(RF)算法實(shí)現(xiàn)回歸預(yù)測詳解
這篇文章主要為大家詳細(xì)介紹了Matlab如何利用隨機(jī)森林(RF)算法實(shí)現(xiàn)回歸預(yù)測,以及自變量重要性排序的操作,感興趣的小伙伴可以了解一下2023-02-02
純c語言優(yōu)雅地實(shí)現(xiàn)矩陣運(yùn)算庫的方法
本文主要介紹了純c語言優(yōu)雅地實(shí)現(xiàn)矩陣運(yùn)算庫,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

