private TurncoatGameClientProgramExecuter.Step GetHumanStep() { TurncoatGameClientProgramExecuter.Step step; do { step = humanStepTemp; humanStepTemp = null; if (step == null) { Thread.Sleep(ActionCheckTimeInterval); Application.DoEvents(); } }while (!Finished && step == null); return(step); }
private void fieldPanel_MouseDown(object sender, MouseEventArgs e) { if (game == null || e.Button != MouseButtons.Left || allowSteps <= 0) { return; } int offset = DrawFieldOffset; int cellSize = CellSize; int x = e.X - offset + 1, y = e.Y - offset + 1; if (x % cellSize < 2 || (cellSize - x % cellSize < 2) || y % cellSize < 2 || (cellSize - y % cellSize < 2)) { return; } int row = y / cellSize, col = x / cellSize; if (row >= game.RowCount || col >= game.ColCount) { return; } if (!selection.clickMade && game[row, col] == nextStepPlayer) { selection.row1 = row; selection.col1 = col; selection.clickMade = true; ViewRefresh(); } else { if (game[row, col] == CellValue.Empty) { selection.row2 = row; selection.col2 = col; if (game.IsStepAllow(nextStepPlayer, selection.row1, selection.col1, selection.row2, selection.col2)) { humanStepTemp = new TurncoatGameClientProgramExecuter.Step(selection.row1, selection.col1, selection.row2, selection.col2); } } selection.ToNull(); ViewRefresh(); } }
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(); }