예제 #1
0
        public static CAMatrix InitialiseMatrix(BaseProgram progEvaulateProgram,
                                                BaseProgram progOpponents)
        {
            CAMatrix matMatrix = new CAMatrix();

            // Position player 0 on board
            ImprovedRandom rndRandom       = GlobalRandom.m_rndRandom;
            int            nMinXToGenerate = matMatrix.Width * 4 / 10;
            int            nMaxXToGenerate = matMatrix.Width * 6 / 10;
            int            nMinYToGenerate = matMatrix.Height * 4 / 10;
            int            nMaxYToGenerate = matMatrix.Height * 6 / 10;

            matMatrix.CreateCell(rndRandom.Next(nMinXToGenerate, nMaxXToGenerate),
                                 rndRandom.Next(nMinYToGenerate, nMaxYToGenerate),
                                 progEvaulateProgram,
                                 PLAYER_COLOR, STARTING_ENERGY);

            // Create possible colors
            List <Color> arrColors = new List <Color>();

            arrColors.Add(Color.RoyalBlue);
            arrColors.Add(Color.Silver);
            arrColors.Add(Color.Salmon);

            // Position other players on board
            BaseProgram progOpponent0 = (BaseProgram)progOpponents.Clone();

            matMatrix.CreateCell(rndRandom.Next(nMinXToGenerate, nMaxXToGenerate),
                                 rndRandom.Next(nMinYToGenerate, nMaxYToGenerate),
                                 progOpponent0, arrColors[0], STARTING_ENERGY);
            BaseProgram progOpponent1 = (BaseProgram)progOpponents.Clone();

            matMatrix.CreateCell(rndRandom.Next(nMinXToGenerate, nMaxXToGenerate),
                                 rndRandom.Next(nMinYToGenerate, nMaxYToGenerate),
                                 progOpponent1, arrColors[1], STARTING_ENERGY);

            return(matMatrix);
        }
예제 #2
0
        public static float ProgramRunner(World cWorld, BaseProgram progEvaulateProgram,
                                          Graphics grpGraphics, bool fDisplayProgress)
        {
            // Reset the variables of all the programs.
            for (int i = 0; i < progEvaulateProgram.Variables.Length; i++)
            {
                progEvaulateProgram.Variables[i].Value = 0;
            }

            ImprovedRandom rndRandom = GlobalRandom.m_rndRandom;
            bool           fFoundGoodPlayerPosition = false;
            float          dPlayerInitialPositionX  = 0;
            float          dPlayerInitialPositionY  = 0;

            while (fFoundGoodPlayerPosition == false)
            {
                dPlayerInitialPositionX = rndRandom.Next(0, cWorld.GetWorldWidth());
                dPlayerInitialPositionY = rndRandom.Next(0, cWorld.GetWorldHeight());
                if (cWorld.CheckIfPositionIsBlocked(new PointF(dPlayerInitialPositionX, dPlayerInitialPositionY)) == false)
                {
                    fFoundGoodPlayerPosition = true;
                }
            }

            Runner cRunner = new Runner(new PointF(dPlayerInitialPositionX, dPlayerInitialPositionY));
            float  dTotalPassedDistnace = 0;

            // Running the matrix players.
            int nNumberOfIterations = 100;

            if (fDisplayProgress == true)
            {
                nNumberOfIterations = 200;
            }

            int nNumOfTimesPlayerHadDistanceZero = 0;
            int nNumOfTimesPlayerWasBlocked      = 1;

            for (int i = 0; i < nNumberOfIterations; i++)
            {
                if (fDisplayProgress == true)
                {
                    DisplayWorld(cWorld, cRunner, grpGraphics);
                    Thread.Sleep(30);
                }

                PointF pntLastRunnerPosition = cRunner.GetPosition();

                progEvaulateProgram.Run(cRunner);
                cRunner.RunTimestep(cWorld);
                PointF pntCurrRunnerPosition = cRunner.GetPosition();
                float  dPassedDistnace       = new Vector(pntLastRunnerPosition, pntCurrRunnerPosition).Distance();

                // If the player did not move, it's gonna cost him.
                if (dPassedDistnace == 0f)
                {
                    nNumOfTimesPlayerHadDistanceZero++;
                }

                if (cRunner.WasBlockedThisTurn() == true)
                {
                    nNumOfTimesPlayerWasBlocked++;
                }

                dTotalPassedDistnace += dPassedDistnace;
            }

            //dTotalPassedDistnace *= (1 - ((float)nNumOfTimesPlayerHadDistanceZero / nNumberOfIterations));
            float dDivision = nNumOfTimesPlayerWasBlocked;

            dTotalPassedDistnace /= dDivision;

            return(dTotalPassedDistnace);
        }