基于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)所在主對角線是否有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)所在副對角線是否有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, "公眾號: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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++進(jìn)程間共享數(shù)據(jù)實(shí)例
這篇文章主要介紹了C++進(jìn)程間共享數(shù)據(jù)的方法,是進(jìn)行C++應(yīng)用程序開發(fā)中非常實(shí)用的技巧,需要的朋友可以參考下2014-10-10讓應(yīng)用程序只運(yùn)行一個(gè)實(shí)例的實(shí)現(xiàn)方法
我們在使用《360軟件管家》時(shí)發(fā)現(xiàn),在《360軟件管家》已經(jīng)運(yùn)行了的情況下,再次點(diǎn)擊《360軟件管家》的圖標(biāo),那么它不會再運(yùn)行另外一個(gè)《360軟件管家》,而是將已有的《360軟件管家》給激活,始終只能運(yùn)行一個(gè)《360軟件管家》的實(shí)例2013-05-05使用C++11實(shí)現(xiàn)Android系統(tǒng)的Handler機(jī)制
這篇文章主要介紹了使用C++11實(shí)現(xiàn)Android系統(tǒng)的Handler機(jī)制,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04如何用C++制作LeetCode刷題小技巧-錯(cuò)題記錄本
這篇文章主要介紹了如何用C++制作LeetCode刷題小技巧-錯(cuò)題記錄本的方法,需要的朋友可以參考下2021-04-04C++關(guān)鍵字之likely和unlikely詳解
這篇文章主要介紹了C++關(guān)鍵字之likely和unlikely,C++20之前的,likely和unlikely只不過是一對自定義的宏,而C++20中正式將likely和unlikely確定為屬性關(guān)鍵字,本文給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10