Esempio n. 1
0
        public int[] Algorithm(Board board, bool tempFirst)
        {
            int[] pos = new int[2] {
                -1, -1
            };
            int score = -100;

            List <int[]> empties = board.GetEmpties();

            for (int i = 0; i < empties.Count; i++)
            {
                int[] tempPos = empties[i];
                bool  winner  = PlaceMarkAndCheckWinner(board, tempPos, tempFirst);

                int tempScore = 0;
                if (!winner && tempBoard.GetEmpties().Count > 0)
                {
                    int[] tempResult = Algorithm(tempBoard, !tempFirst);
                    tempScore = -tempResult[2];
                }

                else
                {
                    tempScore = Heuristics(winner);
                }

                if (tempScore > score)
                {
                    pos   = tempPos;
                    score = tempScore;
                }
            }

            return(new[] { pos[0], pos[1], score });
        }
Esempio n. 2
0
        private void UpdateGameStatus(int[] pos, int index)
        {
            GUISetCell(index);
            board.SetCell(pos, first);

            winner = gameStats.IsWinner(board, first);
            full   = (board.GetEmpties().Count == 0);

            if (winner || full)
            {
                clicked = true;
                GameOver();
            }

            else
            {
                // Check the humanity of the next player
                clicked        = ((first && !player2.IsHuman) || (!first && !player1.IsHuman)) ? true : false;
                first          = !first;
                statusBar.Text = (first) ? player1.Name : player2.Name;

                if (clicked)
                {
                    RobotTurn();
                }
            }
        }
        public int[] Algorithm(Board board, bool tempFirst, int alpha, int beta, int depth)
        {
            int[] pos = new int[2] {-1, -1};
            int score = -100;

            List<int[]> empties = board.GetEmpties();
            bool exit = false;

            for (int i=0; i<empties.Count && !exit; i++) {
                int[] tempPos = empties[i];
                bool winner   = PlaceMarkAndCheckWinner(board, tempPos, tempFirst);

                int tempScore = 0;
                if (!winner && tempBoard.GetEmpties().Count > 0 && depth > 0) {
                    int[] tempResult = Algorithm(tempBoard, !tempFirst, -beta, -alpha, depth - 1);
                    tempScore        = -tempResult[2];
                }

                else
                    tempScore = Heuristics(winner);

                if (tempScore > score) {
                    pos   = tempPos;
                    score = tempScore;
                }

                if (tempScore > alpha)
                    alpha = tempScore;

                if (alpha >= beta)
                    exit = true;
            }

            return new[] {pos[0], pos[1], score};
        }
Esempio n. 4
0
        public int[] Algorithm(Board board, bool tempFirst)
        {
            int[] pos = new int[2] { -1, -1 };
            int score = (tempFirst == first) ? -100 : 100;

            List<int[]> empties = board.GetEmpties();

            for (int i=0; i<empties.Count; i++) {
                int[] tempPos = empties[i];
                bool winner   = PlaceMarkAndCheckWinner(board, tempPos, tempFirst);

                int tempScore = 0;
                if (!winner && tempBoard.GetEmpties().Count > 0) {
                    int[] tempResult = Algorithm(tempBoard, !tempFirst);
                    tempScore        = tempResult[2];
                }

                else
                    tempScore = Heuristics(winner, tempFirst);

                if ((tempFirst == first && tempScore > score) ||
                    (tempFirst != first && tempScore < score)) {
                    pos   = tempPos;
                    score = tempScore;
                }
            }

            return new[] { pos[0], pos[1], score };
        }
Esempio n. 5
0
        public int[] Algorithm(Board board, bool tempFirst, int alpha, int beta, int depth)
        {
            int[] pos = new int[2] {
                -1, -1
            };
            int score = -100;

            List <int[]> empties = board.GetEmpties();
            bool         exit    = false;

            for (int i = 0; i < empties.Count && !exit; i++)
            {
                int[] tempPos = empties[i];
                bool  winner  = PlaceMarkAndCheckWinner(board, tempPos, tempFirst);

                int tempScore = 0;
                if (!winner && tempBoard.GetEmpties().Count > 0 && depth > 0)
                {
                    int[] tempResult = Algorithm(tempBoard, !tempFirst, -beta, -alpha, depth - 1);
                    tempScore = -tempResult[2];
                }

                else
                {
                    tempScore = Heuristics(winner);
                }

                if (tempScore > score)
                {
                    pos   = tempPos;
                    score = tempScore;
                }

                if (tempScore > alpha)
                {
                    alpha = tempScore;
                }

                if (alpha >= beta)
                {
                    exit = true;
                }
            }

            return(new[] { pos[0], pos[1], score });
        }