Advertisement
Guest User

Sapper from Carpet

a guest
Sep 18th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.83 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.Threading;
  7.  
  8. namespace Sapper
  9. {
  10. class Program
  11. {
  12. const byte fieldSizeX = 10,
  13. fieldSizeY = 10;
  14. static byte difficultLevel = 1;
  15. static char[,] map;
  16. static byte[,] fogOfWar; // 0 - открыто, 1 - закрыто, 2 - !, 3 -?
  17.  
  18. static Dictionary<char, ConsoleColor> ColorDictionary;
  19. static List<char> symbolList;
  20.  
  21. static short playerPositionX,
  22. playerPositionY;
  23. static bool game = true,
  24. loose = false;
  25. static byte globalMines = 0;
  26. static ushort openCells = 0;
  27.  
  28. static ushort zeroPoints = 0;
  29. static List<int> zeros = new List<int>();
  30. static int zeroX,
  31. zeroY;
  32.  
  33. static char zeroOnMap = Convert.ToChar(Convert.ToString(0));
  34.  
  35. static void Main(string[] args)
  36. {
  37. Start:
  38. GenerateMap(fieldSizeX, fieldSizeY);
  39. DetermineUserCursorPosition(); //присвоение значения координатам курсора
  40. CountCells();
  41. Console.CursorVisible = false;
  42.  
  43. LocalRestart:
  44. Console.Clear();
  45.  
  46. while (game)
  47. {
  48.  
  49. //RenderInvisibleMap();
  50. //PlayerCursor();
  51. //PlayerMoveController();
  52.  
  53. RenderVisibleMap();
  54. //RenderInvisibleMap();
  55. RenderInterface(fieldSizeX);
  56. PlayerCursor(); //отрисовка курсора
  57. //FindChar(0, 200);
  58. PlayerMoveController();
  59.  
  60. CheckWin();
  61.  
  62. Thread.Sleep(50);
  63.  
  64. }
  65.  
  66. if (loose)
  67. {
  68. Console.Clear();
  69. Console.ForegroundColor = ConsoleColor.Red;
  70. Console.WriteLine("ПОРАЖЕНИЕ");
  71. Console.ForegroundColor = ConsoleColor.White;
  72. Console.WriteLine("Чтобы начать игру на новой карте, нажмите 0 + Enter\n" +
  73. "Чтобы начать игру на этой же карте, нажмите 1 + Enter");
  74. Console.ForegroundColor = ConsoleColor.Yellow;
  75. byte end;
  76. end = Convert.ToByte(Console.ReadLine());
  77. Console.WriteLine(end);
  78. if (end == 0)
  79. {
  80. game = true;
  81. loose = false;
  82. goto Start;
  83. }
  84. if (end == 1)
  85. {
  86. game = true;
  87. loose = false;
  88. goto LocalRestart;
  89. }
  90.  
  91. }
  92. else
  93. {
  94. Console.Clear();
  95. Console.ForegroundColor = ConsoleColor.Green;
  96. Console.WriteLine("ПОБЕДА");
  97. Console.ForegroundColor = ConsoleColor.White;
  98. Console.WriteLine("Чтобы начать игру на новой карте, нажмите 0 + Enter\n" +
  99. "Чтобы начать игру на этой же карте, нажмите 1 + Enter");
  100. Console.ForegroundColor = ConsoleColor.Yellow;
  101. byte end;
  102. end = Convert.ToByte(Console.ReadLine());
  103. Console.WriteLine(end);
  104. if (end == 0)
  105. {
  106. game = true;
  107. loose = false;
  108. goto Start;
  109. }
  110. if (end == 1)
  111. {
  112. game = true;
  113. loose = false;
  114. goto LocalRestart;
  115. }
  116. }
  117.  
  118. //Exp();
  119. //Console.ReadKey();
  120. }
  121.  
  122.  
  123. static void Exp()
  124. {
  125.  
  126. }
  127.  
  128. static void CountCells()
  129. {
  130. for (byte i = 1; i < map.GetLength(0) - 1; i++)
  131. {
  132. for (byte k = 1; k < map.GetLength(1) - 1; k++)
  133. {
  134. if (map[i, k] != symbolList[2])
  135. openCells++;
  136. }
  137. }
  138. }
  139.  
  140. static void CheckWin()
  141. {
  142.  
  143. ushort visibleOpenCell = 0;
  144.  
  145. for (byte i = 1; i < map.GetLength(0) - 1; i++)
  146. {
  147. for (byte k = 1; k < map.GetLength(1) - 1; k++)
  148. {
  149. if (map[i, k] != symbolList[2] && fogOfWar[i, k] == 0)
  150. visibleOpenCell++;
  151. }
  152. }
  153. if (openCells == visibleOpenCell) game = false;
  154. }
  155.  
  156. static void RenderInterface(byte x)
  157. {
  158. x += 3;
  159. Console.SetCursorPosition(x, 0);
  160. Console.WriteLine("Мин осталось : " + globalMines + " ");
  161. }
  162.  
  163. static void PlayerMoveController()
  164. {
  165. ConsoleKeyInfo key = Console.ReadKey();
  166. switch(key.Key)
  167. {
  168. case ConsoleKey.LeftArrow:
  169. MovePlayer(-1, 0);
  170. break;
  171. case ConsoleKey.RightArrow:
  172. MovePlayer(1, 0);
  173. break;
  174. case ConsoleKey.DownArrow:
  175. MovePlayer(0, 1);
  176. break;
  177. case ConsoleKey.UpArrow:
  178. MovePlayer(0, -1);
  179. break;
  180. case ConsoleKey.NumPad0:
  181. ActionPlayer(0);
  182. break;
  183. case ConsoleKey.NumPad1:
  184. ActionPlayer(1);
  185. break;
  186. case ConsoleKey.NumPad2:
  187. ActionPlayer(2);
  188. break;
  189. }
  190. }
  191.  
  192. static void ActionPlayer(byte x)
  193. {
  194. if(x == 0)
  195. {
  196. fogOfWar[playerPositionX, playerPositionY] = 0;
  197. if (map[playerPositionX, playerPositionY] == symbolList[2])
  198. {
  199. game = false;
  200. loose = true;
  201. }
  202. CheckZeros(playerPositionX, playerPositionY);
  203. }
  204. if(x == 1)
  205. {
  206. if (fogOfWar[playerPositionX, playerPositionY] == 2)
  207. {
  208. fogOfWar[playerPositionX, playerPositionY] = 1;
  209. globalMines++;
  210. }
  211. else if (fogOfWar[playerPositionX, playerPositionY] == 1 || fogOfWar[playerPositionX, playerPositionY] == 3)
  212. {
  213. fogOfWar[playerPositionX, playerPositionY] = 2;
  214. globalMines--;
  215. }
  216. }
  217. if(x == 2)
  218. {
  219. if (fogOfWar[playerPositionX, playerPositionY] == 1)
  220. {
  221. fogOfWar[playerPositionX, playerPositionY] = 3;
  222. }
  223. else if (fogOfWar[playerPositionX, playerPositionY] == 3)
  224. {
  225. fogOfWar[playerPositionX, playerPositionY] = 1;
  226. }
  227. if (fogOfWar[playerPositionX, playerPositionY] == 2)
  228. {
  229. fogOfWar[playerPositionX, playerPositionY] = 3;
  230. globalMines++;
  231. }
  232. }
  233. }
  234.  
  235. static void CheckZeros(int x, int y) // первая итерация
  236. {
  237. if (map[x, y] == zeroOnMap) // проверка, является ли клетка нулем
  238. {
  239. zeros.Clear();
  240. zeroX = x;
  241. zeroY = y;
  242.  
  243. //do
  244. {
  245. ZeroQuad(zeroX, zeroY); //в выходе из цикла не участвует
  246. //ZeroSubstitution();
  247. }
  248. //while (zeros.Count != 0);
  249. }
  250. }
  251.  
  252. static void ZeroQuad(int x, int y) // ищет в квадрате нули
  253. {
  254. if(zeros.Count != 0)
  255. {
  256. zeros.RemoveAt(zeroPoints - 2);
  257. zeros.RemoveAt(zeroPoints - 1);
  258. }
  259.  
  260. int a, b;
  261. for (a = x - 1; a < x + 2; a++)
  262. {
  263. for (b = y - 1; b < y + 2; b++)
  264. {
  265. fogOfWar[a, b] = 0;
  266. if (map[a, b] == zeroOnMap && (a != x || b != y))
  267. {
  268. zeros.Add(a);
  269. zeros.Add(b);
  270. zeroPoints += 2;
  271. }
  272. }
  273. }
  274. //
  275. //Console.WriteLine("\nZeroQuad zeros.Count " + zeros.Count + " zeroPoints = " + zeroPoints
  276. // + " ");
  277. //Console.ReadKey();
  278. //
  279. }
  280.  
  281. static void ZeroSubstitution()
  282. {
  283. if (zeros.Count != 0)
  284. {
  285. zeroX = zeros[zeroPoints - 2];
  286. zeroY = zeros[zeroPoints - 1];
  287. zeroPoints -= 2;
  288. }
  289. //
  290. //Console.WriteLine("ZeroSub. zeros.Count " + zeros.Count + " zeroPoints = " + zeroPoints+
  291. // "zeroX = "+ zeroX+ " zeroY = "+ zeroY);
  292. //Console.ReadKey();
  293. //
  294. }
  295.  
  296. #region перемещение курсора
  297. static void MovePlayer(short x, short y)
  298. {
  299. if(CheckCollisions(x, y))
  300. {
  301. playerPositionX += x;
  302. playerPositionY += y;
  303. }
  304. }
  305.  
  306. static bool CheckCollisions(short x, short y)
  307. {
  308. int targetX = playerPositionX + x,
  309. targetY = playerPositionY + y;
  310. if (map[targetX, targetY] == symbolList[0])
  311. return false;
  312. else
  313. return true;
  314. }
  315.  
  316. static void PlayerCursor()//отрисовка курсора
  317. {
  318. Console.SetCursorPosition(playerPositionX, playerPositionY);
  319. Console.ForegroundColor = ColorDictionary[symbolList[1]];
  320. Console.Write(symbolList[1]);
  321. }
  322. #endregion
  323.  
  324. #region карта
  325. static void DetermineUserCursorPosition()
  326. {
  327. playerPositionX = fieldSizeX / 2;
  328. playerPositionY = fieldSizeY / 2;
  329. }
  330.  
  331. static void GenerateMap(byte x, byte y)
  332. {
  333. x += 2;//запас для рамки
  334. y += 2;//запас для рамки
  335. map = new char[x, y];
  336. fogOfWar = new byte[x, y];
  337.  
  338. SymbolsAndColors(); //инициализируем словарь и лист
  339. Frame(x, y); //создаем рамку и поле
  340.  
  341. SeedMines(x, y);
  342. SeedNumbers(x, y);
  343. }
  344.  
  345. static void SymbolsAndColors()
  346. {
  347. symbolList = new List<char>();
  348. symbolList.Add((char)35); // решетка - рамка
  349. symbolList.Add((char)2); // рожица - курсор
  350. symbolList.Add((char)15); // мины
  351. symbolList.Add((char)4); // бубны - неоткрытые клетки
  352. symbolList.Add((char)33); // воскл. знак - помеченная клетка
  353. symbolList.Add('?'); //неизвестные клетки
  354.  
  355. ColorDictionary = new Dictionary<char, ConsoleColor>();
  356. ColorDictionary.Add(symbolList[0], ConsoleColor.DarkYellow); // решетка - рамка
  357. ColorDictionary.Add(symbolList[1], ConsoleColor.Yellow); // рожица - курсор
  358. ColorDictionary.Add(symbolList[2], ConsoleColor.DarkRed); // мины
  359. ColorDictionary.Add(symbolList[3], ConsoleColor.DarkGray); // бубны - неоткрытые клетки
  360. ColorDictionary.Add(symbolList[4], ConsoleColor.Magenta); // вос. зн. - помеченная клетка
  361. ColorDictionary.Add(symbolList[5], ConsoleColor.Cyan); // вопросительный знак
  362. }
  363.  
  364. static void SeedNumbers(byte x, byte y)
  365. {
  366. for (byte i = 1; i < map.GetLength(0) - 1; i++)
  367. {
  368. for (byte k = 1; k < map.GetLength(1) - 1; k++)
  369. {
  370. if (map[i, k] == symbolList[2]) continue;
  371. else
  372. {
  373. map[i, k] = OneNumber(i, k);
  374. }
  375.  
  376. }
  377. }
  378. }
  379.  
  380. static char OneNumber (byte x, byte y)
  381. {
  382. int a, b;
  383. byte mines = 0;
  384. for(a = x - 1; a < x + 2; a++)
  385. {
  386. for(b = y - 1; b < y + 2; b++)
  387. {
  388. if (map[a, b] == symbolList[2])
  389. mines++;
  390. }
  391. }
  392. return Convert.ToChar(Convert.ToString(mines));
  393. }
  394.  
  395. static void SeedMines(byte x, byte y)
  396. {
  397. Random ran = new Random();
  398. for (byte i = 1; i < map.GetLength(0) - 1; i++)
  399. {
  400. for (byte k = 1; k < map.GetLength(1) - 1; k++)
  401. {
  402. if (ran.Next(0, 100) < ValueOfChanceMines(difficultLevel))
  403. {
  404. map[i, k] = symbolList[2];
  405. globalMines++;
  406. }
  407. }
  408. }
  409. }
  410.  
  411. static short ValueOfChanceMines (byte x)
  412. {
  413. if (x == 1) return 13;
  414. if (x == 2) return 16;
  415. if (x == 3) return 21;
  416. return 0;
  417. }
  418.  
  419. static void Frame(byte x, byte y)
  420. {
  421. for (short i = 0; i < map.GetLength(0); i++)
  422. {
  423. for (short k = 0; k < map.GetLength(1); k++)
  424. {
  425. if((i == 0 || i == x - 1) || (k == 0 || k == y - 1))
  426. {
  427. map[i, k] = symbolList[0];
  428. fogOfWar[i, k] = 0;
  429. }
  430. else
  431. {
  432. map[i, k] = ' ';
  433. fogOfWar[i, k] = 1;
  434. }
  435. }
  436. }
  437. }
  438.  
  439. static void Colors(byte x, byte y, byte z) //z = 0 - туман войны выкл, z = 1 туман войны вкл
  440. {
  441. if (z == 1)
  442. {
  443. if (fogOfWar[x, y] == 1)
  444. Console.ForegroundColor = ConsoleColor.DarkGray; //цвет неизвестных обл.
  445. else if (ColorDictionary.ContainsKey(map[x, y]))
  446. Console.ForegroundColor = ColorDictionary[map[x, y]];
  447. else
  448. Console.ForegroundColor = ConsoleColor.White;
  449. if (fogOfWar[x, y] == 2)
  450. Console.ForegroundColor = ColorDictionary[symbolList[4]];
  451. if (fogOfWar[x, y] == 3)
  452. Console.ForegroundColor = ColorDictionary[symbolList[5]];
  453.  
  454. }
  455. else
  456. {
  457. if (ColorDictionary.ContainsKey(map[x, y]))
  458. Console.ForegroundColor = ColorDictionary[map[x, y]];
  459. else
  460. Console.ForegroundColor = ConsoleColor.Gray;
  461. }
  462. }
  463.  
  464. static void RenderInvisibleMap()
  465. {
  466. for (byte i = 0; i < map.GetLength(0); i++)
  467. {
  468. for (byte k = 0; k < map.GetLength(1); k++)
  469. {
  470. //Thread.Sleep(70);
  471. Colors(i, k, 0);
  472. Console.SetCursorPosition(i, k);
  473. Console.Write(map[i, k]);
  474. }
  475. }
  476.  
  477. }
  478.  
  479. static void RenderVisibleMap()
  480. {
  481. for (byte i = 0; i < map.GetLength(0); i++)
  482. {
  483. for (byte k = 0; k < map.GetLength(1); k++)
  484. {
  485. Colors(i, k, 1);
  486. Console.SetCursorPosition(i, k);
  487. if (fogOfWar[i, k] == 1)
  488. Console.Write(symbolList[3]);
  489. else if (fogOfWar[i, k] == 2)
  490. Console.Write(symbolList[4]);
  491. else if (fogOfWar[i, k] == 3)
  492. Console.Write(symbolList[5]);
  493. else if ((fogOfWar[i, k] == 0) && (map[i, k] == zeroOnMap))
  494. Console.Write((char)183);
  495. else
  496. Console.Write(map[i, k]);
  497. }
  498. }
  499.  
  500. }
  501.  
  502. static void FindChar(short x, short y)
  503. {
  504. for(int i = x; i < y; i++)
  505. {
  506. Console.WriteLine(i + ". " + (char)i);
  507. }
  508.  
  509. }
  510. #endregion
  511.  
  512. #region свалка
  513. /*
  514. //Console.SetCursorPosition(0, 15);
  515. //Console.WriteLine(map[x, y]);
  516. //Console.WriteLine(Convert.ToChar(Convert.ToString(f)));
  517.  
  518. //char[] map1 = new char[2];
  519. //map1[0] = (char)48;
  520. //map1[1] = Convert.ToChar(0);
  521. //map1.
  522. //if (map[x, y] == map1[0] || map[x, y] == map1[1])
  523. //int k = Convert.ToInt32(map[x, y]);
  524. //if (k == 0)
  525.  
  526.  
  527. //Console.BackgroundColor = ConsoleColor.DarkRed;
  528. RenderInvisibleMap();
  529. PlayerCursor();
  530. Console.SetCursorPosition(fieldSizeX / 2, fieldSizeY / 2);
  531. Console.ForegroundColor = ConsoleColor.Red;
  532. Console.WriteLine("ПОБЕДА"); */
  533. #endregion
  534. }
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement