Exemplo n.º 1
0
        // The waitingPeriod is used to update the aliens for n extra turns, as if you were not going to shoot this round but rather wait n turns and want to see if it would be successful
        private Tuple<List<Wave[]>, int> PredictAlienPositions(List<Wave[]> waves, int roundsToCalculate)
        {
            waves = new List<Wave[]>(waves);

            Alienmanager manager = new Alienmanager()
            {
                DeltaX = this.otherGuy.AlienManager.DeltaX,
                Disabled = this.otherGuy.AlienManager.Disabled,
                PlayerNumber = this.otherGuy.PlayerNumber,
                ShotEnergy = this.otherGuy.AlienManager.ShotEnergy,
                ShotEnergyCost = this.otherGuy.AlienManager.ShotEnergyCost,
                Waves = CharonBot.WaveArrayDeepCopy(this.otherGuy.AlienManager.Waves)
            };



            for (int x = 0; x < roundsToCalculate; x++) // Count through the moves that it would take to hit the alien
            {
                    // The offset takes into account the  fact that the aliens are in a different row (due to moving down) and would be in a different horizontal position.
                    // We check at the last predicted round in what position is the alien? But due to moving down we must check one row before..
                    // Instead, look at the last row, but take into account the fact that we should check one row before (so basically just roll back the alien by one move per move down)

                    bool topRowMySideContainsAlien = this.gameMap.Rows[this.mapMiddleIndex].Any(r => r != null && r.Type.Equals("Alien"));
                    bool secondTopRowMySideContainsAlien = this.gameMap.Rows[this.mapMiddleIndex + 1].Any(r => r != null && r.Type.Equals("Alien"));

                    Wave[] possibleLefts = new Wave[waves.Count];
                    Wave[] possibleRights = new Wave[waves.Count];

                    for (int i = 0; i < waves.Count; i++)
                    {
                        possibleLefts[i] = waves[i].Aggregate((w1, w2) => w1.X < w2.X ? w1 : w2);
                    }

                    for (int i = 0; i < waves.Count; i++)
                    {
                        possibleRights[i] = waves[i].Aggregate((w1, w2) => w1.X > w2.X ? w1 : w2);
                    }

                    Wave leftMost = possibleLefts.Aggregate((w1, w2) => w1.X < w2.X ? w1 : w2);
                    Wave rightMost = possibleRights.Aggregate((w1, w2) => w1.X > w2.X ? w1 : w2);

                    Row atLeft = leftMost.X > 0 && leftMost.Y < this.gameMap.Height ? this.gameMap.Rows[leftMost.Y][leftMost.X - 1] : null;
                    Row atRight = rightMost.X < this.gameMap.Width - 1 && rightMost.Y < this.gameMap.Height ? this.gameMap.Rows[rightMost.Y][rightMost.X + 1] : null;

                    // Aliens moving right
                    if (manager.DeltaX > 0) 
                    {
                        // We spawn aliens after we have moved down, and when we are about to move in a new direction
                        if (atLeft != null && atLeft.Type.Equals("Wall") && !topRowMySideContainsAlien && !secondTopRowMySideContainsAlien)
                        {
                            waves = this.SpawnAliens(waves, true);
                        }

                        if (atRight != null && atRight.Type.Equals("Wall"))
                        {
                            foreach (Wave[] alienWave in waves)
                            {
                                foreach (Wave alien in alienWave)
                                {
                                    alien.Y++;
                                }
                            }

                            roundsToCalculate--;
                            manager.DeltaX = -1;
                        }
                        else
                        {
                            foreach (Wave[] alienWave in waves)
                            {
                                foreach (Wave alien in alienWave)
                                {
                                    alien.X++;
                                }
                            }
                        }
                    }
                    else // Aliens moving left
                    {
                        if (atRight != null && atRight.Type.Equals("Wall") && !topRowMySideContainsAlien && !secondTopRowMySideContainsAlien)
                        {
                            waves = this.SpawnAliens(waves, false);
                        }

                        if (atLeft != null && atLeft.Type.Equals("Wall"))
                        {
                            foreach (Wave[] alienWave in waves)
                            {
                                foreach (Wave alien in alienWave)
                                {
                                    alien.Y++;
                                }
                            }

                            roundsToCalculate--;
                            manager.DeltaX = 1;
                        }
                        else
                        {
                            foreach (Wave[] alienWave in waves)
                            {
                                foreach (Wave alien in alienWave)
                                {
                                    alien.X--;
                                }
                            }
                        }
                    }
            }

            return new Tuple<List<Wave[]>, int>(new List<Wave[]>(waves), manager.DeltaX);
        }
Exemplo n.º 2
0
        private List<Wave[]> SpawnAliens(List<Wave[]> alienWaves, bool isAtLeft)
        {
            Wave[] highestWave = alienWaves.Aggregate((w1, w2) => w1[0].Y < w2[0].Y ? w1 : w2);

            int newAliensCount = 3;

            if (otherGuy.AlienFactory != null)
            {
                newAliensCount++;
            }

            if (this.round > 40)
            {
                newAliensCount++;
            }

            Wave[] newWave = new Wave[newAliensCount];

            int xPosition = isAtLeft ? 2 : this.gameMap.Width - 3;

            for (int i = 0; i < newAliensCount; i++)
            {
                newWave[i] = new Wave()
                {
                    Alive = true,
                    Height = 1,
                    Id = 123,
                    PlayerNumber = this.otherGuy.PlayerNumber,
                    Type = highestWave[0].Type,
                    Width = 1,
                    X = xPosition,
                    Y = this.mapMiddleIndex + 1
                };

                gameMap.Rows[newWave[i].Y][newWave[i].X] = new Row()
                {
                    Alive = true,
                    Height = 1,
                    Id = 123,
                    PlayerNumber = this.otherGuy.PlayerNumber,
                    Type = highestWave[0].Type,
                    Width = 1,
                    X = xPosition,
                    Y = this.mapMiddleIndex + 1,
                };

                xPosition = isAtLeft ? xPosition + 3 : xPosition - 3;
            }

            alienWaves.Add(newWave);
            return alienWaves;
        }
Exemplo n.º 3
0
 public static Wave[][] WaveArrayDeepCopy(Wave[][] waveToCopy)
 {
     using (MemoryStream memoryStream = new MemoryStream())
     {
         BinaryFormatter binaryFormatter = new BinaryFormatter();
         binaryFormatter.Serialize(memoryStream, waveToCopy);
         memoryStream.Seek(0, SeekOrigin.Begin);
         return (Wave[][])binaryFormatter.Deserialize(memoryStream);
     }
 }