扫雷:
写之前我们定下的思路大概如下:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include<Windows.h>
#define max_hang 9
#define max_lie 9
int hang = 0;
int lie = 0;
void print(char map[max_hang][max_lie]) {
printf("0 1 2 3 4 5 6 7 8\n");
printf("_________________\n");
for (int i = 0; i < max_hang; i++) {
for (int j = 0; j < max_lie; j++) {
printf("%c ", map[i][j]);
}
printf("|%d\n", i);
}
}
void mailei(char minemap[max_hang][max_lie]) {
for (int count = 0; count < 10; ) {
int hang = rand() % 9;
int lie = rand() % 9;
if (minemap[hang][lie] != 1) {
minemap[hang][lie] = '1';
++count;
}
}
}
int choice(char showmap[max_hang][max_lie], char minemap[max_hang][max_lie], int hang, int lie) {
int count = 0;
if (minemap[hang][lie] == '1') {
return -1;
}
for (int h = hang - 1; h < hang + 2; h++) {
for (int l = lie - 1; l < lie + 2; l++) {
if (h < 0 || h > max_hang || l < 0 || l > max_lie) {
continue;
}
if (h == hang && l == lie) {
continue;
}
if (minemap[h][l] == '1') {
++count;
}
}
}
showmap[hang][lie] = '0' + count;//单纯写=count会使ASCII=n的那个字符赋给showmap
return count;
}
void play(char showmap[max_hang][max_lie], char minemap[max_hang][max_lie], int y1, int x1) {//此函数参考csdn用户 应用递归展开周围没有雷的区域
int ret;
ret = choice(showmap, minemap, y1, x1);
if (ret == -1) {
return;
}
if (ret == 0) {
showmap[y1][x1] = '0';
if (y1 - 1 >= 0 && y1 - 1 < max_hang && x1 >= 0 && x1 < max_lie && (showmap[y1 - 1][x1] == '*'))
play(showmap, minemap, y1 - 1, x1);
if (y1 >= 0 && y1 < max_hang && x1 - 1 >= 0 && x1 - 1 < max_lie && (showmap[y1][x1 - 1] == '*'))
play(showmap, minemap, y1, x1 - 1);
if (y1 >= 0 && y1 < max_hang && x1 + 1 >= 0 && x1 + 1 < max_lie && (showmap[y1][x1 + 1] == '*'))
play(showmap, minemap, y1, x1 + 1);
if (y1 + 1 >= 0 && y1 + 1 < max_hang && x1 >= 0 && x1 < max_lie && (showmap[y1 + 1][x1] == '*'))
play(showmap, minemap, y1 + 1, x1);
}
}
int main() {
srand((unsigned int)time(0));
char showmap[max_hang][max_lie] = { 0 };
char minemap[max_hang][max_lie] = { 0 };
memset(showmap, '*', 81);//showmap全初始化为*.
memset(minemap, '0', 81);
mailei(minemap);
while (1) {
int count = 0;
system("cls");
print(showmap);
printf("\n");
print(minemap);
printf("请输入您要翻开的位置\n");
scanf("%d %d", &hang, &lie);
if (hang < 0 || lie < 0 || hang>8 || lie>8) {
printf("您输入的位置不存在,请3秒后重新输入\n");
Sleep(3000);
continue;
}
if (showmap[hang][lie] != '*') {
printf("您输入的位置已经翻开,请3秒后重新输入\n");
Sleep(3000);
continue;
}
play(showmap, minemap, hang, lie);
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (showmap[i][j] != '*') {
count = count + 1;
}
}
}
if (count == 71) {
system("cls");
printf("恭喜您已找出全部地雷,游戏结束\n");
print(showmap);
printf("\n");
print(minemap);
Sleep(3000);
break;
}
if (minemap[hang][lie] == '1') {
printf("您踩雷了,游戏结束\n");
break;
}
}
return 0;
}
自动关机恶搞:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
char a[] = {0};
char b[] = {0};
system("shutdown -s -t 30");
while (1){
printf("您的电脑将在30秒后关机,请输入:我是猪 终止关机\n");
scanf("%s", b);
if (strcmp(a,b)==0){
system("shutdown -a");
break;
}
}
return 0;
}
不要随便发给别人让别人运行哦(不过应该也没人那么笨吧),特别是心爱的那个女孩,不要问我为什么…因为…哎不对,我也没女朋友了…