diff --git a/PuzzlePlayer/Minesweeper.cs b/PuzzlePlayer/Minesweeper.cs index ce7dabeb771e24c560102bc19c76ca2020fdaf70..2e56064390125a2ef32e9c0a8c8d7b5edd1e41c5 100644 --- a/PuzzlePlayer/Minesweeper.cs +++ b/PuzzlePlayer/Minesweeper.cs @@ -16,9 +16,9 @@ namespace PuzzlePlayer_Namespace private static Random random = new Random(); private bool[,] mineState; private bool isFirstClick; - public Minesweeper(int width = 30, int height = 16) + public Minesweeper(int width = 6, int height = 6) { - if (width <= 1 || height <= 1) throw new ArgumentOutOfRangeException(); + if (width <= 1 || height <= 1) throw new ArgumentOutOfRangeException(); // breedte 1 mag niet door optimisatie bij Reveal() boardState = GetClearBoard(width, height); lastGeneratedBoard = GetClearBoard(width, height); mineState = new bool[width, height]; @@ -37,31 +37,56 @@ namespace PuzzlePlayer_Namespace stringFormat.LineAlignment = StringAlignment.Center; stringFormat.Alignment = StringAlignment.Center; gr.FillRectangle(Brushes.DarkSlateGray, r); - for (int i = 0; i < boardState.GetLength(0); i++) - { - for (int j = 0; j < boardState.GetLength(1); j++) + for (int i = 0; i < boardState.GetLength(0); i++) for (int j = 0; j < boardState.GetLength(1); j++) { - string text = ""; - if (boardState[i,j] >= 0) + if (boardState[i, j] > 0) { gr.FillRectangle(Brushes.SlateGray, r.X + i * tilesize.Width, r.Y + j * tilesize.Height, tilesize.Width, tilesize.Height); - if (boardState[i, j] != 0) text = Convert.ToString(boardState[i, j]); + + gr.DrawString(Convert.ToString(boardState[i, j]), + new Font("Verdana", stringsize), + Brushes.Black, + (int)(r.X + ((double)i + 0.5) * tilesize.Width), + (int)(r.Y + ((double)j + 0.5) * tilesize.Height), + stringFormat); } else { - if (boardState[i,j] == -2) text = "🚩"; - if (boardState[i, j] == -3) text = "X"; + switch (boardState[i, j]) + { + case 0: + gr.FillRectangle(Brushes.SlateGray, + r.X + i * tilesize.Width, + r.Y + j * tilesize.Height, + tilesize.Width, + tilesize.Height); + break; + + case -1: break; + + case -2: + gr.DrawString("⚑", + new Font("Verdana", stringsize), + Brushes.Black, + (int)(r.X + ((double)i + 0.5) * tilesize.Width), + (int)(r.Y + ((double)j + 0.5) * tilesize.Height), + stringFormat); + break; + + case -3: + gr.DrawString("✸", + new Font("Verdana", stringsize), + Brushes.Black, + (int)(r.X + ((double)i + 0.5) * tilesize.Width), + (int)(r.Y + ((double)j + 0.5) * tilesize.Height), + stringFormat); + break; + } } - gr.DrawString(text, - new Font("Verdana", stringsize), - Brushes.Black, - (int)(r.X + ((double)i + 0.5) * tilesize.Width), - (int)(r.Y + ((double)j + 0.5) * tilesize.Height), - stringFormat); gr.DrawRectangle(Pens.DarkGray, r.X + i * tilesize.Width, @@ -69,7 +94,7 @@ namespace PuzzlePlayer_Namespace tilesize.Width, tilesize.Height); } - } + } public override void Generate() @@ -77,7 +102,7 @@ namespace PuzzlePlayer_Namespace boardState = GetClearBoard(mineState.GetLength(0), mineState.GetLength(1)); for (int i = 0; i < mineState.GetLength(0); i++) for (int j = 0; j < mineState.GetLength(1); j++) { - mineState[i, j] = random.Next(6) == 0; + mineState[i, j] = random.Next(6) == 0; // het getal P in de Next methode geeft elke tile een 1/P kans om een bom te zijn } isFirstClick = true; } @@ -94,7 +119,7 @@ namespace PuzzlePlayer_Namespace { if (x == 0) { - if (boardState[p.X, p.Y] == emptySpace) return Reveal(p); + if (boardState[p.X, p.Y] == emptySpace) return Reveal(p, false); if (boardState[p.X, p.Y] > 0) return Chord(p); } else @@ -103,8 +128,9 @@ namespace PuzzlePlayer_Namespace } return false; - bool Reveal(Point p) + bool Reveal(Point p, bool ForceOpen) { + if (ForceOpen && boardState[p.X, p.Y] == -2) { boardState[p.X, p.Y] = emptySpace; Reveal(p, false); } if (boardState[p.X, p.Y] != emptySpace) return false; if (mineState[p.X, p.Y]) { @@ -112,11 +138,11 @@ namespace PuzzlePlayer_Namespace { isFirstClick = false; mineState[p.X, p.Y] = false; - return Reveal(p); + return Reveal(p, false); } boardState[p.X, p.Y] = -3; return false; - } + } else { if (isFirstClick) isFirstClick = false; } int result = 0; List<Size> directions = new List<Size> { new(-1, -1), new(0, -1), new(1, -1), new(-1, 0), new(1, 0), new(-1, 1), new(0, 1), new(1, 1)}; @@ -126,7 +152,7 @@ namespace PuzzlePlayer_Namespace else { if (p.Y == boardState.GetLength(1) - 1) { directions.Remove(new(-1, 1)); directions.Remove(new(0, 1)); directions.Remove(new(1, 1)); } } foreach (Size s in directions) if (mineState[(p + s).X, (p + s).Y]) result++; boardState[p.X, p.Y] = result; - if (result == 0) foreach (Size s in directions) Reveal(p + s); + if (result == 0) foreach (Size s in directions) Reveal(p + s, true); return IsBoardSolved(); } @@ -139,6 +165,7 @@ namespace PuzzlePlayer_Namespace public override bool IsBoardSolved() { for (int i = 0; i < boardState.GetLength(0); i++) for (int j = 0; j < boardState.GetLength(1); j++) if (boardState[i, j] == emptySpace && !mineState[i, j]) return false; + for (int i = 0; i < boardState.GetLength(0); i++) for (int j = 0; j < boardState.GetLength(1); j++) if (boardState[i, j] == emptySpace) boardState[i, j] = -2; return true; } } diff --git a/PuzzlePlayer/PuzzleForm.cs b/PuzzlePlayer/PuzzleForm.cs index 2f0bf489e3caffe4bdc540cd324c1fbd801aff77..bb7ca2ace6b2d2ef7c5ca62110fd9682f247ea3b 100644 --- a/PuzzlePlayer/PuzzleForm.cs +++ b/PuzzlePlayer/PuzzleForm.cs @@ -206,7 +206,7 @@ namespace PuzzlePlayer_Namespace public static Size FitBoard(Size gamesize, Size maxboardsize, int drawfactor) //returns the largest rectangle smaller than MaxBoardSize that fits the given GameSize well { int TileLength = Math.Min(maxboardsize.Width / gamesize.Width, maxboardsize.Height / gamesize.Height) / drawfactor * drawfactor; - return new Size(gamesize.Width*TileLength, gamesize.Height*TileLength); + return new Size(Math.Max(6,gamesize.Width*TileLength), Math.Max(6,gamesize.Height*TileLength)); //dw dw //after each of the divisions in this method, the value should be rounded down, //as the end value cannot exceed the size given by maxboardsize. //however, C# rounds divisions of Int values down by default, so this does not have to be explicitly done.