Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Main.cs
- // Author: Jeffrey Cartagena
- //
- //
- using System;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace Minesweeper
- {
- class MainClass
- {
- public static void Main(string[] args)
- {
- try
- {
- StreamReader inFile = new StreamReader("data.in");
- StreamWriter outFile = new StreamWriter("data.out");
- MatchCollection matches = Regex.Matches(inFile.ReadLine(), @"\d+");
- if(matches.Count != 2)
- throw new Exception("Invalid input");
- int rows = Convert.ToInt32(matches[0].ToString()), cols = Convert.ToInt32(matches[1].ToString());
- int numField = 1;
- while(rows != 0 && cols != 0)
- {
- char[,] field = new char[rows,cols];
- for(int i=0; i < rows; i++)
- {
- for(int j=0; j < cols; j++)
- {
- field[i,j] = (char)inFile.Read();
- // Console.Write("{0} ", field[i,j]);
- }
- inFile.Read();
- //Console.WriteLine();
- //Console.Read();
- }
- CheckMines(field, rows, cols);
- outFile.WriteLine("Field #{0}:",numField);
- numField++;
- for(int i=0; i < rows; i++)
- {
- for(int j=0; j < cols; j++)
- {
- outFile.Write(field[i,j]);
- // Console.Write(field[i,j]);
- }
- outFile.WriteLine();
- //Console.WriteLine();
- //Console.Read();
- }
- outFile.WriteLine();
- matches = Regex.Matches(inFile.ReadLine(), @"\d+");
- if(matches.Count != 2)
- throw new Exception("Invalid input");
- rows = Convert.ToInt32(matches[0].ToString());
- cols = Convert.ToInt32(matches[1].ToString());
- }
- Console.Write("Finishing the program");
- Console.Read();
- inFile.Close();
- outFile.Close();
- }
- catch(System.IO.FileNotFoundException)
- {
- Console.Write("Input File not found");
- Console.Read();
- }
- catch
- {
- Console.Write("Invalid Input");
- Console.Read();
- }
- }
- private static void CheckMines(char[,] field, int rows, int cols)
- {
- int mines;
- for(int i=0; i < rows; i++)
- {
- for(int j=0; j < cols; j++)
- {
- if(field[i,j] == '*')
- continue;
- mines = 0;
- if(i == 0)
- {
- if(field[i+1,j] == '*')
- mines++;
- if(j == 0)
- {
- if(field[i,j+1] == '*')
- mines++;
- if(field[i+1,j+1] == '*')
- mines++;
- }
- else if(j == cols-1)
- {
- if(field[i,j-1] == '*')
- mines++;
- if(field[i+1,j-1] == '*')
- mines++;
- }
- else
- {
- if(field[i,j+1] == '*')
- mines++;
- if(field[i+1,j+1] == '*')
- mines++;
- if(field[i,j-1] == '*')
- mines++;
- if(field[i+1,j-1] == '*')
- mines++;
- }
- }
- else if(i == rows-1)
- {
- if(field[i-1,j] == '*')
- mines++;
- if(j == cols-1)
- {
- if(field[i,j-1] == '*')
- mines++;
- if(field[i-1,j-1] == '*')
- mines++;
- }
- else if(j == 0)
- {
- if(field[i,j+1] == '*')
- mines++;
- if(field[i-1,j+1] == '*')
- mines++;
- }
- else
- {
- if(field[i-1,j+1] == '*')
- mines++;
- if(field[i-1,j-1] == '*')
- mines++;
- if(field[i,j-1] == '*')
- mines++;
- if(field[i,j+1] == '*')
- mines++;
- }
- }
- else
- {
- if(field[i+1,j] == '*')
- mines++;
- if(field[i-1,j] == '*')
- mines++;
- if(j == 0)
- {
- if(field[i+1,j+1] == '*')
- mines++;
- if(field[i,j+1] == '*')
- mines++;
- if(field[i-1,j+1] == '*')
- mines++;
- }
- else if(j == cols-1)
- {
- if(field[i+1,j-1] == '*')
- mines++;
- if(field[i,j-1] == '*')
- mines++;
- if(field[i-1,j-1] == '*')
- mines++;
- }
- else
- {
- if(field[i+1,j-1] == '*')
- mines++;
- if(field[i,j-1] == '*')
- mines++;
- if(field[i-1,j-1] == '*')
- mines++;
- if(field[i+1,j+1] == '*')
- mines++;
- if(field[i,j+1] == '*')
- mines++;
- if(field[i-1,j+1] == '*')
- mines++;
- }
- }
- field[i,j] = (char)(mines + 0x30);
- // Console.Write("{0} ",field[i,j]);
- }
- //Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment