Example #1
0
        public static void AddRightDiagonal(ChessBoard chessBoard)
        {
            var levels = RandomGenerator.GetDistinctInts(4, 1, 16);

            chessBoard.AddNew(new Position(0, 3), levels[0]);
            chessBoard.AddNew(new Position(1, 2), levels[1]);
            chessBoard.AddNew(new Position(2, 1), levels[2]);
            chessBoard.AddNew(new Position(3, 0), levels[3]);
        }
Example #2
0
        public void TestBottomLineMoveDownFail()
        {
            var chessBoard = new ChessBoard();

            chessBoard.AddNew(new Position(3, 0), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(3, 1), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(3, 2), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(3, 3), ChessBoardHandler.RandomLevel());

            Assert.AreEqual(chessBoard.Move(Direction.Down), false);
        }
Example #3
0
        public void TestRightLineMoveRightFail()
        {
            var chessBoard = new ChessBoard();

            chessBoard.AddNew(new Position(0, 3), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(1, 3), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(2, 3), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(3, 3), ChessBoardHandler.RandomLevel());

            Assert.AreEqual(chessBoard.Move(Direction.Right), false);
        }
Example #4
0
        public void TestLeftLineMoveLeftFail()
        {
            var chessBoard = new ChessBoard();

            chessBoard.AddNew(new Position(0, 0), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(1, 0), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(2, 0), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(3, 0), ChessBoardHandler.RandomLevel());

            Assert.AreEqual(chessBoard.Move(Direction.Left), false);
        }
Example #5
0
        public void TestToplLineMoveUpFail()
        {
            var chessBoard = new ChessBoard();

            chessBoard.AddNew(new Position(0, 0), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(0, 1), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(0, 2), ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(new Position(0, 3), ChessBoardHandler.RandomLevel());

            Assert.AreEqual(chessBoard.Move(Direction.Up), false);
        }
Example #6
0
        public void SingleRandomAddTheSamePlaceTest()
        {
            var chessBoard     = new ChessBoard();
            int randomRow      = RandomGenerator.Next(4);
            int randomCol      = RandomGenerator.Next(4);
            var randomPosition = new Position(randomRow, randomCol);

            chessBoard.AddNew(randomPosition, ChessBoardHandler.RandomLevel());
            chessBoard.AddNew(randomPosition, ChessBoardHandler.RandomLevel());

            Assert.AreEqual(chessBoard.EmptyCount, 15);
        }
Example #7
0
        public static void AddSymmetryToRightDiagonal(ChessBoard chessBoard)
        {
            var oneSideCount   = RandomGenerator.Next(1, 6);
            var levels         = RandomGenerator.GetDistinctInts(oneSideCount, 1, 16);
            var emptyPositions = chessBoard.GetEmptyPositions();
            int occupyCount    = Math.Min(oneSideCount, chessBoard.EmptyCount / 2);

            for (int i = 0; i < occupyCount; ++i)
            {
                var position = emptyPositions[i];
                chessBoard.AddNew(position, levels[i]);
                chessBoard.AddNew(new Position(3 - position.Col, 3 - position.Row), levels[i]);
            }
        }
Example #8
0
        public void TestLineMoveUpMergeOnce()
        {
            for (int col = 0; col < 4; ++col)
            {
                var chessBoard = new ChessBoard();
                int level      = ChessBoardHandler.RandomLevel();
                var rows       = RandomGenerator.GetDistinctInts(2, 4);

                chessBoard.AddNew(new Position(rows[0], col), level);
                chessBoard.AddNew(new Position(rows[1], col), level);

                Assert.AreEqual(chessBoard.Move(Direction.Up), true);
                Assert.AreEqual(chessBoard.EmptyCount, 15);
            }
        }
Example #9
0
        public void TestLineMoveUpSuccess()
        {
            for (int col = 0; col < 4; ++col)
            {
                var chessBoard = new ChessBoard();
                var levels     = RandomGenerator.GetDistinctInts(2, 1, 16);
                var rows       = RandomGenerator.GetDistinctInts(2, 1, 4);

                chessBoard.AddNew(new Position(rows[0], col), levels[0]);
                chessBoard.AddNew(new Position(rows[1], col), levels[1]);

                Assert.AreEqual(chessBoard.Move(Direction.Up), true);
                Assert.AreEqual(chessBoard.EmptyCount, 14);
            }
        }
Example #10
0
        public void TestLineMoveRightMergeOnce()
        {
            for (int row = 0; row < 4; ++row)
            {
                var chessBoard = new ChessBoard();
                int level      = ChessBoardHandler.RandomLevel();
                var cols       = RandomGenerator.GetDistinctInts(2, 4);

                chessBoard.AddNew(new Position(row, cols[0]), level);
                chessBoard.AddNew(new Position(row, cols[1]), level);

                Assert.AreEqual(chessBoard.Move(Direction.Right), true);
                Assert.AreEqual(chessBoard.EmptyCount, 15);
            }
        }
Example #11
0
        public void TestMoveRightOneDistinctLineFail()
        {
            for (int row = 0; row < 4; ++row)
            {
                var chessBoard = new ChessBoard();
                var levels     = ChessBoardHandler.GetRandomDistinctLevels(4);

                chessBoard.AddNew(new Position(row, 0), levels[0]);
                chessBoard.AddNew(new Position(row, 1), levels[1]);
                chessBoard.AddNew(new Position(row, 2), levels[2]);
                chessBoard.AddNew(new Position(row, 3), levels[3]);

                Assert.AreEqual(chessBoard.Move(Direction.Right), false);
            }
        }
Example #12
0
        public void TestDistinctLineMoveUpFail()
        {
            for (int col = 0; col < 4; ++col)
            {
                var chessBoard = new ChessBoard();
                var levels     = RandomGenerator.GetDistinctInts(4, 1, 16);

                chessBoard.AddNew(new Position(0, col), levels[0]);
                chessBoard.AddNew(new Position(1, col), levels[1]);
                chessBoard.AddNew(new Position(2, col), levels[2]);
                chessBoard.AddNew(new Position(3, col), levels[3]);

                Assert.AreEqual(chessBoard.Move(Direction.Up), false);
            }
        }
Example #13
0
        public void TestDistinctLineMoveDownFail()
        {
            for (int col = 0; col < 4; ++col)
            {
                var chessBoard = new ChessBoard();
                var levels     = ChessBoardHandler.GetRandomDistinctLevels(4);

                chessBoard.AddNew(new Position(0, col), levels[0]);
                chessBoard.AddNew(new Position(1, col), levels[1]);
                chessBoard.AddNew(new Position(2, col), levels[2]);
                chessBoard.AddNew(new Position(3, col), levels[3]);

                Assert.AreEqual(chessBoard.Move(Direction.Down), false);
            }
        }
Example #14
0
        public void TestLineMoveRightSuccess()
        {
            for (int row = 0; row < 4; ++row)
            {
                var chessBoard = new ChessBoard();
                var levels     = ChessBoardHandler.GetRandomDistinctLevels(2);
                var cols       = RandomGenerator.GetDistinctInts(2, 0, 3);

                chessBoard.AddNew(new Position(row, cols[0]), levels[0]);
                chessBoard.AddNew(new Position(row, cols[1]), levels[1]);

                Assert.AreEqual(chessBoard.Move(Direction.Right), true);
                Assert.AreEqual(chessBoard.EmptyCount, 14);
            }
        }
Example #15
0
 public void TransposeRightTest()
 {
     for (int i = 0; i < 100; ++i)
     {
         var chessBoard = new ChessBoard();
         ChessBoardHandler.AddRightDiagonal(chessBoard);
         chessBoard.AddNew(new Position(0, 0), 10);
         chessBoard.AddNew(new Position(3, 3), 10);
         Assert.AreEqual(chessBoard, chessBoard.ToTransposeRight());
         chessBoard.RandomAdd();
         Assert.AreNotEqual(chessBoard, chessBoard.ToTransposeRight());
         Assert.AreEqual(Evaluator.EvalForMove(chessBoard), Evaluator.EvalForMove(chessBoard.ToTransposeRight()), 0.01);
         Assert.AreEqual(Evaluator.EvalForAdd(chessBoard), Evaluator.EvalForAdd(chessBoard.ToTransposeRight()), 0.01);
     }
 }
Example #16
0
        public void TestLineMoveLeftMergeTwice()
        {
            for (int i = 0; i < 4; ++i)
            {
                var chessBoard = new ChessBoard();
                var levels     = ChessBoardHandler.GetRandomDistinctLevels(2);

                chessBoard.AddNew(new Position(i, 0), levels[0]);
                chessBoard.AddNew(new Position(i, 1), levels[0]);
                chessBoard.AddNew(new Position(i, 2), levels[1]);
                chessBoard.AddNew(new Position(i, 3), levels[1]);

                Assert.AreEqual(chessBoard.Move(Direction.Left), true);
                Assert.AreEqual(chessBoard.EmptyCount, 14);
            }
        }
Example #17
0
        public void TestLineMoveRightMergeTwice()
        {
            for (int row = 0; row < 4; ++row)
            {
                var chessBoard = new ChessBoard();
                var levels     = ChessBoardHandler.GetRandomDistinctLevels(2);

                chessBoard.AddNew(new Position(row, 0), levels[0]);
                chessBoard.AddNew(new Position(row, 1), levels[0]);
                chessBoard.AddNew(new Position(row, 2), levels[1]);
                chessBoard.AddNew(new Position(row, 3), levels[1]);

                Assert.AreEqual(chessBoard.Move(Direction.Right), true);
                Assert.AreEqual(chessBoard.EmptyCount, 14);
            }
        }
Example #18
0
        public void TestLineMoveUpMergeTwice()
        {
            for (int col = 0; col < 4; ++col)
            {
                var chessBoard = new ChessBoard();
                var levels     = ChessBoardHandler.GetRandomDistinctLevels(2);

                chessBoard.AddNew(new Position(0, col), levels[0]);
                chessBoard.AddNew(new Position(1, col), levels[0]);
                chessBoard.AddNew(new Position(2, col), levels[1]);
                chessBoard.AddNew(new Position(3, col), levels[1]);

                Assert.AreEqual(chessBoard.Move(Direction.Up), true);
                Assert.AreEqual(chessBoard.EmptyCount, 14);
            }
        }
Example #19
0
        private void ChooseAnnoyingChess(ChessBoard chessBoard)
        {
            this[AddLevels[0]] = new List <Position>();
            this[AddLevels[1]] = new List <Position>();
            var emptyPositions = chessBoard.GetEmptyPositions();

            foreach (int level in AddLevels)
            {
                foreach (var position in emptyPositions)
                {
                    chessBoard.AddNew(position, level);
                    double eval = Evaluator.EvalForAdd(chessBoard);
                    if (eval < minEval)
                    {
                        minEval = eval;
                        this[AddLevels[0]].Clear();
                        this[AddLevels[1]].Clear();
                    }

                    if (eval == minEval)
                    {
                        this[level].Add(position);
                    }

                    chessBoard.SetEmpty(position);
                }
            }
        }
Example #20
0
        public static void RandomAddLevelOne(ChessBoard chessBoard, int count)
        {
            var emptyPositions = chessBoard.GetEmptyPositions();
            int occupyCount    = Math.Min(count, chessBoard.EmptyCount);
            var levels         = RandomGenerator.GetDistinctInts(occupyCount, 1, 16);

            for (int i = 0; i < occupyCount; ++i)
            {
                chessBoard.AddNew(emptyPositions[i], levels[i]);
            }
        }
Example #21
0
        public double MoveStateEvaluation(ChessBoard newBoard, int depth)
        {
            if (depth == 0)
            {
                return(Evaluator.EvalForMove(newBoard));
            }
            double result         = Evaluator.EvalForMove(newBoard);
            var    emptyPositions = newBoard.GetEmptyPositions();

            foreach (int level in addLevels)
            {
                foreach (var position in emptyPositions)
                {
                    newBoard.AddNew(position, addLevels[0]);
                    result += AddStateEvaluation(newBoard, depth - 1) * (1 - levelTwoPossibility);

                    newBoard.AddNew(position, addLevels[1]);
                    result += AddStateEvaluation(newBoard, depth - 1) * levelTwoPossibility;
                    newBoard.SetEmpty(position);
                }
            }
            return(result);
        }
Example #22
0
        public void RandomAddNewSetEmptyTest()
        {
            var addEmptyBoard  = new ChessBoard();
            var emptyPositions = addEmptyBoard.GetEmptyPositions();
            int occupyCount    = RandomGenerator.Next(emptyPositions.Count);

            for (int i = 0; i < occupyCount; ++i)
            {
                addEmptyBoard.AddNew(emptyPositions[i], ChessBoardHandler.RandomLevel());
                addEmptyBoard.SetEmpty(emptyPositions[i]);
            }
            var emptyBoard = new ChessBoard();

            Assert.AreEqual(addEmptyBoard.EmptyCount, 16);
            Assert.AreEqual(addEmptyBoard, emptyBoard);
        }
Example #23
0
        public static void SingleDifferentAddSetEmptyTest()
        {
            var chessBoard     = new ChessBoard();
            var newBoard       = new ChessBoard(chessBoard);
            var emptyPositions = chessBoard.GetEmptyPositions();
            var emptyIndices   = RandomGenerator.GetDistinctInts(2, chessBoard.EmptyCount);

            chessBoard.AddNew(emptyPositions[emptyIndices[0]], 2);
            var addedBoard = new ChessBoard(chessBoard);

            chessBoard.SetEmpty(emptyPositions[emptyIndices[1]]);

            Assert.AreEqual(chessBoard.EmptyCount, 15);
            Assert.AreNotEqual(chessBoard, newBoard);
            Assert.AreEqual(addedBoard, chessBoard);
        }
Example #24
0
        public void AllAddNewSetEmptyTest()
        {
            var addEmptyBoard = new ChessBoard();
            var emptyBoard    = new ChessBoard();

            for (int row = 0; row < 4; ++row)
            {
                for (int col = 0; col < 4; ++col)
                {
                    addEmptyBoard.AddNew(new Position(row, col), 1);
                    addEmptyBoard.SetEmpty(new Position(row, col));

                    Assert.AreEqual(addEmptyBoard, emptyBoard);
                    Console.WriteLine("{0}\t{1}", row, col);
                }
            }
        }
Example #25
0
        public void TestDistinctCount()
        {
            var chessBoard = new ChessBoard();
            var levels     = ChessBoardHandler.GetRandomCountDistinctLevels();
            int levelIndex = 0;

            for (int row = 0; row < 4; ++row)
            {
                for (int col = 0; col < 4; ++col)
                {
                    if (levelIndex == levels.Length)
                    {
                        break;
                    }
                    chessBoard.AddNew(new Position(row, col), levels[levelIndex]);
                    ++levelIndex;
                }
            }
            Assert.AreEqual(chessBoard.DistinctCount, levels.Length);
        }
Example #26
0
        public double AddSearch(int depth, double alpha, double beta)
        {
            int    hashFlag = HashAlpha;
            double val;

            if ((val = ProbeHash(depth, alpha, beta, out var prevDecision)) != Infinity)
            {
                ++curOff;
                return(val);
            }
            if (depth == 0)
            {
                val = -Evaluator.EvalForMove(chessBoard);
                RecordHash(depth, val, HashExact, prevDecision);
                return(val);
            }
            var addPositions = SortedPositions(prevDecision);

            foreach (var position in addPositions)
            {
                foreach (int level in addLevels)
                {
                    chessBoard.AddNew(position, level);
                    val = -MoveSearch(depth - 1, -beta, -alpha);
                    chessBoard.SetEmpty(position);
                    if (val >= beta)
                    {
                        RecordHash(depth, beta, HashBeta, prevDecision);
                        return(beta);
                    }
                    if (val > alpha)
                    {
                        hashFlag = HashExact;
                        prevDecision.bestPosition = position;
                        alpha = val;
                    }
                }
            }
            RecordHash(depth, alpha, hashFlag, prevDecision);
            return(alpha);
        }
Example #27
0
        public ChessBoard GetTestBoard2()
        {
            var chessBoard = new ChessBoard();

            chessBoard.AddNew(new Position(0, 0), 0);
            chessBoard.AddNew(new Position(0, 1), 0);
            chessBoard.AddNew(new Position(0, 2), 0);
            chessBoard.AddNew(new Position(0, 3), 0);

            chessBoard.AddNew(new Position(1, 0), 2);
            chessBoard.AddNew(new Position(1, 1), 0);
            chessBoard.AddNew(new Position(1, 2), 1);
            chessBoard.AddNew(new Position(1, 3), 0);

            chessBoard.AddNew(new Position(2, 0), 3);
            chessBoard.AddNew(new Position(2, 1), 3);
            chessBoard.AddNew(new Position(2, 2), 2);
            chessBoard.AddNew(new Position(2, 3), 0);

            chessBoard.AddNew(new Position(3, 0), 11);
            chessBoard.AddNew(new Position(3, 1), 8);
            chessBoard.AddNew(new Position(3, 2), 3);
            chessBoard.AddNew(new Position(3, 3), 0);
            return(chessBoard);
        }