public void FillField(Field field, int startX, int startY) { int[,] fieldTemplate = new int[field.SizeX, field.SizeY]; for (var i = 0; i < field.MinesCount; i++) { var y = i / field.SizeX; var x = i % field.SizeX; fieldTemplate[x, y] = 1; } if (field.Length != field.MinesCount) { do { Common.Shuffle(new Random(), fieldTemplate); } while (fieldTemplate[startX, startY] == 1); } for (var i = 0; i < field.SizeY; i++) { for (var j = 0; j < field.SizeX; j++) { if (fieldTemplate[j, i] == 1) { field[j, i] = new MineButton(field, MineButtonType.Mine, j, i); } else { field[j, i] = new MineButton(field, MineButtonType.Clear, j, i); } } } }
private static void Create_Mine_Field(int howManyMines) { List <bool> minesList = new List <bool>(); for (int i = 0; i < howManyMines; i++) { minesList.Add(true); } for (int i = 0; i < mineFieldButtons.Length - howManyMines; i++) { minesList.Add(false); } Random getRandom = new Random(); int index; for (int i = 0; i < mineFieldButtons.GetLength(0); i++) { for (int j = 0; j < mineFieldButtons.GetLength(1); j++) { mineFieldButtons[i, j] = new MineButton(); mineFieldButtons[i, j].Font = new Font("Microsoft Sans Serif", 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238))); mineFieldButtons[i, j].BackColor = Color.LightSlateGray; index = getRandom.Next(0, minesList.Count - 1); mineFieldButtons[i, j]._Mine = minesList[index]; minesList.RemoveAt(index); } } Bound_Mines_Buttons(); }
public bool IsAtOffset(MineButton btn, int x, int y) { if (btn.FieldX + x != FieldX) { return(false); } return(btn.FieldY + y == FieldY); }
private void OpenNearbyFields(MineButton button) { var nearbyButtons = GetNearbyButtons(button); foreach (var b in nearbyButtons) { OpenField(b); } }
public bool IsNearTo(MineButton btn) { /* * * as3 compiler chokes on this: * * if (!((((btn.FieldX - 1) != this.FieldX)) ? true : !((btn.FieldY - 1) == this.FieldY))) * { * return true; * } * */ if (IsAtOffset(btn, -1, -1)) { return(true); } if (IsAtOffset(btn, 0, -1)) { return(true); } if (IsAtOffset(btn, 1, -1)) { return(true); } if (IsAtOffset(btn, 1, 0)) { return(true); } if (IsAtOffset(btn, 1, 1)) { return(true); } if (IsAtOffset(btn, 0, 1)) { return(true); } if (IsAtOffset(btn, -1, 1)) { return(true); } if (IsAtOffset(btn, -1, 0)) { return(true); } return(false); }
private List <MineButton> GetNearbyButtons(MineButton button) { if (!AreValidCoordinates(button.XPosition, button.YPosition)) { return(new List <MineButton>()); } return(MineGrid.Children .OfType <MineButton>() .Where(b => b.IsEnabled && (b.XPosition != button.XPosition || b.YPosition != button.YPosition) && b.XPosition > button.XPosition - 2 && b.XPosition < button.XPosition + 2 && b.YPosition > button.YPosition - 2 && b.YPosition < button.YPosition + 2) .ToList()); }
public static void Draw(UniformGrid grid, Field field, RoutedEventHandler clickMethod, MouseButtonEventHandler rightClickMethod) { grid.Children.Clear(); grid.Rows = field.VerticalyCount; grid.Columns = field.HorisontalCount; var allCells = field.GetAllCells(); foreach (var cell in allCells) { var button = new MineButton() { Height = 30, Width = 30, X = cell.X, Y = cell.Y }; if (cell.IsShow) { if (cell.Mine is MineBase) { button.Content = "*"; button.Background = Brushes.IndianRed; } else { button.Content = cell.CountMineAround == 0 ? "" : cell.CountMineAround.ToString(); button.Background = Brushes.Gray; } } if (cell.Flag is Flag.Flag) { button.Content = "!"; } button.Click += clickMethod; button.PreviewMouseRightButtonDown += rightClickMethod; grid.Children.Add(button); Grid.SetRow(button, cell.Y + 1); Grid.SetColumn(button, cell.X); } }
public void OpenNeighbor(int i, int j) { // encounter a button that could be: notfound val=0, notfound val!=0, flag, MineButton but = buttonRows[i].buttons[j]; if (but.state == State.notfound && but.value != 0) { but.SetState(State.found); but.UpdateState(); remainingBlank += -1; return; } if (but.state == State.flag) { return; } if (but.state == State.notfound && but.value == 0) { but.SetState(State.found); but.UpdateState(); remainingBlank += -1; for (int x = -1; x < 2; x++) { for (int y = -1; y < 2; y++) { if (i + x >= 0 && j + y >= 0 && i + x < 10 && j + y < 10) { if (buttonRows[i + x].buttons[y + j].state == State.notfound) { OpenNeighbor(i + x, y + j); } // else { // Debug.Log("not found"); // } } // else { // Debug.Log("out of bounds"); // } } } } }
public void ShowAllEmptyCells(MineButton button) { var listForOpen = new List <Cell> { GetCell(button.X, button.Y) }; while (listForOpen.Count != 0) { var cell = listForOpen.First(); int x = cell.X; int y = cell.Y; if (cell.CountMineAround == 0 || cell.Mine is MineBase) { listForOpen.AddRange(GetAllCellsAround(cell) .Where(c => { // позоляет избежать поторного добаления одной и той же ячейки foreach (var elementForOpen in listForOpen) { if (c.X == elementForOpen.X && c.Y == elementForOpen.Y) { return(false); } } if (c.IsShow || c.Mine != null || c.Flag is Flag.Flag) { return(false); } return(true); })); } cell.Show(); listForOpen.Remove(cell); } }
private void MarkField(MineButton button) { if (_core == null) { return; } var status = _core.Mark(button.XPosition, button.YPosition); if (status == MarkFieldStatus.FieldIsMarked) { _minesLeft--; MinesLeftLabel.Text = String.Format(MinesLeftText, _minesLeft > 0 ? _minesLeft : 0); button.Source = _flag; } else { _minesLeft++; MinesLeftLabel.Text = String.Format(MinesLeftText, _minesLeft > 0 ? _minesLeft : 0); button.Source = _blank; } }
public bool IsNearTo(MineButton btn) { if (btn.X - 1 == X && btn.Y - 1 == Y) { return(true); } if (btn.X + 0 == X && btn.Y - 1 == Y) { return(true); } if (btn.X + 1 == X && btn.Y - 1 == Y) { return(true); } if (btn.X + 1 == X && btn.Y + 0 == Y) { return(true); } if (btn.X + 1 == X && btn.Y + 1 == Y) { return(true); } if (btn.X - 0 == X && btn.Y + 1 == Y) { return(true); } if (btn.X - 1 == X && btn.Y + 1 == Y) { return(true); } if (btn.X - 1 == X && btn.Y - 0 == Y) { return(true); } return(false); }
private void ServeGameOver(MineButton button) { foreach (var child in MineGrid.Children) { var b = child as MineButton; b.IsEnabled = false; b.BackgroundColor = Color.Silver; if (!_core.IsMarked(b.XPosition, b.YPosition)) { if (_core.HasMine(b.XPosition, b.YPosition)) { b.Source = _mine; } } else if (!_core.HasMine(b.XPosition, b.YPosition)) { b.Source = _nomine; } } button.Source = _explodedmine; DisplayAlert("Game over", "BOOM! You lose!", "OK"); }
public bool IsNearTo(MineButton btn) { /* as3 compiler chokes on this: if (!((((btn.FieldX - 1) != this.FieldX)) ? true : !((btn.FieldY - 1) == this.FieldY))) { return true; } */ if (IsAtOffset(btn, -1, -1)) return true; if (IsAtOffset(btn, 0, -1)) return true; if (IsAtOffset(btn, 1, -1)) return true; if (IsAtOffset(btn, 1, 0)) return true; if (IsAtOffset(btn, 1, 1)) return true; if (IsAtOffset(btn, 0, 1)) return true; if (IsAtOffset(btn, -1, 1)) return true; if (IsAtOffset(btn, -1, 0)) return true; return false; }
private void OpenField(MineButton button) { if (_core == null) { _core = new MineSweeper.Core.MineSweeper(columns, rows, mines, button.XPosition, button.YPosition); } if (_core.IsMarked(button.XPosition, button.YPosition)) { return; } button.IsEnabled = false; button.BackgroundColor = Color.Silver; var status = _core.Open(button.XPosition, button.YPosition); switch (status) { case FieldStatus.FieldIsMine: ServeGameOver(button); return; case FieldStatus.MinesNearby0: button.Source = _blank; OpenNearbyFields(button); break; case FieldStatus.MinesNearby1: button.Source = _1; break; case FieldStatus.MinesNearby2: button.Source = _2; break; case FieldStatus.MinesNearby3: button.Source = _3; break; case FieldStatus.MinesNearby4: button.Source = _4; break; case FieldStatus.MinesNearby5: button.Source = _5; break; case FieldStatus.MinesNearby6: button.Source = _6; break; case FieldStatus.MinesNearby7: button.Source = _7; break; default: button.Source = _8; break; } if (MineGrid.Children.OfType <MineButton>().Count(b => b.IsEnabled) == mines) { ServeGameWin(); } }
public MineField(int FieldXCount, int FieldYCount, double percentage) { this.percentage = percentage; var a = new List<MineButton>(); var k = 0; for (int x = 0; x < FieldXCount; x++) for (int y = 0; y < FieldYCount; y++) { var n = new MineButton { x = x * MineButton.Width, y = y * MineButton.Height, FieldX = x, FieldY = y, Others = a, Field = this }; var j = k; n.IsFlagChanged += delegate { if (IsFlagChanged != null) IsFlagChanged(j, n.IsFlag); }; n.OnReveal += delegate { if (OnReveal != null) OnReveal(j); }; n.OneStepClosedToTheEnd += LocalPlayer => { if (this.OneStepClosedToTheEnd != null) this.OneStepClosedToTheEnd(LocalPlayer); }; a.Add( n ); k++; } Buttons = a.ToArray(); Action<int, Action> Delay = (time, h) => { var t = new Timer(time, 1); t.timer += delegate { h(); }; t.start(); }; Action<int, Action[]> DelayArray = (time, h) => { var i = 0; var Next = default(Action); Next = delegate { if (i < h.Length) Delay(time, delegate { h[i](); i++; Next(); } ); }; Next(); }; foreach (var v in a) { var z = v; v.OnComplete += LocalPlayer => { foreach (var i in a) i.Enabled = false; if (OnComplete != null) OnComplete(LocalPlayer); DelayArray(1000, new Action[] { delegate { snd_tick.play(); }, delegate { snd_tick.play(); }, delegate { if (LocalPlayer) { Reset(); if (GameResetByLocalPlayer != null) GameResetByLocalPlayer(); } if (GameReset != null) GameReset(LocalPlayer); } }); }; v.OnBang += LocalPlayer => { if (OnBang != null) OnBang(LocalPlayer); Delay( 3000, delegate { DelayArray(1000, new Action[] { delegate { snd_tick.play(); z.img = z.img_mine; }, delegate { snd_tick.play(); if (z.img == z.img_mine) z.img = z.img_mine_found; }, delegate { if (LocalPlayer) { Reset(); if (GameResetByLocalPlayer != null) GameResetByLocalPlayer(); } if (GameReset != null) GameReset(LocalPlayer); } }); } ); }; addChild(v); } Reset(); }
public MineSweeperControl(int ButtonsX, int ButtonsY, double Mines, Assets MyAssets) { var sndclick = new click(); var sndflag = new flag(); var sndexplosion = new explosion(); this.MyAssets = MyAssets; this.Alive = true; this.Mines = Mines; if (this.Mines > 0.8) this.Mines = 0.8; this.ButtonsX = ButtonsX; this.ButtonsY = ButtonsY; Control.style.backgroundColor = Color.Gray; Control.style.SetSize(Width, Height); Control.style.position = IStyle.PositionEnum.relative; for (int x = 0; x < ButtonsX; x++) for (int y = 0; y < ButtonsY; y++) { var btn = new MineButton().AddTo(Buttons); btn.MouseDownChanged += v => { if (!Alive) return; if (v) { if (LookingForMines != null) LookingForMines(); } else { if (DoneLookingForMines != null) DoneLookingForMines(); } }; btn.Source = MyAssets.button; btn.MouseDownSource = MyAssets.empty; btn.Control.AttachTo(Control); btn.MoveTo(x, y); var NearbyButtons = from i in Buttons where i.IsNearTo(btn) select i; var NearbyMines = from i in NearbyButtons where i.IsMined select i; var NearbyFlags = from i in NearbyButtons where i.Source == MyAssets.flag select i; var NearbyNonFlags = from i in NearbyButtons where i.Source != MyAssets.flag select i; var IdleButtons = from i in Buttons where i.Source != MyAssets.flag where !MyAssets.numbers.Contains(i.Source) select i; Action Resolve = delegate { foreach (var v in NearbyNonFlags) { v.RaiseClick(); } }; btn.ContextClick += delegate { sndflag.play(); sndflag = new flag(); if (MyAssets.numbers.Contains(btn.Source)) { if (NearbyMines.Count() == NearbyFlags.Count()) { Resolve(); CheckIdle(IdleButtons); } return; } if (btn.Source == MyAssets.button) btn.Source = MyAssets.flag; else if (btn.Source == MyAssets.flag) btn.Source = MyAssets.question; else if (btn.Source == MyAssets.question) btn.Source = MyAssets.button; if (MinesFoundChanged != null) MinesFoundChanged(); CheckIdle(IdleButtons); }; //sndclick.load(); btn.Click += delegate { btn.Enabled = false; sndclick.play(); sndclick = new click(); //sndclick.load(); if (btn.IsMined) { // end of game foreach (var v in from i in Buttons where !i.IsMined where i.Source == MyAssets.flag select i) { v.Source = MyAssets.notmine; } foreach (var v in from i in Buttons where i.IsMined where i != btn select i) { v.Source = MyAssets.mine; } btn.Source = MyAssets.mine_found; DisableButtons(); Alive = false; sndexplosion.play(); sndexplosion = new explosion(); if (Bang != null) Bang(); } else { // how many mines are near me? var MineCount = NearbyMines.Count(); btn.Source = MyAssets.numbers[MineCount]; if (MineCount == 0) { Resolve(); CheckIdle(IdleButtons); } } }; } AttachMinesToButtons(); }
public bool IsNearTo(MineButton btn) { if (btn.X - 1 == X && btn.Y - 1 == Y) return true; if (btn.X + 0 == X && btn.Y - 1 == Y) return true; if (btn.X + 1 == X && btn.Y - 1 == Y) return true; if (btn.X + 1 == X && btn.Y + 0 == Y) return true; if (btn.X + 1 == X && btn.Y + 1 == Y) return true; if (btn.X - 0 == X && btn.Y + 1 == Y) return true; if (btn.X - 1 == X && btn.Y + 1 == Y) return true; if (btn.X - 1 == X && btn.Y - 0 == Y) return true; return false; }
/// <summary> /// <see cref="ButtonInfo"/> constructor. /// </summary> public ButtonInfo(int x, int y, MineButton mineButton) { X = x; Y = y; Button = mineButton; }
public MineSweeperControl(int ButtonsX, int ButtonsY, double Mines, Assets MyAssets) { var sndclick = new click(); var sndflag = new flag(); var sndexplosion = new explosion(); this.MyAssets = MyAssets; this.Alive = true; this.Mines = Mines; if (this.Mines > 0.8) { this.Mines = 0.8; } this.ButtonsX = ButtonsX; this.ButtonsY = ButtonsY; Control.style.backgroundColor = Color.Gray; Control.style.SetSize(Width, Height); Control.style.position = IStyle.PositionEnum.relative; for (int x = 0; x < ButtonsX; x++) { for (int y = 0; y < ButtonsY; y++) { var btn = new MineButton().AddTo(Buttons); btn.MouseDownChanged += v => { if (!Alive) { return; } if (v) { if (LookingForMines != null) { LookingForMines(); } } else { if (DoneLookingForMines != null) { DoneLookingForMines(); } } }; btn.Source = MyAssets.button; btn.MouseDownSource = MyAssets.empty; btn.Control.AttachTo(Control); btn.MoveTo(x, y); var NearbyButtons = from i in Buttons where i.IsNearTo(btn) select i; var NearbyMines = from i in NearbyButtons where i.IsMined select i; var NearbyFlags = from i in NearbyButtons where i.Source == MyAssets.flag select i; var NearbyNonFlags = from i in NearbyButtons where i.Source != MyAssets.flag select i; var IdleButtons = from i in Buttons where i.Source != MyAssets.flag where !MyAssets.numbers.Contains(i.Source) select i; Action Resolve = delegate { foreach (var v in NearbyNonFlags) { v.RaiseClick(); } }; btn.ContextClick += delegate { sndflag.play(); sndflag = new flag(); if (MyAssets.numbers.Contains(btn.Source)) { if (NearbyMines.Count() == NearbyFlags.Count()) { Resolve(); CheckIdle(IdleButtons); } return; } if (btn.Source == MyAssets.button) { btn.Source = MyAssets.flag; } else if (btn.Source == MyAssets.flag) { btn.Source = MyAssets.question; } else if (btn.Source == MyAssets.question) { btn.Source = MyAssets.button; } if (MinesFoundChanged != null) { MinesFoundChanged(); } CheckIdle(IdleButtons); }; //sndclick.load(); btn.Click += delegate { btn.Enabled = false; sndclick.play(); sndclick = new click(); //sndclick.load(); if (btn.IsMined) { // end of game foreach (var v in from i in Buttons where !i.IsMined where i.Source == MyAssets.flag select i) { v.Source = MyAssets.notmine; } foreach (var v in from i in Buttons where i.IsMined where i != btn select i) { v.Source = MyAssets.mine; } btn.Source = MyAssets.mine_found; DisableButtons(); Alive = false; sndexplosion.play(); sndexplosion = new explosion(); if (Bang != null) { Bang(); } } else { // how many mines are near me? var MineCount = NearbyMines.Count(); btn.Source = MyAssets.numbers[MineCount]; if (MineCount == 0) { Resolve(); CheckIdle(IdleButtons); } } }; } } AttachMinesToButtons(); }
public bool IsAtOffset(MineButton btn, int x, int y) { if (btn.FieldX + x != FieldX) return false; return btn.FieldY + y == FieldY; }
public MineField(int FieldXCount, int FieldYCount, double percentage) { this.percentage = percentage; var a = new List <MineButton>(); var k = 0; for (int x = 0; x < FieldXCount; x++) { for (int y = 0; y < FieldYCount; y++) { var n = new MineButton { x = x * MineButton.Width, y = y * MineButton.Height, FieldX = x, FieldY = y, Others = a, Field = this }; var j = k; n.IsFlagChanged += delegate { if (IsFlagChanged != null) { IsFlagChanged(j, n.IsFlag); } }; n.OnReveal += delegate { if (OnReveal != null) { OnReveal(j); } }; n.OneStepClosedToTheEnd += LocalPlayer => { if (this.OneStepClosedToTheEnd != null) { this.OneStepClosedToTheEnd(LocalPlayer); } }; a.Add( n ); k++; } } Buttons = a.ToArray(); Action <int, Action> Delay = (time, h) => { var t = new Timer(time, 1); t.timer += delegate { h(); }; t.start(); }; Action <int, Action[]> DelayArray = (time, h) => { var i = 0; var Next = default(Action); Next = delegate { if (i < h.Length) { Delay(time, delegate { h[i](); i++; Next(); } ); } }; Next(); }; foreach (var v in a) { var z = v; v.OnComplete += LocalPlayer => { foreach (var i in a) { i.Enabled = false; } if (OnComplete != null) { OnComplete(LocalPlayer); } DelayArray(1000, new Action[] { delegate { snd_tick.play(); }, delegate { snd_tick.play(); }, delegate { if (LocalPlayer) { Reset(); if (GameResetByLocalPlayer != null) { GameResetByLocalPlayer(); } } if (GameReset != null) { GameReset(LocalPlayer); } } }); }; v.OnBang += LocalPlayer => { if (OnBang != null) { OnBang(LocalPlayer); } Delay( 3000, delegate { DelayArray(1000, new Action[] { delegate { snd_tick.play(); z.img = z.img_mine; }, delegate { snd_tick.play(); if (z.img == z.img_mine) { z.img = z.img_mine_found; } }, delegate { if (LocalPlayer) { Reset(); if (GameResetByLocalPlayer != null) { GameResetByLocalPlayer(); } } if (GameReset != null) { GameReset(LocalPlayer); } } }); } ); }; addChild(v); } Reset(); }