码迷,mamicode.com
首页 > Windows程序 > 详细

C language 模拟 win的经典游戏——扫雷

时间:2014-06-25 19:37:46      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:c语言   扫雷   游戏   terminal   

让我们在terminal下愉快的...扫雷


昨天跟奇葩霖聊起“雷区”这个敏感词汇,然后很荣幸的。。。应该轰炸不到我。。。

后来百无聊赖的去玩了把扫雷,然后发现我之前都是乱扫的,没有任何技巧。百科之后才发现,扫雷是有技巧的,接着玩了一把,咦挺有意思的。。。大概感受了一下,今天又要考数电,昨晚写了个框架,就到两点半了。。。睡。。。


今天中午回来,第一件事就是接着写。。。简直是爽。。。


这家伙,原来昨天之前。。。我一直不会玩。。。如果还是跟我一样不会玩的。。我也懒得介绍规则了。。。自行google吧。。。this blog的目的不是推广扫雷。。。


bubuko.com,布布扣



bubuko.com,布布扣



/******************************************************************
Code writer : EOF
code file : mine_sweep.c
code date : 2014.06.23
e-mail: jasonleaster@gmail.com

Code purpose :
	This code is just a simulation of mine sweeping...
	If there are some where wrong with my code, just touch me by e-mail.
Thank you.

	Have fun!

********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MATRIX_SIZE 4   //demention of square matrix
#define BUFFSIZE 1024	//buffer size

#define NUM_MINE 4		//number of mine

#define IS_BOMB  1      //MACRO for determining current location has a bomb
#define NOT_BOMB 0

#define GAMEOVER 0
#define GAMING   1

struct battle
{
	/*
	char current_battlefiled is used for descripting current state of battlefield
	*/
	char current_battlefiled[MATRIX_SIZE][MATRIX_SIZE];

	/*
	char answer is used for determining that location has a bomb or not.
	*/
	int  answer[MATRIX_SIZE][MATRIX_SIZE];

	/*
	char num_bomb is used for determing that how many bombs around the current location.
	*/
	int  num_bomb[MATRIX_SIZE][MATRIX_SIZE];
};


void print_current_battle(struct battle *p_battlefiled);

struct battle initialize_battle(void);

void do_open(struct battle *p_battlefiled,int row,int rank);
void do_mine(struct battle *p_battlefiled,int row,int rank);
void do_unknow(struct battle *p_battlefiled,int row,int rank);

void do_operation(struct battle *p_battlefiled,int row, int rank);

void count_bomb_around(struct battle *p_battlefiled);

int Game = GAMING;
//In fact, I try my best to avoid to use global varible in my code....
//but I think it is ok here.You know I can't accept that long long parameter list.
int Open_safely_times = 0;

int main()
{

	int row  = 0;
	int rank = 0;
	
	struct battle new_york;

	new_york = initialize_battle();

	print_current_battle(&new_york);//Firstly, show player the initial battlefiled.

	printf("Hey guys, please input two numbers that represent \nthe number of row and rank of the location in the battlefiled\n");

	while(!scanf("%d",&row) ||row >= MATRIX_SIZE)
	{
		printf("input row error\nplease input again\n");
		while(getchar() != '\n')
		{

		}
	}

	while(!scanf("%d",&rank) || rank >= MATRIX_SIZE)
	{
		printf("input rank error\nplease input again\n");
		while(getchar() != '\n')
		{

		}
	}

	printf("Remember that there are only three operation could be done.\n");
	printf("If you want to open the current location, just input command \"open\"\n");
	printf("If you want to sign the current location as mine, just input command \"mine\"\n");
	printf("If you want to sign the current location as question mark, just input command \"unknow\"\n");

	printf("Never forget that!\n");
	
	while(Game == GAMING && Open_safely_times < MATRIX_SIZE*MATRIX_SIZE-NUM_MINE)
	{	

		do_operation(&new_york,row,rank);

		if(Game == GAMEOVER)
		{
			break;
		}

		printf("Again! Input two numbers that represent \nthe number of row and rank of the location in the battle\n");

		while(!scanf("%d",&row) ||row >= MATRIX_SIZE)
		{
			printf("input row error\nplease input again\n");
			while(getchar() != '\n')
			{

			}
		}

		while(!scanf("%d",&rank) || rank >= MATRIX_SIZE )
		{
			printf("input rank error\nplease input again\n");
			while(getchar() != '\n')
			{
	
			}
		}
	}

	return 0;
}

void count_bomb_around(struct battle *p_battlefiled)
{
	//This function used in "struct battle initialize_battle(void)" for counting how many bombs around.
	int row = 0;
	int rank = 0;

	for(row = 0; row < MATRIX_SIZE; row++)
	{
		for(rank = 0;rank < MATRIX_SIZE;rank++)
		{
			if(row-1>=0 && rank -1 >= 0 && p_battlefiled->answer[row-1][rank-1] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}

			if(row-1>=0 && rank  >= 0 && p_battlefiled->answer[row-1][rank] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}

			if(row >=0 && rank -1 >= 0 && p_battlefiled->answer[row][rank-1] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}

			if(row+1 <= MATRIX_SIZE-1 && rank +1 <= MATRIX_SIZE-1 && p_battlefiled->answer[row+1][rank+1] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}

			if(row+1 <= MATRIX_SIZE-1 && rank <= MATRIX_SIZE-1 && p_battlefiled->answer[row+1][rank] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}
			
			if(row <= MATRIX_SIZE-1 && rank +1 <= MATRIX_SIZE-1 && p_battlefiled->answer[row][rank+1] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}

			if(row+1 <= MATRIX_SIZE-1 && rank -1 >= 0 && p_battlefiled->answer[row+1][rank-1] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}

			if(row-1 >= 0 && rank+1 <= MATRIX_SIZE && p_battlefiled->answer[row-1][rank+1] == IS_BOMB)
			{
				(p_battlefiled->num_bomb[row][rank])++;
			}
		}
	}
}

void do_open(struct battle *p_battlefiled,int row,int rank)
{

	if(p_battlefiled == NULL)
	{
		printf("parameter error in do_open\n");
		return ;
	}	

	if(p_battlefiled->answer[row][rank] == IS_BOMB)
	{
		printf("BOMB~~!!!\n");
		for(row = 0; row < MATRIX_SIZE; row++)
		{
			for(rank = 0;rank < MATRIX_SIZE;rank++)
			{
				if(p_battlefiled->answer[row][rank] == IS_BOMB)
				{
					p_battlefiled->current_battlefiled[row][rank] = '@';
				}
			}
		}

		//game over...
		Game = GAMEOVER;
	}
	else if(p_battlefiled->answer[row][rank] == NOT_BOMB)
	{
		p_battlefiled->current_battlefiled[row][rank] = ((char)p_battlefiled->num_bomb[row][rank]) + '0';

		//Lucky, not bomb
		Open_safely_times++;
	}
}

void do_mine(struct battle *p_battlefiled,int row,int rank)
{
	if(p_battlefiled == NULL)
	{
		printf("parameter error in do_open\n");
		return ;
	}	
	
	p_battlefiled->current_battlefiled[row][rank] = '!';
}

void do_unknow(struct battle *p_battlefiled,int row,int rank)
{
	
	if(p_battlefiled == NULL)
	{
		printf("parameter error in do_open\n");
		return ;
	}
	
	p_battlefiled->current_battlefiled[row][rank] = '?';
}

void do_operation(struct battle *p_battlefiled,int row, int rank)
{
	char operation[BUFFSIZE] = {0};

	printf("Which operation would you want to do?\n");
	
read_operation:
	scanf("%s",operation);//Obevious,It's unsafe that there may be a overflow,Just a game never mind it...
	
	if(strcmp(operation,"open") == 0)
	{
		do_open(p_battlefiled,row,rank);

		print_current_battle(p_battlefiled);
	}
	else if(strcmp(operation,"mine") == 0)
	{
		do_mine(p_battlefiled,row,rank);

		print_current_battle(p_battlefiled);
	}
	else if(strcmp(operation,"unknow") == 0)
	{
		do_unknow(p_battlefiled,row,rank);

		print_current_battle(p_battlefiled);
	}
	else
	{
		printf("operation error\nAttention!...Just input one of \"open\" \"mine\" or \"unknow\"\n");

		goto read_operation;	
	}

}

struct battle initialize_battle(void)
{
	struct battle s_battle;

	int row = 0;
	int rank = 0;
	int temp = 0;

	for(rank = 0; rank < MATRIX_SIZE; rank++)
	{
		for(row = 0;row < MATRIX_SIZE;row++)
		{
			s_battle.current_battlefiled[row][rank] = '*';
			s_battle.answer[row][rank] = NOT_BOMB;
			s_battle.num_bomb[row][rank] = 0;
		}
	}
	
	//Maybe, it is time consuming...But it do work correctly.
	for(temp = 0; temp < NUM_MINE; temp++)
	{
		row  = (rand() % MATRIX_SIZE);
		rank = (rand() % MATRIX_SIZE);

		if(s_battle.answer[row][rank] == NOT_BOMB)
		{
			s_battle.answer[row][rank] = IS_BOMB;
		}
		else
		{
			temp--;
		}
	}
	
	count_bomb_around(&s_battle);

	return s_battle;
}

void print_current_battle(struct battle *p_battlefiled)
{
	int row = 0;
	int rank = 0;
	for(row = 0; row < MATRIX_SIZE; row++)
	{
		for(rank = 0;rank < MATRIX_SIZE;rank++)
		{
			printf("%c ",p_battlefiled->current_battlefiled[row][rank]);
		}
		printf("\n");
	}
}










bubuko.com,布布扣

C language 模拟 win的经典游戏——扫雷,布布扣,bubuko.com

C language 模拟 win的经典游戏——扫雷

标签:c语言   扫雷   游戏   terminal   

原文地址:http://blog.csdn.net/cinmyheart/article/details/33737309

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!