/// <summary> /// Checks for X or 0 in every position of the array. Sets winner = 0 if full. /// </summary> /// <returns>true if the board is full</returns> private bool IsFull() { for (int i = 0; i < gameBoard.GetBoardSize(); i++) { if (gameBoard.GetBoardValue(i).Equals(BoardValue.Empty)) { return(false); } } winner = 0; return(true); }
private bool Check(int firstPos, int secondPos, int choice, BoardValue symbol) { if (GameBoard.GetBoardValue(firstPos).Equals(GameBoard.GetBoardValue(secondPos)) && GameBoard.GetBoardValue(firstPos).Equals(symbol)) { if (GameBoard.GetBoardValue(choice).Equals(BoardValue.Empty)) { return(true); } } return(false); }
/// <summary> /// Chooses randomly from the free positions in the array /// </summary> /// <returns></returns> public int RandomMove() { List <int> freePositions = new List <int>(); for (int i = 0; i < GameBoard.GetBoardSize(); i++) { if (GameBoard.GetBoardValue(i).Equals(BoardValue.Empty)) { freePositions.Add(i); } } return(freePositions[random.Next(0, freePositions.Count)]); }
public int NewTurn() { Thread.Sleep(2000); if (!HasStarted) //The first move { HasStarted = true; if (Symbol.Equals(BoardValue.X)) { int temp = random.Next(0, 4); return(CORNERS[temp]); } else { if (GameBoard.GetBoardValue(4).Equals(BoardValue.Empty)) //If the middle is free { return(4); } else { return(RandomMove()); } } } int choice; if (CheckForWinOrBlock(Symbol, out choice))//Check for win { return(choice); } if (Symbol.Equals(BoardValue.X)) { if (CheckForWinOrBlock(BoardValue.O, out choice))//Check for block { return(choice); } } else { if (CheckForWinOrBlock(BoardValue.X, out choice))//Check for block { return(choice); } } return(RandomMove()); }