public static bool SingleFight(BotFight botFight)
        {
            bool fightSuccess = false;
            try
            {
                if (isInit)
                {
                    //java -jar tools/PlayGame.jar maps/map43.txt 1000 1000 log.txt "java -jar example_bots/DualBot.jar" "bin/Debug/DefenderBot.exe" 1> stdout.txt 2> stderr.txt

                    FileInfo logPath = new FileInfo(applicationExecPath + "/logs/log_fight" + botFight.FightId().ToString() + ".txt");
                    botFight.Log(logPath);
                    FileInfo viewerInputPath = new FileInfo(applicationExecPath + "/viewer_inputs/input_fight" + botFight.FightId().ToString() + ".txt");
                    botFight.ViewerInput(viewerInputPath);
                    FileInfo resultPath = new FileInfo(applicationExecPath + "/results/result_fight" + botFight.FightId().ToString() + ".txt");
                    botFight.Result(resultPath);

                    //string[] execFileArgs = ExecFileArgsMaker(toolsPath, playGameFile, playGameCmd);
                    //string execFile = execFileArgs[0];
                    //string execArgs = execFileArgs[1];

                    /*string player1Cmd = (botFight.Player1().isJar())
                        ? ("\"java -jar \\\"" + botFight.Player1().BotFileInfo().FullName + "\\\"\"")
                        : ("\"" + botFight.Player1().BotFileInfo().FullName + "\"");
                    string player2Cmd = (botFight.Player2().isJar())
                        ? ("\"java -jar \\\"" + botFight.Player2().BotFileInfo().FullName + "\\\"\"")
                        : ("\"" + botFight.Player2().BotFileInfo().FullName + "\"");*/

                    string player1Cmd = PlayerCommand(botFight.Player1());
                    string player2Cmd = PlayerCommand(botFight.Player2());

                    /*string fightCmd = " -jar " + "\"" + playGamePath + "\" "
                        + "\"" + botFight.FightMap().MapFileInfo().FullName + "\" "
                        + botFight.TimeAmount().ToString() + " "
                        + botFight.TurnAmount().ToString() + " "
                        + "\"" + applicationPath + "/logs/log_fight" + botFight.FightId().ToString() + ".txt\" "
                        + player1Cmd + " "
                        + player2Cmd;/* +" "
                        + "1> \"" + applicationPath + "/viewer_inputs/input_fight" + botFight.FightId().ToString() + ".txt\" "
                        + "2> \"" + applicationPath + "/results/result_fight" + botFight.FightId().ToString() + ".txt\"";*/

                    //string fightCmd = " -jar" + " \"" + playGamePath + "\" "
                    string fightCmd = playGameExecArgs
                        + "\"" + botFight.FightMap().MapFileInfo().FullName + "\" "
                        + botFight.TimeAmount().ToString() + " "
                        + botFight.TurnAmount().ToString() + " "
                        + "\"" + botFight.Log().FullName + "\" "
                        + player1Cmd + " "
                        + player2Cmd;
                    //Process process = new Process();
                    //process.StartInfo.FileName = playGameExecFile;
                    //process.StartInfo.Arguments = fightCmd;
                    //process.StartInfo.UseShellExecute = false;
                    //process.StartInfo.CreateNoWindow = true;
                    //process.StartInfo.RedirectStandardOutput = true;
                    //process.StartInfo.RedirectStandardError = true;
                    //process.Start();
                    //string stdout = process.StandardOutput.ReadToEnd();
                    //string stderr = process.StandardError.ReadToEnd();
                    //process.WaitForExit();
                    botFight.Command(playGameExecFile + " " + fightCmd);
                    ProcessRunner pr = new ProcessRunner(playGameExecFile, fightCmd);
                    pr.ProcessWorker();
                    string stdout = pr.OutputString();
                    string stderr = pr.ErrorString();

                    StreamWriter streamWriter = new StreamWriter(botFight.ViewerInput().FullName);
                    streamWriter.WriteLine(stdout);
                    streamWriter.Close();

                    streamWriter = new StreamWriter(botFight.Result().FullName);
                    streamWriter.WriteLine(stderr);
                    streamWriter.Close();

                    fightSuccess = true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            return fightSuccess;
        }
Ejemplo n.º 2
0
        // DataGRidViewResult on fly
        private void DataGridViewResultOnFly(BotFight botFight)
        {
            int i = dgvResultGrid.Rows.Count;
            string lastTurn = string.Empty;
            string errors = string.Empty;
            int isWinner = botFight.IsWinner(out lastTurn, out errors);

            Color cellsColor = (i % 2 == 0) ? (Color.White) : (Color.LightGray);

            dgvResultGrid.Rows.Add();
            dgvResultGrid.Rows[i].Cells["colID"].Value = botFight.FightId().ToString();
            dgvResultGrid.Rows[i].Cells["colID"].Style.BackColor = cellsColor;
            dgvResultGrid.Rows[i].Cells["colMap"].Value = botFight.FightMap().MapFileInfo().Name;
            dgvResultGrid.Rows[i].Cells["colMap"].Style.BackColor = cellsColor;
            dgvResultGrid.Rows[i].Cells["colPlayer1"].Value = botFight.Player1().BotFileInfo().Name;
            dgvResultGrid.Rows[i].Cells["colPlayer1"].Style.BackColor = cellsColor;
            dgvResultGrid.Rows[i].Cells["colPlayer2"].Value = botFight.Player2().BotFileInfo().Name;
            dgvResultGrid.Rows[i].Cells["colPlayer2"].Style.BackColor = cellsColor;
            switch (isWinner)
            {
                case -1:
                    dgvResultGrid.Rows[i].Cells["colWinner"].Value = "Lose";
                    dgvResultGrid.Rows[i].Cells["colWinner"].Style.BackColor = Color.LightPink;
                    loseCount++;
                    break;

                case 0:
                    dgvResultGrid.Rows[i].Cells["colWinner"].Value = "Draw";
                    dgvResultGrid.Rows[i].Cells["colWinner"].Style.BackColor = Color.LightBlue;
                    drawCount++;
                    break;

                case 1:
                    dgvResultGrid.Rows[i].Cells["colWinner"].Value = "Win";
                    dgvResultGrid.Rows[i].Cells["colWinner"].Style.BackColor = Color.LightGreen;
                    winCount++;
                    break;

                default:
                    break;
            }
            dgvResultGrid.Rows[i].Cells["colErrors"].Value = errors;
            dgvResultGrid.Rows[i].Cells["colErrors"].Style.BackColor = cellsColor;
            dgvResultGrid.Rows[i].Cells["colTurns"].Value = lastTurn;
            dgvResultGrid.Rows[i].Cells["colTurns"].Style.BackColor = cellsColor;
            dgvResultGrid.Rows[i].Cells["colViewGame"].Style.BackColor = cellsColor;
            dgvResultGrid.Rows[i].Cells["colCommand"].Value = botFight.Command();

            fightCount++;
            totalTurnNumbers += Convert.ToInt32(lastTurn.Substring(5));
            int avgTurnNumbers = 0;
            if (fightCount > 0)
            {
                winPercent = ((double)winCount / (double)fightCount) * 100;
                avgTurnNumbers = (int)Math.Ceiling((double)totalTurnNumbers / (double)fightCount);
            }
            lblTotal.Text = String.Format("Wins: {0}, Loses: {1}, Draws: {2}    |    Total:  Wins/Games: {3}/{4},    Win %: {5:N}    |    Avg. turn numbers: {6}", winCount, loseCount, drawCount, winCount, fightCount, winPercent, avgTurnNumbers);

            progressBar.PerformStep();
        }
        public static void ViewGame(BotFight botFight)
        {
            ///string viewGamePath = starterPackagePath + "/tools/ShowGame.jar";
            //string viewCmd = " -jar " + "\"" + viewGamePath + "\"";

            //string[] execFileArgs = ExecFileArgsMaker(toolsPath, showGameFile, showGameCmd);
            //string execFile = execFileArgs[0];
            //string execArgs = execFileArgs[1];

            if (isInit)
            {
                StreamReader streamReader = new StreamReader(botFight.ViewerInput().FullName);
                string viewerInput = streamReader.ReadToEnd();
                streamReader.Close();

                Process process = new Process();
                process.StartInfo.FileName = showGameExecFile;
                process.StartInfo.Arguments = showGameExecArgs;
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardInput = true;
                process.Start();

                StreamWriter streamWriter = process.StandardInput;
                streamWriter.WriteLine(viewerInput);
                streamWriter.Close();

                process.WaitForExit();
            }
        }
Ejemplo n.º 4
0
        // Test fights
        private bool TestFightsPrepare()
        {
            bool prepareFlag = true;
            string logsPath = Application.StartupPath + "/logs";
            string resultsPath = Application.StartupPath + "/results";
            string viewerInputPath = Application.StartupPath + "/viewer_inputs";
            if (!Directory.Exists(logsPath))
                Directory.CreateDirectory(logsPath);
            if (!Directory.Exists(resultsPath))
                Directory.CreateDirectory(resultsPath);
            if (!Directory.Exists(viewerInputPath))
                Directory.CreateDirectory(viewerInputPath);

            //Prepare fight
            myBot = new Bot(0, (FileInfo)cmbChooseMyBot.SelectedItem, true);
            int botId = 1;
            List<Bot> opponentBots = new List<Bot>();
            if (cmbChooseOpponentBot.SelectedIndex == 0)
            {
                for (int i = 1; i < cmbChooseOpponentBot.Items.Count; i++)
                {
                    FileInfo fi = (FileInfo)cmbChooseOpponentBot.Items[i];
                    //if ((cbExampleBots.Checked) && (fi.DirectoryName == (starterPackagePath + "\\example_bots")))
                    //if ((cbExampleBots.Checked) && (fi.DirectoryName == (Path.GetFullPath(Path.Combine(starterPackagePath, "example_bots")))))
                    if ((cbExampleBots.Checked) && (fi.DirectoryName == Path.GetFullPath(exampleBotsPath)))
                    {
                        Bot opponentBot = new Bot(botId, fi);
                        opponentBots.Add(opponentBot);
                        botId++;
                    }

                    //if ((cbOtherMyBots.Checked) && (fi.DirectoryName == myBotsPath))
                    if ((cbOtherMyBots.Checked) && (fi.DirectoryName == Path.GetFullPath(myBotsPath)))
                    {
                        if ((cbMirror.Checked) || ((!cbMirror.Checked) && (fi.Name != myBot.BotFileInfo().Name)))
                        {
                            Bot opponentBot = new Bot(botId, fi);
                            opponentBots.Add(opponentBot);
                            botId++;
                        }
                    }
                }
            }
            else
            {
                Bot opponentBot = new Bot(botId, (FileInfo)cmbChooseOpponentBot.SelectedItem);
                opponentBots.Add(opponentBot);
            }

            List<Map> maps = new List<Map>();
            int mapId = 0;
            if (cmbMap.SelectedIndex == 0)
            {
                for (int i = 1; i < cmbMap.Items.Count; i++)
                {
                    FileInfo fi = (FileInfo)cmbMap.Items[i];
                    Map map = new Map(mapId, fi);
                    maps.Add(map);
                    mapId++;
                }
            }
            else
            {
                Map map = new Map(mapId, (FileInfo)cmbMap.SelectedItem);
                maps.Add(map);
            }

            //Fights
            if ((opponentBots.Count > 0) && (maps.Count > 0))
            {
                botFights = new List<BotFight>();
                int fightId = 1;
                for (int i = 0; i < opponentBots.Count; i++)
                {
                    for (int j = 0; j < maps.Count; j++)
                    {
                        BotFight botFight = new BotFight(fightId, maps[j], myBot, opponentBots[i], turnAmount, timeAmount);
                        botFights.Add(botFight);
                        fightId++;
                        if (cbSwapPlayers.Checked)
                        {
                            BotFight botFight2 = new BotFight(fightId, maps[j], opponentBots[i], myBot, turnAmount, timeAmount);
                            botFights.Add(botFight2);
                            fightId++;
                        }
                    }
                }
            }
            else
            {
                errorString = string.Empty;
                if (opponentBots.Count == 0)
                    errorString += "There no opponent bots in your current fight settings\n";

                if (maps.Count == 0)
                    errorString += "There no maps in your current fight settings.\n";
                prepareFlag = false;
                MessageBox.Show(errorString, "Fight Settings warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return prepareFlag;
        }