示例#1
0
 /// <summary>
 /// Fetches next pending spike.
 /// </summary>
 /// <returns>0/1 or throws exception if there is no pending spikes.</returns>
 public int FetchSpike()
 {
     if (_pendingFractions > 0)
     {
         int spike = Bitwise.IsBitSet(_buffer, (uint)(NumOfCodingFractions - _pendingFractions)) ? 1 : 0;
         --_pendingFractions;
         return(spike);
     }
     else
     {
         throw new Exception("No more spikes to be fetched.");
     }
 }
示例#2
0
    public static string ToString(ulong occupancy)
    {
        var stringBuilder = new StringBuilder();

        for (var rank = 7; rank >= 0; rank--)
        {
            for (var file = 0; file < 8; file++)
            {
                var square = Board.GetSquare(file, rank);
                stringBuilder.Append(Bitwise.IsBitSet(occupancy, square) ? " 1 " : " . ");
            }
            stringBuilder.AppendLine();
        }
        return(stringBuilder.ToString());
    }
示例#3
0
    public void TestKingRingMasks()
    {
        // King on e8.
        // Inner Ring
        var square = "e8";
        var mask   = Board.InnerRingMasks[(int)Board.GetSquare(square)];

        WriteMessageLine($"King on {square} inner ring mask = ");
        WriteMessageLine(Position.ToString(mask));
        Assert.That(Bitwise.IsBitSet(mask, Square.D8), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.D7), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.E7), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.F7), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.F8), Is.EqualTo(true));
        mask &= Bitwise.CreateULongUnmask(Square.D8) & Bitwise.CreateULongUnmask(Square.D7) & Bitwise.CreateULongUnmask(Square.E7) & Bitwise.CreateULongUnmask(Square.F7) & Bitwise.CreateULongUnmask(Square.F8);
        Assert.That(mask & Bitwise.CreateULongMask((int)Square.A8, (int)Square.H1), Is.EqualTo(0));
        // Outer Ring
        mask = Board.OuterRingMasks[(int)Board.GetSquare(square)];
        WriteMessageLine($"King on {square} outer ring mask = ");
        WriteMessageLine(Position.ToString(mask));
        Assert.That(Bitwise.IsBitSet(mask, Square.C8), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.C7), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.C6), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.D6), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.E6), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.F6), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.G6), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.G7), Is.EqualTo(true));
        Assert.That(Bitwise.IsBitSet(mask, Square.G8), Is.EqualTo(true));
        mask &= Bitwise.CreateULongUnmask(Square.C8) & Bitwise.CreateULongUnmask(Square.C7) & Bitwise.CreateULongUnmask(Square.C6) & Bitwise.CreateULongUnmask(Square.D6) & Bitwise.CreateULongUnmask(Square.E6) &
                Bitwise.CreateULongUnmask(Square.F6) & Bitwise.CreateULongUnmask(Square.G6) & Bitwise.CreateULongUnmask(Square.G7) & Bitwise.CreateULongUnmask(Square.G8);
        Assert.That(mask & Bitwise.CreateULongMask((int)Square.A8, (int)Square.H1), Is.EqualTo(0));
        // King on c3.
        square = "c3";
        mask   = Board.InnerRingMasks[(int)Board.GetSquare(square)];
        WriteMessageLine($"King on {square} inner ring mask = ");
        WriteMessageLine(Position.ToString(mask));
        mask = Board.OuterRingMasks[(int)Board.GetSquare(square)];
        WriteMessageLine($"King on {square} outer ring mask = ");
        WriteMessageLine(Position.ToString(mask));
    }
示例#4
0
        private List <long> ApplyAddressMask(long address)
        {
            // apply a mask, to the floating array where each bit represents an entry and whether it is active
            // then check all bits and set the address bit at the index held in floating to whether the bit in the mask that belongs to that index is set.
            List <long> addresses = new List <long>();

            address |= force1;
            //maskValues counts upwards to get all combinations of floating numbers (Mask XX becomes 00 to 11)
            long floatCountMask = Bitwise.GetBitMask(floating.Count);

            for (int maskValues = 0; maskValues <= floatCountMask; maskValues++)
            {
                long newAddress = address;
                //check each bit in the current floating mask and set the bit in the adress that is referenced in the position this bit belongs to
                for (int i = 0; i < floating.Count; i++)
                {
                    newAddress = Bitwise.SetBit(newAddress, floating[i], Bitwise.IsBitSet(maskValues, i));
                }
                addresses.Add(newAddress);
            }
            return(addresses);
        }
示例#5
0
        private List <Node> TraceRecursive(Node start, long collectedKeys, out double newCost, int depth = 0, string pathText = "")
        {
            long dictKey = Bitwise.SetBit(collectedKeys, start.GetKeyBitPos() + 27, true);

            if (knownPaths.ContainsKey(dictKey))
            {
                newCost = knownPaths[dictKey].Item1;
                return(knownPaths[dictKey].Item2.ToList());
            }

            var reachable = GetReachableNodes(pois);
            var keys      = reachable.Where(x => x.Key != '\0' && !Bitwise.IsBitSet(collectedKeys, x.GetKeyBitPos())).ToList();

            newCost = 0;
            if (keys.Count == 0)
            {
                long mask = Bitwise.GetBitMask(this.keys.Count);
                if ((collectedKeys & mask) == mask)
                {
                    return(new List <Node>());
                }
                else
                {
                    return(null);
                }
            }

            double      selCost   = 0;
            List <Node> selection = null;
            Node        selected  = null;

            for (int i = 0; i < keys.Count; i++)
            {
                var key = keys[i];
                collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), true);
                double      currCost;
                List <Node> currSel;
                var         unlocked = new List <Node>();

                //Console.SetCursorPosition(0, maxHeight * (printouts) + singleHeight);
                //Console.Write(outAssis.GetNextProgressChar() + depth.ToString() + " ");
                //Console.CursorLeft = 10;
                //Console.Write((pathText + key.Key).PadRight(20));
                Console.Write(key.Key);

                foreach (Node door in doors)
                {
                    if (door.TryUnlock(key.Key))
                    {
                        unlocked.Add(door);
                    }
                }
                var path = pathfind.GetPath(start, key, out List <BaseNodeConnection> cons, out double partCost).Select(x => (Node)x).ToList();
                if (path.Last() == start)
                {
                    continue;
                }
                var newKey = path.FirstOrDefault(x => x.Key != '\0' && x != key && x != start && !Bitwise.IsBitSet(collectedKeys, x.GetKeyBitPos()));
                if (newKey != null)
                {
                    collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), false);
                    key           = newKey;
                    path          = pathfind.GetPath(start, key, out cons, out partCost).Select(x => (Node)x).ToList();
                    if (path.Last() == start)
                    {
                        continue;
                    }
                    collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), true);
                }


                //PrintConnections(connections, maxHeight * printouts + singleHeight, false);
                //PrintConnections(cons, maxHeight * printouts + singleHeight, true);
                //Console.CursorLeft = key.X;
                //Console.CursorTop = key.Y + maxHeight * printouts + singleHeight;
                //Console.Write("*");

                currSel = TraceRecursive(key, collectedKeys, out currCost, depth + 1, pathText + key.Key);
                Console.CursorLeft--;
                Console.Write(" ");
                Console.CursorLeft--;


                if (currSel != null && currCost + partCost < selCost || selCost == 0)
                {
                    selection = currSel;
                    selCost   = currCost + partCost;
                    selected  = key;
                }
                collectedKeys = Bitwise.SetBit(collectedKeys, key.GetKeyBitPos(), false);
                foreach (Node door in unlocked)
                {
                    door.IsUnlocked = false;
                }
            }
            if (selection != null && selected != null)
            {
                selection.Insert(0, selected);
            }
            newCost = selCost;

            //dictKey = Bitwise.SetBit(collectedKeys, selected.GetKeyBitPos() + 27, true);
            if (!knownPaths.ContainsKey(dictKey))
            {
                knownPaths.Add(dictKey, new Tuple <double, List <Node> >(newCost, selection?.ToList()));
            }
            return(selection);
        }