扫雷
2017-10-29 15:26
113 查看
//game.h
//test.c
//game.c
#ifndef __GAME_H__ #define __GAME_H__ #include<stdio.h> #include<stdlib.h> #include<time.h> #define ROWS 12 #define COLS 12 #define COUNT 10 void Init_Board(char mine[ROWS][COLS], char show[ROWS][COLS]); void Set_Mine(char mine[ROWS][COLS]); void Display_Board(char show[ROWS][COLS]); int Get_Mine_Num(char mine[ROWS][COLS], int x, int y); void Check_Win(char mine[ROWS][COLS], char show[ROWS][COLS]); #endif //__GAME_H__
//test.c
#define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" #include<time.h> void menu() { printf("***********************************\n"); printf("**** 1.play 0.exit ****\n"); printf("***********************************\n"); } void game() { char mine[ROWS][COLS]; char show[ROWS][COLS]; Init_Board(mine, show); Set_Mine(mine); Display_Board(show); Get_Mine_Num(mine, ROWS, COLS); Check_Win(mine, show); } int main() { int input = 0; srand((unsigned int)time(NULL)); do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: break; default: printf("选择错误,请重新输入\n"); break; } } while (input); return 0; }
//game.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" void Init_Board(char mine[ROWS][COLS], char show[ROWS][COLS]) { int i = 0; int j = 0; for (i = 1; i < ROWS - 1; i++) { for (j = 1; j < COLS - 1; j++) { mine[i][j] = '0'; } } for (i = 1; i < ROWS - 1; i++) { for (j = 1; j < COLS - 1; j++) { show[i][j] = '*'; } } } void Set_Mine(char mine[ROWS][COLS]) { int i = 0; int j = 0; int ret = COUNT; while (ret) { i = rand() % ROWS - 1; j = rand() % COLS - 1; if (mine[i][j] == '0') { mine[i][j] = '1'; ret--; } } } void Display_Board(char show[ROWS][COLS]) { int i = 0; int j = 0; printf(" "); for (i = 1; i < COLS - 1; i++) { printf(" %d ", i); } printf("\n"); for (i = 1; i < ROWS - 1; i++) { printf("%2d", i); for (j = 1; j < COLS - 1; j++) { printf(" %c ", show[i][j]); } printf("\n"); } } int Get_Mine_Num(char mine[ROWS][COLS], int x, int y) { int count = 0; if (mine[x - 1][y - 1] == '1') count++; if (mine[x - 1][y] == '1') count++; if (mine[x - 1][y + 1] == '1') count++; if (mine[x][y + 1] == '1') count++; if (mine[x + 1][y + 1] == '1') count++; if (mine[x + 1][y] == '1') count++; if (mine[x + 1][y - 1] == '1') count++; if (mine[x][y - 1] == '1') count++; return count; } void Check_Win(char mine[ROWS][COLS], char show[ROWS][COLS]) { int count = 0; int x = 0; int y = 0; while (count != ((ROWS - 2)*(COLS - 2) - COUNT)) { printf("请输入坐标>:"); scanf("%d%d", &x, &y); if (mine[x][y] == '1') { printf("踩雷了!\n"); Display_Board(mine); return; } else { int ret = Get_Mine_Num(mine, x, y); show[x][y] = ret + '0'; Display_Board(show); count++; } } printf("恭喜你赢了!\n"); Display_Board(mine); }
相关文章推荐
- J2ME-MIDP1.0游戏完整实现-双人扫雷1.0(一)
- MIDP1.0游戏完整实现-双人扫雷1.0(二)
- 我计划工作之外的时间重写微软的经典游戏扫雷,改成网络版,有谁愿参与其中?
- javascript游戏--扫雷 源代码
- 扫雷游戏汇编分析手记(一)
- 扫雷
- 扫雷的逆向思路
- “不死鸟”号历险记---和扫雷过不去篇(无厘头版)
- 扫雷程序思想
- .net下实现鼠标左右两个按键齐击,就好像扫雷下面的效果
- JS + PHP 扫雷
- 模仿自制的扫雷
- 2013 扫雷英雄榜
- 扫雷的启示
- C语言扫雷程序
- 扫雷程序类-指针溢出错误
- UVA——10189(keng die a)扫雷
- 扫雷__重绘优化与数据持久化
- js 自制扫雷
- 小游戏系列算法之四扫雷游戏核心算法,非递归无雷区自动翻开