Guest User

Untitled

a guest
Jul 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. public static Position GetBestMove(GameBoard Gb, Slot P, Difficulty Difficulty)
  2.         {
  3.             Position? bestSpace = null;
  4.             List<Position> openSpaces = Gb.OpenSquares;
  5.             GameBoard newBoard;
  6.  
  7.             for (int i = 0; i < openSpaces.Count; i++)
  8.             {
  9.                 newBoard = Gb.Clone();
  10.                 Position newSpace = openSpaces[i];
  11.  
  12.                 newBoard[newSpace.Row, newSpace.Col] = P;
  13.  
  14.                 if (newBoard.Winner == Slot.Open && newBoard.OpenSquares.Count > 0)
  15.                 {
  16.                     Position tempMove = GetBestMove(newBoard, ((Slot)(-(int)P)), Difficulty);
  17.                     newSpace.Mark = tempMove.Mark;
  18.                 }
  19.                 else
  20.                 {
  21.                     if (newBoard.Winner == Slot.Open)
  22.                         newSpace.Mark = 0;
  23.                     else if (newBoard.Winner == Slot.X)
  24.                         newSpace.Mark = -1;
  25.                     else if (newBoard.Winner == Slot.O)
  26.                         newSpace.Mark = 1;
  27.                 }
  28.  
  29.                 if (bestSpace == null ||
  30.                    (P == Slot.X && newSpace.Mark < ((Position)bestSpace).Mark) ||
  31.                    (P == Slot.O && newSpace.Mark > ((Position)bestSpace).Mark))
  32.                 {
  33.                     bestSpace = newSpace;
  34.                 }
  35.             }
  36.  
  37.             return (Position)bestSpace;
  38.         }
Add Comment
Please, Sign In to add comment