Exemplo n.º 1
0
        //↓↓↓ Принимает координату и значение (Х или 0) и отрисовывает её на игровом поле ↓↓↓ //
        public void VisualizeNextMove(CellCoordinates cellCoordinates, PlayerCellState.playerCellState cellState)
        {
            foreach (Square s in listSquares)
            {
                if (s.CoordX == cellCoordinates.X && s.CoordY == cellCoordinates.Y)
                {
                    switch (cellState)
                    {
                    case PlayerCellState.playerCellState.X:
                        s.Value = 1;
                        break;

                    case PlayerCellState.playerCellState.O:
                        s.Value = 2;
                        break;

                    default:
                        break;
                    }
                }
            }

            playGround.Refresh();
            Application.DoEvents();
            playGround.AutoScrollOffset = new Point(50, 50);
        }
Exemplo n.º 2
0
        public CellCoordinates NextMove(CellState.cellState[,] currentState, byte qtyCellsForWin, TimeSpan remainingTimeForGame)
        {
            CellCoordinates nextMove = new CellCoordinates();

            if (currentState[0, 0] == CellState.cellState.Empty)
            {
                nextMove.X = 0;
                nextMove.Y = 0;
            }
            else if (currentState[0, 2] == CellState.cellState.Empty)
            {
                nextMove.X = 0;
                nextMove.Y = 2;
            }
            else if (currentState[2, 1] == CellState.cellState.Empty)
            {
                nextMove.X = 0;
                nextMove.Y = 2;
            }
            //else if (currentState[2, 2] == CellState.cellState.Empty)
            //{
            //    nextMove.X = 2;
            //    nextMove.Y = 2;
            //}
            //else if (currentState[2, 1] == CellState.cellState.Empty)
            //{
            //    nextMove.X = 2;
            //    nextMove.Y = 1;
            //}
            return(nextMove);
        }
Exemplo n.º 3
0
        private bool CheckVictory(Player algorithm, CellCoordinates cellCoordinates, int maxCellCoordinate, byte qtyCellsForWin, byte maxLengthFieldOfBattlefield, CellState.cellState[,] battleField, bool isVictory, string battleResult)
        {
            int[] correctDifferenceCoordinates = { 1, maxLengthFieldOfBattlefield - 1, maxLengthFieldOfBattlefield, maxLengthFieldOfBattlefield + 1 };
            int   initialCellCoordinate        = cellCoordinates.X * 1000 + cellCoordinates.Y;
            int   currentCellCoordinate;

            byte currentQtyOfVictoryCells;
            bool isOppositeDirectionSearched;

            foreach (sbyte coordNumAdd in correctDifferenceCoordinates)
            {
                sbyte coordinateNumAdd = coordNumAdd;
                currentCellCoordinate       = initialCellCoordinate;
                currentQtyOfVictoryCells    = 0;
                isOppositeDirectionSearched = false;

                while (currentCellCoordinate >= 0 &&
                       currentCellCoordinate / 1000 <= maxCellCoordinate / 1000 &&
                       currentCellCoordinate % 1000 <= maxCellCoordinate % 1000 &&
                       (CellState.cellState)battleField.GetValue(currentCellCoordinate / 1000, currentCellCoordinate % 1000) == (CellState.cellState)algorithm.playerCellState)
                {
                    currentQtyOfVictoryCells += 1;

                    currentCellCoordinate = AddNumToCoordinates(currentCellCoordinate, coordinateNumAdd, maxLengthFieldOfBattlefield);

                    if (currentCellCoordinate < 0 ||
                        currentCellCoordinate / 1000 > maxCellCoordinate / 1000 ||
                        currentCellCoordinate % 1000 > maxCellCoordinate % 1000)
                    {
                        if (!isOppositeDirectionSearched)
                        {
                            coordinateNumAdd            = (sbyte)(0 - coordNumAdd);
                            currentCellCoordinate       = AddNumToCoordinates(initialCellCoordinate, coordinateNumAdd, maxLengthFieldOfBattlefield);
                            isOppositeDirectionSearched = true;
                        }
                    }
                    else if ((CellState.cellState)battleField.GetValue(currentCellCoordinate / 1000, currentCellCoordinate % 1000) != (CellState.cellState)algorithm.playerCellState &&
                             !isOppositeDirectionSearched)
                    {
                        coordinateNumAdd            = (sbyte)(0 - coordNumAdd);
                        currentCellCoordinate       = AddNumToCoordinates(initialCellCoordinate, coordinateNumAdd, maxLengthFieldOfBattlefield);
                        isOppositeDirectionSearched = true;
                    }
                }

                if (currentQtyOfVictoryCells >= qtyCellsForWin)
                {
                    isVictory = true;
                    return(isVictory);
                }
            }

            return(isVictory);
        }
Exemplo n.º 4
0
        private bool SetPointOnBattleField(CellCoordinates pointOnBattleField, Player algorithm, CellState.cellState[,] battleField, bool isVictory, int currentQtyEmptyCellsOnBattleField)
        {
            bool isPointSet;

            //Setting point based on xy-coordinates
            if (battleField[pointOnBattleField.X, pointOnBattleField.Y] == CellState.cellState.Empty)
            {
                battleField[pointOnBattleField.X, pointOnBattleField.Y] = (CellState.cellState)algorithm.playerCellState;
                isPointSet = true;
                currentQtyEmptyCellsOnBattleField  -= 1;
                algorithm.RemainingQtyMovesForGame -= 1;
            }
            else
            {
                isVictory  = true;
                isPointSet = false;
            }

            return(isPointSet);
        }
Exemplo n.º 5
0
        public CellCoordinates NextMove(CellState.cellState[,] currentState, byte qtyCellsForWin, bool isVersusHuman, TimeSpan remainingTimeForGame, int remainingQtyMovesForGame)
        {
            CellCoordinates nextMove = new CellCoordinates();

            if (currentState[10, 10] == CellState.cellState.Empty)
            {
                nextMove.X = 10;
                nextMove.Y = 10;
            }
            else if (currentState[11, 6] == CellState.cellState.Empty)
            {
                nextMove.X = 11;
                nextMove.Y = 6;
            }
            else if (currentState[12, 13] == CellState.cellState.Empty)
            {
                nextMove.X = 12;
                nextMove.Y = 13;
            }

            return(nextMove);
        }
Exemplo n.º 6
0
        public CellCoordinates NextMove(CellState.cellState[,] currentState, byte qtyCellsForWin, bool isHuman, TimeSpan remainingTimeForGame, int remainingQtyMovesForGame)
        {
            CellCoordinates nextMove = new CellCoordinates();

            if (currentState[0, 0] == CellState.cellState.Empty)
            {
                nextMove.X = 0;
                nextMove.Y = 0;
            }
            else if (currentState[1, 1] == CellState.cellState.Empty)
            {
                nextMove.X = 1;
                nextMove.Y = 1;
            }
            else if (currentState[2, 2] == CellState.cellState.Empty)
            {
                nextMove.X = 2;
                nextMove.Y = 2;
            }

            return(nextMove);
        }
Exemplo n.º 7
0
        public CellCoordinates NextMove(CellState.cellState[,] currentState, byte qtyCellsForWin, bool isVersusHuman, TimeSpan remainingTimeForGame, int remainingQtyMovesForGame)
        {
            CellCoordinates nextMove = new CellCoordinates();

            if (currentState[2, 3] == CellState.cellState.Empty)
            {
                nextMove.X = 2;
                nextMove.Y = 3;
            }
            else if (currentState[3, 3] == CellState.cellState.Empty)
            {
                nextMove.X = 3;
                nextMove.Y = 3;
            }
            else if (currentState[5, 8] == CellState.cellState.Empty)
            {
                nextMove.X = 5;
                nextMove.Y = 8;
            }

            return(nextMove);
        }
Exemplo n.º 8
0
        //Setting point on battlefield based on xy-coordinates
        private string SetPointOnBattleField(CellCoordinates pointOnBattleField, Player algorithm, CellState.cellState[,] battleField, bool isVictory)
        {
            string settingPointResult = "";

            try
            {
                if (battleField[pointOnBattleField.X, pointOnBattleField.Y] == CellState.cellState.Empty)
                {
                    battleField[pointOnBattleField.X, pointOnBattleField.Y] = (CellState.cellState)algorithm.playerCellState;
                    currentQtyEmptyCellsOnBattleField -= 1;
                }
                else
                {
                    settingPointResult = "Non-empty cell";
                }
            }
            catch (IndexOutOfRangeException e)
            {
                dbInteraction.PublishGameException("Team \"" + algorithm.TeamName + "\" has returned coordinates outside the bound of battlefield.", e.ToString());
                settingPointResult = "Point is outside the bound";
            }

            return(settingPointResult);
        }
Exemplo n.º 9
0
        private bool CheckVictory(Player algorithm, CellCoordinates cellCoordinates, int maxCellCoordinate, byte qtyCellsForWin, byte maxLengthFieldOfBattlefield, CellState.cellState[,] battleField, bool isVictory, string battleResult)
        {
            int[] correctDifferenceCoordinates = { 1, maxLengthFieldOfBattlefield - 1, maxLengthFieldOfBattlefield, maxLengthFieldOfBattlefield + 1 };
            int   initialCellCoordinate        = cellCoordinates.X * 1000 + cellCoordinates.Y;
            int   currentCellCoordinate;
            int   prevCellCoordinate;

            byte currentQtyOfVictoryCells;
            bool isOppositeDirectionSearched;

            foreach (sbyte coordNumAdd in correctDifferenceCoordinates)
            {
                sbyte coordinateNumAdd = coordNumAdd;
                currentCellCoordinate       = initialCellCoordinate;
                prevCellCoordinate          = initialCellCoordinate;
                currentQtyOfVictoryCells    = 0;
                isOppositeDirectionSearched = false;

                try
                {
                    while (currentCellCoordinate >= 0 &&
                           currentCellCoordinate / 1000 <= maxCellCoordinate / 1000 &&
                           currentCellCoordinate % 1000 <= maxCellCoordinate % 1000 &&
                           (CellState.cellState)battleField.GetValue(currentCellCoordinate / 1000, currentCellCoordinate % 1000) == (CellState.cellState)algorithm.playerCellState)
                    {
                        if (Math.Abs(currentCellCoordinate / 1000 - prevCellCoordinate / 1000) < 2 &&
                            Math.Abs(currentCellCoordinate % 1000 - prevCellCoordinate % 1000) < 2)
                        {
                            currentQtyOfVictoryCells += 1;
                        }

                        prevCellCoordinate    = currentCellCoordinate;
                        currentCellCoordinate = AddNumToCoordinates(currentCellCoordinate, coordinateNumAdd, maxLengthFieldOfBattlefield);

                        if (currentCellCoordinate < 0 ||
                            currentCellCoordinate / 1000 > maxCellCoordinate / 1000 ||
                            currentCellCoordinate % 1000 > maxCellCoordinate % 1000)
                        {
                            if (!isOppositeDirectionSearched)
                            {
                                coordinateNumAdd            = (sbyte)(0 - coordNumAdd);
                                currentCellCoordinate       = AddNumToCoordinates(initialCellCoordinate, coordinateNumAdd, maxLengthFieldOfBattlefield);
                                prevCellCoordinate          = initialCellCoordinate;
                                isOppositeDirectionSearched = true;
                            }
                        }
                        else if ((CellState.cellState)battleField.GetValue(currentCellCoordinate / 1000, currentCellCoordinate % 1000) != (CellState.cellState)algorithm.playerCellState &&
                                 !isOppositeDirectionSearched)
                        {
                            coordinateNumAdd            = (sbyte)(0 - coordNumAdd);
                            currentCellCoordinate       = AddNumToCoordinates(initialCellCoordinate, coordinateNumAdd, maxLengthFieldOfBattlefield);
                            prevCellCoordinate          = initialCellCoordinate;
                            isOppositeDirectionSearched = true;
                        }
                    }
                }
                catch (IndexOutOfRangeException e)
                {
                    dbInteraction.PublishGameException("CheckVictory engine has returned coordinates outside the bound of battlefield.", e.ToString());
                }

                if (currentQtyOfVictoryCells >= qtyCellsForWin)
                {
                    isVictory = true;
                    return(isVictory);
                }
            }

            return(isVictory);
        }
Exemplo n.º 10
0
        private void RunBattle(List <Player> playingPairOfPlayers, byte qtyCellsForWin, byte maxLengthFieldOfBattlefield, FormHost gui)
        {
            gui.ClearBattleField();
            CellState.cellState[,] battleField;
            bool   isVictory    = false;
            string battleResult = "";
            bool   hasAlgorithmError;

            TimeSpan timeBeforePlayerMove;

            currentQtyEmptyCellsOnBattleField = maxLengthFieldOfBattlefield * maxLengthFieldOfBattlefield;
            sbyte           algorithmIndex    = 1;
            CellCoordinates nextMove          = new CellCoordinates();
            int             maxCellCoordinate = (maxLengthFieldOfBattlefield - 1) * 1000 + (maxLengthFieldOfBattlefield - 1);

            battleField = CreateBattleField(maxLengthFieldOfBattlefield);


            do
            {
                algorithmIndex    = (SByte)((algorithmIndex - 1) * (-1));
                hasAlgorithmError = false;
                string settingPointResult = "";

                timeBeforePlayerMove = DateTime.Now.TimeOfDay;

                try
                {
                    nextMove = playingPairOfPlayers[algorithmIndex].initializedPlayer.NextMove(battleField, qtyCellsForWin, playingPairOfPlayers[algorithmIndex].RemainingTimeForGame);
                    gui.VisualizeNextMove(nextMove, playingPairOfPlayers[algorithmIndex].playerCellState);
                    Thread.Sleep(1000);
                }
                catch (Exception e)
                {
                    dbInteraction.PublishGameException("Exception in algorithm's team " + playingPairOfPlayers[algorithmIndex].TeamName + " - nextMove method.", e.ToString());
                    hasAlgorithmError = true;
                }

                playingPairOfPlayers[algorithmIndex].RemainingTimeForGame -= DateTime.Now.TimeOfDay - timeBeforePlayerMove;

                settingPointResult = SetPointOnBattleField(nextMove, playingPairOfPlayers[algorithmIndex], battleField, isVictory);

                //If player exceeds amount of limit time for whole game, he lost
                if (playingPairOfPlayers[algorithmIndex].RemainingTimeForGame < TimeSpan.FromSeconds(0))
                {
                    isVictory = true;
                    playingPairOfPlayers[algorithmIndex].RemainingTimeForGame             = TimeSpan.FromSeconds(0);
                    (playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))]).isWinner = true;
                    playingPairOfPlayers[algorithmIndex].battleResult = "Player has exceeded limit time for whole game";
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].battleResult = "Another player has exceeded limit time for whole game";
                }
                //If algorithm return point coordinates, which are outside the bound of the battlefield, he lost
                else if (settingPointResult == "Point is outside the bound")
                {
                    isVictory = true;
                    (playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))]).isWinner = true;
                    playingPairOfPlayers[algorithmIndex].battleResult = "Algorithm has returned coordinates outside the bound of battlefield";
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].battleResult = "Another algorithm has returned coordinates outside the bound of battlefield";
                }
                //If algorithm throws error, he lost
                else if (hasAlgorithmError)
                {
                    isVictory = true;
                    (playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))]).isWinner = true;
                    playingPairOfPlayers[algorithmIndex].battleResult = "Algorithm has thrown exception";
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].battleResult = "Another algorithm has thrown exception";
                }
                //If Player set point in non-empty cell, he lost
                else if (settingPointResult == "Non-empty cell")
                {
                    isVictory = true;
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].isWinner = true;
                    playingPairOfPlayers[algorithmIndex].battleResult = "Player set point in non-empty cell";
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].battleResult = "Another player set point in non-empty cell";
                }
                //Checking victory after each setting point
                else if (CheckVictory(playingPairOfPlayers[algorithmIndex], nextMove, maxCellCoordinate, qtyCellsForWin, maxLengthFieldOfBattlefield, battleField, isVictory, battleResult))
                {
                    isVictory = true;
                    playingPairOfPlayers[algorithmIndex].isWinner     = true;
                    playingPairOfPlayers[algorithmIndex].battleResult = "Player set line of victory points";
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].battleResult = "Another player set line of victory points";
                }
                //Checking draw
                else if (currentQtyEmptyCellsOnBattleField <= 0 && !isVictory)
                {
                    isVictory = true;
                    playingPairOfPlayers[algorithmIndex].battleResult = "Empty cells are run out";
                    playingPairOfPlayers[(SByte)((algorithmIndex - 1) * (-1))].battleResult = "Empty cells are run out";
                }
            }while (!isVictory);
        }
Exemplo n.º 11
0
 public void VisualizeNextMove(CellCoordinates cellCoordinates, PlayerCellState.playerCellState plCellState)
 {
 }