/// <summary> /// returns a move that will make the AI one step from winning (not a trap) /// </summary> /// <param name="gl">The <seealso cref="GameLogic"/> of the game the ai playing.</param> /// <returns></returns> private Position Ahead(GameLogic gl) { var ex = new GameLogic(gl); var a = ex.AllFreeSpaces(); for (int i = 0; i < a.Length; i++) { PutMe(ex, a[i]); if (EasyWin(ex).row != -1) { return(a[i]); } ex.Undo(); } return(new Position(-1, -1)); }
/// <summary> /// Returns in how many ways can the AI win in it next turn. /// </summary> /// <param name="gl">The game the AI is playing</param> /// <returns></returns> private int WinningWays(GameLogic gl) { var ex = new GameLogic(gl); var pos = ex.AllFreeSpaces(); int result = 0, freeSpaces = pos.Length; for (int i = 0; i < freeSpaces; i++) { PutMe(ex, pos[i]); if (AmIWon(ex)) { result++; } ex.Undo(); } return(result); }
/// <summary> /// Tries to do tricks to elevate the AI situation. /// </summary> /// <param name="gl"></param> /// <returns></returns> private Position Tricks(GameLogic gl) { var ex = new GameLogic(gl); Stack <Position> corners = new Stack <Position>(); Stack <Position> sides = new Stack <Position>(); if (ex.History.Count == 1) { if (HasInCorner(ex, him, out corners)) { return(new Position(1, 1)); } if (HasInSides(ex, him, out sides)) { return(new Position(1, 1)); } if (ex.board.board[1, 1].player == him) { return(new Position(0, 0)); } } if (Ahead(ex).row != -1) { return(Ahead(ex)); } var pos = ex.AllFreeSpaces(); Position best = pos[0]; int best_int = 0; for (int i = 0; i < pos.Length; i++) { PutMe(ex, pos[i]); if (WinningWays(ex) > best_int) { best_int = WinningWays(ex); best = pos[i]; } ex.Undo(); } return(best); return(new Position(-1, -1)); }
/// <summary> /// returns the position in which its an immidate win for the player. /// </summary> /// <param name="gl">The <seealso cref="GameLogic"/> of the game the ai playing.</param> /// <returns></returns> private Position OEasyWin(GameLogic gl) { GameLogic ex = new GameLogic(gl); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (PutHim(ex, new Position(i, j))) { if (!AmIWon(ex) && !ex.IsDraw()) { return(new Position(i, j)); } else { ex.Undo(); } } } } return(new Position(-1, -1)); }
/// <summary> /// return the positin which x will win if put there or -1,-1 if there's no such position /// </summary> /// <param name="gl">game logic of the game (will not affect the game)</param> /// <returns></returns> private Position MustPut(GameLogic gl) { GameLogic ex = new GameLogic(gl); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (PutHim(ex, new Position(i, j))) { if (DoHimWon(ex)) { return(new Position(i, j)); } else { ex.Undo(); } } } } return(new Position(-1, -1)); }