private static void Expand(Int32 Row, Int32 Column) { MineFiledCell TargetCell = MineFiledCells[Row, Column]; //Open it first SetCellOpen(TargetCell); if (TargetCell.MineAroundCount == 0) { for (int RowOffset = -1; RowOffset <= 1; RowOffset++) { for (int ColumnOffset = -1; ColumnOffset <= 1; ColumnOffset++) { Int32 Row_A = Row + RowOffset, Column_A = Column + ColumnOffset; if (Row_A >= 0 && Row_A <= 15 && Column_A >= 0 && Column_A <= 15 && (RowOffset != 0 || ColumnOffset != 0) && !MineFiledCells[Row_A, Column_A].isOpened && !MineFiledCells[Row_A, Column_A].isMine) { Expand(Row_A, Column_A); } } } } }
static GameManger() { myGameWindow = new GameWindow(); for (int i = 0; i < 16 * 16; i++) { Int32 Row = i / 16, Column = i % 16; MineFiledCells[Row, Column] = new MineFiledCell() { }; } //Prepare mine cell image foreach (String ImageName in new String[] { "0", "1", "2", "3", "4", "5", "Covered", "Flag_Blue", "Flag_Red" }) { BitmapImage bi4 = new BitmapImage(); bi4.BeginInit(); bi4.UriSource = new Uri("img/Grid/" + ImageName + ".png", UriKind.Relative); bi4.EndInit(); Image myImage3 = new Image(); myImage3.Source = bi4; CellImageRef.Add(ImageName, myImage3); } NewGameSetup(); //Add CellImage for (int i = 0; i < 16 * 16; i++) { myGameWindow.MineBoard.Children.Add(MineFiledCells[i / 16, i % 16].CellImage); } //Add BlankImage BitmapImage bi3 = new BitmapImage(); bi3.BeginInit(); bi3.UriSource = new Uri("img/Grid/Transparent.png", UriKind.Relative); bi3.EndInit(); for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { Image myImage3 = new Image(); myImage3.Source = bi3; myImage3.SetValue(Grid.RowProperty, i); myImage3.SetValue(Grid.ColumnProperty, j); myGameWindow.MouseDetection.Children.Add(myImage3); } } }
//Process evenet public static void CellClick(Int32 Row, Int32 Column) { MineFiledCell TargetCell = MineFiledCells[Row, Column]; if (!TargetCell.isOpened) { if (TargetCell.MineAroundCount == 0 && !TargetCell.isMine) { Expand(Row, Column); } else { SetCellOpen(TargetCell); } if (TargetCell.isMine) { if ((CurrentPlayer == MSNMineSweeper.CurrentPlayer.Red && PlayerRed.MineFound >= 20) || (CurrentPlayer == MSNMineSweeper.CurrentPlayer.Blue && PlayerBlue.MineFound >= 20) ) { PlaySound("CloseToWin"); } else { PlaySound("FoundMine"); } CheckWinner(); } else { PlaySound("FoundEmpty"); ChangePlayer(); } } }
private static void SetCellOpen(MineFiledCell TargetCell) { if (TargetCell.isMine) { if (CurrentPlayer == MSNMineSweeper.CurrentPlayer.Red) { TargetCell.CellImage.Source = CellImageRef["Flag_Red"].Source; PlayerRed.MineFound++; myGameWindow.MineGot_Red.Text = PlayerRed.MineFound.ToString("D2"); } else { TargetCell.CellImage.Source = CellImageRef["Flag_Blue"].Source; PlayerBlue.MineFound++; myGameWindow.MineGot_Blue.Text = PlayerBlue.MineFound.ToString("D2"); } myGameWindow.RemainMineNo.Content = (51 - (PlayerBlue.MineFound + PlayerRed.MineFound)).ToString(); } else { TargetCell.CellImage.Source = CellImageRef[TargetCell.MineAroundCount.ToString()].Source; } TargetCell.isOpened = true; }