Ejemplo n.º 1
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.º 2
0
        public void MovePiece(GamePlayer player, GameDice dice, GamePiece piece)
        {
            if (player.Turn == true && player.CanMove == true)
            {
                if (player.Color == "Red")
                {
                    if (piece.InPlay == true)
                    {
                        if (piece.Position == 0 && dice.Value == 6)
                        {
                            piece.Position = 40;
                        }
                        else if (piece.Position >= 40 && piece.Position <= 52)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position > 52)
                            {
                                int x = piece.Position - 52;
                                piece.Position  = 0;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 1 && piece.Position <= 38)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position > 38)
                            {
                                int x = piece.Position - 38;
                                piece.Position  = 52;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 53 && piece.Position <= 58)
                        {
                            piece.Position += dice.Value;
                        }

                        if (piece.Position > 58)
                        {
                            int x = ((piece.Position) - 58);
                            piece.Position = (58 - x);
                        }
                        else if (piece.Position == 58)
                        {
                            //win condition
                            piece.InPlay = false;
                        }
                    }
                }

                if (player.Color == "Green")
                {
                    if (piece.InPlay == true)
                    {
                        if (piece.Position == 0 && dice.Value == 6)
                        {
                            piece.Position = 1;
                        }
                        else if (piece.Position >= 1 && piece.Position <= 51)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position >= 52)
                            {
                                int x = piece.Position - 51;
                                piece.Position  = 52;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 53 && piece.Position <= 58)
                        {
                            piece.Position += dice.Value;
                        }

                        if (piece.Position > 58)
                        {
                            int x = ((piece.Position) - 58);
                            piece.Position = (58 - x);
                        }
                        else if (piece.Position == 58)
                        {
                            //win condition
                            piece.InPlay = false;
                        }
                    }
                }

                if (player.Color == "Yellow")
                {
                    if (piece.InPlay == true)
                    {
                        if (piece.Position == 0 && dice.Value == 6)
                        {
                            piece.Position = 27;
                        }
                        else if (piece.Position >= 27 && piece.Position <= 52)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position > 52)
                            {
                                int x = piece.Position - 52;
                                piece.Position  = 0;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 1 && piece.Position <= 25)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position > 25)
                            {
                                int x = piece.Position - 25;
                                piece.Position  = 52;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 53 && piece.Position <= 58)
                        {
                            piece.Position += dice.Value;
                        }

                        if (piece.Position > 58)
                        {
                            int x = ((piece.Position) - 58);
                            piece.Position = (58 - x);
                        }
                        else if (piece.Position == 58)
                        {
                            //win condition
                            piece.InPlay = false;
                        }
                    }
                }

                if (player.Color == "Blue")
                {
                    if (piece.InPlay == true)
                    {
                        if (piece.Position == 0 && dice.Value == 6)
                        {
                            piece.Position = 14;
                        }
                        else if (piece.Position >= 14 && piece.Position <= 52)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position > 52)
                            {
                                int x = piece.Position - 52;
                                piece.Position  = 0;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 1 && piece.Position <= 12)
                        {
                            piece.Position += dice.Value;
                            if (piece.Position > 12)
                            {
                                int x = piece.Position - 12;
                                piece.Position  = 52;
                                piece.Position += x;
                            }
                        }
                        else if (piece.Position >= 53 && piece.Position <= 58)
                        {
                            piece.Position += dice.Value;
                        }

                        if (piece.Position > 58)
                        {
                            int x = ((piece.Position) - 58);
                            piece.Position = (58 - x);
                        }
                        else if (piece.Position == 58)
                        {
                            //win condition
                            piece.InPlay = false;
                        }
                    }
                }
            }
        }