/// <summary> /// Returns the best position to play based on the 2 opponents. /// </summary> /// <param name="board"></param> /// <param name="actionBoundary"></param> /// <returns>best position to play</returns> private static Vector2 GetBestPosition(ref Cell[,] board, ref Boundary actionBoundary) { var bestBlack = new ScoreAndPosition(); var bestWhite = new ScoreAndPosition(); var currentBlack = new ScoreAndPosition(); var currentWhite = new ScoreAndPosition(); for (var row = actionBoundary.YMin; row <= actionBoundary.YMax; row++) { for (var column = actionBoundary.XMin; column <= actionBoundary.XMax; column++) { if (board[row, column].State != State.Free) { continue; } currentBlack.Position.SetVector2(column, row); currentWhite.Position.SetVector2(column, row); GetBestScoreAndPositionFromState(ref currentBlack, ref board, ref actionBoundary, State.Me); GetBestScoreAndPositionFromState(ref currentWhite, ref board, ref actionBoundary, State.Ennemy); if (bestBlack.Score < currentBlack.Score) { bestBlack.Position.SetVector2(column, row); bestBlack.Score = currentBlack.Score; } if (bestWhite.Score < currentWhite.Score) { bestWhite.Position.SetVector2(column, row); bestWhite.Score = currentWhite.Score; } } } return((bestBlack.Score > bestWhite.Score ? bestBlack.Position : bestWhite.Position)); }
/// <summary> /// Affects the score of the current position based on state. /// </summary> /// <param name="scoreAndPosition"></param> /// <param name="board"></param> /// <param name="actionBoundary"></param> /// <param name="state"></param> protected static void GetBestScoreAndPositionFromState(ref ScoreAndPosition scoreAndPosition, ref Cell[,] board, ref Boundary actionBoundary, State state) { int score; int tmpScore; // Vertical score = GetVerticalScore(ref scoreAndPosition.Position, ref board, ref actionBoundary, state); // Horizontal tmpScore = GetHorizontalScore(ref scoreAndPosition.Position, ref board, ref actionBoundary, state); score = (tmpScore > score ? tmpScore : score); // Top-left to bottom-right tmpScore = GetTopLeftToRightBottomScore(ref scoreAndPosition.Position, ref board, ref actionBoundary, state); score = (tmpScore > score ? tmpScore : score); // Top-right to bottom-left tmpScore = GetTopRightToLeftBottomScore(ref scoreAndPosition.Position, ref board, ref actionBoundary, state); score = (tmpScore > score ? tmpScore : score); scoreAndPosition.Score = score; }