minesweeper square

Input
The first line contains 2 integers (should be separated by a comma) cols and rows which represent the number of lines and columns of the field, respectively. Each of the next rows lines contains exactly cols character '.' or '*' with spaces, representing the field. '.' for safe squares and '*' for mine squares, both without a quote.

Output
'.' characters if any, replaced by the number of mines ('*') adjacent to that cell.

minesweeper game output

minesweeper in c

#include<stdio.h>

int main(){
	
	//declaration
int i; int j; int cols; //number of columns
int rows; //number of rows

int r; int ctr; //counter
printf("\n <cols,rows>: "); scanf("%i,%i", &cols, &rows); scanf("%*c"); char mine[cols][rows]; //input
for(i = 0; i < rows; i++){ printf(" <==\t"); for(j = 0; j < cols; j++) scanf("%c%*c", &mine[i][j]); } //count adjacent mine(s)
for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ ctr = 0; if( mine[i][j] != '*' ){ //check directly above cell
if ((i-1)>= 0 && mine[i-1][j] == '*') ctr ++; //check directly below cell
if ((i+1)< rows && mine[i+1][j] == '*') ctr ++; //check left column
r = j - 1; if(r >= 0){ if(mine[i][r] == '*') ctr ++; if ((i-1) >= 0 && mine[i-1][r] == '*') ctr ++; if ((i+1)< rows && mine[i+1][r] == '*') ctr ++; } //check right column
r = j + 1; if(r < cols){ if(mine[i][r] == '*') ctr ++; if ((i-1) >= 0 && mine[i-1][r] == '*') ctr ++; if ((i+1)< rows && mine[i+1][r] == '*') ctr ++; } mine[i][j] = ctr; } } } //output
for(i = 0; i < rows; i++){ printf("\n ==>\t"); for(j = 0; j < cols; j++){ if (mine[i][j] == 42) printf("%c ", mine[i][j]); else printf("%i ", mine[i][j]); } } printf("\n\n"); return 0; }

 

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.