Esempio n. 1
0
        bool?Observe()
        {
            Debug.Log("Observe state");
            int?indexWithLowestEntropy = FindCellWithLowestEntropy();

            //There is the cell with no possible values which means that we found a contradiction
            if (indexWithLowestEntropy == null)
            {
                return(false);
            }

            Debug.Log("Cell with lowest entropy : " + indexWithLowestEntropy);
            // All values has collapsed, fill result in observed function
            if (indexWithLowestEntropy == -1)
            {
                FillGeneratedResult();
                return(true);
            }

            //collapse cell value to one of possiblities randomly based on their weight
            double[] distribution = new double[T];
            for (int t = 0; t < T; t++)
            {
                distribution[t] = wave[indexWithLowestEntropy.Value][t] ? stationary[t] : 0;
            }
            int r = distribution.Random(random.NextDouble());

            for (int t = 0; t < T; t++)
            {
                wave[indexWithLowestEntropy.Value][t] = t == r;
            }
            Change(indexWithLowestEntropy.Value);

            return(null);
        }
Esempio n. 2
0
 void SpawnThings()
 {
     foreach (Rectangle room in rooms)
     {
         for (int x = room.left + 1; x < room.right - 1; x++)
         {
             for (int y = room.top + 1; y < room.bottom - 1; y++)
             {
                 float        chance   = (float)rnd.NextDouble() * 100;
                 List <Thing> canSpawn = _spawnableObjects.ThingsByPercent(chance);
                 if (canSpawn.Count > 0)
                 {
                     Thing spawnThing = canSpawn[rnd.Next(0, canSpawn.Count - 1)];
                     Map[x, y].HaveObject = true;
                     GameObject spawnedThing = Instantiate(spawnThing.thingObject, ToWorldPosition(new Vector2(x, y)), new Quaternion());
                     spawnedThings.Add(spawnedThing);
                     spawnedThing.transform.parent = transform.GetChild(1);
                     Inventory temp = spawnedThing.GetComponent <Inventory>();
                     if (temp != null)
                     {
                         AddItemsToInventory(temp);
                     }
                 }
             }
         }
     }
 }
Esempio n. 3
0
    private void AimTower()
    {
        var v1 = _player.transform.position;
        var v2 = _tower.transform.position;

        if (v2.z < 0)
        {
            Random rnd = new System.Random();
            float  x   = -1;
            float  y   = -1;
            while (x <= 3.1f || x >= 20 || y >= 9 || y <= 3.1f)
            {
                x = 3.1f + (float)(rnd.NextDouble() * (20 - 3.1f + 1));
                y = 3.1f + (float)(rnd.NextDouble() * (9 - 3.1f + 1));
                if (x > _player.transform.position.x - 0.5f && x < _player.transform.position.x + 0.5f)
                {
                    if (y > _player.transform.position.y - 0.5f && y < _player.transform.position.y + 0.5f)
                    {
                        x = -1;
                        y = -1;
                    }
                }
            }
            _tower.transform.position = new Vector3(x, y, 0);
            return;
        }

        var angleBetween = Mathf.Atan2(v2.y - v1.y, v2.x - v1.x) * 180 / Mathf.PI - 90;

        if (_towerAngle < angleBetween)
        {
            _towerAngle += _rotSpeed / 3;
        }
        else
        {
            _towerAngle -= _rotSpeed / 3;
        }
        _tower.transform.rotation = Get2DAngle(_towerAngle);

        if (myTimer2 == 0)
        {
            myTimer2 = 50;

            var obj    = (GameObject)Instantiate(_bullet, v2, Get2DAngle(0));
            var bullet = new BulletInfo(obj, _towerAngle + 90);

            var pos = bullet.Position;
            pos.x          -= 0.7f * Mathf.Cos((_towerAngle + 90) * Mathf.PI / 180);
            pos.y          -= 0.7f * Mathf.Sin((_towerAngle + 90) * Mathf.PI / 180);
            bullet.Position = pos;

            _bullets.Add(bullet);

            AudioSource.PlayClipAtPoint(shot, v2);
        }
    }
Esempio n. 4
0
        /// <summary>
        /// Removes some Employees from the EmployeeForHire List and creates a new one.
        /// </summary>
        public void DayChanged(object date)
        {
            var gameDate = (GameDate)date;

            data.employeesForHire.FindAll(data => data.hireableDays > 0).ForEach(data => data.hireableDays--);
            data.employeesForHire.FindAll(data => data.hireableDays == 0).ForEach(RemoveEmployeeForHire);

            while (data.employeesForHire.Count < minimumNumberOfEmployees || (data.employeesForHire.Count < MaxNumberOfHireableEmployees &&
                                                                              rand.NextDouble() < chanceNewEmpForHirePerDay))
            {
                var employeeData = GenerateEmployeeForHire();
                AddEmployeeForHire(employeeData);
            }

            //Pay Employees, at the start of each week.
            if (gameDate.DayOfWeek == DayOfWeek.Monday)
            {
                NotificationManager.Info("Payday is here!");
                for (int i = 0; i < data.hiredEmployees.Count; i++)
                {
                    var emp = data.hiredEmployees[i];
                    if (!bank.Pay(emp.Salary))
                    {
                        NotificationManager.Info(emp.Name + " has left the company. Your bank balance was too low for the paycheck.");
                        FireEmployee(emp);
                    }
                }
            }
            else if (gameDate.DayOfWeek == DayOfWeek.Sunday)
            {
                NotificationManager.Info("Only one day left till payday.");
            }
        }
        private IEnumerator FlashLightsRandom()
        {
            var colorList = new List <Color> {
                Color.black, Color.green, Color.red, Color.yellow
            };

            System.Random rand      = new System.Random();
            int           index     = rand.Next(colorList.Count);
            Color         randColor = colorList[index];

            double randWait = rand.NextDouble();

            if (gear != null && gear.statusLight != null)
            {
                gear.statusLight.toggleColor = randColor;
                gear.statusLight.Toggle();
            }
            else
            {
                Debug.Log("It seems gear or statuslight is null");
            }
            yield return(new WaitForSeconds((float)randWait));

            if (running)
            {
                this.StartCoroutine(FlashLightsRandom());
            }
        }
Esempio n. 6
0
        public void GetRankTest()
        {
            for (var _ = 0; _ < 100; _++)
            {
                var num  = 100;
                var list = new SortSet <int, int>();
                var lst  = new List <int>();
                var rand = new System.Random();

                for (var i = 0; i < num; i++)
                {
                    if (rand.NextDouble() < 0.1)
                    {
                        lst.Add(i);
                    }
                    list.Add(i, i);
                }

                foreach (var n in lst)
                {
                    if (list.GetRank(n) != n)
                    {
                        Assert.Fail();
                    }
                }

                Assert.AreEqual(-1, list.GetRank(-1));
            }
        }
Esempio n. 7
0
    /// <summary>
    /// Get random gene function which takes into account which gene it is, and calculates the according value.
    /// </summary>
    /// <param name="currentGene">Index of current gene.</param>
    /// <returns>A random value which is modified based off of the currentGene.</returns>
    private float GetRandomGene(int currentGene)
    {
        var enumValues        = Enum.GetValues(typeof(GeneType)) as GeneType[];
        var noOfParamaters    = enumValues.Length;
        var genesPerParamater = Car.NoOfGenes / noOfParamaters;

        var currentGeneParamIndex = -1;

        // Get which current gene this is on.
        for (int i = 1; i < noOfParamaters + 1; ++i)
        {
            int paramStartIndex = (i - 1) * genesPerParamater;
            int paramEndIndex   = i * genesPerParamater;

            if (currentGene >= paramStartIndex && currentGene <= paramEndIndex)
            {
                currentGeneParamIndex = i - 1;
            }
        }

        // Return a value based off of the gene.
        var currentIndexGene = enumValues[currentGeneParamIndex];

        switch (currentIndexGene)
        {
        case GeneType.LeftWheelPosX:
        case GeneType.LeftWheelPosY:
        case GeneType.RightWheelPosX:
        case GeneType.RightWheelPosY:
            return((float)Random.NextDouble() / genesPerParamater);     // Should be a percentage from left (i.e. 0 to 1)

        case GeneType.Speed:
            return((float)Random.NextDouble() * 30f);    // Don't make the car too slow.

        default:
            return((float)Random.NextDouble());
        }
    }
Esempio n. 8
0
        private bool?Observe()
        {
            double minEntropy    = double.MaxValue;
            Cell   argminEntropy = null;

            for (int i = 0; i < cells.Length; i++)
            {
                var cell = cells[i];
                if (cell.PossiblesCount == 0)
                {
                    return(false);
                }

                var entropy = cell.Entropy;

                if (cell.PossiblesCount >= 2 && entropy <= minEntropy)
                {
                    double noise = 1E-6 * random.NextDouble();

                    if (entropy + noise < minEntropy)
                    {
                        minEntropy    = entropy + noise;
                        argminEntropy = cell;
                    }
                }
            }

            // all cells are observed
            if (argminEntropy == null)
            {
                Observed = new Tile[OutputWidth * OutputHeight];
                for (int i = 0; i < Observed.Length; i++)
                {
                    Observed[i] = Tiles[cells[i].ObservedTile];
                }

                return(true);
            }

            argminEntropy.ObserveRandomFromPossibles(random);

            return(null);
        }
Esempio n. 9
0
 public static float RandomNDF()
 {
     return((float)random.NextDouble());
 }
Esempio n. 10
0
 float GenerateFloat()
 {
     return((float)randomGenerator.NextDouble());
 }
Esempio n. 11
0
    public Stage GenerateStage(int eventCount, int timeStep, bool shields, bool asteroids, bool bosses, bool colorsShifts, bool rotators)
    {
        var stage = new List<GameEvent> ();
        var currentColor = Colors.player;
        System.Random randGen = new System.Random ();
        //Set seeds!
        var firstShift = (int)eventCount / 3;
        var secondShift = (int)(eventCount * 2.0 / 3.0);
        for (int i = 0; i < eventCount; i++) {
          if (i == eventCount - 1 && bosses) {
        stage.Add (((GameEvent)new Boss (new Vector3 (0, 0, 0), 1, timeStep)));
        continue;
          }
          if (colorsShifts && (i == firstShift || i == secondShift)) {
        currentColor = ColorUtils.GetRandomBackgroundColor (currentColor, randGen);
        stage.Add ((GameEvent)new BackgroundShift (currentColor, timeStep));
        continue;
          }

          var spawns = new List<Spawn> ();

          foreach (var slot in RandomSample(Constants.slots, randGen, randGen.Next(2,5))) {
        var chance = randGen.NextDouble ();
        var position = new Vector3 (slot, 5, 1);
        var color = ColorUtils.GetRandomColorForBackground (currentColor, randGen);
        var cannons = new bool[8];
        for(int j=0; j<8; j++) {
          if((randGen.Next() % 3) == 0)
            cannons[j] = true;
          else
            cannons[j] = false;
        }

        if (cannons.All(f=>!f)) {
          var luckyCannon = randGen.Next () % 8;
              cannons [luckyCannon] = true;
        }
        spawns.Add (BuildSingleSpawn (position, cannons, shields,  asteroids, rotators, color, chance));
          }
          stage.Add ((GameEvent)new SpawnSet (spawns, timeStep));
        }
        return new Stage (stage);
    }