public MinesweeperItem(MinesweeperItemCellDefinition cell) { this.cell = cell; }
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; } }
private List<MinesweeperItem> getAdjacentCells(int row, int col) { //this method returns all the adjacent items (cells) around the (X,Y)item //each item,at most, it will be surrounded by eight items (cells): see the schema below // -------X // | // | // |Y //------------------------------------------------ // | | | | //------------------------------------------------ // X-1,Y-1 | X,Y-1 | X+1,Y-1 | | //------------------------------------------------ // X-1,Y | X,Y | X+1,Y | | //------------------------------------------------ // X-1,Y+1 | X,Y+1 | X+1,Y+1 | | //------------------------------------------------ try { //steps below work as follow //1) defines cell item coordinate (MinesweeperItemCellDefinition) //2) try to find the item on the grid (findItemAt) //3) if the item exists it will be added on the return list object List<MinesweeperItem> ret = new List<MinesweeperItem>(); MinesweeperItemCellDefinition adjacentCell = null; MinesweeperItem adjacentItem = null; //X - 1, Y - 1 adjacentCell = new MinesweeperItemCellDefinition(row-1, col-1); adjacentItem = findItemAt(adjacentCell); if (adjacentItem!=null) ret.Add(adjacentItem); //X, Y - 1 adjacentCell = new MinesweeperItemCellDefinition(row, col-1); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); //X + 1, Y - 1 adjacentCell = new MinesweeperItemCellDefinition(row+1, col-1); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); //X - 1, Y adjacentCell = new MinesweeperItemCellDefinition(row-1, col ); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); //X + 1, Y adjacentCell = new MinesweeperItemCellDefinition(row+1, col); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); //X - 1, Y + 1 adjacentCell = new MinesweeperItemCellDefinition(row-1, col+1); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); //X , Y + 1 adjacentCell = new MinesweeperItemCellDefinition(row , col+1); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); //X + 1, Y + 1 adjacentCell = new MinesweeperItemCellDefinition(row+1, col+1); adjacentItem = findItemAt(adjacentCell); if (adjacentItem != null) ret.Add(adjacentItem); return ret; } catch (Exception ex) { hanldeError(ex); return null; } }
public MinesweeperItem findItemAt(MinesweeperItemCellDefinition cell) { try{ //find an item by coordinate (row,col) return this.items.Find(x => (x.cell.col == cell.col && x.cell.row == cell.row)); //if you prefer a Linq select on the items collection to find the item you can uss this: /* return (from item in this.items where (item.cell.col==cell.col && item.cell.row==cell.row) select item).FirstOrDefault(); */ } catch (Exception e){ hanldeError(e); return null; } }