Niksi

brmmm

Jun 7th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Minesweeper
  12. {
  13. public partial class Game : Form
  14. {
  15.  
  16. Button[,] btn = new Button[41, 41];
  17. int[,] btn_prop = new int[41, 41];
  18. int[,] saved_btn_prop = new int[41, 41];
  19. Point coord;
  20.  
  21. bool firstPlay = true;
  22. bool gameover = false;
  23.  
  24. //Time
  25. int seconds = 0;
  26. int minutes = 0;
  27.  
  28. //Points that are around
  29. int[] dx8 = { 1, 0, -1, 0, 1, -1, -1, 1 };
  30. int[] dy8 = { 0, 1, 0, -1, 1, -1, 1, -1 };
  31.  
  32. //Table aspect
  33. int start_x, start_y;
  34. int height, width;
  35.  
  36. //Game variables
  37. int mines;
  38. int flag_value = 10;
  39. int gameDifficulty;
  40. int flags;
  41.  
  42. //Button aspect
  43. int buttonSize = 20;
  44. int distance_between = 20;
  45.  
  46. //Coeficients for difficulty
  47. double easyCoef = 0.1f;
  48. double mediumCoef = 0.2f;
  49. double hardCoef = 0.3f;
  50.  
  51. public Game()
  52. {
  53. InitializeComponent();
  54. }
  55.  
  56. private void Game_Load(object sender, EventArgs e)
  57. {
  58.  
  59. }
  60.  
  61. void set_ButtonImage(int x, int y)
  62. {
  63. btn[x, y].Enabled = false;
  64. btn[x, y].BackgroundImageLayout = ImageLayout.Stretch;
  65.  
  66. if (gameover && btn_prop[x,y] == flag_value)
  67. btn_prop[x, y] = saved_btn_prop[x, y];
  68.  
  69. if (gameover)
  70. timer.Stop();
  71.  
  72. switch (btn_prop[x, y])
  73. {
  74. case 0:
  75. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources.blank;
  76. EmptySpace(x, y);
  77. break;
  78.  
  79. case 1:
  80. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._1;
  81. break;
  82.  
  83. case 2:
  84. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._2;
  85. break;
  86.  
  87. case 3:
  88. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._3;
  89. break;
  90.  
  91. case 4:
  92. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._4;
  93. break;
  94.  
  95. case 5:
  96. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._5;
  97. break;
  98.  
  99. case 6:
  100. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._6;
  101. break;
  102.  
  103. case 7:
  104. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._7;
  105. break;
  106.  
  107. case 8:
  108. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources._8;
  109. break;
  110.  
  111. case -1:
  112. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources.bmb;
  113. if(!gameover)
  114. GameOver();
  115. break;
  116. }
  117.  
  118. }
  119.  
  120. int isPointOnMap(int x, int y)
  121. {
  122. if (x < 1 || x > width || y < 1 || y > height)
  123. return 0;
  124. return 1;
  125. }
  126.  
  127. void EmptySpace(int x, int y)
  128. {
  129. if(btn_prop[x,y] == 0)
  130. {
  131. for (int i = 0; i < 8; i++)
  132. {
  133. int cx = x + dx8[i];
  134. int cy = y + dy8[i];
  135.  
  136. if (isPointOnMap(cx, cy) == 1)
  137. if (btn[cx, cy].Enabled == true && btn_prop[cx, cy] != -1 && !gameover)
  138. {
  139. gameProgress.Value++;
  140. score.Text = "Score: " + gameProgress.Value.ToString();
  141. set_ButtonImage(cx, cy);
  142. }
  143. }
  144. }
  145. }
  146.  
  147. void Discover_Map()
  148. {
  149. for (int i = 1; i <= width; i++)
  150. for (int j = 1; j <= height; j++)
  151. if (btn[i, j].Enabled == true)
  152. {
  153. set_ButtonImage(i, j);
  154. }
  155. }
  156.  
  157. void GameOver()
  158. {
  159. gameover = true;
  160. Discover_Map();
  161. MessageBox.Show("Game Over !");
  162. }
  163.  
  164. void Check_FlagWin()
  165. {
  166. bool win = true;
  167.  
  168. for (int i = 1; i <= width; i++)
  169. for (int j = 1; j <= height; j++)
  170. if (btn_prop[i, j] == -1)
  171. win = false;
  172.  
  173. if (win)
  174. {
  175. WinGame();
  176. }
  177. }
  178.  
  179.  
  180. void WinGame()
  181. {
  182. gameover = true;
  183. Discover_Map();
  184. gameProgress.Value = 0;
  185. MessageBox.Show("Win !");
  186. }
  187.  
  188. void Check_ClickWin()
  189. {
  190. bool win = true;
  191. for (int i = 1; i <= width; i++)
  192. for (int j = 1; j <= height; j++)
  193. if (btn[i,j].Enabled == true && saved_btn_prop[i,j] != -1)
  194. win = false;
  195.  
  196. if (win)
  197. {
  198. WinGame();
  199. }
  200. }
  201.  
  202. private void OneClick(object sender, EventArgs e)
  203. {
  204. coord = ((Button)sender).Location;
  205. int x = (coord.X - start_x) / buttonSize;
  206. int y = (coord.Y - start_y) / buttonSize;
  207.  
  208. if (btn_prop[x, y] != flag_value)
  209. {
  210.  
  211. ((Button)sender).Enabled = false;
  212. ((Button)sender).Text = "";
  213.  
  214. ((Button)sender).BackgroundImageLayout = ImageLayout.Stretch;
  215.  
  216. if (btn_prop[x, y] != -1 && !gameover)
  217. {
  218. gameProgress.Value++;
  219. score.Text = "Score: " + gameProgress.Value.ToString();
  220. Check_ClickWin();
  221. }
  222.  
  223. set_ButtonImage(x, y);
  224. }
  225. }
  226.  
  227. int MinesAround(int x, int y)
  228. {
  229. int score = 0;
  230. for(int i = 0; i < 8; i++)
  231. {
  232. int cx = x + dx8[i];
  233. int cy = y + dy8[i];
  234.  
  235. if (isPointOnMap(cx, cy) == 1 && btn_prop[cx, cy] == -1)
  236. score++;
  237. }
  238. return score;
  239. }
  240.  
  241. void SetMapNumbers(int x, int y)
  242. {
  243. for (int i = 1; i <= x; i++)
  244. for (int j = 1; j <= y; j++)
  245. if (btn_prop[i, j] != -1)
  246. {
  247. btn_prop[i, j] = MinesAround(i, j);
  248. //Debugging
  249. //btn[i, j].Text = btn_prop[i, j].ToString();
  250. saved_btn_prop[i, j] = MinesAround(i, j);
  251. }
  252. }
  253.  
  254. private void RightClick(object sender, MouseEventArgs e)
  255. {
  256.  
  257. if (e.Button == MouseButtons.Right)
  258. {
  259.  
  260. coord = ((Button)sender).Location;
  261. int x = (coord.X - start_x) / buttonSize;
  262. int y = (coord.Y - start_y) / buttonSize;
  263.  
  264. if (btn_prop[x, y] != flag_value && flags > 0)
  265. {
  266. btn[x, y].BackgroundImageLayout = ImageLayout.Stretch;
  267. btn[x, y].BackgroundImage = Minesweeper.Properties.Resources.flag;
  268. btn_prop[x, y] = flag_value;
  269. flags--;
  270. Check_FlagWin();
  271. }
  272. else
  273. if (btn_prop[x, y] == flag_value)
  274. {
  275. btn_prop[x, y] = saved_btn_prop[x, y];
  276. btn[x, y].BackgroundImageLayout = ImageLayout.Stretch;
  277. btn[x, y].BackgroundImage = null;
  278. flags++;
  279. }
  280.  
  281. remainingFlags.Text = "Flags: " + flags;
  282. }
  283. }
  284.  
  285. void CreateButtons(int x, int y)
  286. {
  287. for(int i = 1; i <= x; i++)
  288. for(int j = 1; j <= y; j++)
  289. {
  290. btn[i, j] = new Button();
  291. btn[i, j].SetBounds(i * buttonSize + start_x, j * buttonSize + start_y, distance_between, distance_between);
  292. btn[i, j].Click += new EventHandler(OneClick);
  293. btn[i, j].MouseUp += new MouseEventHandler(RightClick);
  294. btn_prop[i, j] = 0;
  295. saved_btn_prop[i, j] = 0;
  296. btn[i, j].TabStop = false;
  297. Controls.Add(btn[i, j]);
  298. }
  299. }
  300.  
  301. void GenerateMap(int x, int y, int mines)
  302. {
  303. Random rand = new Random();
  304. List<int> coordx = new List<int>();
  305. List<int> coordy = new List<int>();
  306.  
  307. while(mines > 0)
  308. {
  309. coordx.Clear();
  310. coordy.Clear();
  311.  
  312. for (int i = 1; i <= x; i++)
  313. for (int j = 1; j <= y; j++)
  314. if(btn_prop[i,j] != -1)
  315. {
  316. coordx.Add(i);
  317. coordy.Add(j);
  318. }
  319.  
  320. int randNum = rand.Next(0, coordx.Count);
  321. btn_prop[coordx[randNum], coordy[randNum]] = -1;
  322. saved_btn_prop[coordx[randNum], coordy[randNum]] = -1;
  323. mines--;
  324. }
  325. }
  326.  
  327. void StartGame()
  328. {
  329. switch (difficulty.Text)
  330. {
  331. case "Easy":
  332. mines = (int)(height * width * easyCoef);
  333. break;
  334.  
  335. case "Medium":
  336. mines = (int)(height * width * mediumCoef);
  337. break;
  338.  
  339. case "Hard":
  340. mines = (int)(height * width * hardCoef);
  341. break;
  342. }
  343.  
  344. flags = mines;
  345. gameover = false;
  346.  
  347. gameProgress.Value = 0;
  348. gameProgress.Maximum = height * width - mines;
  349.  
  350. timer.Start();
  351. seconds = 0;
  352. minutes = 0;
  353.  
  354. remainingFlags.Text = "Flags: " + flags;
  355. score.Text = "Score: " + 0;
  356.  
  357. if(firstPlay)
  358. CreateButtons(width, height);
  359.  
  360. GenerateMap(width, height, mines);
  361. SetMapNumbers(width, height);
  362.  
  363. }
  364.  
  365. void ResetGame(int x, int y)
  366. {
  367. for(int i = 1; i <= x; i++)
  368. for (int j = 1; j <= y; j++)
  369. {
  370. btn[i, j].Enabled = true;
  371. btn[i, j].Text = "";
  372. btn[i, j].BackgroundImage = null;
  373. btn_prop[i, j] = 0;
  374. saved_btn_prop[i, j] = 0;
  375. }
  376. }
  377.  
  378. void Warnings(int id)
  379. {
  380. switch (id)
  381. {
  382. case 1:
  383. MessageBox.Show("Empty Fields !");
  384. break;
  385. case 2:
  386. MessageBox.Show("Wrong Input !");
  387. break;
  388.  
  389. }
  390. }
  391.  
  392. bool hasLetters(string s)
  393. {
  394. for (int i = 0; i < s.Length; i++)
  395. if (!Char.IsDigit(s, i))
  396. return true;
  397.  
  398. return false;
  399. }
  400.  
  401. bool CorrectFields()
  402. {
  403. bool result = true;
  404.  
  405. if (heightBox.Text == "" || widthBox.Text == "" || difficulty.Text == "")
  406. {
  407. Warnings(1);
  408. result = false;
  409. }
  410. else
  411. if (heightBox.Text != "" && widthBox.Text != "" && difficulty.Text != "")
  412. {
  413. if (hasLetters(heightBox.Text) || hasLetters(widthBox.Text))
  414. {
  415. Warnings(2);
  416. result = false;
  417. }
  418. }
  419.  
  420. return result;
  421. }
  422.  
  423. void SetDimensions()
  424. {
  425. height = int.Parse(heightBox.Text);
  426. width = int.Parse(widthBox.Text);
  427.  
  428. if (height > 25)
  429. height = 25;
  430. else
  431. if (height < 5)
  432. height = 5;
  433.  
  434. if (width > 40)
  435. width = 40;
  436. else
  437. if (width < 5)
  438. width = 5;
  439.  
  440. heightBox.Text = height.ToString();
  441. widthBox.Text = width.ToString();
  442.  
  443. }
  444.  
  445. void TableMargins(int x, int y)
  446. {
  447. start_x = (this.Size.Width - (width + 2) * distance_between) / 2;
  448. start_y = (this.Size.Height - (height + 2) * distance_between) / 2;
  449. }
  450.  
  451.  
  452. private void Play_Click(object sender, EventArgs e)
  453. {
  454.  
  455. if (CorrectFields())
  456. {
  457.  
  458. SetDimensions();
  459. TableMargins(height, width);
  460.  
  461. if (firstPlay)
  462. {
  463. StartGame();
  464. firstPlay = false;
  465.  
  466. widthBox.Enabled = false;
  467. heightBox.Enabled = false;
  468. }
  469. else
  470. if (!firstPlay)
  471. {
  472. ResetGame(width, height);
  473. StartGame();
  474. }
  475. }
  476.  
  477. }
  478.  
  479. private void timer_Tick(object sender, EventArgs e)
  480. {
  481. seconds++;
  482.  
  483. if (seconds == 60)
  484. {
  485. minutes++;
  486. seconds = 0;
  487. }
  488.  
  489. secondsBox.Text = seconds.ToString();
  490. minutesBox.Text = minutes.ToString();
  491. }
  492.  
  493. }
  494. }
Advertisement
Add Comment
Please, Sign In to add comment