Beispiel #1
0
        internal void RunGame(/*bool isVersusHuman, string teamName, */ string playersDllPath, List <BattleParams> listBattleParams, FormHost gui)
        {
            dbInteraction.CleanGameLogs();

            Player players = new Player();

            allPlayersList = players.ReturnAllPlayers(/*isVersusHuman, teamName*/ false, "NoName", gui.ReturnPathToAlgorithms());

            int k            = 1;
            int i            = 1;
            var queryPlayers = allPlayersList.SelectMany(PlayerId => allPlayersList, (PlayerId1, PlayerId2) => new { PlayerId1, PlayerId2 }).Where(PlayerId => PlayerId.PlayerId1 != PlayerId.PlayerId2);
            int brCount      = queryPlayers.Count();

            foreach (var player in queryPlayers)
            {
                player.PlayerId1.playerCellState = PlayerCellState.playerCellState.X;
                player.PlayerId2.playerCellState = PlayerCellState.playerCellState.O;

                k = i;
                foreach (BattleParams battleParams in listBattleParams)
                {
                    try
                    {
                        ConstructorInfo constructor1       = player.PlayerId1.AlgorithmClass.GetConstructor(new Type[0]);
                        dynamic         initializedPlayer1 = constructor1.Invoke(new object[0]);
                        player.PlayerId1.initializedPlayer = initializedPlayer1;
                    }
                    catch (Exception e)
                    {
                        dbInteraction.PublishGameException("Team's \"" + player.PlayerId1.TeamName + "\" algorithm has thrown exceptions while creating class object via constructor.", e.ToString());
                    }

                    try
                    {
                        ConstructorInfo constructor2       = player.PlayerId2.AlgorithmClass.GetConstructor(new Type[0]);
                        dynamic         initializedPlayer2 = constructor2.Invoke(new object[0]);
                        player.PlayerId2.initializedPlayer = initializedPlayer2;
                    }
                    catch (Exception e)
                    {
                        dbInteraction.PublishGameException("Team's \"" + player.PlayerId2.TeamName + "\" algorithm has thrown exceptions while creating class object via constructor.", e.ToString());
                    }

                    player.PlayerId1.RemainingTimeForGame        = battleParams.RemainingTimeForGame;
                    player.PlayerId1.QtyCellsForWin              = battleParams.QtyCellsForWin;
                    player.PlayerId1.MaxLengthFieldOfBattlefield = battleParams.MaxLengthFieldOfBattlefield;
                    player.PlayerId1.PlayingPairId               = k;
                    player.PlayerId2.RemainingTimeForGame        = battleParams.RemainingTimeForGame;
                    player.PlayerId2.QtyCellsForWin              = battleParams.QtyCellsForWin;
                    player.PlayerId2.MaxLengthFieldOfBattlefield = battleParams.MaxLengthFieldOfBattlefield;
                    player.PlayerId2.PlayingPairId               = k;
                    playersTournamentList.Add((Player)player.PlayerId1.Clone());
                    playersTournamentList.Add((Player)player.PlayerId2.Clone());
                    k += brCount;
                }
                i += 1;
            }

            for (int n = 1; n <= k - brCount; n++)
            {
                List <Player> playingPairOfPlayers = new List <Player>();
                playingPairOfPlayers.AddRange(playersTournamentList.Where(CurrentPlayingPairId => CurrentPlayingPairId.PlayingPairId == n));

                RunBattle(playingPairOfPlayers, playingPairOfPlayers[0].QtyCellsForWin, playingPairOfPlayers[0].MaxLengthFieldOfBattlefield, gui);
            }

            dbInteraction.PublishGameResults(playersTournamentList);
            FinishGame();
        }
Beispiel #2
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);
        }