掃雷
目錄
選單界面代碼
列印棋盤代碼
埋雷代碼
掃雷代碼
總代碼
相信大家或多或少都玩過掃雷這個小游戲叭!
思路:先制作一個選單讓玩家選擇是玩游戲還是退出游戲,選單做好了,接著我們開始制作掃雷的棋盤并初始化,初始化弄完了我們下一步開始埋雷,雷埋好了就開始掃雷,
選單界面代碼
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("**************************\n");
printf("*** 1. play 0. exit ***\n");
printf("*** 2. clear ***\n");
printf("**************************\n");
printf("請選擇:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();//游戲實作
break;
case 2://清屏選項
system("cls");
break;
case 0:
printf("退出程式!\n");
break;
default:
printf("輸入錯誤,請重新輸入!\n");
Sleep(1000);
system("cls");
break;
}
} while (input);
return 0;
}
效果展示圖:

制作好選單那我們開始實作整個游戲的邏輯框架了,定義兩個二維陣列,一個用于顯示,一個用于存放地雷,如果這兩個東西都只用一個二維陣列的話后面的實作邏輯會比較麻煩所以我選擇使用兩個二維陣列,
char mine[ROWS][COLS] = { 0 };//用于埋雷
char show[ROWS][COLS] = { 0 };//用于游戲的畫面顯示
進行陣列的初始化:
//初始化
void initzeboard(char mine[ROWS][COLS], int rows, int cols, char val)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
mine[i][j] = val;
}
}
}
列印棋盤代碼
//顯示棋盤
void Display_board(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" 掃雷游戲\n");
printf("---------------------------------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
}
此時我們來看下執行結果:
緊接著我們進行埋雷
埋雷代碼
//埋雷
void random_mine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASILY;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')//用于判斷是否正確的埋雷,只有我們這沒被埋過的雷我們才自減
{
mine[x][y] = '1';
count--;
}
}
}
此時我們再來看下執行效果:
現在我們要做的就是寫出掃雷的代碼:
掃雷代碼
//掃雷
void mine_sweeping(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (count < row * col - EASILY)
{
system("cls");
Display_board(show, COL, ROW);
printf("請輸入坐標:>");
scanf("%d %d", &x, &y);
if ((1 <= x && x <= row) && (1 <= y && y <= col))
{
if (mine[x][y] == '0')
{
int leng = statistics_mine(mine, x, y);
show[x][y] = leng + '0';
count++;
}
else
{
printf("很遺憾你被炸死了\n");
Display_board(mine, ROW, COL);
break;
}
}
else
{
printf("請輸入有效數字!\n");
}
}
if (count == row * col - EASILY)
{
printf("恭喜你掃雷成功!\n");
Display_board(mine, ROW, COL);
}
}
讓我們來看下此時的執行效果:

總代碼
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <graphics.h>
#define COL 16//寬
#define ROW 30//長
#define MINE_NUM 60//雷數
#define SIZE 25//圖片大小
#define IMAGE_NUM 14//圖片數量
int map[ROW+2][COL+2];//+2是添加了輔助區,用來防止陣列越界,
int count = 0;
IMAGE img[IMAGE_NUM];
//生成雷
int gameInit()
{
srand((unsigned)time(NULL));
//初始化格子
for (int row = 0; row < ROW + 2; row++)
{
for (int col = 0; col < COL + 2; col++)
{
map[row][col] = 0;
}
}
//生成雷
for (int n = 0; n < MINE_NUM;)
{
int row = rand() % ROW+1;
int col = rand() % COL+1;
if (map[row][col] == 0)
{
map[row][col] = -1;
n++;
}
}
//遍歷陣列,找空格
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
if (map[i][j] != -1)
{
for (int m = i - 1; m <= i + 1; m++)
{
for (int n = j - 1; n <= j + 1; n++)
{
if (map[m][n] == -1)
{
map[i][j]++;
}
}
}
}
}
}
//加密
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
map[i][j] += 20;
}
}
}
//列印游戲區
void gameDraw()
{
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
printf("%3d", map[i][j]);
//貼雷
if (map[i][j] == -1)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);
}
//貼數字
else if (map[i][j] >= 0 && map[i][j] <= 8)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[map[i][j]]);
}
//貼空白
else if (map[i][j] >= 19 && map[i][j] <= 28)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);
}
//標記
else if (map[i][j] >= 39 && map[i][j] <= 48)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[10]);
}
//貼問號
else if (map[i][j] >= 59 && map[i][j] <= 68)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[12]);
}
//貼炸雷
else if (map[i][j] == -2)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[13]);
}
}
printf("\n");
}
}
//加載圖片
void image()
{
for (int i = 0; i < IMAGE_NUM; i++)
{
char fileName[20] = "";
sprintf(fileName, "./image/%d.gif", i);
loadimage(&img[i], fileName, SIZE, SIZE);
}
}
//連續展開
void blankOpen(int r,int c)
{
//打開格子
map[r][c] -= 20;
count++;
//點開后遍歷九宮格
for (int m=r-1;m<=r+1;m++)
{
for (int n=c-1;n<=c+1;n++)
{
if (m >= 1 && m <= ROW && n >= 1 && n <= COL) //保證是游戲區
{
if (map[m][n] >= 19 && map[m][n] <= 28) //必須為空白格
{
if (map[m][n]!=20)
{
map[m][n] -= 20;
count++;
}
else
{
blankOpen(m,n);
}
}
}
}
}
}
//左鍵點擊已經點開的塊,遍歷周圍的塊
int open(int r,int c)
{
int flag = 0;
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (m >= 1 && m <= ROW && n >= 1 && n <= COL) //保證是游戲區
{
if (map[m][n] == 19)
{
flag = 1;
}
}
}
}
if (flag == 0)
{
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (map[m][n] >= 19 && map[m][n] <= 28)
{
map[m][n] -= 20;
blankOpen(r, c);
}
}
}
}
else if (flag == 1)
{
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (map[m][n] > 39 && map[m][n] <= 48)
{
return -1;
}
}
}
}
}
//輸的時候顯示所有雷
void boom()
{
for (int r = 1; r <= ROW; r++)
{
for (int c = 1; c <= COL; c++)
{
if (map[r][c]==19)
{
map[r][c] -= 21;
}
else if (map[r][c] == -1)
{
map[r][c] -= 1;
}
}
}
}
//顯示剩余雷數
int print()
{
char num[MINE_NUM];
int n = 0;
for (int r = 1; r <= ROW; r++)
{
for (int c = 1; c <= COL; c++)
{
if (map[r][c] == 19 || map[r][c] == -1)
{
n++;
}
}
}
outtextxy(770, 200, "剩余的雷:");
sprintf(num, "%02d", n);
outtextxy(790, 230, num);
printf("\n\n\n");
printf("%02d",n);
}
//滑鼠點擊開玩
int play()
{
MOUSEMSG msg = { 0 };
int r, c;
while (1)
{
msg = GetMouseMsg();
switch (msg.uMsg)
{
//翻開
case WM_LBUTTONDOWN:
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
if (map[r][c]==20)
{
blankOpen(r,c);
return map[r][c];
}
else
{
map[r][c] -= 20;
count++;
return map[r][c];
}
}
else if (map[r][c] >= 0 && map[r][c] <= 8)
{
open(r, c);
if (open(r,c)==-1)
{
return -1;
}
else
{
return map[r][c];
}
}
break;
//插旗子,拔旗子
case WM_RBUTTONDOWN:
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
map[r][c] += 20;
}
else if (map[r][c]>=39 && map[r][c]<=48)
{
map[r][c] += 20;
}
else if (map[r][c]>=59 && map[r][c]<=68)
{
map[r][c] -= 40;
}
return map[r][c];
break;
}
}
}
//游戲主函式
int main()
{
HWND scanmine = initgraph(ROW * SIZE + 100, COL * SIZE);
image();
gameInit();
while (1)
{
gameDraw();
print();
if (play() == -1)
{
boom();
gameDraw();
MessageBox(scanmine, "你輸了", "", MB_OK);
break;
}
if (ROW * COL - MINE_NUM == count)
{
MessageBox(scanmine, "你贏了", "", MB_OK);
break;
}
}
closegraph();
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/297380.html
標籤:其他
上一篇:Unity報錯之UnityException: Texture ‘XXX‘ is not readable
下一篇:初識C語言==>掃雷游戲
