Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace pratofiorito
  9. {
  10. public class BombClass : IEquatable<BombClass>
  11. {
  12. public BombClass(int h, int l)
  13. {
  14. this.x = h;
  15. this.y = l;
  16. }
  17. public int x { get; set; }
  18. public int y { get; set; }
  19.  
  20. public bool Equals(BombClass other)
  21. {
  22. if (this.x == other.x && this.y == other.y)
  23. {
  24. return true;
  25. }
  26. else
  27. {
  28. return false;
  29. }
  30. }
  31. }
  32.  
  33. class BoxClass
  34. {
  35. public BoxClass(int h, int l)
  36. {
  37. this.x = h;
  38. this.y = l;
  39. }
  40. public int x { get; set; }
  41. public int y { get; set; }
  42. }
  43.  
  44. class Program
  45. {
  46. static public int lengthX = 6;
  47. static public int lengthY = 9;
  48. static public int bombNum = 0;
  49. static public bool win = false;
  50. static public char[,] map = new char[lengthX, lengthY];
  51. static public bool[,] mapBool = new bool[lengthX, lengthY];
  52. static public List<BombClass> bomb = new List<BombClass>();
  53. static public BoxClass selected_box = new BoxClass(0, 0);
  54.  
  55. static public void create_map()
  56. {
  57. for (int i = 0; i < lengthX; i++)
  58. {
  59. for (int k = 0; k < lengthY; k++)
  60. {
  61. map[i, k] = '~';
  62. }
  63. }
  64. }
  65.  
  66. static public void draw_map()
  67. {
  68. Console.Clear();
  69. for(int i = 0; i < lengthX; i++)
  70. {
  71. for(int k = 0; k < lengthY; k++)
  72. {
  73. if(i == selected_box.x && k == selected_box.y)
  74. {
  75. Console.Write("_ ");
  76. }
  77. else if (mapBool[i, k])
  78. {
  79. Console.Write(map[i, k] + " ");
  80. }else
  81. {
  82. Console.Write("# ");
  83. }
  84. }
  85. Console.WriteLine();
  86. }
  87. }
  88.  
  89. static public void spawn_bomb()
  90. {
  91. for(int i = 0; i < 8; i++)
  92. {
  93. Random rnd = new Random();
  94. int x = rnd.Next(1, lengthX);
  95. int y = rnd.Next(1, lengthY);
  96. while (bomb.Contains(new BombClass(x,y)))
  97. {
  98. x = rnd.Next(1, lengthX);
  99. y = rnd.Next(1, lengthY);
  100. }
  101. bomb.Add(new BombClass(x, y));
  102. map[x, y] = '@';
  103. }
  104. }
  105.  
  106. static public void spawn_number()
  107. {
  108. for (int k = 0; k < lengthY; k++)
  109. {
  110. for (int i = 0; i < lengthX; i++)
  111. {
  112. int bomb_for_pos = 0;
  113.  
  114. if (map[i, k] != '@')
  115. {
  116. if (i - 1 >= 0 && k - 1 >= 0) // bassso sinistra
  117. {
  118. if (map[i - 1, k - 1] == '@') bomb_for_pos++;
  119. }
  120.  
  121. if (k - 1 >= 0) // sinistra
  122. {
  123. if (map[i, k - 1] == '@') bomb_for_pos++;
  124. }
  125.  
  126.  
  127. if (i + 1 < lengthX && k - 1 >= 0) // sinistra alto
  128. {
  129. if (map[i + 1, k - 1] == '@') bomb_for_pos++;
  130. }
  131.  
  132.  
  133. if (i - 1 >= 0) // basso
  134. {
  135. if (map[i - 1, k] == '@') bomb_for_pos++;
  136. }
  137.  
  138. if (i + 1 < lengthX) // alto
  139. {
  140. if (map[i + 1, k] == '@') bomb_for_pos++;
  141. }
  142.  
  143. if (i - 1 >= 0 && k + 1 < lengthY) // destra basso
  144. {
  145. if (map[i - 1, k + 1] == '@') bomb_for_pos++;
  146. }
  147.  
  148. if (k + 1 < lengthY) // destra
  149. {
  150. if (map[i, k + 1] == '@') bomb_for_pos++;
  151. }
  152.  
  153. if (i + 1 < lengthX && k + 1 < lengthY) // destra alto
  154. {
  155. if (map[i + 1, k + 1] == '@') bomb_for_pos++;
  156. }
  157.  
  158. }
  159.  
  160. if (bomb_for_pos > 0)
  161. {
  162. map[i, k] = Convert.ToChar(bomb_for_pos.ToString());
  163. }
  164. }
  165. }
  166.  
  167. }
  168.  
  169. static public void move(string _move)
  170. {
  171. switch (_move)
  172. {
  173. case "DownArrow":
  174. if (selected_box.x + 1 < lengthX)
  175. {
  176. selected_box.x += 1;
  177. draw_map();
  178. }
  179. break;
  180. case "UpArrow":
  181. if (selected_box.x - 1 > -1)
  182. {
  183. selected_box.x -= 1;
  184. draw_map();
  185. }
  186. break;
  187. case "LeftArrow":
  188. if (selected_box.y - 1 > -1)
  189. {
  190. selected_box.y -= 1;
  191. draw_map();
  192. }
  193. break;
  194. case "RightArrow":
  195. if (selected_box.x + 1 < lengthY)
  196. {
  197. selected_box.y += 1;
  198. draw_map();
  199. }
  200. break;
  201. case "Enter":
  202. enter();
  203. break;
  204. }
  205. check_win();
  206. }
  207.  
  208. static public void check_win()
  209. {
  210. int tot = 0;
  211. for(int i = 0; i < lengthX; i++)
  212. {
  213. for(int j = 0; j < lengthY; j++)
  214. {
  215. if (mapBool[i,j]) tot++;
  216. }
  217. }
  218.  
  219. if(tot == bombNum)
  220. {
  221. win = true;
  222. draw_map();
  223. Console.WriteLine("HAI VINTO!");
  224. }
  225. }
  226.  
  227. static public void enter()
  228. {
  229. if(map[selected_box.x, selected_box.y] == '@')
  230. {
  231. for(int i = 0; i < lengthX; i++)
  232. {
  233. for(int j = 0; j < lengthY; j++)
  234. {
  235. mapBool[i, j] = true;
  236. }
  237. }
  238. draw_map();
  239. win = true;
  240. Console.WriteLine("HAI PERSO!");
  241. }else if(map[selected_box.x, selected_box.y] == '~')
  242. {
  243. Debug.WriteLine("Find null");
  244. List<BoxClass> queue = new List<BoxClass>();
  245. queue.Add(new BoxClass(selected_box.x, selected_box.y));
  246. while (queue.Count > 0)
  247. {
  248. Debug.WriteLine(map[queue[0].x, queue[0].y] + " at pos: " + queue[0].x + " , " + queue[0].y);
  249. int i = queue[0].x;
  250. int k = queue[0].y;
  251. queue.RemoveAt(0);
  252. if (map[i, k] != '@')
  253. {
  254. if (i - 1 >= 0 && k - 1 >= 0) // bassso sinistra
  255. {
  256. if (map[i - 1, k - 1] != '~' && map[i - 1, k - 1] != '@') { mapBool[i - 1, k - 1] = true; }
  257. else if (map[i - 1, k - 1] != '@' && !mapBool[i - 1, k - 1]) { queue.Add(new BoxClass(i - 1, k - 1)); mapBool[i - 1, k - 1] = true; }
  258. }
  259.  
  260. if (k - 1 >= 0) // sinistra
  261. {
  262. if (map[i, k - 1] != '~' && map[i, k - 1] != '@') { mapBool[i, k - 1] = true; }
  263. else if (map[i, k - 1] != '@' && !mapBool[i, k - 1]) { queue.Add(new BoxClass(i, k - 1)); mapBool[i, k - 1] = true; }
  264. }
  265.  
  266. if (i + 1 < lengthX && k - 1 >= 0) // sinistra alto
  267. {
  268. if (map[i + 1, k - 1] != '~' && map[i + 1, k - 1] != '@') { mapBool[i + 1, k - 1] = true; }
  269. else if (map[i + 1, k - 1] != '@' && !mapBool[i + 1, k - 1]) { queue.Add(new BoxClass(i + 1, k - 1)); mapBool[i + 1, k - 1] = true; }
  270. }
  271.  
  272. if (i - 1 >= 0) // basso
  273. {
  274. if (map[i - 1, k] != '~' && map[i - 1, k] != '@') { mapBool[i - 1, k] = true; }
  275. else if (map[i - 1, k] != '@' && !mapBool[i - 1, k]) { queue.Add(new BoxClass(i - 1, k)); mapBool[i - 1, k] = true; }
  276. }
  277.  
  278. if (i + 1 < lengthX) // alto
  279. {
  280. if (map[i + 1, k] != '~' && map[i + 1, k] != '@') { mapBool[i + 1, k] = true; }
  281. else if (map[i + 1, k] != '@' && !mapBool[i + 1, k]) { queue.Add(new BoxClass(i + 1, k)); mapBool[i + 1, k] = true; }
  282. }
  283.  
  284. if (i - 1 >= 0 && k + 1 < lengthY) // destra basso
  285. {
  286. if (map[i - 1, k + 1] != '~' && map[i - 1, k + 1] != '@') { mapBool[i - 1, k + 1] = true; }
  287. else if (map[i - 1, k + 1] != '@' && !mapBool[i - 1, k + 1]) { queue.Add(new BoxClass(i - 1, k + 1)); mapBool[i - 1, k + 1] = true; }
  288. }
  289.  
  290. if (k + 1 < lengthY) // destra
  291. {
  292. if (map[i, k + 1] != '~' && map[i, k + 1] != '@') { mapBool[i, k + 1] = true; }
  293. else if (map[i, k + 1] != '@' && !mapBool[i, k + 1]) { queue.Add(new BoxClass(i, k + 1)); mapBool[i, k + 1] = true; }
  294. }
  295.  
  296. if (i + 1 < lengthX && k + 1 < lengthY) // destra alto
  297. {
  298. if (map[i + 1, k + 1] != '~' && map[i + 1, k + 1] != '@') { mapBool[i + 1, k + 1] = true; }
  299. else if (map[i + 1, k + 1] != '@' && !mapBool[i + 1, k + 1]) { queue.Add(new BoxClass(i + 1, k + 1)); mapBool[i + 1, k + 1] = true; }
  300. }
  301. }
  302. }
  303. draw_map();
  304. }else
  305. {
  306. mapBool[selected_box.x, selected_box.y] = true;
  307. draw_map();
  308. }
  309. }
  310.  
  311. static void Main(string[] args)
  312. {
  313. create_map();
  314. draw_map();
  315. bombNum = 56 * (8) / (lengthX*lengthY);
  316. spawn_bomb();
  317. spawn_number();
  318. draw_map();
  319. while (!win)
  320. {
  321. var key = Console.ReadKey();
  322. move(key.Key.ToString());
  323. }
  324. Console.ReadLine();
  325. }
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement