/*
         * Исполняет программу, подсовывая в качестве входных данных состояние в игре game для игрока player;
         * воpащает код выполнения и через row, col, skip - выходные данные;
         * maxTime - максимальное время выполнения в секундах, после чего процесс убивается
         */
        public ExternalProgramExecuteResult Execute(OthelloGame game, CellValue player, double maxTime,
                                                    out int row, out int col, out bool skip, out string comment)
        {
            row     = -1;
            col     = -1;
            skip    = true;
            comment = null;

            if (game == null)
            {
                return(ExternalProgramExecuteResult.WrongInputData);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("{0} {1}", game.RowCount, game.ColCount);
            sb.AppendLine();
            for (int r = 0; r < game.RowCount; r++)
            {
                for (int c = 0; c < game.ColCount; c++)
                {
                    sb.Append(game[r, c] == CellValue.Empty ? '.' : game[r, c] == player ? 'W' : 'B');
                }
                sb.AppendLine();
            }

            String outputFileContent;
            ExternalProgramExecuteResult result = Execute(sb.ToString(), maxTime, out outputFileContent, out comment);

            if (result != ExternalProgramExecuteResult.Ok)
            {
                return(result);
            }

            try {
                StringReader sr        = new StringReader(outputFileContent);
                string       firstLine = sr.ReadLine().Trim();
                sr.Close();
                if (firstLine.ToLower() == "NOT")
                {
                    return(result);
                }

                string[] parts = firstLine.Split(' ');
                row = int.Parse(parts[0].Trim()) - 1;
                col = int.Parse(parts[1].Trim()) - 1;
                if (row < 0 || row >= game.RowCount ||
                    col < 0 || col >= game.ColCount)
                {
                    return(ExternalProgramExecuteResult.WrongInputData);
                }
                skip = false;

                return(result);
            }
            catch (Exception) {
                return(ExternalProgramExecuteResult.WrongOutputFormat);
            }
        }
Esempio n. 2
0
        private void StartGame()
        {
            game  = new OthelloGame(GameConfigFile);
            steps = new List <OthelloStepResult>();

            stepsLogListBox.DataSource = steps;

            gameExecuting = true;
            humanStepTemp = null;
            allowSteps    = 0;

            player1Executer = null;
            if (player1SelectProgramCheckBox.Checked)
            {
                player1Executer = new OthelloGameClientProgramExecuter(player1SelectProgramTextBox.Text,
                                                                       inputFilename, outputFilename);
            }
            player2Executer = null;
            if (player2SelectProgramCheckBox.Checked)
            {
                player2Executer = new OthelloGameClientProgramExecuter(player2SelectProgramTextBox.Text,
                                                                       inputFilename, outputFilename);
            }

            nextStepPlayer = player1FirstStepRadioButton.Checked ? CellValue.WhiteChip : CellValue.BlackChip;
            nextStepCount  = 1;

            ViewRefresh();

            while (true)
            {
                while (!Finished && allowSteps <= 0)
                {
                    Thread.Sleep(ActionCheckTimeInterval);
                    Application.DoEvents();
                }
                //allowSteps--;

                if (Finished)
                {
                    break;
                }

                int thisStepCount = nextStepCount;
                nextStepCount = 1;
                while (thisStepCount > 0)
                {
                    bool isStepAllow = ClassicGame ? game.IsStepAllowClassic(nextStepPlayer) : game.IsStepAllow(nextStepPlayer);
                    if (isStepAllow)
                    {
                        OthelloGameClientProgramExecuter playerExecuter = nextStepPlayer == CellValue.WhiteChip ? player1Executer : player2Executer;
                        if (playerExecuter != null)
                        {
                            int    row, col;
                            bool   skip;
                            string comment;
                            ExternalProgramExecuteResult execResult = playerExecuter.Execute(game, nextStepPlayer, ProgramMaxTime,
                                                                                             out row, out col, out skip, out comment);
                            switch (execResult)
                            {
                            case ExternalProgramExecuteResult.Ok:
                                if (skip)
                                {
                                    AddStep(new OthelloStepResult(nextStepPlayer, null, "пропуск хода"));
                                    nextStepCount = 2;
                                    thisStepCount = 1;
                                }
                                else
                                {
                                    isStepAllow = ClassicGame ? game.IsStepAllowClassic(nextStepPlayer, row, col) :
                                                  game.IsStepAllow(nextStepPlayer, row, col);
                                    if (isStepAllow)
                                    {
                                        game.Step(nextStepPlayer, row, col);
                                        AddStep(new OthelloStepResult(nextStepPlayer, row, col));
                                    }
                                    else
                                    {
                                        AddStep(new OthelloStepResult(nextStepPlayer, row, col, "недопустимый ход!", null));
                                        nextStepCount = 2;
                                        thisStepCount = 1;
                                    }
                                }
                                break;

                            default:
                                AddStep(new OthelloStepResult(nextStepPlayer, null, executeResultToErrorString(execResult)));
                                nextStepCount = 2;
                                thisStepCount = 1;
                                break;
                            }
                        }
                        else
                        {
                            OthelloGameClientProgramExecuter.Step step;
                            do
                            {
                                step        = GetHumanStep();
                                isStepAllow = step != null &&
                                              (ClassicGame ? game.IsStepAllowClassic(nextStepPlayer, step.Row, step.Col) :
                                               game.IsStepAllow(nextStepPlayer, step.Row, step.Col));
                            }while (step != null && !isStepAllow);

                            if (step != null)
                            {
                                game.Step(nextStepPlayer, step.Row, step.Col);
                                AddStep(new OthelloStepResult(nextStepPlayer, step.Row, step.Col));
                            }
                        }
                    }
                    else
                    {
                        AddStep(new OthelloStepResult(nextStepPlayer, null, "Нет возможности хода"));
                        thisStepCount = 1;
                    }

                    thisStepCount--;
                    ViewRefresh();
                }

                nextStepPlayer = nextStepPlayer == CellValue.WhiteChip ? CellValue.BlackChip : CellValue.WhiteChip;
                allowSteps--;
                //ViewRefresh();

                if (player1Executer != null && player1Executer != null)
                {
                    Sleep(AutoModeBetweenStepsTimeInterval);
                }
            }

            ViewRefresh();
        }