All pastes #523241 Raw Edit

blablabla

public text v1 · immutable
#523241 ·published 2007-05-30 18:39 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 9
#define COL 9
#define MINES 10

void Display(char dis[][COL]);
void randMines(char prim[][COL]);
char getLetter(int flagCounter);
int getRowCol();
void open(char primary[][COL],char display[][COL],int m, int n);
void checkEnd( char primary[][COL], char display[][COL]);


int main()
{
	int flagCounter=0;
	char primary[ROW][COL];
	char display[ROW][COL];
	int i;
	int j;
	char inputCh;
	int inputRow;
	int inputCol;
	srand((int)time(0));


	for(i=0;i<ROW;i++)
	{
		for(j=0;j<COL;j++)
		{
			display[i][j]='.';
			primary[i][j]=' ';
		}
	}
	Display(display);
	randMines(primary);

	inputCh=getLetter(flagCounter);



	while(inputCh!='f')
	{
		printf("enter the row number");
		inputRow=getRowCol();
		printf("enter the col number");
		inputCol=getRowCol();

		if(inputCh=='m')
		{
			display[inputRow-1][inputCol-1]='*';
			flagCounter++;
		}
		if(inputCh=='c')
		{
			display[inputRow-1][inputCol-1]='.';
			flagCounter--;
		}
		if(inputCh=='o')
		{
			open(primary,display, inputRow, inputCol);
		}

		Display(display);
		getLetter(flagCounter);
	}


	checkEnd(primary, display);

	return 0;



}

void Display(char display[][COL])
{
	int i;
	int j;

	for( i=0; i<9; i++)
		{
			if(i==0)
			{
				printf("  ");
			}
			printf(" %d", i+1);
			if(i==8)
			{
				printf("\n");
			}
		}
		for(i=0;i<9;i++)
		{
			if(i==0)
			{
				printf("  ");
			}
			printf(" -");
			if(i==8)
			{
				printf("\n");
			}
		}
		
		for(i=0;i<ROW;i++)
		{
			printf("%d|",i+1);
			for(j=0;j<COL;j++)
			{
			
				printf(" %c", display[i][j]);
			}
			printf("\n");
		}

		return;
}

void randMines(char prim[][COL])
{
	int m;
	int n;
	int i=0;

	m=rand()%ROW;
	n=rand()%COL;

	prim[m][n]='X';
	while (i<MINES)
	{
		m=rand()%9+0;
		n=rand()%9+0;
		if (prim[m][n]!='X')
		{
			prim[m][n]='X';
			i++;
		}
	}
	return;
}

char getLetter(int flagCounter)
{
	char ch;

	printf("type m to mark a mine\n");
	printf("type o to open a square\n");
	printf("type c to delete a mark of a mine\n");
	printf("type f to finish\n");

	if(scanf("%c", &ch)!=1){
		printf("input error\n");
		exit(1);
	}

	while((ch!='m')&&(ch!='o')&&(ch!='c')&&(ch!='f'))
	{
		printf("your input is wwrong enter again\n");
		
		if(scanf("%c", &ch)!=1){
			printf("input error\n");
			exit(1);
		}
	}

	if((ch=='f')&&(flagCounter!=10)){

		printf("you dont have 10 mine flags in order to finish enter again\n");

		if(scanf("%c", &ch)!=1){
			printf("input error\n");
			exit(1);
		}
		while((ch!='m')&&(ch!='o')&&(ch!='c'))
		{
			if(ch=='f')
			{
				printf("you cant finish unless you marked 10 mines\n");
			}
			else
			{
				printf("wrong input enter again\n");
				
			}
			if(scanf("%c", &ch)!=1){
					printf("input error\n");
					exit(1);
			}
		}
	}
				

	
	return ch;
}

int getRowCol()
{
	int num;

	if (scanf("%d", &num)!=1){
		printf("input error\n");
		exit(1);
	}

	while((num<0)||(num>9))
	{
		printf("wrong input, retype\n");
		if(scanf("%d", &num)!=1){
			printf("input error\n");
			exit(1);
		}
		
	}

	return num;
}

void open(char primary[][COL],char display[][COL],int m, int n)
{
	int count=0;
	static int mone=0;
	if((primary[m-1][n-1]=='X')&&(mone==0)){
		printf("you lose\n the field was:\n");
		Display(primary);
		exit(1);
	}
	mone++;
	if((m-2>=0)&&(n-2>=0))
	{
		if(primary[m-2][n-2]=='X'){
			count++;
		}
	}
	if((m-2>=0)&&(n-1>=0))
	{
		if(primary[m-2][n-1]=='X'){
			count++;
		}
	}
	if((m-2>=0)&&(n>=0))
	{
		if(primary[m-2][n]=='X'){
			count++;
		}
	}
	if((m-1>=0)&&(n-2>=0))
	{
		if(primary[m-1][n-2]=='X'){
			count++;
		}
	}
	if((m-1>=0)&&(n>=0))
	{
		if(primary[m-1][n]=='X'){
			count++;
		}
	}
	if((m>=0)&&(n-2>=0))
	{
		if(primary[m][n-2]=='X'){
			count++;
		}
	}
	if((m>=0)&&(n-1>=0))
	{
		if(primary[m][n-1]=='X'){
			count++;
		}
	}
	if((m>=0)&&(n>=0))
	{
		if(primary[m][n]=='X'){
			count++;
		}
	}

	if(count==0)
	{
		if((m-2>=0)&&(n-2>=0)){
		open(primary, display,m-2,n-2);
		}
		if((m-2>=0)&&(n-1>=0)){
		open(primary, display,m-2,n-1);
		}
		if((m-2>=0)&&(n>=0)){
		open(primary, display, m-2,n);
		}
		if((m-1>=0)&&(n-2>=0)){
		open(primary, display,m-1,n-2);
		}
		if((m-1>=0)&&(n>=0)){
		open(primary,display, m-1,n);
		}
		if((m>=0)&&(n-2>=0)){
		open(primary, display,m,n-2);
		}
		if((m>=0)&&(n-1>=0)){
		open(primary, display,m,n-1);
		}
		if((m>=0)&&(n>=0)){
		open(primary, display,m,n);
		}
	}
	else
	{
		display[m-1][n-1]=((char)(count+'0'));
	}

	return;
}

void checkEnd( char primary[][COL], char display[][COL])
{
	int i;
	int j;
	int count=0;

	for(i=0; i<ROW; i++)
	{
		for(j=0; j<COL; j++)
		{
			if(primary[i][j]=='X')
			{
				if(display[i][j]=='*'){
					count++;
				}
			}
		}
	}

	if (count==10){
		printf("congredulations!!! you won\n");
		Display(display);
		exit(1);
	}
	printf("you lose\n");
	Display(display);
	exit(1);

	return;
}