private void gameGrid_cellOpeningCompleted(MinesweeperItem item) { //this event will be raised after calling setAdjacentCells() method try { //gets the button from the item tag (see getGridButton method) Button button = (Button)item.tag; switch (item.type) { case MinesweeperItemType.MinesweeperItem_Empty: button.Content = ""; button.Background = Brushes.SeaGreen; break; case MinesweeperItemType.MinesweeperItem_Mine: button.Content = "*"; button.Background = Brushes.Red; break; case MinesweeperItemType.MinesweeperItem_MineWarning: button.Content = item.value.ToString(); button.Background = Brushes.Orange; break; case MinesweeperItemType.MinesweeperItemType_None: break; } //if items is opened, remove the click event handler from the button if (item.type != MinesweeperItemType.MinesweeperItemType_None) { button.Click -= gridButton_Click; } } catch (Exception ex) { handleException(ex); } }
private void gameGrid_itemMineAdded(MinesweeperItem item) { //TODO }
private void gameGrid_gameOver(MinesweeperItem item) { MessageBox.Show("Oh no!\nI'm a mine :(", "GAME OVER", MessageBoxButton.OK, MessageBoxImage.Warning); }
private Button getGridButton(MinesweeperItem item) { try { //creates a button Button button = new Button(); //stores the button on the item tag item.tag = button; //stores the item on the tag button button.Tag = item; button.Content = "."; button.Width = CELL_SIDE; button.Height = CELL_SIDE; button.FontSize = 16; button.FontWeight = FontWeights.Bold; button.Click += gridButton_Click; return button; } catch (Exception ex) { handleException(ex); return null; } }
private bool cellIsTypeOf(int row, int col, MinesweeperItemType type, out MinesweeperItem item) { try{ //find an item and compare the input type //return true if item is not null and the input type is the same of the found item type //return fals if the item is null or the input type is not equal to the found item type MinesweeperItemCellDefinition adjacentCell = null; MinesweeperItem adjacentItem = null; adjacentCell = new MinesweeperItemCellDefinition(row, col); adjacentItem = findItemAt(adjacentCell); item = adjacentItem; if (adjacentItem != null){ return adjacentItem.type == type; } return false; } catch (Exception e){ hanldeError(e); item = null; return false; } }
public void setAdjacentCells(MinesweeperItem item) { try { int adjacentMines = 0; if (item.type == MinesweeperItemType.MinesweeperItemType_None) { //if the item is not "opened" it could be //1) An Item with adjacent mines:wil be raised a cellOpeningCompleted event and on the "client-side" you can perform the presentation of the adjacent mines by using the value property of the item (see MinesweeperItem) //2) An empty item: recursively will be raised a cellOpeningCompleted event until an item will have an empty adjacent item //gets all adjacent cells for te input item List<MinesweeperItem> adjacentCells = getAdjacentCells(item.cell.row, item.cell.col); //loop thorugh all adjacent cells to calculate adjacent mines. //adjacentMines could be zero or greater than zero foreach (MinesweeperItem adjacentCell in adjacentCells) { if (cellIsTypeOf(adjacentCell.cell.row, adjacentCell.cell.col, MinesweeperItemType.MinesweeperItem_Mine)) { adjacentMines++; } } if (adjacentMines != 0) { //item has adjacentMines, so its type will be set as MinesweeperItem_MineWarning item.type = MinesweeperItemType.MinesweeperItem_MineWarning; item.value = adjacentMines; } else if (adjacentMines == 0) { //item does not have adjacentMines, so we have to inspect all empty cells item.type = MinesweeperItemType.MinesweeperItem_Empty; foreach (MinesweeperItem adjacentCell in adjacentCells) { MinesweeperItem adjacentItem = null; cellIsTypeOf(adjacentCell.cell.row, adjacentCell.cell.col, MinesweeperItemType.MinesweeperItemType_None, out adjacentItem); //here we implement a recursive behaviour to find all empty blocks if (adjacentItem != null) setAdjacentCells(adjacentItem); } } } //raise event if (cellOpeningCompleted != null) { cellOpeningCompleted(item); } } catch (Exception e) { hanldeError(e); } }
public void makeGrid() { //it makes the game grid by adding and item for each cell (rox,cols) //and at the end of this process defines randmly the mine positions (mine cells) try{ for (int r = 0; r < this.rows; r++){ for (int c = 0; c < this.cols; c++){ MinesweeperItem item = new MinesweeperItem(new MinesweeperItemCellDefinition(r, c)); this.items.Add(item); if (itemAdded != null){ itemAdded(item); } } } //it places mines on the grid placeMines(); //raise event if (loadingCompleted != null){ loadingCompleted(this.items); } } catch (Exception e){ hanldeError(e); } }
public void evaluateItem(MinesweeperItem item) { //with this method you can implement all the game behavior //because if the item will be a mine, then will be raised a game over event //and all cells will be opened //otherwise it will find all the item's adjacent cells try { if (item.type == MinesweeperItemType.MinesweeperItem_Mine) { openAllCells(); //raise event game over if (gameOver != null) { gameOver(item); } } else { setAdjacentCells(item); } } catch (Exception ex) { hanldeError(ex); } }