예제 #1
0
        private bool checkCol()
        {
            _head = _justMove;
            _tail = _justMove;
            Box.State currSTT    = _matrix[_justMove.Row, _justMove.Col].STT;
            int       continuous = 1;

            for (int i = _justMove.Row + 1; i < _sizeBoard; i++)
            {
                if (_matrix[i, _justMove.Col].STT == currSTT)
                {
                    continuous++;
                    _tail = new Position(i, _justMove.Col);
                }
                else
                {
                    break;
                }
            }
            for (int i = _justMove.Row - 1; i >= 0; i--)
            {
                if (_matrix[i, _justMove.Col].STT == currSTT)
                {
                    continuous++;
                    _head = new Position(i, _justMove.Col);
                }
                else
                {
                    break;
                }
            }
            return(continuous == 5);
        }
예제 #2
0
        private bool checkRightDiagonal()
        {
            _head = _justMove;
            _tail = _justMove;
            Box.State currSTT    = _matrix[_justMove.Row, _justMove.Col].STT;
            int       continuous = 1;

            for (int i = _justMove.Row + 1, j = _justMove.Col - 1; i < _sizeBoard && j >= 0; i++, j--)
            {
                if (_matrix[i, j].STT == currSTT)
                {
                    continuous++;
                    _tail = new Position(i, j);
                }
                else
                {
                    break;
                }
            }
            for (int i = _justMove.Row - 1, j = _justMove.Col + 1; i >= 0 && j < _sizeBoard; i--, j++)
            {
                if (_matrix[i, j].STT == currSTT)
                {
                    continuous++;
                    _head = new Position(i, j);
                }
                else
                {
                    break;
                }
            }
            return(continuous == 5);
        }
예제 #3
0
파일: AlphaBeta.cs 프로젝트: user360/gomoku
        private static bool checkRightDiagonal(Node node, Position move)
        {
            Box.State currSTT    = node.Matrix[move.Row, move.Col].STT;
            int       continuous = 1;

            for (int i = move.Row + 1, j = move.Col - 1; i < node.SizeBoard && j >= 0; i++, j--)
            {
                if (node.Matrix[i, j].STT == currSTT)
                {
                    continuous++;
                }
                else
                {
                    break;
                }
            }
            for (int i = move.Row - 1, j = move.Col + 1; i >= 0 && j < node.SizeBoard; i--, j++)
            {
                if (node.Matrix[i, j].STT == currSTT)
                {
                    continuous++;
                }
                else
                {
                    break;
                }
            }
            return(continuous == 5);
        }
예제 #4
0
        private bool checkRow()
        {
            _head = _justMove;
            _tail = _justMove;
            Box.State currSTT    = _matrix[_justMove.Row, _justMove.Col].STT;
            int       continuous = 1;

            for (int j = _justMove.Col + 1; j < _sizeBoard; j++)
            {
                if (_matrix[_justMove.Row, j].STT == currSTT)
                {
                    continuous++;
                    _tail = new Position(_justMove.Row, j);
                }
                else
                {
                    break;
                }
            }
            for (int j = _justMove.Col - 1; j >= 0; j--)
            {
                if (_matrix[_justMove.Row, j].STT == currSTT)
                {
                    _head = new Position(_justMove.Row, j);
                    continuous++;
                }
                else
                {
                    break;
                }
            }
            return(continuous == 5);
        }
예제 #5
0
파일: AlphaBeta.cs 프로젝트: user360/gomoku
        private static bool checkRow(Node node, Position move)
        {
            Box.State currSTT    = node.Matrix[move.Row, move.Col].STT;
            int       continuous = 1;

            for (int j = move.Col + 1; j < node.SizeBoard; j++)
            {
                if (node.Matrix[move.Row, j].STT == currSTT)
                {
                    continuous++;
                }
                else
                {
                    break;
                }
            }
            for (int j = move.Col - 1; j >= 0; j--)
            {
                if (node.Matrix[move.Row, j].STT == currSTT)
                {
                    continuous++;
                }
                else
                {
                    break;
                }
            }
            return(continuous == 5);
        }
예제 #6
0
파일: AlphaBeta.cs 프로젝트: user360/gomoku
        private static long Minimax(Node currNode, Position currMove, int currDepth, Box.State currState, long alpha, long beta, ref int openedNodes)
        {
            if (IsTerminal(currNode, currMove, currDepth))
            {
                if (currState == _opp)
                {
                    return(Heuristic.Eluavation(currNode, currMove));
                }
                else if (currState == _own)
                {
                    return(Heuristic.Eluavation(currNode, currMove) * (-1));
                }
            }

            List <Position> moveList = currNode.IsCanMoves();

            if (currState == _own)
            {
                long bestScore = long.MinValue;
                foreach (Position move in moveList)
                {
                    currNode.Move(move, _own);
                    openedNodes += 1;
                    long score = Minimax(currNode, move, currDepth - 1, _opp, alpha, beta, ref openedNodes);
                    currNode.UnMove(move);
                    bestScore = Math.Max(bestScore, score);
                    alpha     = Math.Max(alpha, bestScore);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(bestScore);
            }
            else
            {
                long bestScore = long.MaxValue;
                foreach (Position move in moveList)
                {
                    openedNodes += 1;
                    currNode.Move(move, _opp);
                    long score = Minimax(currNode, move, currDepth - 1, _own, alpha, beta, ref openedNodes);
                    currNode.UnMove(move);
                    bestScore = Math.Min(bestScore, score);
                    beta      = Math.Min(beta, bestScore);
                    if (beta <= alpha)
                    {
                        break;
                    }
                }
                return(bestScore);
            }
        }
예제 #7
0
        private bool checkRightDiagonal1()
        {
            _head = _justMove;
            _tail = _justMove;
            Box.State currSTT = _matrix[_justMove.Row, _justMove.Col].STT;
            int       continuous = 1;
            int       r1 = _sizeBoard, r2 = -1;
            bool      flag = true;

            for (int i = _justMove.Row + 1, j = _justMove.Col - 1; i < _sizeBoard && j >= 0; i++, j--)
            {
                if (_matrix[i, j].STT == currSTT && flag)
                {
                    continuous++;
                    _tail = new Position(i, j);
                }
                else if (_matrix[i, j].STT != Box.State.blank)
                {
                    r1 = i;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            flag = true;
            for (int i = _justMove.Row - 1, j = _justMove.Col + 1; i >= 0 && j < _sizeBoard; i--, j++)
            {
                if (_matrix[i, j].STT == currSTT && flag)
                {
                    continuous++;
                    _head = new Position(i, j);
                }
                else if (_matrix[i, j].STT != Box.State.blank)
                {
                    r2 = i;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            return(continuous == 5 && r1 - r2 > 6);
        }
예제 #8
0
        private bool checkRow1()
        {
            _head = _justMove;
            _tail = _justMove;
            Box.State currSTT = _matrix[_justMove.Row, _justMove.Col].STT;
            int       continuous = 1;
            int       c1 = _sizeBoard, c2 = -1;
            bool      flag = true;

            for (int j = _justMove.Col + 1; j < _sizeBoard; j++)
            {
                if (_matrix[_justMove.Row, j].STT == currSTT && flag)
                {
                    continuous++;
                    _tail = new Position(_justMove.Row, j);
                }
                else if (_matrix[_justMove.Row, j].STT != Box.State.blank)
                {
                    c1 = j;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            flag = true;
            for (int j = _justMove.Col - 1; j >= 0; j--)
            {
                if (_matrix[_justMove.Row, j].STT == currSTT && flag)
                {
                    continuous++;
                    _head = new Position(_justMove.Row, j);
                }
                else if (_matrix[_justMove.Row, j].STT != Box.State.blank)
                {
                    c2 = j;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            return(continuous == 5 && c1 - c2 > 6);
        }
예제 #9
0
파일: AlphaBeta.cs 프로젝트: user360/gomoku
        public static Position GetBestMove(Node currNode, Box.State maximizingPlayerSTT, int depth, ref int openedNodes)
        {
            openedNodes = 0;
            Random rand = new Random();

            _own = maximizingPlayerSTT;
            if (_own == Box.State.blue)
            {
                _opp = Box.State.red;
            }
            else
            {
                _opp = Box.State.blue;
            }

            long bestScore = long.MinValue;
            long alpha     = long.MinValue;
            long beta      = long.MaxValue;
            Dictionary <Position, long> dict     = new Dictionary <Position, long>();
            List <Position>             moveList = currNode.IsCanMoves();

            openedNodes += moveList.Count;
            for (int i = 0; i < moveList.Count; i++)
            {
                currNode.Move(moveList[i], _own);
                long score = Minimax(currNode, moveList[i], depth - 1, _opp, alpha, beta, ref openedNodes);
                currNode.UnMove(moveList[i]);
                dict.Add(moveList[i], score);
                if (bestScore < score)
                {
                    bestScore = score;
                }
            }
            List <Position> res = new List <Position>();

            foreach (Position key in dict.Keys)
            {
                if (dict[key] == bestScore)
                {
                    res.Add(key);
                }
            }
            return(res[rand.Next(res.Count)]);
        }
예제 #10
0
파일: AlphaBeta.cs 프로젝트: user360/gomoku
        private static bool checkLeftDiagonal1(Node node, Position move)
        {
            Box.State currSTT = node.Matrix[move.Row, move.Col].STT;
            int       continuous = 1;
            int       r1 = node.SizeBoard, r2 = -1;
            bool      flag = true;

            for (int i = move.Row + 1, j = move.Col + 1; i < node.SizeBoard && j < node.SizeBoard; i++, j++)
            {
                if (node.Matrix[i, j].STT == currSTT && flag)
                {
                    continuous++;
                }
                else if (node.Matrix[i, j].STT != Box.State.blank)
                {
                    r1 = i;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            flag = true;
            for (int i = move.Row - 1, j = move.Col - 1; i >= 0 && j >= 0; i--, j--)
            {
                if (node.Matrix[i, j].STT == currSTT && flag)
                {
                    continuous++;
                }
                else if (node.Matrix[i, j].STT != Box.State.blank)
                {
                    r2 = i;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            return(continuous == 5 && r1 - r2 > 6);
        }
예제 #11
0
파일: AlphaBeta.cs 프로젝트: user360/gomoku
        private static bool checkRow1(Node node, Position move)
        {
            Box.State currSTT = node.Matrix[move.Row, move.Col].STT;
            int       continuous = 1;
            int       c1 = node.SizeBoard, c2 = -1;
            bool      flag = true;

            for (int j = move.Col + 1; j < node.SizeBoard; j++)
            {
                if (node.Matrix[move.Row, j].STT == currSTT)
                {
                    continuous++;
                }
                else if (node.Matrix[move.Row, j].STT != Box.State.blank)
                {
                    c1 = j;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            flag = true;
            for (int j = move.Col - 1; j >= 0; j--)
            {
                if (node.Matrix[move.Row, j].STT == currSTT && flag)
                {
                    continuous++;
                }
                else if (node.Matrix[move.Row, j].STT != Box.State.blank)
                {
                    c2 = j;
                    break;
                }
                else
                {
                    flag = false;
                }
            }
            return(continuous == 5 && c1 - c2 > 6);
        }