Exemplo n.º 1
0
        private Move MiniMax(int[][] bOARD, int depth, int whosTurn, int oroginalI, int originalJ)
        {
            MINMAXCOUNTER++;
            if (MINMAXCOUNTER % 100000 == 0)
            {
                Console.Write("\rCalculation: {0}", MINMAXCOUNTER.ToString("N0"));
            }
            if (depth > BIGGESTDEPTH)
            {
                BIGGESTDEPTH++;
            }
            //if (depth >= MAXDEPTH)
            //  if (whosTurn==COMPUTERSYMBOL)
            //   return new Move(-5, -5, Score.TooDeep);

            var checkEndGameResult = CheckEndGame();//user view, computer win = loose(-100)

            if (checkEndGameResult != -3)
            {
                if (checkEndGameResult == (int)Score.Win)
                {
                    return(new Move(-2, -2, Score.Loose));
                }
                else if (checkEndGameResult == (int)Score.Loose)
                {
                    return(new Move(-2, -2, Score.Win));
                }
                else if (checkEndGameResult == (int)Score.Draw)
                {
                    return(new Move(-2, -2, Score.Draw));
                }
            }

            if (whosTurn == COMPUTERSYMBOL)
            {
                var best_move = new Move();
                best_move.score = Score.Loose;
                for (var i = 0; i < BOARD.Length; i++)
                {
                    for (var j = 0; j < BOARD[i].Length; j++)
                    {
                        if (BOARD[i][j] == EMPTYSYMBOL)
                        {
                            BOARD[i][j] = COMPUTERSYMBOL;
                            var temp_move = new Move(-5, -5, Score.Loose);
                            if (depth == 0)
                            {
                                temp_move = MiniMax(BOARD, depth + 1, USERSYMBOL, i, j);
                            }
                            else
                            {
                                temp_move = MiniMax(BOARD, depth + 1, USERSYMBOL, oroginalI, originalJ);
                            }
                            if ((int)temp_move.score >= (int)best_move.score)
                            {
                                best_move.score = temp_move.score;
                                best_move.x     = i; best_move.y = j;
                            }
                            BOARD[i][j] = 0;
                            if (best_move.score == Score.Win)
                            {
                                return(best_move);
                            }
                            if (clock.Elapsed.TotalSeconds >= ((oroginalI + 1) + (originalJ * BOARD.Length)) * MAXTIMEPERCELL)
                            {
                                return(best_move);
                            }
                        }
                    }
                }
                return(best_move);
            }

            else
            {
                var best_move = new Move();
                best_move.score = Score.Win;
                for (var i = 0; i < BOARD.Length; i++)
                {
                    for (var j = 0; j < BOARD[i].Length; j++)
                    {
                        if (BOARD[i][j] == EMPTYSYMBOL)
                        {
                            BOARD[i][j] = USERSYMBOL;
                            var temp_move = new Move(-5, -5, Score.Loose);
                            if (depth == 0)
                            {
                                temp_move = MiniMax(BOARD, depth + 1, COMPUTERSYMBOL, i, j);
                            }
                            else
                            {
                                temp_move = MiniMax(BOARD, depth + 1, COMPUTERSYMBOL, oroginalI, originalJ);
                            }

                            if (temp_move.score < best_move.score)
                            {
                                best_move.score = temp_move.score;
                                best_move.x     = i; best_move.y = j;
                            }
                            BOARD[i][j] = 0;
                            if (best_move.score == Score.Loose)
                            {
                                return(best_move);
                            }
                            if (clock.Elapsed.TotalSeconds >= ((oroginalI + 1) + (originalJ * BOARD.Length)) * MAXTIMEPERCELL)
                            {
                                return(best_move);
                            }
                        }
                    }
                }
                return(best_move);
            }
        }
Exemplo n.º 2
0
            private Move MiniMax(int[][] bOARD, int depth, int whosTurn)
            {
                MINMAXCOUNTER++;
                if (MINMAXCOUNTER % 100000 == 0)
                {
                    Console.Write("\rCalculation: {0}", MINMAXCOUNTER.ToString("N0"));
                }
                if (depth > BIGGESTDEPTH)
                {
                    BIGGESTDEPTH++;
                }
                if (depth >= MAXDEPTH)
                {
                    //  if (whosTurn==COMPUTERSYMBOL)
                    return(new Move(-5, -5, Score.Draw));
                }

                var checkEndGameResult = CheckEndGame();//user view, computer win = loose(-100)

                if (checkEndGameResult != -3)
                {
                    if (checkEndGameResult == (int)Score.Win)
                    {
                        return(new Move(-2, -2, Score.Loose));//////////////////////////////
                    }
                    else if (checkEndGameResult == (int)Score.Loose)
                    {
                        return(new Move(-2, -2, Score.Win));//////////////////////////////////////
                    }
                    else if (checkEndGameResult == (int)Score.Draw)
                    {
                        return(new Move(-2, -2, Score.Draw));
                    }
                }

                if (whosTurn == COMPUTERSYMBOL)
                {
                    var best_move = new Move();
                    best_move.score = Score.Loose;
                    for (var i = 0; i < BOARD.Length; i++)
                    {
                        for (var j = 0; j < BOARD[i].Length; j++)
                        {
                            if (BOARD[i][j] == EMPTYSYMBOL)
                            {
                                BOARD[i][j] = COMPUTERSYMBOL;
                                var temp_move = MiniMax(BOARD, depth + 1, USERSYMBOL);
                                if ((int)temp_move.score >= (int)best_move.score)
                                {
                                    best_move.score = temp_move.score;
                                    best_move.x     = i; best_move.y = j;
                                }
                                BOARD[i][j] = 0;
                                if (best_move.score == Score.Win)
                                {
                                    return(best_move);
                                }
                            }
                        }
                    }
                    return(best_move);
                }

                else
                {
                    var best_move = new Move();
                    best_move.score = Score.Win;
                    for (var i = 0; i < BOARD.Length; i++)
                    {
                        for (var j = 0; j < BOARD[i].Length; j++)
                        {
                            if (BOARD[i][j] == EMPTYSYMBOL)
                            {
                                BOARD[i][j] = USERSYMBOL;
                                var temp_move = MiniMax(BOARD, depth + 1, COMPUTERSYMBOL);
                                if (temp_move.score < best_move.score)
                                {
                                    best_move.score = temp_move.score;
                                    best_move.x     = i; best_move.y = j;
                                }
                                BOARD[i][j] = 0;
                                if (best_move.score == Score.Loose)
                                {
                                    return(best_move);
                                }
                            }
                        }
                    }
                    return(best_move);
                }
            }