private bool RevealTile(Button b) { var res = false; if (gameOn) { Point p = b.GetLocationInGrid(); if (firstClick) { firstClick = false; SetMat(p); //OutMat(); } if (!revealedMat[p.Y, p.X] && hintMat[p.Y, p.X] == Hint.NOTHING) { revealedMat[p.Y, p.X] = true; SetContent(p); switch (mineMat[p.Y, p.X]) { case -1: { return(true); } case 0: { return(RevealAround(p)); } } } } return(false); }
private bool RevealAround(Point p) { return(ReturnAround(p, (aroundP) => { return RevealTile(buttonMat[aroundP.Y, aroundP.X]); }).Aggregate((b1, b2) => b1 || b2)); }
private void FlagClick(object sender, MouseButtonEventArgs e) { Button b = sender as Button; Hint hint = Hint.NOTHING; switch (b.Content) { case null: { ShownMines--; b.Content = "F"; hint = Hint.FLAG; break; } case "F": { ShownMines++; b.Content = "?"; hint = Hint.QUESTION; break; } case "?": { b.Content = null; hint = Hint.NOTHING; break; } } Point p = b.GetLocationInGrid(); hintMat[p.Y, p.X] = hint; }
private IEnumerable <T> ReturnAround <T>(Point p, Func <Point, T> func) { Point aroundPoint = new Point(0, 0); for (aroundPoint.Y = p.Y - 1; aroundPoint.Y < p.Y + 2; aroundPoint.Y++) { for (aroundPoint.X = p.X - 1; aroundPoint.X < p.X + 2; aroundPoint.X++) { if (InMat(aroundPoint)) { if (!p.Equals(aroundPoint)) { yield return(func(aroundPoint)); } } } } }
private void DoAround(Point p, Action <Point> action) { Point aroundPoint = new Point(0, 0); for (aroundPoint.Y = p.Y - 1; aroundPoint.Y < p.Y + 2; aroundPoint.Y++) { for (aroundPoint.X = p.X - 1; aroundPoint.X < p.X + 2; aroundPoint.X++) { if (InMat(aroundPoint)) { if (!p.Equals(aroundPoint)) { action(aroundPoint); } } } } }
private void ShowAllMines() { Point p = new Point(0, 0); for (p.Y = 0; p.Y < matHeight; p.Y++) { for (p.X = 0; p.X < matWidth; p.X++) { if (HasMine(p) && hintMat[p.Y, p.X] != Hint.FLAG) { SetContent(p); } if (!HasMine(p) && hintMat[p.Y, p.X] == Hint.FLAG) { SetContent(p); buttonMat[p.Y, p.X].Background = Brushes.Red; } } } }
private void SetMat(Point firstP) { for (int i = 0; i < mines; i++) { Point rndP = new Point(rnd.Next(matWidth), rnd.Next(matHeight)); if (!HasMine(rndP) && !firstP.Equals(rndP)) { mineMat[rndP.Y, rndP.X] = -1; DoAround(rndP, (aroundP) => { if (!HasMine(aroundP)) { mineMat[aroundP.Y, aroundP.X]++; } } ); } else { i--; } } }
private void B_MouseDoubleClick(object sender, MouseButtonEventArgs e) { e.Handled = true; Button b = sender as Button; Point p = b.GetLocationInGrid(); if (revealedMat[p.Y, p.X]) { int flagCount = ReturnAround(p, (aroundP) => { return(hintMat[aroundP.Y, aroundP.X] == Hint.FLAG); }).Count(f => f); if (flagCount == mineMat[p.Y, p.X]) { if (RevealAround(p)) { LoseGame(); } } } CheckWin(); }
private void SetContent(Point p) { Button b = buttonMat[p.Y, p.X]; int val = mineMat[p.Y, p.X]; b.Background = Brushes.White; switch (val) { case -1: { b.Foreground = Brushes.Black; b.Content = "*"; break; } case 0: { b.Foreground = Brushes.Black; b.Content = ""; break; } case 1: { b.Foreground = Brushes.Blue; b.Content = "1"; break; } case 2: { b.Foreground = Brushes.Green; b.Content = "2"; break; } case 3: { b.Foreground = Brushes.Red; b.Content = "3"; break; } case 4: { b.Foreground = Brushes.DarkBlue; b.Content = "4"; break; } case 5: { b.Foreground = Brushes.DarkRed; b.Content = "5"; break; } case 6: { b.Foreground = Brushes.Cyan; b.Content = "6"; break; } case 7: { b.Foreground = Brushes.Black; b.Content = "7"; break; } case 8: { b.Foreground = Brushes.Gray; b.Content = "8"; break; } } }
private bool InMat(Point p) { return(p.X >= 0 && p.Y >= 0 && p.X < matWidth && p.Y < matHeight); }
private bool HasMine(Point p) { return(mineMat[p.Y, p.X] == -1); }