Venciity

Minesweeper

Oct 10th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.47 KB | None | 0 0
  1. namespace MinesweeperGame
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     public class Minesweeper
  8.     {
  9.         public class Ranking
  10.         {
  11.             private string name;
  12.  
  13.             private int points;
  14.  
  15.             public string Name
  16.             {
  17.                 get
  18.                 {
  19.                     return name;
  20.                 }
  21.  
  22.                 set
  23.                 {
  24.                     this.name = value;
  25.                 }
  26.             }
  27.  
  28.             public int Points
  29.             {
  30.                 get
  31.                 {
  32.                     return this.points;
  33.                 }
  34.  
  35.                 set
  36.                 {
  37.                     this.points = value;
  38.                 }
  39.             }
  40.  
  41.             public Ranking()
  42.             {
  43.             }
  44.  
  45.             public Ranking(string name, int points)
  46.             {
  47.                 this.Name = name;
  48.                 this.Points = points;
  49.             }
  50.         }
  51.  
  52.         private static void Main(string[] args)
  53.         {
  54.             string command = string.Empty;
  55.             char[,] gameField = CreateGameField();
  56.             char[,] mines = SetMines();
  57.             int counter = 0;
  58.             bool isFired = false;
  59.             List<Ranking> winners = new List<Ranking>(6);
  60.             int row = 0;
  61.             int col = 0;
  62.             bool isNewGame = true;
  63.             const int MaxTurns = 35;
  64.             bool areTurnsMaxed = false;
  65.  
  66.             do
  67.             {
  68.                 if (isNewGame)
  69.                 {
  70.                     Console.WriteLine(
  71.                         "Let's go! Play the Minesweeper game. The command 'top' show the ranking," +
  72.                         "'restart' - reset the game and command 'exit' - left the game");
  73.                     SetGameBoard(gameField);
  74.                     isNewGame = false;
  75.                 }
  76.  
  77.                 Console.Write("Enter row and col : ");
  78.                 command = Console.ReadLine().Trim();
  79.                 if (command.Length >= 3)
  80.                 {
  81.                     if (int.TryParse(command[0].ToString(), out row) &&
  82.                         int.TryParse(command[2].ToString(), out col) &&
  83.                         row <= gameField.GetLength(0) &&
  84.                         col <= gameField.GetLength(1))
  85.                     {
  86.                         command = "turn";
  87.                     }
  88.                 }
  89.  
  90.                 switch (command)
  91.                 {
  92.                     case "top":
  93.                         GetRanking(winners);
  94.                         break;
  95.                     case "restart":
  96.                         gameField = CreateGameField();
  97.                         mines = SetMines();
  98.                         SetGameBoard(gameField);
  99.                         isFired = false;
  100.                         isNewGame = false;
  101.                         break;
  102.                     case "exit":
  103.                         Console.WriteLine("Bye, Bye!");
  104.                         break;
  105.                     case "turn":
  106.                         if (mines[row, col] != '*')
  107.                         {
  108.                             if (mines[row, col] == '-')
  109.                             {
  110.                                 ChangeTurn(gameField, mines, row, col);
  111.                                 counter++;
  112.                             }
  113.  
  114.                             if (MaxTurns == counter)
  115.                             {
  116.                                 areTurnsMaxed = true;
  117.                             }
  118.                             else
  119.                             {
  120.                                 SetGameBoard(gameField);
  121.                             }
  122.                         }
  123.                         else
  124.                         {
  125.                             isFired = true;
  126.                         }
  127.  
  128.                         break;
  129.                     default:
  130.                         Console.WriteLine("\nError! Invalid command!\n");
  131.                         break;
  132.                 }
  133.  
  134.                 if (isFired)
  135.                 {
  136.                     SetGameBoard(mines);
  137.                     Console.WriteLine("\n Game over! You died a hero with : {0} points. Enter your nickname : ", counter);
  138.                     string nickname = Console.ReadLine();
  139.                     Ranking winner = new Ranking(nickname, counter);
  140.                     int winnersLimit = 5;
  141.                     if (winners.Count < winnersLimit)
  142.                     {
  143.                         winners.Add(winner);
  144.                     }
  145.                     else
  146.                     {
  147.                         for (int i = 0; i < winners.Count; i++)
  148.                         {
  149.                             if (winners[i].Points < winner.Points)
  150.                             {
  151.                                 winners.Insert(i, winner);
  152.                                 winners.RemoveAt(winners.Count - 1);
  153.                                 break;
  154.                             }
  155.                         }
  156.                     }
  157.  
  158.                     winners.Sort((Ranking ranking1, Ranking ranking2) => ranking2.Name.CompareTo(ranking1.Name));
  159.                     winners.Sort((Ranking ranking1, Ranking ranking2) => ranking2.Points.CompareTo(ranking1.Points));
  160.                     GetRanking(winners);
  161.  
  162.                     gameField = CreateGameField();
  163.                     mines = SetMines();
  164.                     counter = 0;
  165.                     isFired = false;
  166.                     isNewGame = true;
  167.                 }
  168.  
  169.                 if (areTurnsMaxed)
  170.                 {
  171.                     Console.WriteLine("\ncongratulations! You find 35 cells without hurt yourself!");
  172.                     SetGameBoard(mines);
  173.                     Console.WriteLine("Enter your name: ");
  174.                     string name = Console.ReadLine();
  175.                     Ranking points = new Ranking(name, counter);
  176.                     winners.Add(points);
  177.                     GetRanking(winners);
  178.                     gameField = CreateGameField();
  179.                     mines = SetMines();
  180.                     counter = 0;
  181.                     areTurnsMaxed = false;
  182.                     isNewGame = true;
  183.                 }
  184.             }
  185.             while (command != "exit");
  186.             Console.Read();
  187.         }
  188.  
  189.         private static void GetRanking(List<Ranking> points)
  190.         {
  191.             Console.WriteLine("\nPoints:");
  192.             if (points.Count > 0)
  193.             {
  194.                 for (int i = 0; i < points.Count; i++)
  195.                 {
  196.                     Console.WriteLine("{0}. {1} --> {2} points", i + 1, points[i].Name, points[i].Points);
  197.                 }
  198.  
  199.                 Console.WriteLine();
  200.             }
  201.             else
  202.             {
  203.                 Console.WriteLine("Empty rating list!\n");
  204.             }
  205.         }
  206.  
  207.         private static void ChangeTurn(char[,] field, char[,] mines, int row, int col)
  208.         {
  209.             char numberOfMines = CountMines(mines, row, col);
  210.             mines[row, col] = numberOfMines;
  211.             field[row, col] = numberOfMines;
  212.         }
  213.  
  214.         private static void SetGameBoard(char[,] board)
  215.         {
  216.             int rows = board.GetLength(0);
  217.             int cols = board.GetLength(1);
  218.             Console.WriteLine("\n    0 1 2 3 4 5 6 7 8 9");
  219.             Console.WriteLine("   ---------------------");
  220.             for (int i = 0; i < rows; i++)
  221.             {
  222.                 Console.Write("{0} | ", i);
  223.                 for (int j = 0; j < cols; j++)
  224.                 {
  225.                     Console.Write(string.Format("{0} ", board[i, j]));
  226.                 }
  227.  
  228.                 Console.Write("|");
  229.                 Console.WriteLine();
  230.             }
  231.  
  232.             Console.WriteLine("   ---------------------\n");
  233.         }
  234.  
  235.         private static char[,] CreateGameField()
  236.         {
  237.             int boardRows = 5;
  238.             int boardColumns = 10;
  239.             char[,] board = new char[boardRows, boardColumns];
  240.             for (int i = 0; i < boardRows; i++)
  241.             {
  242.                 for (int j = 0; j < boardColumns; j++)
  243.                 {
  244.                     board[i, j] = '?';
  245.                 }
  246.             }
  247.  
  248.             return board;
  249.         }
  250.  
  251.         private static char[,] SetMines()
  252.         {
  253.             int rows = 5;
  254.             int cols = 10;
  255.             char[,] fieldForPlay = new char[rows, cols];
  256.  
  257.             for (int i = 0; i < rows; i++)
  258.             {
  259.                 for (int j = 0; j < cols; j++)
  260.                 {
  261.                     fieldForPlay[i, j] = '-';
  262.                 }
  263.             }
  264.  
  265.             int numberOfMines = 15;
  266.             List<int> mines = new List<int>();
  267.             while (mines.Count < numberOfMines)
  268.             {
  269.                 Random random = new Random();
  270.                 int newMine = random.Next(50);
  271.                 if (!mines.Contains(newMine))
  272.                 {
  273.                     mines.Add(newMine);
  274.                 }
  275.             }
  276.  
  277.             foreach (int mine in mines)
  278.             {
  279.                 int col = mine / cols;
  280.                 int row = mine % cols;
  281.                 if (row == 0 && mine != 0)
  282.                 {
  283.                     col--;
  284.                     row = cols;
  285.                 }
  286.                 else
  287.                 {
  288.                     row++;
  289.                 }
  290.  
  291.                 fieldForPlay[col, row - 1] = '*';
  292.             }
  293.  
  294.             return fieldForPlay;
  295.         }
  296.  
  297.         private static char CountMines(char[,] field, int currentRow, int currentCol)
  298.         {
  299.             int mineCounter = 0;
  300.             int rows = field.GetLength(0);
  301.             int cols = field.GetLength(1);
  302.  
  303.             if (currentRow - 1 >= 0)
  304.             {
  305.                 if (field[currentRow - 1, currentCol] == '*')
  306.                 {
  307.                     mineCounter++;
  308.                 }
  309.             }
  310.  
  311.             if (currentRow + 1 < rows)
  312.             {
  313.                 if (field[currentRow + 1, currentCol] == '*')
  314.                 {
  315.                     mineCounter++;
  316.                 }
  317.             }
  318.  
  319.             if (currentCol - 1 >= 0)
  320.             {
  321.                 if (field[currentRow, currentCol - 1] == '*')
  322.                 {
  323.                     mineCounter++;
  324.                 }
  325.             }
  326.  
  327.             if (currentCol + 1 < cols)
  328.             {
  329.                 if (field[currentRow, currentCol + 1] == '*')
  330.                 {
  331.                     mineCounter++;
  332.                 }
  333.             }
  334.  
  335.             if ((currentRow - 1 >= 0) && (currentCol - 1 >= 0))
  336.             {
  337.                 if (field[currentRow - 1, currentCol - 1] == '*')
  338.                 {
  339.                     mineCounter++;
  340.                 }
  341.             }
  342.  
  343.             if ((currentRow - 1 >= 0) && (currentCol + 1 < cols))
  344.             {
  345.                 if (field[currentRow - 1, currentCol + 1] == '*')
  346.                 {
  347.                     mineCounter++;
  348.                 }
  349.             }
  350.  
  351.             if ((currentRow + 1 < rows) && (currentCol - 1 >= 0))
  352.             {
  353.                 if (field[currentRow + 1, currentCol - 1] == '*')
  354.                 {
  355.                     mineCounter++;
  356.                 }
  357.             }
  358.  
  359.             if ((currentRow + 1 < rows) && (currentCol + 1 < cols))
  360.             {
  361.                 if (field[currentRow + 1, currentCol + 1] == '*')
  362.                 {
  363.                     mineCounter++;
  364.                 }
  365.             }
  366.  
  367.             return char.Parse(mineCounter.ToString());
  368.         }
  369.     }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment