AlgorithmQuiz/MineSweeper/angss

   1 import java.io.BufferedReader;
   2 import java.io.IOException;
   3 import java.io.InputStreamReader;
   4 
   5 /**
   6  * Excercise Logic skill
   7  * @since 2006.5.3
   8  * @author angss
   9  * TODO 키보드에서 받은 값의 크기만큼의 지뢰밭을 표시한다. N*M크기.
  10  *      o: 지뢰없는 밭.
  11  *      *: 지뢰.
  12  *
  13  */
  14 public class MinesweeperTest2
  15 {
  16     /**
  17      * @param args
  18      * @throws IOException
  19      */
  20     public static void main(String[] args) throws IOException
  21     {
  22         String sin = null;     
  23         char ch = 0;
  24         char[][] cfinal = new char[10][10];
  25         String[] ct = null;
  26         char count = '0';
  27        
  28         BufferedReader stdin1 = new BufferedReader(new InputStreamReader(System.in));
  29         System.out.print("Enter a line: ");       
  30         
  31         if((sin = stdin1.readLine())!= null)
  32         {            
  33             /*
  34              * TODO 입력 받은 지뢰 밭을 그려주기.
  35              */
  36             ct = sin.split(" ");
  37             for(int a=0; a < ct.length; a++)
  38             {
  39                 for (int i = 0; i < ct[a].length(); i++)
  40                 {
  41                     ch = ct[a].charAt(i);
  42                     if ( ch == '*' )
  43                         cfinal[a][i] = ch;
  44                     else
  45                         cfinal[a][i] = '0';
  46                     System.out.print(cfinal[a][i]);
  47                 }
  48                 System.out.println("");
  49             }
  50 
  51 //            MS_Search sr = new MS_Search();
  52 //            sr.search();
  53 
  54             /**
  55              * TODO 지뢰 갯수가 찍히는 부분 
  56              */
  57        
  58         
  59         for(int b=0; b < ct.length; b++)
  60         {
  61             if (b == 0)
  62             {
  63                 System.out.println("");
  64                 System.out.println("Field #" + b+1);
  65             }                
  66             else
  67             {
  68                 for (int i = 0; i < ct[b].length()  ; i++)
  69                 {                       
  70                     if(cfinal[b][i] == '*')
  71                     {
  72                         for (int j = b-1; j < b+2; j++)
  73                         {
  74                             if(j >= 0)
  75                             {                            
  76                                 for (int k = i-1; k < i+2; k++)
  77                                 {
  78                                     if(k>=0)
  79                                     {
  80                                         if(b==j && i==k)
  81                                         {
  82                                             cfinal[j][k] = '*';
  83                                         }
  84                                         else
  85                                         {
  86                                             count = cfinal[j][k];
  87                                             if ( count != '*')
  88                                             {
  89                                                 count++;
  90                                                 cfinal[j][k] = count;
  91                                             }
  92                                         }
  93                                     }    
  94                                     
  95                                 }   //k for() loop End.
  96                             }
  97                         }   //j for() loop End.
  98                     }   //if(cfinal[b][i] == '*') End.
  99                 }
 100             }   //else End.
 101         }
 102                   
 103             /**
 104              * TODO 지뢰을 찾은 배열을 화면에 표시한다.
 105              */
 106             for(int c=1; c < ct.length; c++)
 107             {
 108                 for (int i = 0; i < ct[c].length(); i++)
 109                 {
 110                     System.out.print(cfinal[c][i]);
 111                 }
 112                 System.out.println("");
 113             }
 114             
 115         }
 116     }
 117 }

AlgorithmQuiz/MineSweeper/angss (last edited 2011-08-03 11:01:16 by localhost)

web biohackers.net