private void VerticalCheck(IBoard board, HashSet <BoardChange> changes) { for (int x = 0; x < board.Width; x++) { var ruleLine = board.TopRules[x]; var sum = ruleLine.Sum(); var numberOfRules = ruleLine.Count; var sizeNeeded = sum + numberOfRules - 1; if (sizeNeeded == board.Height) { int y = 0; foreach (var rule in ruleLine) { for (int i = 0; i < rule; i++) { if (board[x, y] == CellState.None) { changes.Add(BoardChange.Filled(x, y)); } y += 1; } if (y < board.Height) { if (board[x, y] == CellState.None) { changes.Add(BoardChange.Blocked(x, y)); } y += 1; } } } } }
private void VerticalCheck(IBoard board, HashSet <BoardChange> changes) { bool lineIsCompleted; for (int x = 0; x < board.Width; x++) { lineIsCompleted = false; IRuleLine?ruleLine = board.TopRules[x]; int? firstRule = ruleLine?.FirstOrDefault(); if (firstRule == null) { continue; } GroupCollection?groups = SimpleGrouper.GroupVertical(board, x); if (groups == null) { continue; } Group?firstEmptyGroup = default; int totalSpace = 0; foreach (Group group in groups.Groups) { switch (group.State) { case CellState.None: if (firstEmptyGroup == null) { firstEmptyGroup = group; } totalSpace += group.Count; break; case CellState.Filled: totalSpace += group.Count; break; case CellState.Blocked: if (totalSpace < firstRule.Value && firstEmptyGroup != null) { for (int i = 0; i < firstEmptyGroup.Value.Count; i++) { changes.Add(BoardChange.Blocked(x, i + firstEmptyGroup.Value.StartIndex)); } lineIsCompleted = true; } break; default: break; } // Jump out of the group loop if (lineIsCompleted) { break; } } } }
private void VerticalCheck(IBoard board, HashSet <BoardChange> changes) { for (int x = 0; x < board.Width; x++) { var groups = SimpleGrouper.GroupVertical(board, x); var ruleLine = board.TopRules[x]; if (ruleLine.Count == 0) { continue; } var highestRuleNumber = ruleLine.Max(); if (!groups.ContainsNones || highestRuleNumber == 0) { continue; } foreach (var group in groups.Groups) { if (group.State == CellState.Filled && group.Count == highestRuleNumber) { // Block off before the start of the group. if (group.StartIndex > 0 && board[x, group.StartIndex - 1] == CellState.None) { changes.Add(BoardChange.Blocked(x, group.StartIndex - 1)); } var lastIndex = group.StartIndex + group.Count; if (lastIndex < board.Height && board[x, lastIndex] == CellState.None) { changes.Add(BoardChange.Blocked(x, lastIndex)); } } } } }
private void Vertical(IBoard board, HashSet <BoardChange> changes) { for (int x = 0; x < board.Width; x++) { IRuleLine?ruleLine = board.TopRules[x]; if (ruleLine.Count != 1) { continue; } int f = (ruleLine[0] * 2) - board.Height; if (f > 0) { int e = ruleLine[0] - f; for (int filledCells = e; filledCells < e + f; filledCells++) { if (board[x, filledCells] == CellState.None) { changes.Add(BoardChange.Filled(x, filledCells)); } } } } }
private void UpdateEnemyInfo(BoardChange boardChange) { if (!boardChange.WasThereAnAttack()) { return; } PieceInfo thisPiece = boardChange.GetPieceFromAction(side); PieceInfo otherPiece = boardChange.GetPieceFromAction(side.Flip()); RankPossibilities otherPieceRank = enemyPieces[otherPiece.ID]; if (boardChange.GetWinningPiece() == null) { otherPieceRank.TiedBattle(thisPiece.Rank); } else if (otherPiece == boardChange.GetWinningPiece()) { otherPieceRank.WonBattle(thisPiece.Rank); } else // This piece is the winner { otherPieceRank.LostBattle(thisPiece.Rank); } // Remove from piece pool once piece has been discovered PieceRank guaranteedRank = otherPieceRank.GuaranteedRank; if (guaranteedRank == PieceRank.Invalid) { return; } // DEBUG piecePool[(int)guaranteedRank]--; pieceCounterPanel.UpdateText(piecePool); // If piece pool reaches 0, all other pieces can't be this piece if (piecePool[(int)guaranteedRank] > 0) { return; } foreach (RankPossibilities rankPossibilities in enemyPieces.Values) { if (rankPossibilities.GuaranteedRank == guaranteedRank) { continue; } rankPossibilities.RemovePiecePossibility(guaranteedRank); } }
public void SemanticEquals() { var a = new BoardChange(6, 3, CellState.Filled); var b = new BoardChange(6, 3, CellState.Filled); Assert.True(a.Equals(b)); Assert.Equal(a.GetHashCode(), b.GetHashCode()); var c = new BoardChange(3, 6, CellState.Filled); Assert.False(a.Equals(c)); Assert.NotEqual(a.GetHashCode(), c.GetHashCode()); }
public void BoardWithTwoToFillHorizontal_Flipped_MarkOneAsBlocked() { var board = BoardSamples.Board7_ManualFlipped .ApplyChanges( BoardChange.Blocked(3, 4) ); var solver = new FirstGroupTooSmall(); var actualChanges = solver.Solve(board); var expected = new List <BoardChange> { BoardChange.Blocked(4, 4) }; Assert.Equal(expected, actualChanges); }
public void BoardWithTwoToFillVertical_MarkOneAsBlocked() { var board = BoardSamples.Board7 .ApplyChanges( BoardChange.Blocked(0, 1) ); var solver = new FirstGroupTooSmall(); var actualChanges = solver.Solve(board); var expected = new List <BoardChange> { BoardChange.Blocked(0, 0) }; Assert.Equal(expected, actualChanges); }
private static void HorizontalRulesCheck(IBoard board, HashSet <BoardChange> changes) { for (int y = 0; y < board.Height; y++) { GroupCollection?groups = SimpleGrouper.GroupHorizontal(board, y); IRuleLine? ruleLine = board.LeftRules[y]; if (groups.ContainsNones && groups.SatisfiesRuleLine(ruleLine)) { for (int x = 0; x < board.Width; x++) { CellState state = board[x, y]; if (state == CellState.None) { changes.Add(BoardChange.Blocked(x, y)); } } } } }
private BoardChange EnPassantMove(BoardPieceMove move) { int pawnDirectionOpposite = BoardHelpers.GetPlayerDirection(BoardHelpers.GetOpponentPlayer(PlayerTurn)); BoardChange change = (new EnPassantTakePieceChange { Type = BoardChangeType.EnPassantTakePiece, Move = move, Player = PlayerTurn, TakingPieceType = BoardPieces[move.From.X, move.From.Y].PieceType, TakenPawnPosition = new Vector2I(move.To.X, move.To.Y + pawnDirectionOpposite) }); // Move piece StandardMoveBoardChange(BoardPieces, move); // Remove pawn BoardPieces[move.To.X, move.To.Y + pawnDirectionOpposite] = new BoardPiece(); return(change); }