internal bool IsWinningMove(VisualNode move, out NodeLocation winDirection) { int count = 1; Action <VisualNode> Count = (n) => { // we should not count the original move as we count that ouside of this loop. if (n.X == move.X && n.Y == move.Y) { return; } if (n.Value == move.Value) { count++; } }; count = 1; winDirection = NodeLocation.TopLeft; TraverseBoard(move, winDirection, Count); TraverseBoard(move, winDirection.GetReverseDirection(), Count); if (count >= NeededForWin) { return(true); } count = 1; winDirection = NodeLocation.TopCenter; TraverseBoard(move, winDirection, Count); TraverseBoard(move, winDirection.GetReverseDirection(), Count); if (count >= NeededForWin) { return(true); } count = 1; winDirection = NodeLocation.TopRight; TraverseBoard(move, winDirection, Count); TraverseBoard(move, winDirection.GetReverseDirection(), Count); if (count >= NeededForWin) { return(true); } count = 1; winDirection = NodeLocation.Left; TraverseBoard(move, winDirection, Count); TraverseBoard(move, winDirection.GetReverseDirection(), Count); if (count >= NeededForWin) { return(true); } return(false); }
private int CountOnBothDirectionsUsingBoard(NodeLocation direction, int currentX, int currentY, TicTacToeValue currentPlayer) { var moveFirstDir = CountOnSingleDirectionUsingBoard(direction, currentX, currentY, currentPlayer); var moveSecondDir = CountOnSingleDirectionUsingBoard(direction.GetReverseDirection(), currentX, currentY, currentPlayer); int count = moveFirstDir.countPerDirection + moveSecondDir.countPerDirection; // this is the case where we have something like: o x _ x x o // there is no point to put an o in there as that can never ever be a winning move from x if (count <= 3 && (moveFirstDir.endsWithOpponentMove && moveSecondDir.endsWithOpponentMove)) { count = 0; } if (count >= 4) { count *= count; } else if (count >= 3) { count *= 2; } if (count >= 2 && !moveFirstDir.endsWithOpponentMove) { count++; } if (count >= 2 && !moveSecondDir.endsWithOpponentMove) { count++; } return(count); }
internal void MarkWinningNodes(VisualNode node, NodeLocation winDirection) { // flag all the nodes on that direction so that when they are drawn, you can tell who won. TraverseBoard(node, winDirection, (n) => n.PartOfWinningMove = true); TraverseBoard(node, winDirection.GetReverseDirection(), (n) => n.PartOfWinningMove = true); }