/// <summary> /// For each position in the grid assigns a value based on symbols placed nearby /// and returns a set of points having the maximum value /// </summary> /// <param name="coin"></param> /// <returns><Point>List</Point></returns> public List <Point> StepOne(Symbol coin) { IEnumerable <Point> Coordinate = Board.CreatePoint(); List <Point> PossiblePosition = new List <Point>(); int MaxValue = 0; foreach (Point position in Coordinate) { if (Board.GetSymbol(position) != Symbol.blank) { continue; } int CurrentValue = CalculateStepOne(position, coin); int compareResult = CurrentValue.CompareTo(MaxValue); if (compareResult < 0) { continue; } if (compareResult > 0) { PossiblePosition.Clear(); MaxValue = CurrentValue; } PossiblePosition.Add(position); } return(PossiblePosition); }
/// <summary> /// For each point calculates a value based on the immediate neighbours /// By doing this we assign a weight for blocking the opponent and our chnace of winning /// </summary> /// <param name="position"></param> /// <param name="coin"></param> /// <returns>int</returns> public int CalculateStepTwo(Point position, Symbol coin) { Point TempPoint; int TempX = position.X; int TempY = position.Y; int ComputerValue = 0; int UserValue = 0; for (int x = TempX - 1; x <= TempX + 1; x++) { for (int y = TempY - 1; y <= TempY + 1; y++) { TempPoint = new Point(x, y); if (Board.GetSymbol(TempPoint) == coin) { ComputerValue++; } if (Board.GetSymbol(TempPoint) == GetUserSymbol(coin)) { UserValue++; } } } return(2 * ComputerValue + UserValue); }
/// <summary> /// This event will be generated when the user clicks on the playing grid, /// and checks if resize is required, places the user move, selects the best /// move for the computer and places the computer move. /// </summary> /// <param name="e"></param> protected override void OnMouseClick(MouseEventArgs e) { base.OnMouseClick(e); int CoordX = 0; int CoordY = 0; if (e.Button == MouseButtons.Left) { int ScrollX = (AutoScrollPosition.X % CellSize); int ScrollY = (AutoScrollPosition.Y % CellSize); AutoScrollPosition.Offset(ScrollX, ScrollY); CoordX = (int)Math.Round(((-(decimal)AutoScrollPosition.X) / (decimal)CellSize), 0); CoordY = (int)Math.Round(((-(decimal)AutoScrollPosition.Y) / (decimal)CellSize), 0); Point CellCoord = new Point((e.X / CellSize) + CoordX, (e.Y / CellSize) + CoordY); if (GmBoard.GetSymbol(CellCoord) == Symbol.blank) { Logic.MakeMove(CellCoord, UserSymbol); Refresh(); if (Logic.CheckResult(CellCoord, UserSymbol)) { GView.ShowResult(UserSymbol); GmBoard.Clear(); ScreenFlag = Screens.exit; } if (CheckResizeRequired(CellCoord) == 1) { ResizeBoard(); Refresh(); } else if (CheckResizeRequired(CellCoord) == 2) { IsTopLeft = true; ResizeBoard(); Refresh(); } Point ComputerPosition = Logic.SelectBestMove(ComputerSymbol); Logic.MakeMove(ComputerPosition, ComputerSymbol); Refresh(); if (Logic.CheckResult(ComputerPosition, ComputerSymbol)) { GView.ShowResult(ComputerSymbol); GmBoard.Clear(); ScreenFlag = Screens.exit; } Refresh(); } else { MessageBox.Show("Please make a valid move"); } } else { MessageBox.Show("Right Click not allowed"); } }
/// <summary> /// Counts the coins placed in the direction given by xIncrement and yIncrement /// </summary> /// <param name="startPos"></param> /// <param name="xIncrement"></param> /// <param name="yIncrement"></param> /// <param name="coin"></param> /// <returns>int</returns> int CountCoins(Point startPos, int xIncrement, int yIncrement, Symbol coin) { int score = 0; for (int i = 1; i < GameSize; i++) { Point current = startPos + new Size(i * xIncrement, i * yIncrement); if (Board.GetSymbol(current) != coin) { break; } score++; } return(score); }
/// <summary> /// Checks whether the the passed symbol has won /// </summary> /// <param name="position"></param> /// <param name="coin"></param> /// <returns>bool</returns> public bool IsWon(Point position, Symbol coin) { return(Board.GetSymbol(position) == coin && CountCoin(position, coin) >= GameSize - 1); }