예제 #1
0
        /// <summary>
        /// Ход фигуры "Конь" по индексу
        /// </summary>
        /// <param name="turnIndex">Индекс хода фигуры "Конь"</param>
        /// <param name="horsePos">Структура позиции фигуры "Конь"</param>
        /// <param name="undo">Флаг отмены хода</param>
        /// <returns></returns>

        static HorsePosition HorseTurn(int turnIndex, HorsePosition horsePos, bool undo)
        {
            if (undo)
            {
                ToUndo(ref turnIndex);
            }

            switch (turnIndex)
            {
            case 0:
                horsePos.posY -= 2;
                horsePos.posX -= 1;
                break;

            case 1:
                horsePos.posY -= 2;
                horsePos.posX += 1;
                break;

            case 2:
                horsePos.posY -= 1;
                horsePos.posX += 2;
                break;

            case 3:
                horsePos.posY += 1;
                horsePos.posX += 2;
                break;

            case 4:
                horsePos.posY += 2;
                horsePos.posX += 1;
                break;

            case 5:
                horsePos.posY += 2;
                horsePos.posX -= 1;
                break;

            case 6:
                horsePos.posY += 1;
                horsePos.posX -= 2;
                break;

            case 7:
                horsePos.posY -= 1;
                horsePos.posX -= 2;
                break;

            default:
                return(horsePos);
            }

            return(horsePos);
        }
예제 #2
0
        /// <summary>
        /// Проверка возможности сделать ход
        /// </summary>
        /// <param name="horsePos">Структура позиции фигуры "Конь"</param>
        /// <param name="chessField">Двумерный массив (шахматное поле)</param>
        /// <returns></returns>

        static bool CheckTurnPossibly(HorsePosition horsePos, char[,] chessField)
        {
            if (horsePos.posY < 0 || horsePos.posX < 0)
            {
                return(false);
            }

            if (horsePos.posY >= chessField.GetLength(0) || horsePos.posX >= chessField.GetLength(1))
            {
                return(false);
            }

            return(chessField[horsePos.posY, horsePos.posX] != Cells.usedCell);
        }
예제 #3
0
        /// <summary>
        /// Начало поиска возможного решения задачи
        /// </summary>

        public void StartSearching()
        {
            stats.startTime = DateTime.Now;

            for (var i = 0; i < techVar.fieldY; i++)
            {
                horse.posY = i;
                for (var j = 0; j < techVar.fieldX; j++)
                {
                    horse.posX = j;

                    chessField[i, j] = Cells.horseCell;
                    DrawChessField(chessField);
                    stats.allOperations += 2;

                    Thread.Sleep(techVar.pauseValue);

                    for (var k = 0; k < 8; k++)
                    {
                        tempHorse = HorseTurn(k, horse, false);

                        if (!CheckTurnPossibly(tempHorse, chessField))
                        {
                            if (k < 7)
                            {
                                continue;
                            }

                            if (thisTurn == 0)
                            {
                                chessField[horse.posY, horse.posX] = Cells.emptyCell;
                                break;
                            }

                            thisTurn--;

                            if (passTurns[thisTurn] != 7)
                            {
                                k = passTurns[thisTurn];

                                chessField[horse.posY, horse.posX] = Cells.emptyCell;
                                horse = HorseTurn(k, horse, true);
                                chessField[horse.posY, horse.posX] = Cells.horseCell;
                                DrawChessField(chessField);
                                stats.allOperations += 3;
                            }
                            else
                            {
                                while (thisTurn >= 0 && passTurns[thisTurn] == 7)
                                {
                                    k = passTurns[thisTurn];

                                    chessField[horse.posY, horse.posX] = Cells.emptyCell;
                                    horse = HorseTurn(k, horse, true);
                                    chessField[horse.posY, horse.posX] = Cells.horseCell;
                                    DrawChessField(chessField);
                                    stats.allOperations += 3;

                                    thisTurn--;
                                }

                                if (thisTurn < 0)
                                {
                                    if (i < techVar.fieldY - 1)
                                    {
                                        thisTurn = 0;
                                        break;
                                    }
                                    ImpossibleTask();
                                    return;
                                }

                                k = passTurns[thisTurn];

                                chessField[horse.posY, horse.posX] = Cells.emptyCell;
                                horse = HorseTurn(k, horse, true);
                                chessField[horse.posY, horse.posX] = Cells.horseCell;
                                DrawChessField(chessField);
                                stats.allOperations += 3;
                            }

                            continue;
                        }

                        finishTurns[thisTurn] =
                            $"{thisTurn + 1,2}-й ход: Y{horse.posY}, X{horse.posX} -> Y{tempHorse.posY}, X{tempHorse.posX}";

                        passTurns[thisTurn] = k;

                        chessField[horse.posY, horse.posX] = Cells.usedCell;
                        horse = tempHorse;
                        chessField[horse.posY, horse.posX] = Cells.horseCell;
                        DrawChessField(chessField);
                        stats.allOperations += 4;
                        stats.turnOperations++;

                        thisTurn++;
                        k = -1;

                        Thread.Sleep(techVar.pauseValue);

                        if (CheckFinish(chessField, finishTurns, ref stats))
                        {
                            return;
                        }
                    }
                }
            }
        }