Guest User

Blackone

a guest
Nov 15th, 2009
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.44 KB | None | 0 0
  1. // Main.cs
  2. // Author: Jeffrey Cartagena
  3. //
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace Minesweeper
  10. {
  11.     class MainClass
  12.     {
  13.         public static void Main(string[] args)
  14.         {
  15.             try
  16.             {
  17.                 StreamReader inFile = new StreamReader("data.in");
  18.                 StreamWriter outFile = new StreamWriter("data.out");
  19.                
  20.                 MatchCollection matches = Regex.Matches(inFile.ReadLine(), @"\d+");
  21.                
  22.                 if(matches.Count != 2)
  23.                     throw new Exception("Invalid input");
  24.                
  25.                 int rows = Convert.ToInt32(matches[0].ToString()), cols = Convert.ToInt32(matches[1].ToString());
  26.                 int numField = 1;
  27.                
  28.                 while(rows != 0 && cols != 0)
  29.                 {
  30.                     char[,] field = new char[rows,cols];
  31.                    
  32.                     for(int i=0; i < rows; i++)
  33.                     {
  34.                         for(int j=0; j < cols; j++)
  35.                         {
  36.                             field[i,j] = (char)inFile.Read();
  37.                         //  Console.Write("{0} ", field[i,j]);
  38.                         }
  39.                         inFile.Read();
  40.                         //Console.WriteLine();
  41.                         //Console.Read();
  42.                     }
  43.                    
  44.                     CheckMines(field, rows, cols);
  45.                    
  46.                     outFile.WriteLine("Field #{0}:",numField);
  47.                     numField++;
  48.                     for(int i=0; i < rows; i++)
  49.                     {
  50.                         for(int j=0; j < cols; j++)
  51.                         {
  52.                             outFile.Write(field[i,j]);
  53.                         //  Console.Write(field[i,j]);
  54.                         }
  55.                         outFile.WriteLine();
  56.                         //Console.WriteLine();
  57.                         //Console.Read();
  58.                     }                  
  59.                     outFile.WriteLine();
  60.                    
  61.                    
  62.                     matches = Regex.Matches(inFile.ReadLine(), @"\d+");
  63.                
  64.                     if(matches.Count != 2)
  65.                         throw new Exception("Invalid input");
  66.                    
  67.                     rows = Convert.ToInt32(matches[0].ToString());
  68.                     cols = Convert.ToInt32(matches[1].ToString());
  69.                 }
  70.                
  71.                 Console.Write("Finishing the program");
  72.                 Console.Read();
  73.                 inFile.Close();
  74.                 outFile.Close();
  75.             }
  76.             catch(System.IO.FileNotFoundException)
  77.             {
  78.                 Console.Write("Input File not found");
  79.                 Console.Read();
  80.             }
  81.             catch
  82.             {
  83.                 Console.Write("Invalid Input");
  84.                 Console.Read();
  85.             }              
  86.         }
  87.        
  88.         private static void CheckMines(char[,] field, int rows, int cols)
  89.         {
  90.             int mines;
  91.             for(int i=0; i < rows; i++)
  92.             {
  93.                 for(int j=0; j < cols; j++)
  94.                 {
  95.                     if(field[i,j] == '*')
  96.                         continue;
  97.                    
  98.                     mines = 0;
  99.                     if(i == 0)
  100.                     {
  101.                         if(field[i+1,j] == '*')
  102.                                 mines++;
  103.                        
  104.                         if(j == 0)
  105.                         {                          
  106.                             if(field[i,j+1] == '*')
  107.                                 mines++;
  108.                             if(field[i+1,j+1] == '*')
  109.                                 mines++;                           
  110.                         }
  111.                         else if(j == cols-1)
  112.                         {                          
  113.                             if(field[i,j-1] == '*')
  114.                                 mines++;
  115.                             if(field[i+1,j-1] == '*')
  116.                                 mines++;       
  117.                         }
  118.                         else
  119.                         {                          
  120.                             if(field[i,j+1] == '*')
  121.                                 mines++;
  122.                             if(field[i+1,j+1] == '*')
  123.                                 mines++;
  124.                             if(field[i,j-1] == '*')
  125.                                 mines++;
  126.                             if(field[i+1,j-1] == '*')
  127.                                 mines++;
  128.                         }
  129.                     }
  130.                    
  131.                     else if(i == rows-1)
  132.                     {
  133.                         if(field[i-1,j] == '*')
  134.                                 mines++;
  135.                        
  136.                         if(j == cols-1)
  137.                         {                          
  138.                             if(field[i,j-1] == '*')
  139.                                 mines++;
  140.                             if(field[i-1,j-1] == '*')
  141.                                 mines++;   
  142.                            
  143.                         }
  144.                         else if(j == 0)
  145.                         {                          
  146.                             if(field[i,j+1] == '*')
  147.                                 mines++;
  148.                             if(field[i-1,j+1] == '*')
  149.                                 mines++;   
  150.                         }
  151.                         else
  152.                         {
  153.                             if(field[i-1,j+1] == '*')
  154.                                 mines++;
  155.                             if(field[i-1,j-1] == '*')
  156.                                 mines++;
  157.                             if(field[i,j-1] == '*')
  158.                                 mines++;
  159.                             if(field[i,j+1] == '*')
  160.                                 mines++;
  161.                         }
  162.                     }
  163.                     else
  164.                     {
  165.                         if(field[i+1,j] == '*')
  166.                                 mines++;
  167.                         if(field[i-1,j] == '*')
  168.                                 mines++;
  169.                        
  170.                         if(j == 0)
  171.                         {                          
  172.                             if(field[i+1,j+1] == '*')
  173.                                 mines++;
  174.                             if(field[i,j+1] == '*')
  175.                                 mines++;
  176.                             if(field[i-1,j+1] == '*')
  177.                                 mines++;
  178.                         }
  179.                         else if(j == cols-1)
  180.                         {
  181.                             if(field[i+1,j-1] == '*')
  182.                                 mines++;
  183.                             if(field[i,j-1] == '*')
  184.                                 mines++;
  185.                             if(field[i-1,j-1] == '*')
  186.                                 mines++;
  187.                         }
  188.                         else
  189.                         {
  190.                             if(field[i+1,j-1] == '*')
  191.                                 mines++;
  192.                             if(field[i,j-1] == '*')
  193.                                 mines++;
  194.                             if(field[i-1,j-1] == '*')
  195.                                 mines++;
  196.                             if(field[i+1,j+1] == '*')
  197.                                 mines++;
  198.                             if(field[i,j+1] == '*')
  199.                                 mines++;
  200.                             if(field[i-1,j+1] == '*')
  201.                                 mines++;
  202.                         }
  203.                     }
  204.                     field[i,j] = (char)(mines + 0x30);
  205.                 //  Console.Write("{0} ",field[i,j]);
  206.                 }
  207.                 //Console.WriteLine();
  208.             }
  209.         }                  
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment