private SilentGameEngine CreateSilentEngine()
        {
            SilentGameEngine silentEngine = new SilentGameEngine();
            int maxTurns;

            if (int.TryParse(textBoxMaxBatchTurns.Text, out maxTurns))
            {
                silentEngine.MaxTurns = maxTurns;
            }
            return(silentEngine);
        }
        private void PerformBatchRun()
        {
            if (MakeSureDbExists())
            {
                _Aborted                     = false;
                buttonStart.Enabled          = false;
                textBoxMaxBatchTurns.Enabled = false;
                bool errors = false;
                try
                {
                    string turnsColumnName;
                    string winnerColumnName;
                    DetermineColumNames(out turnsColumnName, out winnerColumnName);
                    SilentGameEngine silentEngine = CreateSilentEngine();
                    int       counter             = 0;
                    DataRow[] selection           = _MatchesDataBase.Select(turnsColumnName + " = -1");
                    int       total = selection.Length;
                    foreach (DataRow dr in selection)
                    {
                        labelProgress.Text = counter++ + " / " + total;
                        ExecuteSelectedRow(turnsColumnName, winnerColumnName, silentEngine, dr);

                        Application.DoEvents();

                        if (_Aborted)
                        {
                            break;
                        }
                    }
                }
                catch
                {
                    errors = true;
                    if (MessageBox.Show("Error occurred, save anyway?", "Whoops", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        SaveTestDb();
                    }
                }
                finally
                {
                    if (!errors)
                    {
                        if (MessageBox.Show("Save results?", "Whoops", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            SaveTestDb();
                        }
                    }
                    buttonStart.Enabled          = true;
                    textBoxMaxBatchTurns.Enabled = true;
                }
            }
        }
 private void PerformRunOnSelectedRecord()
 {
     if (MakeSureDbExists())
     {
         if (dataGridView1.SelectedRows.Count > 0)
         {
             string turnsColumnName;
             string winnerColumnName;
             DetermineColumNames(out turnsColumnName, out winnerColumnName);
             SilentGameEngine silentEngine = CreateSilentEngine();
             DataRowView      dr           = (DataRowView)dataGridView1.SelectedRows[0].DataBoundItem;
             ExecuteSelectedRow(turnsColumnName, winnerColumnName, silentEngine, dr.Row);
         }
     }
 }
Beispiel #4
0
        public bool Start()
        {
            if (IsRunning)
            {
                return(false);
            }
            else
            {
                System.Diagnostics.Debug.Assert(Player1 != null, "Player 1 should be assigned");
                System.Diagnostics.Debug.Assert(Player2 != null, "Player 2 should be assigned");
                System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(MapName), "MapName should be assigned");

                if (MaxTurns <= 0)
                {
                    MaxTurns = 200;
                }
                Aborted   = false;
                IsRunning = true;

                TurnsPlayed    = 0;
                PlayerWinnerId = 0;
                try
                {
                    engine = new Game(MapName, MaxTurns, 0, null);
                    engine.Init();
                    List <botDebugBase> players = new List <botDebugBase>();
                    players.Add(Player1);
                    players.Add(Player2);
                    IQueryable <botDebugBase> parallel = players.AsQueryable <botDebugBase>();
                    int turnsLeft = MaxTurns;

                    // I looked at the original engine, which had a lot of overhead because it was working with application instances
                    // and was capturing the output. Since I have a direct instance to the bot (wall, the debug wrapper) I can skip a lot of that
                    // and just use the bare essentials.
                    while (turnsLeft > 0 && (engine.Winner()) < 0)
                    {
                        if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[0]))
                        {
                            PlayerWinnerId = 1;
                        }
                        if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[1]))
                        {
                            PlayerWinnerId = 2;
                        }
                        if (PlayerWinnerId > 0)
                        {
                            break;
                        }


                        players.ForEach(player => player.GameBoardData = engine.PovRepresentation(player.Id));

                        foreach (botDebugBase player in parallel)
                        {
                            PushGameDataToPlayer(player);
                        }

                        turnsLeft--;
                        if (Aborted)
                        {
                            return(false);
                        }
                        else
                        {
                            TurnsPlayed++;
                            engine.FlushGamePlaybackString();
                            engine.DoTimeStep();

                            if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[0]))
                            {
                                PlayerWinnerId = 1;
                            }
                            if (!SilentGameEngine.CheckForAlliveOrDropPlayer(engine, players[1]))
                            {
                                PlayerWinnerId = 2;
                            }
                            if (PlayerWinnerId > 0)
                            {
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    IsRunning = false;
                }

                return(true);
            }
        }
        private void ExecuteSelectedRow(string turnsColumnName, string winnerColumnName, SilentGameEngine silentEngine, DataRow dr)
        {
            bool isCounterMatch = (bool)dr["CounterMatch"];
            int  botIndex;

            if (isCounterMatch)
            {
                botIndex             = int.Parse(dr["Player1"].ToString());
                silentEngine.Player1 = GetBotInstance(false, botIndex, 1);
                silentEngine.Player2 = GetBotInstance(true, botIndex, 2);
            }
            else
            {
                botIndex             = int.Parse(dr["Player2"].ToString());
                silentEngine.Player1 = GetBotInstance(true, botIndex, 1);
                silentEngine.Player2 = GetBotInstance(false, botIndex, 2);
            }

            silentEngine.MapName = Path.Combine(MapFolder, (string)dr["Map"]);

            if (silentEngine.Start())
            {
                switch (silentEngine.PlayerWinnerId)
                {
                case 0: dr[winnerColumnName] = 0;
                    break;

                default:
                    if (isCounterMatch)
                    {
                        dr[winnerColumnName] = silentEngine.PlayerWinnerId;
                    }
                    else
                    {
                        dr[winnerColumnName] = 3 - silentEngine.PlayerWinnerId;
                    }
                    break;
                }

                dr[turnsColumnName] = silentEngine.TurnsPlayed;
            }
        }
        private void Start(string filename)
        {
            _NumberOfTurnsPlayed = 0;
            _CurrentGameEngine   = null;
            _Aborted             = false;
            if (dataGridView1.SelectedRows.Count == 0)
            {
                //you forgot to chose a map, try again.
                MessageBox.Show("Please select a map on the \"Batch Run\" tavb first.");
                tabControlBotDebugger.SelectedTab = tabPageBatchRun;
                return;
            }
            InitializeColors();
            buttonPlay.Enabled = false;
            List <botDebugBase> players = new List <botDebugBase>();

            try
            {
                players.Add(GetBotInstance(cbOpponentOneOwnBot.Checked, comboBoxOpponent1.SelectedIndex, 1));
                players.Add(GetBotInstance(cbOpponentTwoOwnBot.Checked, comboBoxOpponent2.SelectedIndex, 2));

                int turnsLeft;

                if (!int.TryParse(textBoxTurns.Text, NumberStyles.Any, CultureInfo.InvariantCulture, out turnsLeft))
                {
                    turnsLeft = 200;
                }


                _CurrentGameEngine = new Game(filename, turnsLeft, 0, null);
                _CurrentGameEngine.Init();

                //CalcTSM(cbOpponentOneOwnBot.Checked ? 1 : 2);
                ForceRender();
                IQueryable <botDebugBase> parallel = players.AsQueryable <botDebugBase>();
                // I looked at the original engine, which had a lot of overhead because it was working with application instances
                // and was capturing the output. Since I have a direct instance to the bot (wall, the debug wrapper) I can skip a lot of that
                // and just use the bare essentials.
                while (turnsLeft > 0 && _CurrentGameEngine.Winner() < 0)
                {
                    if (!SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[0]) &&
                        SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[1]))
                    {
                        return;
                    }

                    players.ForEach(player => player.GameBoardData = _CurrentGameEngine.PovRepresentation(player.Id));

                    if (checkBoxBreakpoint.Checked && _HitBreakPointOnTurnNumber == _NumberOfTurnsPlayed)
                    {
                        _PlayAtFullSpeed = false;
                        while (checkBoxBreakpoint.Checked && !_Aborted && !_ReleaseBreakpoint)
                        {
                            // Blink
                            labelBreakpoint.Visible = DateTime.Now.Millisecond % 300 < 100;
                            Application.DoEvents();
                        }
                        labelBreakpoint.Visible = false;
                    }
                    _ReleaseBreakpoint = false;

                    if (checkBoxParallel.Checked)
                    {
                        foreach (botDebugBase player in parallel)
                        {
                            PushGameDataToPlayer(player);
                        }
                    }
                    else
                    {
                        players.ForEach(PushGameDataToPlayer);
                    }

                    if (!SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[0]) &&
                        SilentGameEngine.CheckForAlliveOrDropPlayer(_CurrentGameEngine, players[1]))
                    {
                        return;
                    }

                    turnsLeft--;
                    if (_Aborted)
                    {
                        break;
                    }
                    else
                    {
                        _NumberOfTurnsPlayed++;
                        _CurrentGameEngine.FlushGamePlaybackString();
                        labelTurn.Text = _NumberOfTurnsPlayed.ToString();

                        _CurrentGameEngine.DoTimeStep();
                        if (_Aborted)
                        {
                            break;
                        }
                        #region Rendering and pausing
                        if (cbRender.Checked)
                        {
                            panelRender.Refresh();
                            Application.DoEvents();
                            if (!_PlayAtFullSpeed)
                            {
                                int value = -1 + trackBarRenderDelay.Value;
                                System.Threading.Thread.Sleep(value);
                            }
                        }
                        else
                        {
                            Application.DoEvents();
                        }
                        #endregion
                    }
                }
            }
            finally
            {
                ForceRender();
                buttonPlay.Enabled = true;
                if (_Restart)
                {
                    _Restart = false;
                    Start(filename);
                }
                else
                {
                    _PlayAtFullSpeed = false;
                }
            }
        }