扫雷 收获很大
尤其是打印数据区域和随机数
根据雷的分布填充不为雷的区域
边上区域遍历时候 考虑越界
第一版本
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> #define ROW 10//行 #define COL 10//列 #define NUM 10//雷的个数 int map[ROW+2][COL+2];//需要辅助区域,用来计算9宫格中雷的数量 void gameinit(){ //随机数的种子 srand((unsigned int)time(NULL)); for (int i = 0; i < ROW; i++) { for ( int j = 0; j < COL; j++) { /* code */ map[i][j]=0; } } //布雷 -1表示雷 NUM表示雷数量 int n=0; while(n<NUM) { int r=rand()% ROW; int c=rand()% COL; if (map[r][c]==0)//如果前一个位置不是雷,那么使他为雷。 { map[r][c]=-1; n++; } }//while //根据雷的分布,填充不为雷的区域的数据 for (int i = 1; i <=ROW+1; i++) { for (int j = 1; j <COL+1; 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]++; n++; /* code */ } } } } } } } void gamedraw(){ //打印游戏区,而不是辅助区域 for (int i = 1; i <= ROW; i++) { for ( int j = 1; j <=COL; j++) { /* code */ printf("%3d",map[i][j]); } printf("\n"); /* code */ } } int main(){ gameinit(); gamedraw(); return 0; }
vscode和dev c++搞easyx环境比较麻烦 也是先放弃了
但是vs里面打代码没有提示,提示不舒服
第二版(图形化ok,简单加密ok)
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> //图片宽度 #define SIZE 50 #define ROW 10//行 #define COL 10//列 #define NUM 10//雷的个数 IMAGE img[12];//12张图片 int map[ROW + 2][COL + 2];//需要辅助区域,用来计算9宫格中雷的数量 void gameinit() { //随机数的种子 srand((unsigned int)time(NULL)); for (int i = 0; i < ROW+2; i++) { for (int j = 0; j < COL+2; j++) { /* code */ map[i][j] = 0; } } //布雷 -1表示雷 NUM表示雷数量 int n = 0; while (n < NUM) { int r = rand() % ROW; int c = rand() % COL; if (map[r][c] == 0)//如果前一个位置不是雷,那么使他为雷。 { map[r][c] = -1; n++; } }//while //根据雷的分布,填充不为雷的区域的数据 for (int i = 1; i <= ROW + 1; i++) { for (int j = 1; j < COL + 1; 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]++; n++; /* code */ } } } } } } //先进行加密 for (int i = 1;i < ROW + 1;i++) { for (int j = 1;j < COL + 1;j++) { map[i][j] += 20; } } } void Playgame() { //定义一个鼠标消息 MOUSEMSG msg = { 0 }; while (1) { msg = GetMouseMsg(); } } void gamedraw() { //打印游戏区,而不是辅助区域 for (int i = 1; i <= ROW; i++) { for (int j = 1; j <= COL; j++) { /* code */ printf("%3d", map[i][j]); if(map[i][j] == -1){ putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);//雷 } 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[10]);//空白图片 } else if (map[i][j] >30) { putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);//标记 } } printf("\n"); /* code */ } } int main() { initgraph(SIZE * ROW, SIZE* COL, SHOWCONSOLE); loadimage(&img[0], L"0.jpg", SIZE, SIZE); loadimage(&img[1], L"1.jpg", SIZE, SIZE); loadimage(&img[2], L"2.jpg", SIZE, SIZE); loadimage(&img[3], L"3.jpg", SIZE, SIZE); loadimage(&img[4], L"4.jpg", SIZE, SIZE); loadimage(&img[5], L"5.jpg", SIZE, SIZE); loadimage(&img[6], L"6.jpg", SIZE, SIZE); loadimage(&img[7], L"7.jpg", SIZE, SIZE); loadimage(&img[8], L"8.jpg", SIZE, SIZE); loadimage(&img[9], L"9.jpg", SIZE, SIZE);//雷 loadimage(&img[10], L"10.jpg", SIZE, SIZE);//空白 loadimage(&img[11], L"11.jpg", SIZE, SIZE);//标记 //之前取值范围为-1到8,加密(加20),得到19到28 为空白的对应 gameinit(); gamedraw(); while (1); return 0; }
第三版(完成左右键相应功能)
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> //图片宽度 #define SIZE 50 #define ROW 10//行 #define COL 10//列 #define NUM 10//雷的个数 IMAGE img[12];//12张图片 int map[ROW + 2][COL + 2];//需要辅助区域,用来计算9宫格中雷的数量 void gameinit() { //随机数的种子 srand((unsigned int)time(NULL)); for (int i = 0; i < ROW+2; i++) { for (int j = 0; j < COL+2; j++) { /* code */ map[i][j] = 0; } } //布雷 -1表示雷 NUM表示雷数量 int n = 0; while (n < NUM) { int r = rand() % ROW; int c = rand() % COL; if (map[r][c] == 0)//如果前一个位置不是雷,那么使他为雷。 { map[r][c] = -1; n++; } }//while //根据雷的分布,填充不为雷的区域的数据 for (int i = 1; i <= ROW + 1; i++) { for (int j = 1; j < COL + 1; 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]++; n++; /* code */ } } } } } } //先进行加密 for (int i = 1;i < ROW + 1;i++) { for (int j = 1;j < COL + 1;j++) { map[i][j] += 20; } } } int Playgame() { //定义一个鼠标消息 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) { map[r][c]-=20; } 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] += 50; } else if (map[r][c]>30) {//取消标记 map[r][c] -= 50; } return map[r][c]; break; } } } void gamedraw() { //打印游戏区,而不是辅助区域 for (int i = 1; i <= ROW; i++) { for (int j = 1; j <= COL; j++) { /* code */ printf("%3d", map[i][j]); if(map[i][j] == -1){ putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);//雷 } 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[10]);//空白图片 } else if (map[i][j] >30) { putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);//标记 } } printf("\n"); /* code */ } } int main() { initgraph(SIZE * ROW, SIZE* COL, SHOWCONSOLE); loadimage(&img[0], L"0.jpg", SIZE, SIZE); loadimage(&img[1], L"1.jpg", SIZE, SIZE); loadimage(&img[2], L"2.jpg", SIZE, SIZE); loadimage(&img[3], L"3.jpg", SIZE, SIZE); loadimage(&img[4], L"4.jpg", SIZE, SIZE); loadimage(&img[5], L"5.jpg", SIZE, SIZE); loadimage(&img[6], L"6.jpg", SIZE, SIZE); loadimage(&img[7], L"7.jpg", SIZE, SIZE); loadimage(&img[8], L"8.jpg", SIZE, SIZE); loadimage(&img[9], L"9.jpg", SIZE, SIZE);//雷 loadimage(&img[10], L"10.jpg", SIZE, SIZE);//空白 loadimage(&img[11], L"11.jpg", SIZE, SIZE);//标记 //之前取值范围为-1到8,加密(加20),得到19到28 为空白的对应 gameinit(); while (1) { gamedraw(); Playgame(); } return 0; }
第四版 (判断输赢)
#include<stdio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> //图片宽度 #define SIZE 50 #define ROW 10//行 #define COL 10//列 #define NUM 10//雷的个数 int count;//点开的数量 IMAGE img[12];//12张图片 int map[ROW + 2][COL + 2];//需要辅助区域,用来计算9宫格中雷的数量 void gameinit() { //随机数的种子 srand((unsigned int)time(NULL)); for (int i = 0; i < ROW+2; i++) { for (int j = 0; j < COL+2; j++) { /* code */ map[i][j] = 0; } } //布雷 -1表示雷 NUM表示雷数量 int n = 0; while (n < NUM) { int r = rand() % ROW; int c = rand() % COL; if (map[r][c] == 0)//如果前一个位置不是雷,那么使他为雷。 { map[r][c] = -1; n++; } }//while //根据雷的分布,填充不为雷的区域的数据 for (int i = 1; i <= ROW + 1; i++) { for (int j = 1; j < COL + 1; 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]++; n++; /* code */ } } } } } } //先进行加密 for (int i = 1;i < ROW + 1;i++) { for (int j = 1;j < COL + 1;j++) { map[i][j] += 20; } } } int Playgame() { //定义一个鼠标消息 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) { map[r][c]-=20; count++; } 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] += 50; } else if (map[r][c]>30) {//取消标记 map[r][c] -= 50; } return map[r][c]; break; } } } void gamedraw() { //打印游戏区,而不是辅助区域 for (int i = 1; i <= ROW; i++) { for (int j = 1; j <= COL; j++) { /* code */ printf("%3d", map[i][j]); if(map[i][j] == -1){ putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);//雷 } 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[10]);//空白图片 } else if (map[i][j] >30) { putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);//标记 } } printf("\n"); /* code */ } } int main() { HWND hwnd=initgraph(SIZE * ROW, SIZE* COL, SHOWCONSOLE); loadimage(&img[0], L"0.jpg", SIZE, SIZE); loadimage(&img[1], L"1.jpg", SIZE, SIZE); loadimage(&img[2], L"2.jpg", SIZE, SIZE); loadimage(&img[3], L"3.jpg", SIZE, SIZE); loadimage(&img[4], L"4.jpg", SIZE, SIZE); loadimage(&img[5], L"5.jpg", SIZE, SIZE); loadimage(&img[6], L"6.jpg", SIZE, SIZE); loadimage(&img[7], L"7.jpg", SIZE, SIZE); loadimage(&img[8], L"8.jpg", SIZE, SIZE); loadimage(&img[9], L"9.jpg", SIZE, SIZE);//雷 loadimage(&img[10], L"10.jpg", SIZE, SIZE);//空白 loadimage(&img[11], L"11.jpg", SIZE, SIZE);//标记 //之前取值范围为-1到8,加密(加20),得到19到28 为空白的对应 gameinit(); while (1) { gamedraw(); //输的时候 if (Playgame() == -1) { gamedraw(); MessageBox(hwnd, L"you are lost", L"", MB_OK); break; } //赢 if (ROW*COL-NUM==count) { gamedraw(); MessageBox(hwnd, L"you are win!", L"", MB_OK); break; } } return 0; }
第五版(递归成片打开 )
bug已找到,昨晚找了半小时没找出来,今天找了5min
else要和第一个最内层的if匹配
问题解决,睡觉。