Advertisement
Guest User

minesweeper

a guest
Oct 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MinesweeperConsole
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. bool active = true;
  15. while (active)
  16. {
  17. // Printing selection menu
  18. Thread.Sleep(750);
  19. Console.Clear();
  20. Console.WriteLine("-----------------------------------------------");
  21. Console.WriteLine("Minesweeper 0.01");
  22. Console.WriteLine("By Debby Collaris, Kevin Withoff & Twan Slegers");
  23. Console.WriteLine("-----------------------------------------------");
  24. Console.WriteLine("1. Start new game");
  25. Console.WriteLine("2. Scoreboard");
  26. Console.WriteLine("3. Exit game");
  27. Console.WriteLine("-----------------------------------------------");
  28. // Assigning values
  29. int choice = 0;
  30. // Reading key entered by user
  31. string charEntered = Console.ReadKey(true).KeyChar.ToString();
  32.  
  33. try
  34. {
  35. choice = Convert.ToInt16(charEntered);
  36. switch (choice)
  37. {
  38. case 1:
  39. MinesweeperGame();
  40. break;
  41.  
  42. case 2:
  43. Console.WriteLine("Not yet finished");
  44. break;
  45.  
  46. case 3:
  47. Console.WriteLine("Exiting game...");
  48. Thread.Sleep(1250);
  49. return;
  50. }
  51. }
  52. catch(Exception ex)
  53. {
  54. Console.WriteLine("");
  55. Console.WriteLine(ex.Message);
  56. }
  57. }
  58. }
  59.  
  60. static string cursor = "█";
  61. // Y X coordinates
  62. /* Y = up down
  63. * X = left right
  64. */
  65. static int[] cursorPosition = new int[] { 6, 11 };
  66. static string[,] raster = new string[12, 22]
  67. //raster
  68. {
  69. { "╔","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","╗" },
  70. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  71. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  72. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  73. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  74. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  75. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  76. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  77. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  78. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  79. { "║","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","#","║" },
  80. { "╚","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","═","╝" }
  81. };
  82.  
  83. private static void BombGenerator()
  84. {
  85.  
  86. Random r = new Random();
  87.  
  88. string[] bombArray = new string[30];
  89.  
  90. //Creating HashSet, because HashSet removes duplicate values
  91. HashSet <string> numberSet = new HashSet<string>();
  92. while (numberSet.Count < 30)
  93. {
  94. numberSet.Add(r.Next(1, 11) + "," + r.Next(1, 21));
  95. }
  96. // Copy value from HashSet numberSet to Array bombArray
  97. numberSet.CopyTo(bombArray);
  98.  
  99.  
  100. }
  101.  
  102.  
  103. private static void MinesweeperGame()
  104. {
  105. BombGenerator();
  106. while (true)
  107. {
  108. Console.Clear();
  109. PrintGameField();
  110. ConsoleKeyInfo key = Console.ReadKey(true);
  111.  
  112. switch (key.Key)
  113. {
  114. case ConsoleKey.UpArrow:
  115. cursorPosition = new int[] { cursorPosition[0] - 1, cursorPosition[1] };
  116. break;
  117. case ConsoleKey.DownArrow:
  118. cursorPosition = new int[] { cursorPosition[0] + 1, cursorPosition[1] };
  119. break;
  120. case ConsoleKey.LeftArrow:
  121. cursorPosition = new int[] { cursorPosition[0], cursorPosition[1] - 1 };
  122. break;
  123. case ConsoleKey.RightArrow:
  124. cursorPosition = new int[] { cursorPosition[0], cursorPosition[1] + 1 };
  125. break;
  126. }
  127. }
  128. }
  129.  
  130. private static void PrintGameField()
  131. {
  132. for (int i = 0; i < 12; i++)
  133. {
  134. for (int j = 0; j < 22; j++)
  135. {
  136. //index 0 is Y
  137. //index 1 is X
  138. if (i == cursorPosition[0] && j == cursorPosition[1])
  139. {
  140. Console.Write(cursor);
  141. }
  142. else
  143. {
  144. Console.Write(raster[i, j]);
  145. }
  146. }
  147. Console.WriteLine();
  148. }
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement