HashSet <Chain> DetectVerticalMatches() { HashSet <Chain> chainsSet = new HashSet <Chain>(); for (int column = 0; column < COLUMNS_NUMBER; column++) { for (int row = 0; row < ROWS_NUMBER - 2;) { if (animals[column, row] != null) { AnimalType matchType = animals[column, row].animalType; if (animals[column, row + 1] != null && animals[column, row + 1].animalType == matchType && animals[column, row + 2] != null && animals[column, row + 2].animalType == matchType) { Chain chain = new Chain(); chain.chainType = ChainType.Vertical; do { chain.AddAnimal(animals[column, row]); row += 1; }while (row < ROWS_NUMBER && animals[column, row] != null && animals[column, row].animalType == matchType); chainsSet.Add(chain); continue; } } row += 1; } } return(chainsSet); }
HashSet <Chain> DetectHorizontalMatches() { HashSet <Chain> chainsSet = new HashSet <Chain>(); for (int row = 0; row < ROWS_NUMBER; row++) { //因为基数是3,所以序列是0开始需要减2的范围 for (int column = 0; column < COLUMNS_NUMBER - 2;) { if (animals[column, row] != null) { AnimalType matchType = animals[column, row].animalType; if (animals[column + 1, row] != null && animals[column + 1, row].animalType == matchType && animals[column + 2, row] != null && animals[column + 2, row].animalType == matchType) { Chain chain = new Chain(); chain.chainType = ChainType.Horizontal; do { //把整个链结构保存,例如行是保存到最后一个不是相同类型的item为止 chain.AddAnimal(animals[column, row]); column += 1; }while (column < COLUMNS_NUMBER && animals[column, row] != null && animals[column, row].animalType == matchType); chainsSet.Add(chain); continue; } } column += 1; } } return(chainsSet); }