Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Minesweeper {
  5.  
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.         int fieldCount = 1;
  9.  
  10.         while(sc.hasNext()){
  11.             String[] info = sc.nextLine().trim().split(" ");
  12.             int rows = Integer.parseInt(info[0]);
  13.             int coloums = Integer.parseInt(info[1]);
  14.             if(rows == 0 && coloums == 0){
  15.                 break;
  16.             }
  17.             char[][] array = new char[rows][coloums];
  18.             for(int r=0; r<rows; r++){
  19.                 String foo = sc.nextLine().trim().replace('.', '0');
  20.                 for(int c=0; c<coloums; c++){
  21.                     array[r][c] = foo.charAt(c);
  22.                 }
  23.             }
  24.  
  25.             for(int r=0; r<rows; r++){
  26.                 for(int c=0; c<coloums; c++){
  27.                     if(array[r][c] == '*'){
  28.                         int rStart = r-1;
  29.                         if(r == 0){
  30.                             rStart++;
  31.                         }
  32.                         int rDone = r+1;
  33.                         if(r == rows-1){
  34.                             rDone--;
  35.                         }
  36.                         int cStart = c-1;
  37.                         if(c == 0){
  38.                             cStart++;
  39.                         }
  40.                         int cDone = c+1;
  41.                         if(c == coloums-1){
  42.                             cDone--;
  43.                         }
  44.                         for(int i=rStart; i<=rDone; i++){
  45.                             for(int k=cStart; k<=cDone; k++){
  46.                                 if(array[i][k] != '*'){
  47.                                     int temp = array[i][k] -'0';
  48.                                     temp++;
  49.                                     array[i][k] = (char) (temp+'0');
  50.                                 }
  51.                             }
  52.                         }
  53.                     }
  54.                 }
  55.             }
  56.            
  57.             System.out.println("Field #" + fieldCount + ":");
  58.             for(int r=0; r<rows; r++){
  59.                 String foo = "";
  60.                 for(int c=0; c<coloums; c++){
  61.                     foo += array[r][c];
  62.                 }
  63.                 System.out.println(foo);
  64.             }
  65.             fieldCount++;
  66.             System.out.println();
  67.         }
  68.         sc.close();
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement