/* * Исполняет программу, подсовывая в качестве входных данных состояние в игре game для игрока player; * воpащает код выполнения и через row1, col1, row2, col2, skip - выходные данные; * maxTime - максимальное время выполнения в секундах, после чего процесс убивается */ public ExternalProgramExecuteResult Execute(TurncoatGame game, CellValue player, double maxTime, out int row1, out int col1, out int row2, out int col2, out bool skip, out string comment) { row1 = -1; col1 = -1; row2 = -1; col2 = -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(); string secondLine = sr.ReadLine(); sr.Close(); if (firstLine.ToLower() == "NOT") { return(result); } string[] parts1 = firstLine.Split(' '); row1 = int.Parse(parts1[0].Trim()) - 1; col1 = int.Parse(parts1[1].Trim()) - 1; if (row1 < 0 || row1 >= game.RowCount || col1 < 0 || col1 >= game.ColCount) { return(ExternalProgramExecuteResult.WrongInputData); } string[] parts2 = secondLine.Split(' '); row2 = int.Parse(parts2[0].Trim()) - 1; col2 = int.Parse(parts2[1].Trim()) - 1; if (row2 < 0 || row2 >= game.RowCount || col2 < 0 || col2 >= game.ColCount) { return(ExternalProgramExecuteResult.WrongInputData); } skip = false; return(result); } catch (Exception) { return(ExternalProgramExecuteResult.WrongOutputFormat); } }
private void StartGame() { game = new TurncoatGame(GameConfigFile); steps = new List <StepResult>(); stepsLogListBox.DataSource = steps; gameExecuting = true; humanStepTemp = null; allowSteps = 0; player1Executer = null; if (player1SelectProgramCheckBox.Checked) { player1Executer = new TurncoatGameClientProgramExecuter(player1SelectProgramTextBox.Text, inputFilename, outputFilename); } player2Executer = null; if (player2SelectProgramCheckBox.Checked) { player2Executer = new TurncoatGameClientProgramExecuter(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 = game.IsStepAllow(nextStepPlayer); if (isStepAllow) { TurncoatGameClientProgramExecuter playerExecuter = nextStepPlayer == CellValue.WhiteChip ? player1Executer : player2Executer; if (playerExecuter != null) { int row1, col1, row2, col2; bool skip; string comment; ExternalProgramExecuteResult execResult = playerExecuter.Execute(game, nextStepPlayer, ProgramMaxTime, out row1, out col1, out row2, out col2, out skip, out comment); switch (execResult) { case ExternalProgramExecuteResult.Ok: if (skip) { AddStep(new StepResult(nextStepPlayer, null, "пропуск хода")); nextStepCount = 2; thisStepCount = 1; } else { isStepAllow = game.IsStepAllow(nextStepPlayer, row1, col1, row2, col2); if (isStepAllow) { game.Step(nextStepPlayer, row1, col1, row2, col2); AddStep(new StepResult(nextStepPlayer, row1, col1, row2, col2)); } else { AddStep(new StepResult(nextStepPlayer, row1, col1, row2, col2, "недопустимый ход!", null)); nextStepCount = 2; thisStepCount = 1; } } break; default: AddStep(new StepResult(nextStepPlayer, null, executeResultToErrorString(execResult))); nextStepCount = 2; thisStepCount = 1; break; } } else { TurncoatGameClientProgramExecuter.Step step; do { step = GetHumanStep(); isStepAllow = step != null && game.IsStepAllow(nextStepPlayer, step.Row1, step.Col1, step.Row2, step.Col2); }while (step != null && !isStepAllow); if (step != null) { game.Step(nextStepPlayer, step.Row1, step.Col1, step.Row2, step.Col2); AddStep(new StepResult(nextStepPlayer, step.Row1, step.Col1, step.Row2, step.Col2)); } } } else { AddStep(new StepResult(nextStepPlayer, null, "Нет возможности хода")); thisStepCount = 1; } thisStepCount--; ViewRefresh(); } nextStepPlayer = nextStepPlayer == CellValue.WhiteChip ? CellValue.BlackChip : CellValue.WhiteChip; allowSteps--; //ViewRefresh(); if (player1Executer != null && player1Executer != null) { Sleep(BetweenTime); } } CellValue c = WhoWin(); string s; switch (c) { case CellValue.WhiteChip: s = "White"; break; case CellValue.BlackChip: s = "Black"; break; case CellValue.Empty: s = "Friendship"; break; default: s = "Who?"; break; } MessageBox.Show(s + " win"); ViewRefresh(); }