Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.85 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 Task2SholeHamokshim
  12. {
  13. public partial class Form1 : Form
  14. {
  15. int boardSize; // המשתנה יכלול כמה כפתורים יהיו בצלע אחת של הלוח
  16. int mokeshCount; // המשתנה יכלול כמה מוקשים יהיו בלוח
  17. Button[] levels = new Button[3]; // המערך כולל 3 כפתורים שהמשתמש יבחר בעזרתם את רמת המשחק
  18. Button discovery = new Button(); // הכפתור יגיד למשתמש אם הוא במצב של זיהוי מוקשים או חשיפת משבצות ריקות
  19. Label text = new Label();
  20. bool isOver = false; // המשתנה יגיד אם נגמר המשחק
  21. int[] inTheBoard; // המערך ישמור בתוכו מה יש מאחורי הכפתורים
  22. Button[] board; // המערך ירכיב את לוח המשחק
  23. Button newGame; // אם המשתמש ילחץ על הכפתור, יתחיל משחק חדש
  24. public Form1()
  25. {
  26. InitializeComponent();
  27. chooseLevel();
  28. }
  29. // בתום הפונקציה המשתמש יבחר רמת משחק והפונקציה תפעיל את הפונקציה שמציירת את הלוח
  30. public void chooseLevel()
  31. {
  32. text.Font = new Font("David", 22);
  33. text.Location = new Point(725, 150);
  34. text.Width = 500;
  35. text.Text = "SELECT A DIFFICULTY LEVEL";
  36. Controls.Add(text);
  37.  
  38.  
  39. for (int i = 0; i < levels.Length; i++) // יוצר את הכפתורים שבעזרתם נבחר את מהלך המשחק
  40. {
  41. levels[i] = new Button();
  42. levels[i].Font = new Font("David", 20);
  43. levels[i].Width = 200;
  44. levels[i].Height = 200;
  45. levels[i].Location = new Point(825, 250 + i * 215);
  46. levels[i].Click += drawBoard;
  47. Controls.Add(levels[i]);
  48. }
  49. levels[0].Text = "EASY";
  50. levels[1].Text = "MEDIUM";
  51. levels[2].Text = "HARD";
  52. levels[0].BackColor = Color.Green;
  53. levels[1].BackColor = Color.Orange;
  54. levels[2].BackColor = Color.Red;
  55. }
  56.  
  57. // הפונקציה מציירת את הלוח ומפעילה פונקציות ששמות בו מוקשים ומספרים רגילים
  58. public void drawBoard(object sender, EventArgs e)
  59. {
  60. blockDetection();
  61. playAgain();
  62. for (int i = 0; i < 3; i++) // מוחק את הכפתורים של בחירת השלב
  63. {
  64. this.Controls.Remove(levels[i]);
  65. }
  66. this.Controls.Remove(text);
  67. Button b = (Button)(sender);
  68. if (b.Text == "EASY")
  69. {
  70. boardSize = 6;
  71. mokeshCount = 10;
  72. }
  73. if (b.Text == "MEDIUM")
  74. {
  75. boardSize = 8;
  76. mokeshCount = 25;
  77. }
  78. if (b.Text == "HARD")
  79. {
  80. boardSize = 12;
  81. mokeshCount = 50;
  82. }
  83. int buttonSize = 85;
  84. int y = 0;
  85.  
  86. board = new Button[boardSize * boardSize];
  87. for (int i = 0; i < board.Length; i++) // מצייר את הלוח
  88. {
  89. board[i] = new Button();
  90. board[i].Width = buttonSize;
  91. board[i].Height = buttonSize;
  92. board[i].Name = i.ToString();
  93. board[i].Click += whenBoardPushed;
  94. board[i].Location = new Point((i % boardSize) * buttonSize, y);
  95.  
  96. if ((i + 1) % boardSize == 0)
  97. {
  98. y += buttonSize;
  99. }
  100. Controls.Add(board[i]);
  101. }
  102. mokshim(); // שם מוקשים
  103. putNumbersInBoard();// בודק בכמה מוקשים כל ריבוע נוגע
  104. }
  105. // מפזר מוקשים בלוח באופן רנדומלי
  106. public void mokshim()
  107. {
  108. inTheBoard = new int[board.Length]; // יוצר את המערך שיכיל מה יש בכל משבצת, ומאתחל את כל התאים לאפס
  109. for (int i = 0; i < inTheBoard.Length; i++)
  110. inTheBoard[i] = 0;
  111.  
  112. int[] boardIndexes = new int[boardSize * boardSize]; // המערך ישמור את האינדקסים של הכפתורים המרכיבים את הלוח כדי שנוכל לסדר אותם באפן אקראי ולשים בהם מוקשים
  113. for (int i = 0; i < boardIndexes.Length; i++)
  114. {
  115. boardIndexes[i] = i;
  116. }
  117. int rndNum; // המשתנה שומר בתוכו מספר אקראי
  118. int index; // המשתנה ישמור בתוכו את הערך של התא במקום האיי
  119. Random rnd = new Random();
  120. for (int i = 0; i < boardIndexes.Length; i++) // מבלגן את האינדקסים
  121. {
  122. rndNum = rnd.Next(0, boardIndexes.Length);
  123. index = boardIndexes[i];
  124. boardIndexes[i] = boardIndexes[rndNum];
  125. boardIndexes[rndNum] = index;
  126. }
  127. for (int i = 0; i < mokeshCount; i++) // שם בכל הריבועים שנבחרו -1
  128. {
  129. inTheBoard[boardIndexes[i]] = -1;
  130. }
  131.  
  132. }
  133. // בודק בכמה מוקשים המשבצת נוגעת
  134. public void putNumbersInBoard()
  135. {
  136. int mokshimTouch = 0; // המשתנה ישמור בתוכו בכמה מוקשים המשבצת תיגע
  137. for (int i = 0; i < board.Length; i++)
  138. {
  139. mokshimTouch = 0;
  140. if (inTheBoard[i] != -1)
  141. {
  142. // אלכסון למעלה שמאלה
  143. if (i % boardSize != 0 && i >= boardSize)
  144. {
  145. if (inTheBoard[i - boardSize - 1] == -1)
  146. mokshimTouch++;
  147. }
  148. // למעלה
  149. if (i >= boardSize)
  150. {
  151. if (inTheBoard[i - boardSize] == -1)
  152. mokshimTouch++;
  153. }
  154. // אלכסון למעלה ימינה
  155. if (i >= boardSize && (i + 1) % boardSize != 0)
  156. {
  157. if (inTheBoard[i - boardSize + 1] == -1)
  158. mokshimTouch++;
  159. }
  160. // שמאלה
  161. if (i % boardSize != 0)
  162. {
  163. if (inTheBoard[i - 1] == -1)
  164. mokshimTouch++;
  165. }
  166. // ימינה
  167. if ((i + 1) % boardSize != 0)
  168. {
  169. if (inTheBoard[i + 1] == -1)
  170. mokshimTouch++;
  171. }
  172. // אלכסון למטה שמאלה
  173. if (board.Length - boardSize > i && i % boardSize != 0)
  174. {
  175. if (inTheBoard[i + boardSize - 1] == -1)
  176. mokshimTouch++;
  177. }
  178. // למטה
  179. if (board.Length - boardSize > i)
  180. {
  181. if (inTheBoard[i + boardSize] == -1)
  182. mokshimTouch++;
  183. }
  184. // אלכסון למטה ימינה
  185. if (board.Length - boardSize > i && (i + 1) % boardSize != 0)
  186. {
  187. if (inTheBoard[i + boardSize + 1] == -1)
  188. mokshimTouch++;
  189. }
  190. inTheBoard[i] = mokshimTouch;
  191. //board[i].Text = mokshimTouch.ToString();
  192.  
  193. }
  194. }
  195. }
  196. // הפונקציה יוצרת כפתור שאומר למשתמש אם הוא במצב של גילוי משבצות או של גילוי מוקשים
  197. public void blockDetection()
  198. {
  199. discovery = new Button();
  200. discovery.Font = new Font("David", 15);
  201. discovery.Location = new Point(1300, 450);
  202. discovery.Width = 200;
  203. discovery.Height = 100;
  204. discovery.Text = "You are in a state: clean space detection";
  205. discovery.Click += changeState;
  206. Controls.Add(discovery);
  207. }
  208. // משנה את המצב של הכפתור בכל פעם שלוחצים עליו
  209. public void changeState(object sender, EventArgs e)
  210. {
  211. if (discovery.Text == "You are in a state: clean space detection")
  212. discovery.Text = "You are in a state: find mines";
  213. else
  214. discovery.Text = "You are in a state: clean space detection";
  215. }
  216. // הפונקציה מופעלת בכל פעם שאחד מהכפתורים שבלוח נלחץ
  217. public void whenBoardPushed(object sender, EventArgs e)
  218. {
  219. if (isOver == false)
  220. {
  221. Button b = (Button)sender;
  222. string name = b.Name;
  223. int btnNum = int.Parse(b.Name); // ישמור את האינדקס של הכפתור שנלחץ
  224. if (discovery.Text == "You are in a state: clean space detection") // אם במצב של גילוי שטח נקי
  225. {
  226. if (inTheBoard[btnNum] == -1) // אם נלחץ מוקש
  227. {
  228. isOver = true; // המשחק נגמר
  229. MessageBox.Show("boom");
  230. }
  231. else
  232. {
  233. openButton(btnNum); // מפעיל את הפונקציה שחושפת מה יש מאחורי המשבצת
  234. }
  235. }
  236. else // אם במצב של גילוי מוקשים
  237. {
  238. board[btnNum].Text = "-1"; // שם בכפתור -1
  239. board[btnNum].BackColor = Color.Red; // משנה את צבעו לאדום
  240. }
  241. checkVictory(); // בודק אם נגמר המשחק
  242. }
  243. }
  244. // חושף מה יש מאחורי המשבצת, אם זה 0 בודק את השכנים שלו ואם הם לא לחוצים, הוא מפעיל את הפונקציה עליהם וכן הלאה עד שכל השכנים הם מוקשים או מספרים
  245. public void cleanPlace(int btnNum)
  246. {
  247. // אלכסון למעלה שמאלה
  248. if (btnNum % boardSize != 0 && btnNum >= boardSize)
  249. {
  250. if (board[btnNum - boardSize - 1].Enabled == true)
  251. openButton(btnNum - boardSize - 1);
  252. }
  253. // למעלה
  254. if (btnNum > boardSize)
  255. {
  256. if (board[btnNum - boardSize].Enabled == true)
  257. openButton(btnNum - boardSize);
  258. }
  259. // אלכסון למעלה ימינה
  260. if (btnNum >= boardSize && (btnNum + 1) % boardSize != 0)
  261. {
  262. if (board[btnNum - boardSize + 1].Enabled == true)
  263. openButton(btnNum - boardSize + 1);
  264. }
  265. // שמאלה
  266. if (btnNum % boardSize != 0 && btnNum != 0)
  267. {
  268. if (board[btnNum - 1].Enabled == true)
  269. openButton(btnNum - 1);
  270. }
  271. // ימינה
  272. if ((btnNum + 1) % boardSize != 0)
  273. {
  274. if (board[btnNum + 1].Enabled == true)
  275. openButton(btnNum + 1);
  276. }
  277. // אלכסון למטה שמאלה
  278. if (board.Length - boardSize > btnNum && btnNum % boardSize != 0)
  279. {
  280. if (board[btnNum + boardSize - 1].Enabled == true)
  281. openButton(btnNum + boardSize - 1);
  282. }
  283. // למטה
  284. if (board.Length - boardSize > btnNum)
  285. {
  286. if (board[btnNum + boardSize].Enabled == true)
  287. openButton(btnNum + boardSize);
  288. }
  289. // אלכסון למטה ימינה
  290. if (board.Length - boardSize > btnNum && (btnNum + 1) % boardSize != 0)
  291. {
  292. if (board[btnNum + boardSize + 1].Enabled == true)
  293. openButton(btnNum + boardSize + 1);
  294. }
  295. }
  296. // בודק אם הלוח מלא, אם כן בודק אם נכון, אם לא ממשיך במשחק
  297. public bool checkVictory()
  298. {
  299. Label end;
  300. int isPlayerSmart = 0; // המשתנה יבדוק אם המשתמש מילא נכון את הלוח
  301. for (int i = 0; i < board.Length; i++)
  302. {
  303. if (board[i].Text == "")
  304. return false; // הלוח לא מלא
  305. }
  306. for (int i = 0; i < board.Length; i++)
  307. {
  308. if (board[i].Text == inTheBoard[i].ToString()) // אם המשתמש סימן נכון את המשבצת
  309. {
  310. isPlayerSmart++;
  311. }
  312. }
  313. end = new Label();
  314. end.Location = new Point(700, 400);
  315. end.BackColor = Color.Purple;
  316. end.Width = 180;
  317. end.Height = 50;
  318. end.Font = new Font("David", 20);
  319.  
  320. if (isPlayerSmart == board.Length) // הכל נכון
  321. {
  322. end.Text = "YOU WON!!!";
  323. isOver = true; // נגמר המשחק
  324. Controls.Add(end);
  325. return true;
  326. }
  327. // המשתמש מילא לא נכון
  328. end.Text = "You Lose";
  329. isOver = true;
  330. Controls.Add(end);
  331. return true; // נגמר המשחק
  332. }
  333. // יוצר כפתור שאם נלחץ עלוי יתחיל משחק חדש
  334. public void playAgain()
  335. {
  336. newGame = new Button();
  337. newGame.Location = new Point(1300, 700);
  338. newGame.Width = 200;
  339. newGame.Height = 100;
  340. newGame.BackColor = Color.Green;
  341. newGame.Text = "START NEW GAME";
  342. newGame.Click += playNewGame;
  343. Controls.Add(newGame);
  344. }
  345. // נלחץ הכפתור ומתחיל משחק חדש
  346. public void playNewGame(object sender, EventArgs e)
  347. {
  348. isOver = false; // המשחק מתחיל מההתחלה
  349. this.Controls.Remove(newGame); // מוחק כפתורים
  350. this.Controls.Remove(discovery);
  351. for (int i = 0; i < board.Length; i++)
  352. {
  353. this.Controls.Remove(board[i]);
  354. }
  355. chooseLevel(); // מפעיל את המשחק מההתחלה
  356. }
  357. // הפונקציה פותחת את הכפתור שנלחץ וחושפת מה יש מאחריו
  358. public void openButton(int btnNum)
  359. {
  360. board[btnNum].Enabled = false;
  361. board[btnNum].Text = inTheBoard[btnNum].ToString();
  362. if (inTheBoard[btnNum] == 0)
  363. {
  364. cleanPlace(btnNum);
  365. }
  366. }
  367. }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement