Exemplo n.º 1
0
        public void testHash()
        {
            var hash = new Hash(Board.Piece.X);

            // First, check that all 9 indicies can be used (and also test isEmpty)
            foreach (int i in Enumerable.Range(0, (int)Board.pieceCount))
            {
                Assert.IsTrue(hash.isEmpty(i));
            }

            // Test setPiece and getPiece
            hash.setPiece(Board.Piece.Empty, 0);
            hash.setPiece(Board.Piece.O, 1);
            hash.setPiece(Board.Piece.X, 2);
            Assert.AreEqual(hash.getPiece(0), Board.Piece.Empty);
            Assert.AreEqual(hash.getPiece(1), Board.Piece.O);
            Assert.AreEqual(hash.getPiece(2), Board.Piece.X);

            // Test setPiece exception
            try
            {
                hash.setPiece(Board.Piece.X, 0, true);  // Set the 0th piece to x
                hash.setPiece(Board.Piece.O, 0, false); // Then try to overwrite it, with the overwrite flag to set to false. Triggering the exception.
                Assert.Fail("The exception for setPiece wasn't thrown.");
            }
            catch (HashException) { }

            // Test the enforceIndex exception
            try
            {
                hash.setPiece(Board.Piece.Empty, (int)Board.pieceCount);
                Assert.Fail("The exception for enforceIndex wasn't thrown.");
            }
            catch (ArgumentOutOfRangeException) { }

            // Test isMyPiece
            hash.setPiece(hash.myPiece, 0, true);
            Assert.IsTrue(hash.isMyPiece(0));

            // Test ToString
            hash.setPiece(hash.myPiece, 0, true);
            hash.setPiece(Board.Piece.Empty, 1, true);
            hash.setPiece(hash.otherPiece, 2, true);
            Assert.AreEqual(hash.ToString(), $"{Hash.myChar}.{Hash.otherChar}......");

            // Test second constructor of Hash
            hash = new Hash(Board.Piece.X, "MO.OM.MOM");

            var mineIndicies  = new int[] { 0, 4, 6, 8 };
            var otherIndicies = new int[] { 1, 3, 7 };
            var emptyIndicies = new int[] { 2, 5 };

            Array.ForEach(mineIndicies, i => { Assert.IsTrue(hash.isMyPiece(i)); });
            Array.ForEach(otherIndicies, i => { Assert.IsTrue(!hash.isMyPiece(i)); });
            Array.ForEach(emptyIndicies, i => { Assert.IsTrue(hash.isEmpty(i)); });

            // Test Clone and Equals
            Assert.IsTrue(hash.Clone().Equals(hash));
        }
Exemplo n.º 2
0
        public void walkTest()
        {
            var m = Hash.myChar;
            var o = Hash.otherChar;
            var p = Board.Piece.X;

            var root = Node.root;

            root.children.AddRange(new Node[]
            {
                new Node(new Hash(p, $"{m}........"), 0),
                new Node(new Hash(p, $".{o}......."), 1)
            });
            root.children[0].children.Add(new Node(new Hash(p, $"{m}.{o}......"), 2));

            // Visualisation of what 'tree' looks like

            /*                               /----[0]----/ "M.O......"
             *      /----[0]----/ "M........"
             * root
             *      /----[1]----/ ".O......."
             * */

            // This action is used with Node.walk, it will keep track of the last node it visited.
            Node          last    = null;
            Action <Node> getLast = (node => last = node);

            // First, seeing if it returns false on an invalid path.
            // "M........" -> "MM......."
            var path = new Hash[] { new Hash(p, $"{m}........"),
                                    new Hash(p, $"{m}{m}.......") };

            Assert.IsFalse(root.walk(path, getLast)); // "walk" returns false if the entire path couldn't be followed.
            Assert.IsTrue(last == root.children[0]);  // Confirm that the only node we walked to was "M........"

            // Then see if depth works
            Assert.IsTrue(root.walk(path, getLast, 1)); // It will return true now, since we walked 'depth' amount of nodes sucessfully
            Assert.IsTrue(last == root.children[0]);

            // Then finally see if it walks through an entire path properly
            // We have to change the last hash in path first though
            path[1] = new Hash(p, $"{m}.{o}......");             // "M........" -> "M.O......"

            Assert.IsTrue(root.walk(path, getLast));             // If this returns true, then the entire path was walked
            Assert.IsTrue(last == root.children[0].children[0]); // THen we make sure the last node walked to is correct.
        }
Exemplo n.º 3
0
        public void hashSerialiseTest()
        {
            var dir  = "Temp";
            var file = "Serialised_Hash.bin";
            var path = $"{dir}/{file}";

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            using (var stream = File.Create(path))
            {
                using (var writer = new BinaryWriter(stream))
                {
                    var hash  = new Hash(Board.Piece.X, "MO.OM.MOM");
                    var hash2 = new Hash(Board.Piece.O, "OM.MO.OMO");

                    hash.serialise(writer);
                    hash2.serialise(writer);
                }
            }

            using (var stream = File.OpenRead(path))
            {
                using (var reader = new BinaryReader(stream))
                {
                    var hash = new Hash();

                    hash.deserialise(reader, GameFiles.treeFileVersion);
                    Assert.AreEqual("MO.OM.MOM", hash.ToString());
                    Assert.AreEqual(Board.Piece.X, hash.myPiece);
                    Assert.AreEqual(Board.Piece.O, hash.otherPiece);

                    hash.deserialise(reader, GameFiles.treeFileVersion);
                    Assert.AreEqual("OM.MO.OMO", hash.ToString());
                    Assert.AreEqual(Board.Piece.O, hash.myPiece);
                    Assert.AreEqual(Board.Piece.X, hash.otherPiece);
                }
            }
        }