Exemple #1
0
        public Position offset       = new Position();                                //Position offset in pixels for use when drawing the bitmap


        //END GLOBALS------------------------------------------------------------------------------------

        /// <summary>
        /// Developer:		Anthony Harris
        /// Function Name:	InitializeData()
        /// Parameters:		None
        /// Returns:		None
        /// Description:	Iterates through the JSON map file and defines the starting
        ///					points for each entity, initializes the neural network data,
        ///					and initializes the entities.
        ///	Last Modified:	02 October 2019
        ///	Modification:	Successfully implemented logic to initialize the Edges of the
        ///	                neural networks.
        /// </summary>
        private void InitializeData()
        {
            worldState = WorldState.GameInProgress;

            startTime       = DateTime.Now;
            currentTime     = startTime;
            currentWinBonus = Player.baseFitness;

            xMapBlock = 0;
            yMapBlock = 0;
            for (int index = 0; index < terrainMapN.Length; ++index)
            {
                int pass;                       //To be passed to the TileType function

                //If the current char at terrainMapN[index] can be parsed as an int, then step in
                if (int.TryParse(terrainMapN[index].ToString(), out pass))
                {
                    //Call the function to define the type of tile
                    TileType(pass);
                }
            }

            if (firstTime)
            {
                List <List <Edge> > tempEdges = new List <List <Edge> >()
                {
                    new List <Edge>(), new List <Edge>()
                };
                List <int> outputLayer = new List <int>();
                //Create the edges of the neural networks to be used

                int inLayerId;
                int outLayerId;

                //The following is for when you use the entire map as an input.

                //for (int x = 0; x < WIDTH_IN_TILES; ++x)
                //{
                //	for (int y = 0; y < HEIGHT_IN_TILES; ++y)
                //	{
                //		for (int z = 0; z < MapBlock.numOfStates; ++z)
                //		{
                //			inputLayer.Add(inLayerId);
                //          for (int command = (int)ControlCommand.NONE; command < (int)ControlCommand.FINAL_UNUSED; ++command)
                //			{
                //              outLayerId = WIDTH_IN_TILES * HEIGHT_IN_TILES * MapBlock.numOfStates + command + 1;
                //				tempEdges.Add(new Edge(inLayerId, outLayerId, 1, 0));
                //			}
                //          inLayerId++;
                //      }
                //	}
                //}

                //The following is for when you use the tankDegreeToTarget and turretDegreeToTarget
                //distanceToTarget attributes of player as inputs

                Random rng = new Random();
                for (int playerCounter = 0; playerCounter < 2; ++playerCounter)
                {
                    inLayerId = 0;
                    for (int i = 0; i < Player.NUM_OF_INPUTS; ++i)
                    {
                        for (int command = (int)ControlCommand.NONE; command < (int)ControlCommand.FINAL_UNUSED; ++command)
                        {
                            outLayerId = Player.NUM_OF_INPUTS + command;
                            tempEdges[playerCounter].Add(new Edge(inLayerId, outLayerId, rng.NextDouble(), rng.NextDouble()));
                            if (!outputLayer.Contains(outLayerId))
                            {
                                outputLayer.Add(outLayerId);
                            }
                        }
                        inLayerId++;
                    }
                }

                red.organism.botBrain  = new NeuralNetwork(tempEdges[0], Player.NUM_OF_INPUTS + (int)ControlCommand.FINAL_UNUSED, outputLayer);
                blue.organism.botBrain = new NeuralNetwork(tempEdges[1], Player.NUM_OF_INPUTS + (int)ControlCommand.FINAL_UNUSED, outputLayer);
            }

            //Initialize the players
            red.reset(MAX_INPUT_VALUES);
            blue.reset(MAX_INPUT_VALUES);

            red.playerController(ControlCommand.NONE, ControlCommand.Right);
            blue.playerController(ControlCommand.NONE, ControlCommand.Left);

            red.updatePlayer(mapBlocks);
            blue.updatePlayer(mapBlocks);

            red.calcDegreeAndDistanceToTarget(blue, MAX_INPUT_VALUES);
            blue.calcDegreeAndDistanceToTarget(red, MAX_INPUT_VALUES);
        }