private bool CheckVerticalWin(int slotColumn, int slotRow, ConnectFourSlot.Colour colour) { List<ConnectFourSlot> winningSlots = new List<ConnectFourSlot> { this.Slots[slotColumn,slotRow] }; int row = slotRow + 1; // Check towards the top first ConnectFourSlot slot = row < Rows ? this.Slots[slotColumn,row] : null; while (slot != null && slot.Status == colour) { winningSlots.Add(slot); row++; slot = row < Rows ? this.Slots[slotColumn,row] : null; } row = slotRow - 1; // Check towards the bottom now slot = row >= 0 ? this.Slots[slotColumn,row] : null; while (slot != null && slot.Status == colour) { winningSlots.Add(slot); row--; slot = row >= 0 ? this.Slots[slotColumn,row] : null; } if (winningSlots.Count >= 4) { this.HighlightWinningSlots(winningSlots); return true; } return false; }
// TRBL = Top Right Bottom Left private bool CheckTRBLDiagonalWin(int slotColumn, int slotRow, ConnectFourSlot.Colour colour) { List<ConnectFourSlot> winningSlots = new List<ConnectFourSlot> { this.Slots[slotColumn,slotRow] }; int row = slotRow + 1; // Check towards the top right first int col = slotColumn + 1; ConnectFourSlot slot = (row < Rows && col < Columns) ? this.Slots[col,row] : null; while (slot != null && slot.Status == colour) { winningSlots.Add(slot); row++; col++; slot = (row < Rows && col < Columns) ? this.Slots[col,row] : null; } row = slotRow - 1; // Check towards the bottom left now col = slotColumn - 1; slot = (row >= 0 && col >= 0) ? this.Slots[col,row] : null; while (slot != null && slot.Status == colour) { winningSlots.Add(slot); row--; col--; slot = (row >= 0 && col >= 0) ? this.Slots[col,row] : null; } if (winningSlots.Count >= 4) { // Only highlight for players in the game if (this.Playing) { this.HighlightWinningSlots(winningSlots); } return true; } return false; }
private bool CheckHorizontalWin(int slotColumn, int slotRow, ConnectFourSlot.Colour colour) { List<ConnectFourSlot> winningSlots = new List<ConnectFourSlot> { this.Slots[slotColumn,slotRow] }; int col = slotColumn + 1; // Check towards the right first ConnectFourSlot slot = col < Columns ? this.Slots[col,slotRow] : null; while (slot != null && slot.Status == colour) { winningSlots.Add(slot); col++; slot = col < Columns ? this.Slots[col,slotRow] : null; } col = slotColumn - 1; // Check towards the left now slot = col >= 0 ? this.Slots[col,slotRow] : null; while (slot != null && slot.Status == colour) { winningSlots.Add(slot); col--; slot = col >= 0 ? this.Slots[col,slotRow] : null; } if (winningSlots.Count >= 4) { this.HighlightWinningSlots(winningSlots); return true; } return false; }