Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Task2SholeHamokshim
- {
- public partial class Form1 : Form
- {
- int boardSize; // המשתנה יכלול כמה כפתורים יהיו בצלע אחת של הלוח
- int mokeshCount; // המשתנה יכלול כמה מוקשים יהיו בלוח
- Button[] levels = new Button[3]; // המערך כולל 3 כפתורים שהמשתמש יבחר בעזרתם את רמת המשחק
- Button discovery = new Button(); // הכפתור יגיד למשתמש אם הוא במצב של זיהוי מוקשים או חשיפת משבצות ריקות
- Label text = new Label();
- bool isOver = false; // המשתנה יגיד אם נגמר המשחק
- int[] inTheBoard; // המערך ישמור בתוכו מה יש מאחורי הכפתורים
- Button[] board; // המערך ירכיב את לוח המשחק
- Button newGame; // אם המשתמש ילחץ על הכפתור, יתחיל משחק חדש
- public Form1()
- {
- InitializeComponent();
- chooseLevel();
- }
- // בתום הפונקציה המשתמש יבחר רמת משחק והפונקציה תפעיל את הפונקציה שמציירת את הלוח
- public void chooseLevel()
- {
- text.Font = new Font("David", 22);
- text.Location = new Point(725, 150);
- text.Width = 500;
- text.Text = "SELECT A DIFFICULTY LEVEL";
- Controls.Add(text);
- for (int i = 0; i < levels.Length; i++) // יוצר את הכפתורים שבעזרתם נבחר את מהלך המשחק
- {
- levels[i] = new Button();
- levels[i].Font = new Font("David", 20);
- levels[i].Width = 200;
- levels[i].Height = 200;
- levels[i].Location = new Point(825, 250 + i * 215);
- levels[i].Click += drawBoard;
- Controls.Add(levels[i]);
- }
- levels[0].Text = "EASY";
- levels[1].Text = "MEDIUM";
- levels[2].Text = "HARD";
- levels[0].BackColor = Color.Green;
- levels[1].BackColor = Color.Orange;
- levels[2].BackColor = Color.Red;
- }
- // הפונקציה מציירת את הלוח ומפעילה פונקציות ששמות בו מוקשים ומספרים רגילים
- public void drawBoard(object sender, EventArgs e)
- {
- blockDetection();
- playAgain();
- for (int i = 0; i < 3; i++) // מוחק את הכפתורים של בחירת השלב
- {
- this.Controls.Remove(levels[i]);
- }
- this.Controls.Remove(text);
- Button b = (Button)(sender);
- if (b.Text == "EASY")
- {
- boardSize = 6;
- mokeshCount = 10;
- }
- if (b.Text == "MEDIUM")
- {
- boardSize = 8;
- mokeshCount = 25;
- }
- if (b.Text == "HARD")
- {
- boardSize = 12;
- mokeshCount = 50;
- }
- int buttonSize = 85;
- int y = 0;
- board = new Button[boardSize * boardSize];
- for (int i = 0; i < board.Length; i++) // מצייר את הלוח
- {
- board[i] = new Button();
- board[i].Width = buttonSize;
- board[i].Height = buttonSize;
- board[i].Name = i.ToString();
- board[i].Click += whenBoardPushed;
- board[i].Location = new Point((i % boardSize) * buttonSize, y);
- if ((i + 1) % boardSize == 0)
- {
- y += buttonSize;
- }
- Controls.Add(board[i]);
- }
- mokshim(); // שם מוקשים
- putNumbersInBoard();// בודק בכמה מוקשים כל ריבוע נוגע
- }
- // מפזר מוקשים בלוח באופן רנדומלי
- public void mokshim()
- {
- inTheBoard = new int[board.Length]; // יוצר את המערך שיכיל מה יש בכל משבצת, ומאתחל את כל התאים לאפס
- for (int i = 0; i < inTheBoard.Length; i++)
- inTheBoard[i] = 0;
- int[] boardIndexes = new int[boardSize * boardSize]; // המערך ישמור את האינדקסים של הכפתורים המרכיבים את הלוח כדי שנוכל לסדר אותם באפן אקראי ולשים בהם מוקשים
- for (int i = 0; i < boardIndexes.Length; i++)
- {
- boardIndexes[i] = i;
- }
- int rndNum; // המשתנה שומר בתוכו מספר אקראי
- int index; // המשתנה ישמור בתוכו את הערך של התא במקום האיי
- Random rnd = new Random();
- for (int i = 0; i < boardIndexes.Length; i++) // מבלגן את האינדקסים
- {
- rndNum = rnd.Next(0, boardIndexes.Length);
- index = boardIndexes[i];
- boardIndexes[i] = boardIndexes[rndNum];
- boardIndexes[rndNum] = index;
- }
- for (int i = 0; i < mokeshCount; i++) // שם בכל הריבועים שנבחרו -1
- {
- inTheBoard[boardIndexes[i]] = -1;
- }
- }
- // בודק בכמה מוקשים המשבצת נוגעת
- public void putNumbersInBoard()
- {
- int mokshimTouch = 0; // המשתנה ישמור בתוכו בכמה מוקשים המשבצת תיגע
- for (int i = 0; i < board.Length; i++)
- {
- mokshimTouch = 0;
- if (inTheBoard[i] != -1)
- {
- // אלכסון למעלה שמאלה
- if (i % boardSize != 0 && i >= boardSize)
- {
- if (inTheBoard[i - boardSize - 1] == -1)
- mokshimTouch++;
- }
- // למעלה
- if (i >= boardSize)
- {
- if (inTheBoard[i - boardSize] == -1)
- mokshimTouch++;
- }
- // אלכסון למעלה ימינה
- if (i >= boardSize && (i + 1) % boardSize != 0)
- {
- if (inTheBoard[i - boardSize + 1] == -1)
- mokshimTouch++;
- }
- // שמאלה
- if (i % boardSize != 0)
- {
- if (inTheBoard[i - 1] == -1)
- mokshimTouch++;
- }
- // ימינה
- if ((i + 1) % boardSize != 0)
- {
- if (inTheBoard[i + 1] == -1)
- mokshimTouch++;
- }
- // אלכסון למטה שמאלה
- if (board.Length - boardSize > i && i % boardSize != 0)
- {
- if (inTheBoard[i + boardSize - 1] == -1)
- mokshimTouch++;
- }
- // למטה
- if (board.Length - boardSize > i)
- {
- if (inTheBoard[i + boardSize] == -1)
- mokshimTouch++;
- }
- // אלכסון למטה ימינה
- if (board.Length - boardSize > i && (i + 1) % boardSize != 0)
- {
- if (inTheBoard[i + boardSize + 1] == -1)
- mokshimTouch++;
- }
- inTheBoard[i] = mokshimTouch;
- //board[i].Text = mokshimTouch.ToString();
- }
- }
- }
- // הפונקציה יוצרת כפתור שאומר למשתמש אם הוא במצב של גילוי משבצות או של גילוי מוקשים
- public void blockDetection()
- {
- discovery = new Button();
- discovery.Font = new Font("David", 15);
- discovery.Location = new Point(1300, 450);
- discovery.Width = 200;
- discovery.Height = 100;
- discovery.Text = "You are in a state: clean space detection";
- discovery.Click += changeState;
- Controls.Add(discovery);
- }
- // משנה את המצב של הכפתור בכל פעם שלוחצים עליו
- public void changeState(object sender, EventArgs e)
- {
- if (discovery.Text == "You are in a state: clean space detection")
- discovery.Text = "You are in a state: find mines";
- else
- discovery.Text = "You are in a state: clean space detection";
- }
- // הפונקציה מופעלת בכל פעם שאחד מהכפתורים שבלוח נלחץ
- public void whenBoardPushed(object sender, EventArgs e)
- {
- if (isOver == false)
- {
- Button b = (Button)sender;
- string name = b.Name;
- int btnNum = int.Parse(b.Name); // ישמור את האינדקס של הכפתור שנלחץ
- if (discovery.Text == "You are in a state: clean space detection") // אם במצב של גילוי שטח נקי
- {
- if (inTheBoard[btnNum] == -1) // אם נלחץ מוקש
- {
- isOver = true; // המשחק נגמר
- MessageBox.Show("boom");
- }
- else
- {
- openButton(btnNum); // מפעיל את הפונקציה שחושפת מה יש מאחורי המשבצת
- }
- }
- else // אם במצב של גילוי מוקשים
- {
- board[btnNum].Text = "-1"; // שם בכפתור -1
- board[btnNum].BackColor = Color.Red; // משנה את צבעו לאדום
- }
- checkVictory(); // בודק אם נגמר המשחק
- }
- }
- // חושף מה יש מאחורי המשבצת, אם זה 0 בודק את השכנים שלו ואם הם לא לחוצים, הוא מפעיל את הפונקציה עליהם וכן הלאה עד שכל השכנים הם מוקשים או מספרים
- public void cleanPlace(int btnNum)
- {
- // אלכסון למעלה שמאלה
- if (btnNum % boardSize != 0 && btnNum >= boardSize)
- {
- if (board[btnNum - boardSize - 1].Enabled == true)
- openButton(btnNum - boardSize - 1);
- }
- // למעלה
- if (btnNum > boardSize)
- {
- if (board[btnNum - boardSize].Enabled == true)
- openButton(btnNum - boardSize);
- }
- // אלכסון למעלה ימינה
- if (btnNum >= boardSize && (btnNum + 1) % boardSize != 0)
- {
- if (board[btnNum - boardSize + 1].Enabled == true)
- openButton(btnNum - boardSize + 1);
- }
- // שמאלה
- if (btnNum % boardSize != 0 && btnNum != 0)
- {
- if (board[btnNum - 1].Enabled == true)
- openButton(btnNum - 1);
- }
- // ימינה
- if ((btnNum + 1) % boardSize != 0)
- {
- if (board[btnNum + 1].Enabled == true)
- openButton(btnNum + 1);
- }
- // אלכסון למטה שמאלה
- if (board.Length - boardSize > btnNum && btnNum % boardSize != 0)
- {
- if (board[btnNum + boardSize - 1].Enabled == true)
- openButton(btnNum + boardSize - 1);
- }
- // למטה
- if (board.Length - boardSize > btnNum)
- {
- if (board[btnNum + boardSize].Enabled == true)
- openButton(btnNum + boardSize);
- }
- // אלכסון למטה ימינה
- if (board.Length - boardSize > btnNum && (btnNum + 1) % boardSize != 0)
- {
- if (board[btnNum + boardSize + 1].Enabled == true)
- openButton(btnNum + boardSize + 1);
- }
- }
- // בודק אם הלוח מלא, אם כן בודק אם נכון, אם לא ממשיך במשחק
- public bool checkVictory()
- {
- Label end;
- int isPlayerSmart = 0; // המשתנה יבדוק אם המשתמש מילא נכון את הלוח
- for (int i = 0; i < board.Length; i++)
- {
- if (board[i].Text == "")
- return false; // הלוח לא מלא
- }
- for (int i = 0; i < board.Length; i++)
- {
- if (board[i].Text == inTheBoard[i].ToString()) // אם המשתמש סימן נכון את המשבצת
- {
- isPlayerSmart++;
- }
- }
- end = new Label();
- end.Location = new Point(700, 400);
- end.BackColor = Color.Purple;
- end.Width = 180;
- end.Height = 50;
- end.Font = new Font("David", 20);
- if (isPlayerSmart == board.Length) // הכל נכון
- {
- end.Text = "YOU WON!!!";
- isOver = true; // נגמר המשחק
- Controls.Add(end);
- return true;
- }
- // המשתמש מילא לא נכון
- end.Text = "You Lose";
- isOver = true;
- Controls.Add(end);
- return true; // נגמר המשחק
- }
- // יוצר כפתור שאם נלחץ עלוי יתחיל משחק חדש
- public void playAgain()
- {
- newGame = new Button();
- newGame.Location = new Point(1300, 700);
- newGame.Width = 200;
- newGame.Height = 100;
- newGame.BackColor = Color.Green;
- newGame.Text = "START NEW GAME";
- newGame.Click += playNewGame;
- Controls.Add(newGame);
- }
- // נלחץ הכפתור ומתחיל משחק חדש
- public void playNewGame(object sender, EventArgs e)
- {
- isOver = false; // המשחק מתחיל מההתחלה
- this.Controls.Remove(newGame); // מוחק כפתורים
- this.Controls.Remove(discovery);
- for (int i = 0; i < board.Length; i++)
- {
- this.Controls.Remove(board[i]);
- }
- chooseLevel(); // מפעיל את המשחק מההתחלה
- }
- // הפונקציה פותחת את הכפתור שנלחץ וחושפת מה יש מאחריו
- public void openButton(int btnNum)
- {
- board[btnNum].Enabled = false;
- board[btnNum].Text = inTheBoard[btnNum].ToString();
- if (inTheBoard[btnNum] == 0)
- {
- cleanPlace(btnNum);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement