Beispiel #1
0
        private static State MinSearch(State state, int depth, int side)
        {
            if (depth <= 0)
            {
                state.EvaluateValue();
                return(state);
            }

            var moveList = state.GenerateAllMoves(side);
            var it       = moveList.GetEnumerator();

            State minState = null;
            var   value    = int.MaxValue;

            while (it.MoveNext())
            {
                var newState = PieceMove.MovePiece(state, it.Current);
                var newValue = MaxSearch(newState, depth - 1, ChangeSide(side)).GetValue();
                if (newValue < value)
                {
                    minState = newState;
                    value    = newValue;
                    minState.SetValue(newValue);
                }
            }
            return(minState);
        }
Beispiel #2
0
        private static State MaxSearch(State state, int depth, int side, int alpha, int beta)
        {
            if (depth <= 0)
            {
                state.EvaluateValue();
                return(state);
            }

            var newAlpha = alpha;
            var newBeta  = beta;
            var moveList = state.GenerateAllMoves(side);
            var it       = moveList.GetEnumerator();

            var maxState = new State();

            maxState.SetValue(newAlpha);

            while (it.MoveNext())
            {
                var newState = PieceMove.MovePiece(state, it.Current);
                var newValue = MinSearch(newState, depth - 1, ChangeSide(side), newAlpha, newBeta).GetValue();
                if (newValue > newAlpha)
                {
                    maxState = newState;
                    newAlpha = newValue;
                    maxState.SetValue(newValue);
                }
                if (newBeta <= newAlpha)
                {
                    return(maxState);
                }
            }
            return(maxState);
        }
Beispiel #3
0
        public void TestGenerateAllMoveAtMidState()
        {
            var fromX       = 1;
            var fromY       = 7;
            var toX         = 4;
            var toY         = 7;
            var midState    = PieceMove.MovePiece(State, fromX, fromY, toX, toY);
            var newMoveList = Cannon.GenerateAllMove(midState, toX, toY);

            Assert.AreEqual(8, newMoveList.Count);
        }
Beispiel #4
0
        public void TestPieceListPrint()
        {
            var    stringBefore = State.ToString(State.GetPieceList());
            int    fromX        = 1;
            int    fromY        = 9;
            int    toX          = 2;
            int    toY          = 7;
            State  midState     = PieceMove.MovePiece(State, fromX, fromY, toX, toY);
            String stringAfter  = midState.ToString(midState.GetPieceList());

            Assert.AreNotEqual(stringBefore, stringAfter);
        }
Beispiel #5
0
        public void TestLegalMoveSuicide()
        {
            var fromX = 1;
            var fromY = 7;
            var toX   = 1;
            var toY   = 9;

            var midState = PieceMove.MovePiece(State, fromX, fromY, toX, toY);
            var toXFinal = 1;
            var toYFinal = 9;

            Assert.AreEqual(false, Cannon.IsLegalMove(midState, toX, toY, toXFinal, toYFinal));
        }
Beispiel #6
0
        public void TestCloningCap()
        {
            var fromX = 1;
            var fromY = 7;
            var toX   = 1;
            var toY   = 0;

            var before = State.ToString();

            PieceMove.MovePiece(State, fromX, fromY, toX, toY);
            var after = State.ToString();

            Assert.AreEqual(before, after);
        }
Beispiel #7
0
        public void TestPieceListSimple()
        {
            int fromX = 1;
            int fromY = 9;
            int toX   = 2;
            int toY   = 7;

            var pieceListBefore = State.GetPieceList();
            var midState        = PieceMove.MovePiece(State, fromX, fromY, toX, toY);
            var pieceListAfter  = midState.GetPieceList();

            var piece1 = pieceListBefore.Get(Utility.GetOneDimention(fromX, fromY));
            var piece2 = pieceListAfter.Get(Utility.GetOneDimention(fromX, fromY));

            Console.WriteLine(midState);
            Assert.AreNotEqual(piece1.GetNumber(), piece2.GetNumber());
        }
Beispiel #8
0
        public void TestMovePieceSimple()
        {
            var states = new State();

            states = PieceMove.MovePiece(states, 7, 7, 4, 7);//user

            Debug.WriteLine(states.ToString());
            //states = PieceMove.MovePiece(states, 1, 2, 1, 9);//com
            states = (AlphaBetaSearch.DoSearch(states, 1));
            Debug.WriteLine(states.ToString());
            states = PieceMove.MovePiece(states, 0, 9, 1, 9);//user
            Debug.WriteLine(states.ToString());
            Assert.AreEqual(30, states.GetPieceList().Count);
            var piece = PieceFactory.GetPiece(23, 1, 9);

            Assert.AreEqual(typeof(Rook), piece.GetType());
        }
Beispiel #9
0
        public void TestPieceList()
        {
            Setup();
            var fromX      = 1;
            var fromY      = 9;
            var toX        = 2;
            var toY        = 7;
            var midState   = PieceMove.MovePiece(State, fromX, fromY, toX, toY);
            var pieceList  = midState.GetPieceList();
            var sizeBefore = pieceList.Count;

            var toXkillTeamMate = 3;
            var toYkillTeamMate = 9;

            midState  = PieceMove.MovePiece(midState, toX, toY, toXkillTeamMate, toYkillTeamMate);
            pieceList = midState.GetPieceList();
            var sizeAfter = pieceList.Count;

            Assert.AreEqual(sizeBefore, sizeAfter + 1);
        }
Beispiel #10
0
        private Move.MoveStatus HandleUserMove(State state, int fromX, int fromY, int toX, int toY)
        {
            var move = Move.MoveStatus.NoError;

            var fromK = Utility.GetOneDimention(fromX, fromY);
            var piece = CurrentState.GetPieceList().Get(fromK);

            if (piece.GetSide() == State.EmptySpace)
            {
                move = Move.MoveStatus.WrongPiece;
            }
            else if (piece.GetSide() != State.UserTurn)
            {
                move = Move.MoveStatus.WrongPiece;
            }
            else if (!PieceMove.MovePieceLegal(state, fromX, fromY, toX, toY))
            {
                move = Move.MoveStatus.Illegal;
            }
            PieceMove.MovePiece(state, fromX, fromY, toX, toY);
            return(move);
        }