Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.68 KB | None | 0 0
  1. ///Snake
  2.  
  3. using System;
  4. using System.Text;
  5. using System.Timers;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. namespace newsnake
  10. {
  11. class Program
  12. {
  13. int size = 10, snakeSize;
  14. int[,] grid;
  15. static Coord direction;
  16. Coord position, previousDirection;
  17. System.Timers.Timer mainLoop;
  18. Random random = new Random();
  19. Thread keyListen;
  20.  
  21. static void Main(string[] args)
  22. {
  23. Program p = new Program();
  24. p.Start();
  25. }
  26.  
  27. public void Start()
  28. {
  29. grid = new int[size, size];
  30.  
  31. for (int x = 0; x < size; x++)
  32. for (int y = 0; y < size; y++)
  33. grid[x, y] = 0;
  34.  
  35. snakeSize = 3;
  36. position = new Coord(size / 2, size / 2);
  37. grid[position.x, position.y] = snakeSize;
  38.  
  39. direction = new Coord(1, 0);
  40. previousDirection = direction;
  41.  
  42. SpawnCherry();
  43.  
  44. if (keyListen == null)
  45. {
  46. keyListen = new Thread(ListenForKeys);
  47. keyListen.Start();
  48. }
  49.  
  50. if (mainLoop == null)
  51. {
  52. mainLoop = new System.Timers.Timer(500);
  53. mainLoop.Elapsed += UpdateLoop;
  54. mainLoop.AutoReset = true;
  55. mainLoop.Enabled = true;
  56. }
  57. }
  58.  
  59. void SpawnCherry()
  60. {
  61. bool posFound = false;
  62.  
  63. while (!posFound)
  64. {
  65. Coord newCoord = new Coord(random.Next(0, size - 1), random.Next(0, size - 1));
  66.  
  67. if (grid[newCoord.x, newCoord.y] == 0)
  68. {
  69. grid[newCoord.x, newCoord.y] = -1;
  70. posFound = true;
  71. }
  72. }
  73. }
  74.  
  75. void UpdateLoop(object sender, ElapsedEventArgs e)
  76. {
  77. Move();
  78. DrawGrid();
  79. }
  80.  
  81. void Move()
  82. {
  83. for (int y = 0; y < size; y++)
  84. for (int x = 0; x < size; x++)
  85. if (grid[x, y] > 0)
  86. grid[x, y]--;
  87.  
  88. Coord newPos = new Coord(position.x + direction.x, position.y + direction.y);
  89.  
  90. if (previousDirection.x + direction.x == 0 || previousDirection.y + direction.y == 0)
  91. newPos = new Coord(position.x + previousDirection.x, position.y + previousDirection.y);
  92.  
  93. if (newPos.x < 0)
  94. newPos.x = size - 1;
  95. if (newPos.x >= size)
  96. newPos.x = 0;
  97.  
  98. if (newPos.y < 0)
  99. newPos.y = size - 1;
  100. if (newPos.y >= size)
  101. newPos.y = 0;
  102.  
  103. int gridCell = grid[newPos.x, newPos.y];
  104.  
  105. if (gridCell > 0)
  106. {
  107. Start();
  108. return;
  109. }
  110.  
  111. if (gridCell == -1)
  112. {
  113. snakeSize++;
  114. SpawnCherry();
  115. }
  116.  
  117. grid[newPos.x, newPos.y] = snakeSize;
  118. position = newPos;
  119.  
  120. if (previousDirection.x != direction.x && direction.y != previousDirection.y)
  121. previousDirection = direction;
  122. }
  123.  
  124. void ListenForKeys()
  125. {
  126. while (true)
  127. {
  128. switch (Console.ReadKey(true).Key)
  129. {
  130. case ConsoleKey.W:
  131. direction = new Coord(0, -1);
  132. break;
  133.  
  134. case ConsoleKey.S:
  135. direction = new Coord(0, 1);
  136. break;
  137.  
  138. case ConsoleKey.A:
  139. direction = new Coord(-1, 0);
  140. break;
  141.  
  142. case ConsoleKey.D:
  143. direction = new Coord(1, 0);
  144. break;
  145. }
  146. }
  147. }
  148.  
  149. void DrawGrid()
  150. {
  151. Console.Clear();
  152. Console.WriteLine("Score: " + (snakeSize - 3) + "\n");
  153.  
  154. string c;
  155.  
  156. for (int y = 0; y < size; y++)
  157. {
  158. for (int x = 0; x < size; x++)
  159. {
  160. c = " .";
  161.  
  162. if (grid[x, y] >= 1)
  163. {
  164. Console.ForegroundColor = ConsoleColor.DarkGreen;
  165.  
  166. c = " ■";
  167.  
  168. if (grid[x, y] == snakeSize)
  169. Console.ForegroundColor = ConsoleColor.Green;
  170. }
  171.  
  172. if (grid[x, y] == -1)
  173. {
  174. Console.ForegroundColor = ConsoleColor.Red;
  175. c = " %";
  176. }
  177.  
  178. Console.Write(c);
  179. Console.ResetColor();
  180. }
  181. Console.WriteLine();
  182. }
  183. }
  184.  
  185. struct Coord
  186. {
  187. public int x, y;
  188.  
  189. public Coord(int X, int Y)
  190. {
  191. x = X;
  192. y = Y;
  193. }
  194. }
  195. }
  196. }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. ///Minesweeper
  210.  
  211. using System;
  212. using System.Collections.Generic;
  213. using System.Linq;
  214. using System.Text;
  215. using System.Threading.Tasks;
  216.  
  217. namespace minesweeper
  218. {
  219. class Program
  220. {
  221. static void Main(string[] args)
  222. {
  223. Program p = new Program();
  224. p.Start();
  225. }
  226.  
  227. int[,] grid;
  228. bool[,] revealed;
  229. bool[,] bombFlags;
  230.  
  231. bool bombsSpawned = false;
  232.  
  233. int gridHeight = 10, gridWidth = 10, bombCount = 15;
  234. Coord selected;
  235. Random random = new Random();
  236.  
  237. void Start()
  238. {
  239. bombsSpawned = false;
  240. grid = new int[gridWidth, gridHeight];
  241. revealed = new bool[gridWidth, gridHeight];
  242. bombFlags = new bool[gridWidth, gridHeight];
  243.  
  244. for (int y = 0; y < gridHeight - 1; y++)
  245. for (int x = 0; x < gridWidth - 1; x++)
  246. {
  247. grid[x, y] = 0;
  248. revealed[x, y] = false;
  249. bombFlags[x, y] = false;
  250. }
  251.  
  252. selected = new Coord(gridWidth / 2, gridHeight / 2);
  253.  
  254. Draw();
  255. ChooseAction();
  256. }
  257.  
  258. void ChooseAction()
  259. {
  260. switch (Console.ReadKey(true).Key)
  261. {
  262. case ConsoleKey.W:
  263. selected.y--;
  264. break;
  265.  
  266. case ConsoleKey.S:
  267. selected.y++;
  268. break;
  269.  
  270. case ConsoleKey.D:
  271. selected.x++;
  272. break;
  273.  
  274. case ConsoleKey.A:
  275. selected.x--;
  276. break;
  277.  
  278. case ConsoleKey.E:
  279. Reveal();
  280. break;
  281.  
  282. case ConsoleKey.Q:
  283. Flag();
  284. break;
  285. }
  286.  
  287. if (selected.y < 0)
  288. selected.y = gridHeight - 1;
  289. if (selected.y == gridHeight)
  290. selected.y = 0;
  291.  
  292. if (selected.x < 0)
  293. selected.x = gridWidth - 1;
  294. if (selected.x == gridWidth)
  295. selected.x = 0;
  296.  
  297. Draw();
  298.  
  299. }
  300.  
  301. void Flag()
  302. {
  303. if (!revealed[selected.x, selected.y] && bombsSpawned)
  304. {
  305. if (bombFlags[selected.x, selected.y])
  306. bombFlags[selected.x, selected.y] = false;
  307. else
  308. bombFlags[selected.x, selected.y] = true;
  309. }
  310. }
  311.  
  312. void RevealAdjacent(Coord cellToReveal)
  313. {
  314. Queue<Coord> toReveal = new Queue<Coord>();
  315. Queue<Coord> alreadyChecked = new Queue<Coord>();
  316.  
  317. toReveal.Enqueue(cellToReveal);
  318.  
  319. while (true)
  320. {
  321. if (toReveal.Count > 0)
  322. {
  323. Coord working = toReveal.Dequeue();
  324. alreadyChecked.Enqueue(working);
  325.  
  326. for (int x = working.x - 1; x <= working.x + 1; x++)
  327. for (int y = working.y - 1; y <= working.y + 1; y++)
  328. {
  329. if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight)
  330. {
  331. if (!bombFlags[x, y] && grid[x, y] != -1)
  332. revealed[x, y] = true;
  333.  
  334. Coord newCoord = new Coord(x, y);
  335.  
  336. if (grid[x, y] == 0 && !alreadyChecked.Contains(newCoord))
  337. toReveal.Enqueue(newCoord);
  338. }
  339. }
  340. }
  341. else
  342. break;
  343. }
  344. }
  345.  
  346. void Reveal()
  347. {
  348. if (!bombsSpawned)
  349. SpawnBombs();
  350.  
  351. if (!bombFlags[selected.x, selected.y])
  352. {
  353. if (grid[selected.x, selected.y] != -1)
  354. {
  355. revealed[selected.x, selected.y] = true;
  356.  
  357. if (grid[selected.x, selected.y] == 0)
  358. RevealAdjacent(selected);
  359. }
  360. else
  361. {
  362. DrawEndScreen(false);
  363. Start();
  364. }
  365. }
  366.  
  367. int revealedCount = 0, emptyCount = 0;
  368.  
  369. foreach (int i in grid)
  370. if (i != -1)
  371. emptyCount++;
  372.  
  373. foreach (bool b in revealed)
  374. if (b)
  375. revealedCount++;
  376.  
  377. if (revealedCount == emptyCount)
  378. {
  379. DrawEndScreen(true);
  380. Start();
  381. }
  382. }
  383.  
  384. void DrawEndScreen(bool win)
  385. {
  386. Console.Clear();
  387.  
  388. if (!win)
  389. Console.Write("\n You lost!");
  390. else
  391. {
  392. Console.ReadKey();
  393. Console.Write("\n You won!");
  394. }
  395.  
  396. Console.ReadKey();
  397. }
  398.  
  399. void Draw()
  400. {
  401. Console.Clear();
  402. string cell;
  403.  
  404. for (int y = 0; y < gridHeight; y++)
  405. {
  406. for (int x = 0; x < gridWidth; x++)
  407. {
  408. cell = " ■";
  409.  
  410. if (revealed[x, y])
  411. if (grid[x, y] > 0)
  412. cell = " " + grid[x, y];
  413. else if (grid[x, y] == 0)
  414. cell = " .";
  415.  
  416. if (bombFlags[x, y])
  417. {
  418. Console.BackgroundColor = ConsoleColor.Red;
  419. Console.ForegroundColor = ConsoleColor.Black;
  420. }
  421.  
  422. if (x == selected.x && y == selected.y)
  423. Console.BackgroundColor = ConsoleColor.DarkGreen;
  424.  
  425. Console.Write(cell);
  426. Console.ResetColor();
  427. }
  428. Console.WriteLine();
  429. }
  430.  
  431. ChooseAction();
  432. }
  433.  
  434. void SpawnBombs()
  435. {
  436. int count = bombCount;
  437.  
  438. while (count > 0)
  439. {
  440. Coord rand = new Coord(random.Next(0, gridWidth - 1), random.Next(0, gridHeight - 1));
  441.  
  442. if (grid[rand.x, rand.y] != -1 && rand.x != selected.x && rand.y != selected.y)
  443. {
  444. grid[rand.x, rand.y] = -1;
  445. count--;
  446. }
  447. }
  448.  
  449. for (int Y = 0; Y < gridHeight; Y++)
  450. for (int X = 0; X < gridWidth; X++)
  451. {
  452. for (int x = X - 1; x <= X + 1; x++)
  453. for (int y = Y - 1; y <= Y + 1; y++)
  454. {
  455. if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight)
  456. {
  457. if (grid[X, Y] != -1)
  458. if (grid[x, y] == -1)
  459. grid[X, Y]++;
  460. }
  461. }
  462. }
  463.  
  464. RevealAdjacent(selected);
  465. bombsSpawned = true;
  466. }
  467.  
  468. struct Coord
  469. {
  470. public int x, y;
  471.  
  472. public Coord(int X, int Y)
  473. {
  474. x = X;
  475. y = Y;
  476. }
  477. }
  478. }
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement