C语言小游戏:简易版扫雷
2018-03-23 16:00
453 查看
我们小时候都在电脑上玩过一个游戏,就是扫雷,这么个游戏是怎么做出来的呢?
我们今天来实现一下这个小游戏。
首先是对这个游戏所需要的函数的声明与定义:
接下来就是代码的一一实现了:
欢迎大家共同讨论,如有错误及时联系作者指出,并改正。谢谢大家!
我们今天来实现一下这个小游戏。
首先是对这个游戏所需要的函数的声明与定义:
#pragma once #include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> #define rows 11 #define cols 11 #define Count 10 void menu();//菜单 void display(char show[rows][cols]); int Game(char mine[rows][cols],char show[rows][cols]);//游戏 void set_mine(char mine[rows][cols]);//设置雷 int Sweep(char mine[rows][cols], char show[rows][cols]);//开始扫雷 int get_num(char mine[rows][cols], int x, int y);//计算雷的个数
接下来就是代码的一一实现了:
#include "game.h" //菜单函数 void menu() { printf("********************************************\n"); printf("********************************************\n"); printf("*************welcome to saolei*************\n"); printf("************* 1. play *************\n"); printf("************* 0. exit *************\n"); printf("********************************************\n"); printf("********************************************\n"); } //设置雷的位置 void set_mine(char mine[rows][cols]) { int count = Count; int x = 0; int y = 0; srand((unsigned)time(NULL)); while (count) { x = rand() % 9 + 1; y = rand() % 9 + 1; if (mine[x][y] == '0') { mine[x][y] = '1'; count--; } } } //打印下棋完了显示的界面 void display(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("%d", i); for (j = 1; j < cols - 1; j++) { printf(" %c ", show[i][j]); } printf("\n"); } } //计算雷的个数 int get_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][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++; } return count; } //扫雷 int Sweep(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("请输入坐标:\n"); scanf("%d%d", &x, &y); if (mine[x][y] == '1') { printf("你踩到雷了!\n"); return 0; } else { int ret = get_num(mine, x, y); show[x][y] = ret + '0'; //set_mine(mine); display(show); count++; } } printf("恭喜你赢了!\n"); display(mine); return 0; } int Game(char mine[rows][cols],char show[rows][cols]) { set_mine(mine); display(show); //display(mine);//可以将雷的位置显示出来 Sweep(mine,show); return 0; } int main() { int input = 0; char mine[rows][cols]; char show[rows][cols]; int i = 0; int j = 0; for (i = 0; i < rows - 1; i++) { for (j = 0; j < cols - 1; j++) { mine[i][j] = '0'; show[i][j] = '*'; } } menu(); while (1) { printf("请选择:"); scanf("%d", &input); if (input == 1) { printf("进入游戏\n"); Game(mine,show); break; } else if (input == 0) { printf("退出游戏!\n"); exit(0); break; } else { printf("输入有误!\n"); } } return 0; }
欢迎大家共同讨论,如有错误及时联系作者指出,并改正。谢谢大家!
相关文章推荐
- C语言开发简易版扫雷小游戏
- C语言实现简易2048小游戏
- C语言小游戏---扫雷
- C语言实现小游戏--扫雷
- well,c语言简易扫雷
- 【C语言】简易扫雷游戏——C语言实现
- [置顶] 小游戏:扫雷 (C语言实现扫雷的基本功能)
- c语言小游戏---扫雷
- 如何用C语言写出简易的扫雷程序
- C语言小游戏“扫雷”
- C语言简易版小游戏【flappy bird】
- C语言小游戏_扫雷
- C语言实现简易版扫雷游戏
- C语言实现简易版扫雷
- c语言实现小游戏-扫雷
- 小项目 : C语言实现扫雷小游戏
- 简易小游戏扫雷——C语言编程实现
- c语言实现扫雷(简易版,控制台)
- C++小游戏扫雷——如何写出一个简易的扫雷
- 【扫雷】编写一个小游戏--扫雷的c语言实现