示例#1
0
        public GameState GenerateNewNumber()
        {
            FreeSpace.Clear();
            GameState OKGameStateReturn = GameState.Continue;

            // fill with nulls
            for (int i = 0; i < Cols; i++)
            {
                for (int j = 0; j < Rows; j++)
                {
                    if (GameBoard[i, j] == 0)
                    {
                        FreeSpace.Add(new int[2] {
                            i, j
                        });
                    }
                    if (GameBoard[i, j] == 2048)
                    {
                        if (!ContinueGameMode)
                        {
                            OKGameStateReturn = GameState.Won;
                        }
                    }
                }
            }

            if (FreeSpace.Count > 0)
            {
                // 80% chance to generate one number, 20% chance to generate two numbers
                int repeatNewNumber = 1;
                if (RNG.Next(1, 6) == 5)
                {
                    repeatNewNumber = 2;
                }

                for (int k = 1; k <= repeatNewNumber; k++)
                {
                    // 80% chance to 2, 20% chance to 4
                    int numberValue = 2;
                    if (RNG.Next(1, 6) == 5)
                    {
                        numberValue = 4;
                    }

                    // select random free spot
                    int[] selectedSpot = FreeSpace[RNG.Next(0, FreeSpace.Count)];
                    // place the number to the spot
                    GameBoard[selectedSpot[0], selectedSpot[1]] = numberValue;
                }

                return(OKGameStateReturn);
            }
            else
            {
                return(GameState.Lost);
            }
        }
示例#2
0
        /// <summary>
        /// Resize the heap encreasing the size by the specified value mantaining all data
        /// in the same position.
        /// </summary>
        /// <param name="size">Grow size in bytes (the actual grow size could be bigger than the specified).</param>
        /// <param name="noOvergrowth">Force the allocator to grow the heap by the exact specified size.</param>
        /// <returns>Returns <see cref="true"/> if the memory allocation has been done.</returns>
        public virtual bool Grow(uint size, bool noOvergrowth = false)
        {
            // - Check if the heap can grow
            if (!CanGrow)
            {
                return(false);
            }

            // - Check if we reached the memory limit
            if (HeapSize + size > MaximumSize)
            {
                return(false);
            }

            // - Calculates the actual grow size
            uint growSize = noOvergrowth ? size : (uint)(size + GrowFactor * HeapSize);

            try
            {
                // - Realloc the heap memory
                RawMemory = Marshal.ReAllocHGlobal(RawMemory, new IntPtr(HeapSize + growSize));
            }
            catch (OutOfMemoryException)
            {
                return(false);
            }

            // - Gets the last avaiable chunk of memory
            var lastChunk = FreeSpace.LastOrDefault();

            // - Create a new memory pointer pointing to the last block if there's no free block at the end
            if (lastChunk == null || lastChunk.Offset + lastChunk.Size < HeapSize)
            {
                FreeSpace.Add(new MemoryPointer(this, growSize, HeapSize));
            }

            // - Modify the existing chunk if it's at the end of the heap
            if (lastChunk != null || lastChunk.Offset + lastChunk.Size >= HeapSize)
            {
                lastChunk.ChangeOffsetAndSize(lastChunk.Offset, lastChunk.Size + growSize);
            }

            // - Update the heap size
            HeapSize += growSize;

            return(true);
        }