All pastes #523451 Raw Edit

Anonymous

public text v1 · immutable
#523451 ·published 2007-05-30 20:02 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 mone=0;


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')
		{
			if(primary[inputRow][inoutCol]=='X')
			{
				printf("you lose\n");
				Display(display);
			}
			else{
			open(primary,display, inputRow-1, inputCol-1);
			}
		}

		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;
        int i, j;
 
        for (i = m-1; i <= m+1; i++)
        {
                for (j = n-1; j <= j+1; j++)
                {
                        if (i >= 0 && i <ROW && j >= 0 && j < COL)
                        {
                                if (!(i==m && j==n) && primary[i][j] == 'X'){
                                        count++;
                                }
                        }
                }
        }
 
        display[m][n] = count + '0';
 
        if (count > 0)
        {
                for (i = m-1; i <= m+1; i++)
                {
                        for (j = n-1; j <= n+1; j++)
                        {
                                if (i >= 0 && i < ROW && j >= 0 && j < COL)
                                {
                                        if (!(i==m && j==n) && !(display[i][j] >= '0' && display[i][j] < '9')){
                                                open(primary, display, i, j);
                                        } 
								}
						}
				}
		}
		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;
}