예제 #1
0
        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;
            }

            humanStepTemp = new OthelloGameClientProgramExecuter.Step(row, col);
        }
예제 #2
0
        private OthelloGameClientProgramExecuter.Step GetHumanStep()
        {
            OthelloGameClientProgramExecuter.Step step;
            do
            {
                step          = humanStepTemp;
                humanStepTemp = null;
                if (step == null)
                {
                    Thread.Sleep(ActionCheckTimeInterval);
                    Application.DoEvents();
                }
            }while (!Finished && step == null);

            return(step);
        }
예제 #3
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();
        }