第一个让我觉得有点小激动的扫雷程序

刚刚学完一个扫雷程序,虽然写的有点挫(好像很挫难过),但是还是想贴出来看看   激励自己


等写完三子棋或者五子棋,也要发出来害羞


1.game.h

#ifndef __GAME_H__
#define __GAME_H__

#define ROWS 10
#define COLS 10
#define DEFAULT_COUNT 20
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

void init_board(char arr[ROWS + 2][COLS + 2], int row, int col, char ch);
void display(char arr[ROWS + 2][COLS + 2], int row, int col);
void set_mine(char arr[ROWS + 2][COLS + 2], int row, int col);
void sweep(char mine[ROWS+2][COLS+2], char show[ROWS+2][COLS+2]);
int get_mine(char mine[ROWS+2][COLS+2], int x, int y);


#endif //__GAME_H__

2.test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "game.h"

void menu()
{
	printf("*******************************\n");
	printf("******  1.play   0.exit  ******\n");
	printf("*******************************\n");
}
void game()
{
	char mine[ROWS + 2][COLS + 2] = { 0 };
	char show[ROWS + 2][COLS + 2] = { 0 };
	init_board(mine, ROWS + 2, COLS + 2, '0');
	init_board(show, ROWS + 2, COLS + 2, '*');
	//display(mine, ROWS + 2, COLS + 2);
	display(show, ROWS + 2, COLS + 2);
	set_mine(mine, ROWS + 2, COLS + 2);
	//display(mine, ROWS + 2, COLS + 2);
	sweep(mine, show);
}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			break;
		default:
			printf("选择错误,请重新选择:<\n");
			break;
		}
	} while (input);
	system("pause\n");
	return 0;
}


3.game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void init_board(char arr[ROWS + 2][COLS + 2], int row, int col,char ch)
{
	//int i = 0;
	//int j = 0;
	//for (i = 1; i < row; i++)
	//{
	//	for (j = 1; j < col; j++)
	//	{
	//		arr[i][j] = '0';
	//	}
	//}
	memset(arr, ch, row*col);
}
int get_mine_num(int x, int y)
{
	return rand() % (y - x + 1) + x;
}
void set_mine(char arr[ROWS + 2][COLS + 2], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = DEFAULT_COUNT;
	srand((unsigned)time(NULL));
	while (count)
	{
		x = get_mine_num(1, 10);
		y = get_mine_num(1, 10);
		if (arr[x][y] != '1')
		{
			arr[x][y] = '1';
			count--;
		}

	}

}
void display(char arr[ROWS + 2][COLS + 2], int row, int col)
{
	int i = 0;
	int j = 0;
	printf("   ");
	for (i = 0; i < col - 2; i++)
	{
		printf("%d ", i + 1);
	}
	printf("\n");

	for (i = 0; i < row - 2; i++)
	{
		printf("%2d ", i + 1);
		for (j = 0; j < col - 2; j++)
		{
			printf("%c ", arr[i + 1][j + 1]);
		}
		printf("\n");
	}	
}
void sweep(char mine[ROWS + 2][COLS + 2], char show[ROWS + 2][COLS + 2])
{
	int x = 0;
	int y = 0;
	int count = 0;
	while (count != (ROWS*COLS - DEFAULT_COUNT))
	{
		printf("请输入坐标:>\n");
		scanf("%d %d", &x, &y);
		if (mine[x][y] == '1')
		{
			printf("哎呀,踩雷了:<\n");
			break;
		}
		else
		{
			int ret = get_mine(mine, x, y);
			show[x][y] = ret + '0';
			display(show,ROWS+2,COLS+2);
			//count++;
		}
	}
	if (count == ROWS*COLS - DEFAULT_COUNT)
	printf("恭喜你,你赢了!\n");
	display(mine, ROWS + 2, COLS + 2);
}
int get_mine(char mine[ROWS + 2][COLS + 2], int x, int y)
{
	return (mine[x + 1][y + 1] - '0') +
		   (mine[x + 1][y - 1] - '0') +
		   (mine[x - 1][y + 1] - '0') +
		   (mine[x - 1][y - 1] - '0') +
		   (mine[x + 1][y] - '0') +
		   (mine[x - 1][y] - '0') +
		   (mine[x][y + 1] - '0') +
		   (mine[x][y - 1] - '0');
	/*int count = 0;
	if (mine[x + 1][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 - 1][y - 1] == '1')count++;
	if (mine[x - 1][y] == '1')count++;

	if (mine[x][y + 1] == '1')count++;
	if (mine[x][y - 1] == '1')count++;

	return count;*/
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1扫雷小游戏1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值