/// <summary>
        /// Set the possible positions that a block can start in
        /// </summary>
        private void setPossiblePositions(ref BlockStartPosition[] bucket)
        {
            if (loadedPositions == null || loadedPositions.Count == 0)
            {
                setKnownPossiblePositions(ref bucket);
                setKnownPossiblePositions(ref bucket);
            }
            else
            {
                int count = 0;

                foreach (String[] block in loadedPositions)
                {
                    bucket[count]          = new BlockStartPosition();
                    bucket[count].position = new Boolean[block.Length, block.Length];
                    // load the positions
                    for (var row = 0; row < block.Length; row++)
                    {
                        char[] rowArr = block[row].ToCharArray();
                        for (var col = 0; col < block[row].Length; col++)
                        {
                            bucket[count].position[row, col] = rowArr[col] == BlockLoader.solidIndicator;
                        }
                    }
                    // give it a random color. Pretty ^_^
                    KnownColor[] names           = (KnownColor[])Enum.GetValues(typeof(KnownColor));
                    KnownColor   randomColorName = names[randGen.Next(names.Length)];
                    bucket[count].color = Color.FromKnownColor(randomColorName);

                    count++;
                }
            }

            shuffle(ref bucket);
        }
        /// <summary>
        /// A method of tracking which blocks have been spawned recently, creating a bucket which
        /// prevents long strings of the same block.
        /// </summary>
        public BlockPositionSpawner()
        {
            currentBucket = new BlockStartPosition[7];
            nextBucket    = new BlockStartPosition[7];

            setPossiblePositions(ref currentBucket);
            setPossiblePositions(ref nextBucket);
        }
Example #3
0
        /// <summary>
        /// A block (tetromino) that falls down the grid
        /// </summary>
        /// <param name="positionSpawner">The position spawner to determine which block it should be</param>
        public Block(BlockPositionSpawner positionSpawner)
        {
            // decide which block it is
            Random             random   = new Random();
            BlockStartPosition startPos = positionSpawner.Next();

            squares = startPos.position;
            color   = startPos.color;

            // set the initial position
            x = 3;
            y = 0;
        }
Example #4
0
        /// <summary>
        /// Constructor
        /// </summary>
        public Block()
        {
            // decide which block it is
            Random             random   = new Random();
            BlockStartPosition startPos = new BlockStartPosition();

            startPos = new PossibleBlockStartPositions().positions[random.Next(7)];
            squares  = startPos.position;
            color    = startPos.color;

            // set the initial position
            x = 3;
            y = 0;
        }
        /// <summary>
        /// Returns the position of the next block to play with
        /// </summary>
        /// <returns>The position block to play with</returns>
        public BlockStartPosition Next()
        {
            BlockStartPosition blockToReturn = currentBucket[nextBlock];

            currentBucket[nextBlock] = nextBucket[nextBlock];
            nextBlock++;

            if (nextBlock >= currentBucket.Length)
            {
                nextBlock = 0;
                setPossiblePositions(ref nextBucket);
            }

            return(blockToReturn);
        }
        /// <summary>
        /// Create a block position spawner where the block positions are loaded from a file
        /// </summary>
        /// <param name="name">The name of the file to load</param>
        public BlockPositionSpawner(String name)
        {
            loadedPositions = BlockLoader.load(name);

            if (loadedPositions != null && loadedPositions.Count != 0)
            {
                currentBucket = new BlockStartPosition[loadedPositions.Count];
                nextBucket    = new BlockStartPosition[loadedPositions.Count];
            }
            else
            {
                currentBucket = new BlockStartPosition[7];
                nextBucket    = new BlockStartPosition[7];
            }

            setPossiblePositions(ref currentBucket);
            setPossiblePositions(ref nextBucket);
        }
 /// <summary>
 /// Specify the possible positions that a block can start in
 /// </summary>
 private void setPossiblePositions()
 {
     // straight across in a line
     positions[0]          = new BlockStartPosition();
     positions[0].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, false, false },
         { true, true, true, true },
         { false, false, false, false }
     };
     positions[0].color = Color.Cyan;
     // L with a spike on the left
     positions[1]          = new BlockStartPosition();
     positions[1].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, true, false, false },
         { false, true, true, true },
         { false, false, false, false }
     };
     positions[1].color = Color.Blue;
     // L with a spike on the right
     positions[2]          = new BlockStartPosition();
     positions[2].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, false, true },
         { false, true, true, true },
         { false, false, false, false }
     };
     positions[2].color = Color.Orange;
     // square
     positions[3]          = new BlockStartPosition();
     positions[3].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, true, true, false },
         { false, true, true, false },
         { false, false, false, false }
     };
     positions[3].color = Color.Yellow;
     // zig-zag up to the right
     positions[4]          = new BlockStartPosition();
     positions[4].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, true, true },
         { false, true, true, false },
         { false, false, false, false }
     };
     positions[4].color = Color.Red;
     // zig-zag up to the left
     positions[5]          = new BlockStartPosition();
     positions[5].position = new Boolean[, ]
     {
         { false, false, false, false },
         { true, true, false, false },
         { false, true, true, false },
         { false, false, false, false }
     };
     positions[5].color = Color.Green;
     // T shape
     positions[6]          = new BlockStartPosition();
     positions[6].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, true, false },
         { false, true, true, true },
         { false, false, false, false }
     };
     positions[6].color = Color.Purple;
 }
 /// <summary>
 /// Specify the possible positions that a block can start in if nothing has loaded from a file
 /// </summary>
 private void setKnownPossiblePositions(ref BlockStartPosition[] bucket)
 {
     // straight across in a line
     bucket[0]          = new BlockStartPosition();
     bucket[0].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, false, false },
         { true, true, true, true },
         { false, false, false, false }
     };
     bucket[0].color = Color.Cyan;
     // L with a spike on the left
     bucket[1]          = new BlockStartPosition();
     bucket[1].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, true, false, false },
         { false, true, true, true },
         { false, false, false, false }
     };
     bucket[1].color = Color.Blue;
     // L with a spike on the right
     bucket[2]          = new BlockStartPosition();
     bucket[2].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, false, true },
         { false, true, true, true },
         { false, false, false, false }
     };
     bucket[2].color = Color.Orange;
     // square
     bucket[3]          = new BlockStartPosition();
     bucket[3].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, true, true, false },
         { false, true, true, false },
         { false, false, false, false }
     };
     bucket[3].color = Color.Yellow;
     // zig-zag up to the right
     bucket[4]          = new BlockStartPosition();
     bucket[4].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, true, true },
         { false, true, true, false },
         { false, false, false, false }
     };
     bucket[4].color = Color.Red;
     // zig-zag up to the left
     bucket[5]          = new BlockStartPosition();
     bucket[5].position = new Boolean[, ]
     {
         { false, false, false, false },
         { true, true, false, false },
         { false, true, true, false },
         { false, false, false, false }
     };
     bucket[5].color = Color.Green;
     // T shape
     bucket[6]          = new BlockStartPosition();
     bucket[6].position = new Boolean[, ]
     {
         { false, false, false, false },
         { false, false, true, false },
         { false, true, true, true },
         { false, false, false, false }
     };
     bucket[6].color = Color.Purple;
 }