Ejemplo n.º 1
0
        public void Given_1_Game_In_Database_Expect_1_Game_From_Database()
        {
            //Arange
            var LudoGames = new List <LudoGame>()
            {
                new LudoGame()
                {
                    LudoGameId = 1, GameName = "testGame", Created = DateTime.Now, Status = "Created"
                }
            };

            //Act
            var input = new StringReader("1");

            Console.SetIn(input);

            var output = new StringWriter();

            Console.SetOut(output);

            var expectedOutput =
                $"Choose game from list:\r\n" +
                $"1) {LudoGames[0].GameName} (id: {LudoGames[0].LudoGameId}), created: {LudoGames[0].Created.ToString("yyyy:MM:dd")}, status: {LudoGames[0].Status}\r\n";
            var returnGame = InputDialogs.GetLudoGame(LudoGames);

            //Assert
            Assert.AreEqual(expectedOutput, output.ToString());
            //Assert.AreEqual(GamePieces[12], gamePiece);
        }
Ejemplo n.º 2
0
        public GameRunner CreateNewGame()
        {
            // Player chooses amount of players

            Game = new LudoGame()
            {
                Created = DateTime.Now
            };
            Game.GamePlayers.Players = new List <GamePlayer>();
            Game.Moves    = new List <GameMove>();
            Game.Status   = "Created";
            Game.GameName = InputDialogs.GetGameName();
            int playerAmount = InputDialogs.GetPlayerAmount();

            Game.GamePlayers.Players = InputDialogs.GetPlayers(playerAmount);

            int startingPlayerIndex = new Random().Next(0, Game.GamePlayers.Players.Count);

            Game.NextPlayer = Game.GamePlayers.Players[startingPlayerIndex];

            Game.PieceSetup = Tools.GetGamePieceSetup(Game.GamePlayers.Players);
            Board.UpdateBoardBases(Game.PieceSetup);

            if (DbConnectionIsActive)
            {
                var db = new LudoGameDbContext();
                SaveNewGameTask = new Task(() => SaveGameToDataBase(db));
                SaveNewGameTask.Start();
            }

            return(this);
        }
        public void Given_WrongINputs_Expect_()
        {
            var input = new StringReader(
                $"5\r\n" +
                $"1\r\n" +
                $"abc\r\n" +
                $"3");

            Console.SetIn(input);

            var output = new StringWriter();

            Console.SetOut(output);

            var expectedOutput =
                $"Choose how many players (2, 3 or 4): \r\n" +
                $"Choose between 2 and 4\r\n" +
                $"Choose how many players (2, 3 or 4): \r\n" +
                $"Choose between 2 and 4\r\n" +
                $"Choose how many players (2, 3 or 4): \r\n" +
                $"Input not accepted. Choose between 2 and 4\r\n" +
                $"Choose how many players (2, 3 or 4): \r\n" +
                $"3 players will play!\r\n";

            var playerAmount = InputDialogs.GetPlayerAmount();

            Assert.AreEqual(expectedOutput, output.ToString());
            Assert.AreEqual(3, playerAmount);
        }
Ejemplo n.º 4
0
        private void NewTopLevelComponentButton_Click(object sender, EventArgs e)
        {
            string input  = "";
            var    result = InputDialogs.String(ref input, "Введите имя компонента");

            if (result == DialogResult.OK)
            {
                var repo = new ComponentsRepo();

                var componentExists = repo.Components.Count(c => c.Name == input) > 0;
                if (componentExists)
                {
                    MessageBox.Show("Компонент уже существует", "Ошибка");
                }
                else
                {
                    var newComponent = new Component {
                        Name = input, IsTopLevel = true
                    };
                    repo.Components.Add(newComponent);

                    RefreshView();
                }
            }
        }
Ejemplo n.º 5
0
        private void RenameComponent_Click(object sender, EventArgs e)
        {
            var selectedNode = MainView.SelectedNode;

            if (selectedNode == null)
            {
                MessageBox.Show("Ни один компонент не выбран.", "Ошибка");
                return;
            }

            var repo = new ComponentsRepo();

            var selectedNodeId    = (int)selectedNode.Tag;
            var selectedComponent = repo.Components
                                    .Find(c => c.Id == selectedNodeId);

            DialogResult result;
            bool         componentNameExistsInDatabase;
            string       input = "";

            do
            {
                if (selectedComponent != null)
                {
                    input = selectedComponent.Name;
                }

                result = InputDialogs.String(ref input, "Введите новое имя");

                var componentByName = repo.Components.
                                      Find(c => c.Name == input);

                componentNameExistsInDatabase = (componentByName != null);

                if ((result == DialogResult.OK) && componentNameExistsInDatabase)
                {
                    MessageBox.Show("Компонент с таким именем (" +
                                    componentByName.Name + "@" + componentByName.Id + ") уже существует в базе",
                                    "Ошибка");
                }
            } while (result == DialogResult.OK && componentNameExistsInDatabase);

            if (result == DialogResult.OK)
            {
                if (selectedComponent != null)
                {
                    selectedComponent.Name = input;

                    repo.Components.Update(selectedComponent, selectedComponent.Id);
                }


                RefreshView();
            }
        }
        public void Given_Input4_Expect_Int4()
        {
            var input = new StringReader("4");

            Console.SetIn(input);

            var output = new StringWriter();

            Console.SetOut(output);

            var playerAmount = InputDialogs.GetPlayerAmount();

            Assert.AreEqual(4, playerAmount);
            Assert.AreEqual("Choose how many players (2, 3 or 4): \r\n4 players will play!\r\n", output.ToString());
        }
Ejemplo n.º 7
0
        public GameRunner LoadGameFromDataBase()
        {
            if (DbConnectionIsActive)
            {
                var db          = new LudoGameDbContext();
                var allGames    = LoadAllGamesFromDataBase(db);
                var oneLudoGame = InputDialogs.GetLudoGame(allGames);
                LoadGameFromDatabase(oneLudoGame, db);
                Board.UpdateTracks(Game.PieceSetup);
            }
            else
            {
                Console.WriteLine("Db connections is not active");
            }

            return(this);
        }
Ejemplo n.º 8
0
        public void Given_Name_From_Input_Expect_Name_As_Output()
        {
            // Arrange
            var input = new StringReader("abc");

            Console.SetIn(input);

            var output = new StringWriter();

            Console.SetOut(output);

            var expectedOutput =
                $"Name The Game: \r\n";

            // Act
            var result = InputDialogs.GetGameName();

            // Assert
            Assert.AreEqual(expectedOutput, output.ToString());
            Assert.AreEqual(result, "abc");
        }
        public void Given_Options_2PiecToChose_and_1Chosen()
        {
            var color      = (GameColor)0;
            var diceResult = 6;

            var input = new StringReader("2");

            Console.SetIn(input);

            var output = new StringWriter();

            Console.SetOut(output);

            var expectedOutput =
                $"Choose your game piece:\r\n" +
                $"1) Piece number: 1 at base\r\n" +
                $"2) Piece number: 4 at position 16\r\n";

            var gamePiece = InputDialogs.GetGamePieceToMove(GamePieces, color, diceResult);

            Assert.AreEqual(expectedOutput, output.ToString());
            Assert.AreEqual(GamePieces[3], gamePiece);
        }
Ejemplo n.º 10
0
        public void Given_Information_Expect_3Players()
        {
            var palyerAmount = 3;
            var input        = new StringReader(
                $"Bob\r\n" +
                $"2\r\n" +
                $"1\r\n" +
                $"Rob\r\n" +
                $"0\r\n" +
                $"3\r\n" +
                $"Bil\r\n" +
                $"99\r\n" +
                $"abc\r\n" +
                $"5\r\n" +
                $"\r\n");

            Console.SetIn(input);

            var output = new StringWriter();

            Console.SetOut(output);

            var expectedOutput =
                $"Player 1 choose a name: \r\n" +
                $"Choose type for Bob: \r\n" +
                $"1) Human\r\n" +
                $"2) Robot\r\n" +
                $"Bob choose a color:\r\n" +
                $"1) Blue\r\n" +
                $"2) Red\r\n" +
                $"3) Yellow\r\n" +
                $"4) Green\r\n" +
                $"Player 2 choose a name: \r\n" +
                $"Choose type for Rob: \r\n" +
                $"1) Human\r\n" +
                $"2) Robot\r\n" +
                $"Rob choose a color:\r\n" +
                $"1) Red\r\n" +
                $"2) Yellow\r\n" +
                $"3) Green\r\n" +
                $"Player 3 choose a name: \r\n" +
                $"Choose type for Bil: \r\n" +
                $"1) Human\r\n" +
                $"2) Robot\r\n" +
                $"Bil choose a color:\r\n" +
                $"1) Red\r\n" +
                $"2) Yellow\r\n" +
                $"Input not accepted, choose an available color\r\n" +
                $"Input not accepted, choose an available color\r\n";

            var players = InputDialogs.GetPlayers(palyerAmount);

            Assert.AreEqual(3, players.Count);
            Assert.AreEqual(expectedOutput, output.ToString());
            Assert.AreEqual("Bob", players[0].Name);
            Assert.AreEqual("Bil", players[1].Name);
            Assert.AreEqual("Rob", players[2].Name);
            Assert.AreEqual((GameColor)0, players[0].Color);
            Assert.AreEqual((GameColor)1, players[1].Color);
            Assert.AreEqual((GameColor)3, players[2].Color);
        }
Ejemplo n.º 11
0
        public void PlayGame()

        {
            if (Game.GamePlayers.Players.FindAll(p => p.Type == (PlayerType)1).Count > 0)
            {
                AI = new GameAI(Board, Game.PieceSetup, Dice);
            }
            var alive = true;

            while (alive && Game.Winer == null)
            {
                var playerChoise = InputDialogs.GetPlayerMenuChoise(Game, Board);

                switch (playerChoise)
                {
                case "1":

                    Dice.ThrowDice(Game.NextPlayer.Color);     //animation (true if color param)
                    GamePiece gamePieceToMove = null;

                    if (Game.NextPlayer.Type == (PlayerType)1)
                    {
                        gamePieceToMove = AI.ChoosePieceToMove(Game.NextPlayer.Color, Dice.Result);
                    }
                    else
                    {
                        gamePieceToMove = InputDialogs.GetGamePieceToMove(Game.PieceSetup, Game.NextPlayer.Color, Dice.Result);
                    }

                    CreateMove(gamePieceToMove);
                    if (Game.Moves.Last().Piece != null)
                    {
                        ExecuteMove();
                    }
                    break;

                case "2":
                    FileHandler.WriteToFile($"{Game.GameName}_{Game.LudoGameId}", Game);
                    Console.WriteLine($"Game saved to file {Game.GameName}_{Game.LudoGameId}.json");
                    Console.ReadKey();
                    break;

                case "3":
                    //Spel är uppdaterat i basen
                    alive = false;
                    break;
                }
                if (alive)
                {
                    if (Dice.Result != 6)
                    {
                        var currentPlayerIndex  = Game.GamePlayers.Players.IndexOf(Game.NextPlayer);
                        var nextTurnPlayerIndex = (currentPlayerIndex + 1) % (Game.GamePlayers.Players.Count());
                        Game.NextPlayer = Game.GamePlayers.Players[nextTurnPlayerIndex];
                    }
                    else
                    {
                        Console.WriteLine("Congratulations you can roll again!");
                        if (Game.NextPlayer.Type == (PlayerType)1)
                        {
                            Thread.Sleep(1000);
                        }
                        else
                        {
                            Console.ReadKey();
                        }
                    }
                    if (DbConnectionIsActive)
                    {
                        var db = new LudoGameDbContext();
                        DbTransactionWrapper(db, db => SaveMoveToDataBase(db));
                    }
                }
            }
            if (Game.Winer != null)
            {
                Console.WriteLine($"{Game.Winer} wins!");
                Console.ReadKey();
            }
        }
Ejemplo n.º 12
0
        private void RemoveComponent_Click(object sender, EventArgs e)
        {
            var selectedNode = MainView.SelectedNode;

            if (selectedNode == null)
            {
                MessageBox.Show("Ни один компонент не выбран.", "Ошибка");
                return;
            }

            var repo = new ComponentsRepo();

            var selectedNodeId    = (int)selectedNode.Tag;
            var selectedComponent = repo.Components.Find(c => c.Id == selectedNodeId);

            if (selectedComponent == null)
            {
                return;
            }

            if (!selectedComponent.IsTopLevel)
            {
                var parentNodeId    = (int)selectedNode.Parent.Tag;
                var parentComponent = repo.Components.Get(parentNodeId);

                var result = InputDialogs.RemoveChoice(
                    "Хотите ли вы разорвать связь между текущим компонентом (" +
                    selectedComponent + ") и его родителем (" +
                    parentComponent + ") " +
                    "или только удалить текущий узел?");

                if (result == DialogResult.Cancel)
                {
                    return;
                }

                if (result == DialogResult.Yes)
                {
                    var linkToBroke = repo.ComponentLinks
                                      .Find(cl =>
                                            cl.ParentComponentId == parentComponent.Id &&
                                            cl.ChildComponentId == selectedComponent.Id
                                            );

                    if (linkToBroke != null)
                    {
                        repo.ComponentLinks.Delete(linkToBroke);
                    }
                }
            }

            RemoveSubComponents(selectedComponent);

            var componentCount = GetComponentCountInTree(selectedComponent);

            if (componentCount == 1)
            {
                var uplink = repo.ComponentLinks
                             .Find(cl => cl.ChildComponentId == selectedComponent.Id);
                if (uplink != null)
                {
                    repo.ComponentLinks.Delete(uplink);
                }
                repo.Components.Delete(selectedComponent);
            }
            else
            {
                TreeNode parentNode      = selectedNode.Parent;
                var      componentsChain = new List <Component>();
                do
                {
                    var parentComponent = repo.Components.Get((int)parentNode.Tag);
                    var parentCount     = GetComponentCountInTree(parentComponent);

                    componentsChain.Add(parentComponent);

                    if (parentCount == 1)
                    {
                        break;
                    }

                    parentNode = parentNode.Parent;
                } while (true);

                var newComponentsChain = new List <Component>();
                for (int i = 0; i < componentsChain.Count - 1; i++)
                {
                    var oldComponent = componentsChain[i];

                    var newComponent = new Component
                    {
                        Name       = oldComponent.Name,
                        IsTopLevel = oldComponent.IsTopLevel
                    };
                    repo.Components.Add(newComponent);
                    newComponentsChain.Add(newComponent);
                }

                if (componentsChain.Count == 1)
                {
                    var parentId = componentsChain[0].Id;
                    var childId  = selectedComponent.Id;
                    var link     = repo.ComponentLinks
                                   .Find(cl =>
                                         cl.ParentComponentId == parentId &&
                                         cl.ChildComponentId == childId
                                         );
                    if (link != null)
                    {
                        repo.ComponentLinks.Delete(link);
                    }
                }
                else
                {
                    var chainRootId       = componentsChain[componentsChain.Count - 1].Id;
                    var chainFirstChildId = componentsChain[componentsChain.Count - 2].Id;
                    var linkToUpdate      = repo.ComponentLinks.
                                            Find(cl =>
                                                 cl.ParentComponentId == chainRootId &&
                                                 cl.ChildComponentId == chainFirstChildId);
                    if (linkToUpdate != null)
                    {
                        linkToUpdate.ChildComponentId = newComponentsChain[newComponentsChain.Count - 1].Id;
                    }

                    for (int i = componentsChain.Count - 1; i >= 1; i--)
                    {
                        if (i != componentsChain.Count - 1)
                        {
                            var oldLinkParentId = componentsChain[i].Id;
                            var oldLinkChildId  = componentsChain[i - 1].Id;

                            var oldLink = repo.ComponentLinks
                                          .Find(cl =>
                                                cl.ParentComponentId == oldLinkParentId &&
                                                cl.ChildComponentId == oldLinkChildId
                                                );

                            var oldLinkQuantity = -1;
                            if (oldLink != null)
                            {
                                oldLinkQuantity = oldLink.Quantity;
                            }

                            var newChainLink = new ComponentLink
                            {
                                ParentComponentId = newComponentsChain[i].Id,
                                ChildComponentId  = newComponentsChain[i - 1].Id,
                                Quantity          = oldLinkQuantity
                            };
                            repo.ComponentLinks.Add(newChainLink);
                        }

                        var restoreExceptionId =
                            (i == 1) ? selectedComponent.Id :
                            componentsChain[i - 2].Id;

                        var oldParentId      = componentsChain[i - 1].Id;
                        var oldChildrenLinks = repo.ComponentLinks
                                               .FindAll(cl => cl.ParentComponentId == oldParentId &&
                                                        cl.ChildComponentId != restoreExceptionId)
                                               .ToList();
                        foreach (var oldChildLink in oldChildrenLinks)
                        {
                            var restoredOldLink = new ComponentLink
                            {
                                ParentComponentId = newComponentsChain[i - 1].Id,
                                ChildComponentId  = oldChildLink.ChildComponentId,
                                Quantity          = oldChildLink.Quantity
                            };
                            repo.ComponentLinks.Add(restoredOldLink);
                        }
                    }
                }
            }

            RefreshView();
        }
Ejemplo n.º 13
0
        private void NewEmbeddedComponentButton_Click(object sender, EventArgs e)
        {
            var selectedNode = MainView.SelectedNode;

            if (selectedNode == null)
            {
                MessageBox.Show("Ни один компонент не выбран.", "Ошибка");
                return;
            }

            string  input = "";
            int     componentId;
            decimal quantity = 0;

            var repo = new ComponentsRepo();

            var embeddedComponents = repo.Components.FindAll(c => !c.IsTopLevel).ToList();

            var selectedComponentId = (int)selectedNode.Tag;
            var selectedComponent   = repo.Components
                                      .Find(c => c.Id == selectedComponentId);

            DialogResult result;
            bool         circularLinkFound = false;

            do
            {
                componentId = -1;

                result = InputDialogs.ComponentNameAndQuantity(
                    ref input, ref componentId, ref quantity,
                    "Введите имя компонента и количество", embeddedComponents);

                if (result == DialogResult.OK)
                {
                    circularLinkFound = componentId != -1 && CheckUplinks(selectedComponentId, componentId);
                }

                if ((result == DialogResult.OK) && circularLinkFound)
                {
                    MessageBox.Show("Обнаружено рекурсиное вложение компонентов", "Ошибка");
                }
            } while (result == DialogResult.OK && circularLinkFound);

            if (result == DialogResult.OK)
            {
                ComponentLink existingLink = null;

                Component newComponent;

                if (componentId == -1)
                {
                    newComponent = new Component {
                        Name = input
                    };
                    repo.Components.Add(newComponent);
                }
                else
                {
                    newComponent = repo.Components.Find(c => c.Id == componentId);

                    existingLink = repo.ComponentLinks
                                   .Find(cl =>
                                         cl.ParentComponentId == selectedComponentId &&
                                         cl.ChildComponentId == componentId);
                }

                if (existingLink != null)
                {
                    existingLink.Quantity += (int)quantity;
                }
                else
                {
                    if ((selectedComponent != null) && (newComponent != null))
                    {
                        var newLink = new ComponentLink
                        {
                            ParentComponentId = selectedComponent.Id,
                            ChildComponentId  = newComponent.Id,
                            Quantity          = (int)quantity
                        };
                        repo.ComponentLinks.Add(newLink);
                    }
                }

                RefreshView();
            }
        }