Exemplo n.º 1
0
        private int CheckNeighbours(int x, int y)
        {
            var neighbours     = GetNeighbours(x, y);
            int neighbourCount = 0;

            foreach (var neighbour in neighbours)
            {
                if (CurrentGenCells.Contains(new Tuple <int, int>(neighbour.Item1, neighbour.Item2)))
                {
                    neighbourCount++;
                }
                else
                {
                    var currentNeighbour = new Tuple <int, int>(neighbour.Item1, neighbour.Item2);
                    int currentNeighbourCount;
                    if (PotentialCells.TryGetValue(currentNeighbour,
                                                   out currentNeighbourCount))
                    {
                        PotentialCells[currentNeighbour] += 1;
                    }
                    else
                    {
                        PotentialCells.Add(currentNeighbour, 1);
                    }
                }
            }
            return(neighbourCount);
        }
Exemplo n.º 2
0
        public HashSet <Tuple <int, int> > PopulateNextGen(bool shouldNotify = true)
        {
            PotentialCells.Clear();
            NextGenCells.Clear();

            foreach (var cell in CurrentGenCells)
            {
                if (Rules.ShouldLive(CheckNeighbours(cell.Item1, cell.Item2)))
                {
                    NextGenCells.Add(new Tuple <int, int>(cell.Item1, cell.Item2));
                }
            }

            var results = PotentialCells.Where(p => Rules.ShouldZombiefy(p.Value)).ToHashSet();

            foreach (var result in results)
            {
                NextGenCells.Add(new Tuple <int, int>(result.Key.Item1, result.Key.Item2));
            }

            if (shouldNotify)
            {
                GlobalHost.ConnectionManager.GetHubContext <GameHub>().Clients.All.nextUniverseStep(HsToList(CurrentGenCells), Generation);
            }

            CurrentGenCells = NextGenCells;
            NextGenCells    = new HashSet <Tuple <int, int> >();

            return(CurrentGenCells);
        }
Exemplo n.º 3
0
        private int FindNakedPair(int begin, out int bitMask)
        {
            bitMask = 0;

            int i;

            for (i = begin; i < N;)
            {
                PotentialCells pc = mPotentialCells[i];

                if (pc.cellCount != 2)
                {
                    ++i;
                    continue;
                }

                int end = i + pc.cellCount;

                if (end > N)
                {
                    break;
                }

                int j;
                for (j = i + 1; j < end; ++j)
                {
                    if (mPotentialCells[j].cellMask != pc.cellMask)
                    {
                        break;
                    }
                }

                if (j < end)
                {
                    i = j;
                    continue;
                }

                for (j = i; j < end; ++j)
                {
                    bitMask |= (1 << mPotentialCells[j].number);
                }

                bitMask = ~bitMask;

                return(i);
            }

            return(N);
        }
Exemplo n.º 4
0
            public int CompareTo(object otherObj)
            {
                PotentialCells other = (PotentialCells)otherObj;

                if (cellMask < other.cellMask)
                {
                    return(-1);
                }
                if (cellMask > other.cellMask)
                {
                    return(1);
                }

                return(0);
            }
Exemplo n.º 5
0
        private void GetPotentialCells(int pattern)
        {
            for (int i = 0; i < N; ++i)
            {
                PotentialCells pc = mPotentialCells[i];
                pc.number    = i;
                pc.cellMask  = 0;
                pc.cellCount = 0;
            }

            for (int cell = 0; cell < N; ++cell)
            {
                int row = mPatterns[pattern, cell].row;
                int col = mPatterns[pattern, cell].col;
                UpdatePotentialCells(mCells[row, col], cell);
            }
        }
Exemplo n.º 6
0
        // cellNumber can be row, col or block index
        // Transpose bits (possible values in cell)
        // to cellMask (possible cells with value)
        private void UpdatePotentialCells(int bits, int cellNumber)
        {
            if ((bits & CONFIRMED) != 0)
            {
                return;
            }

            for (int number = 0, bit = 1; number < N; ++number, bit <<= 1)
            {
                if ((bits & bit) != 0)
                {
                    PotentialCells pc = mPotentialCells[number];

                    pc.cellMask |= (1 << cellNumber);
                    ++pc.cellCount;
                }
            }
        }
Exemplo n.º 7
0
        public Board()
        {
            InitializeComponent();
            int i;

            for (i = 0; i < N; ++i)
            {
                mPotentialCells[i] = new PotentialCells();
            }

            // Build LUT for each type of scan: row, col, block
            i = 0;

            // By Row
            for (int row = 0; row < N; ++row)
            {
                mPatternLabels[i] = "row " + row;
                for (int col = 0; col < N; ++col)
                {
                    mPatterns[i, col].row = row;
                    mPatterns[i, col].col = col;
                }
                ++i;
            }

            // By Column
            for (int col = 0; col < N; ++col)
            {
                mPatternLabels[i] = "col " + col;
                for (int row = 0; row < N; ++row)
                {
                    mPatterns[i, row].row = row;
                    mPatterns[i, row].col = col;
                }
                ++i;
            }

            // By Block
            for (int br = 0; br < B; ++br)
            {
                int rowBegin = br * B;
                int rowEnd   = rowBegin + B;

                for (int bc = 0; bc < B; ++bc)
                {
                    int colBegin = bc * B;
                    int colEnd   = colBegin + B;
                    int j        = 0;

                    mPatternLabels[i] = "block " + br + "," + bc;

                    for (int row = rowBegin; row < rowEnd; ++row)
                    {
                        for (int col = colBegin; col < colEnd; ++col)
                        {
                            mPatterns[i, j].row = row;
                            mPatterns[i, j].col = col;
                            ++j;
                        }
                    }

                    ++i;
                }
            }
        }
Exemplo n.º 8
0
        private bool FindNakedTriples()
        {
            for (int pattern = 0; pattern < NUM_PATTERNS; ++pattern)
            {
                GetPotentialCells(pattern);

                for (int i = 0; i < (N - 2); ++i)
                {
                    ref PotentialCells iCell = ref mPotentialCells[i];

                    if ((iCell.cellCount < 2) || (iCell.cellCount > 3))
                    {
                        continue;
                    }

                    for (int j = i + 1; j < (N - 1); ++j)
                    {
                        ref PotentialCells jCell = ref mPotentialCells[j];

                        if ((jCell.cellCount < 2) || (jCell.cellCount > 3))
                        {
                            continue;
                        }

                        for (int k = j + 1; k < N; ++k)
                        {
                            ref PotentialCells kCell = ref mPotentialCells[k];

                            if ((kCell.cellCount < 2) || (kCell.cellCount > 3))
                            {
                                continue;
                            }

                            int cellMask = iCell.cellMask | jCell.cellMask | kCell.cellMask;

                            if (BitCount(cellMask) != 3)
                            {
                                continue;
                            }

                            // Confirmed a naked triple; but is it new?

                            int bitMask = ~((1 << iCell.number) | (1 << jCell.number) | (1 << kCell.number));

                            bool excluded = false;
                            int  bit      = 1;

                            // Do any of the cells referenced in the cellMask have bits not in the bitMask?
                            for (int cell = 0; (cell < N) && (cellMask != 0); ++cell, bit <<= 1)
                            {
                                if ((cellMask & bit) != 0)
                                {
                                    cellMask &= ~bit;
                                }
                                else
                                {
                                    continue;
                                }

                                int row = mPatterns[pattern, cell].row;
                                int col = mPatterns[pattern, cell].col;

                                Debug.Assert((mCells[row, col] & CONFIRMED) == 0);

                                if ((mCells[row, col] & bitMask) != 0)
                                {
                                    mCells[row, col] &= ~bitMask;
                                    excluded          = true;
                                }
                            }

                            if (excluded)
                            {
                                Debug.Print("Naked triple " + GetGroupName(~bitMask) + " in " + mPatternLabels[pattern]);
                                return(true);
                            }
                        }
                    }