예제 #1
0
파일: AI.cs 프로젝트: AV2606/Tic-tac-toe
        /// <summary>
        /// Do brute force for the opponent turn logic
        /// </summary>
        /// <param name="gl"></param>
        /// <param name="p"></param>
        /// <param name="me">the AI sign (X or O)</param>
        /// <returns></returns>
        private int oRBruteForce(GameLogic gl, int current_depth)
        {
            //todo
            depth++;
            var ex         = new GameLogic(gl);
            int freespaces = ex.GetFreeSpaces();
            var positions  = ex.AllFreeSpaces();

            return(-1);
        }
예제 #2
0
파일: AI.cs 프로젝트: AV2606/Tic-tac-toe
        /// <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));
        }
예제 #3
0
파일: AI.cs 프로젝트: AV2606/Tic-tac-toe
        /// <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);
        }
예제 #4
0
파일: AI.cs 프로젝트: AV2606/Tic-tac-toe
        /// <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));
        }
예제 #5
0
파일: AI.cs 프로젝트: AV2606/Tic-tac-toe
 /// <summary>
 /// procceding to win and "thinks" about the further game.
 /// </summary>
 /// <param name="gl">The <seealso cref="GameLogic"/> of the game the ai playing.</param>
 /// <returns></returns>
 private Position Hard(GameLogic gl)
 {
     if (gl.GetFreeSpaces() == 0)
     {
         return(new Position(-1));
     }
     if (EasyWin(gl).row != -1)
     {
         return(EasyWin(gl));
     }
     if (MustPut(gl).row != -1)
     {
         return(MustPut(gl));
     }
     if (Tricks(gl).row != -1)
     {
         return(Tricks(gl));
     }
     return(gl.AllFreeSpaces()[0]);
     //return BruteForce(gl, CellE.o);
 }