コード例 #1
0
ファイル: DbModel.cs プロジェクト: PGVSNH20/ludo-game-ludo-2
        public static void SaveGame(List <Player> players, int currentPlayer, int saveId)
        {
            Game.Rules.SaveGameId = saveId;
            var context = new DbModel();

            //if (saveId < 0)
            //{
            context.SaveGames.Add(new SaveGame(currentPlayer));
            saveId = context.SaveGames.Count() + 1;
            context.Rules.Add(new Rules(Game.Rules.NumberOfPlayers, Game.Rules.PiecesPerPlayer, Game.Rules.ThrowAgainOnSixEnabled, Game.Rules.InitialSixRuleEnabled));

            for (var i = 0; i < players.Count; i++)
            {
                var player    = players[i];
                var newPlayer = new Player(Game.Rules.PiecesPerPlayer, players[i].Color, i, new Position(players[i].Row, players[i].Col));
                newPlayer.SaveGameId = saveId;

                for (var j = 0; j < player.Pieces.Count; j++)
                {
                    var piece    = player.Pieces[j];
                    var pos      = GameBoardGenerator.FindObject(Game.GameBoard, piece);
                    var newPiece = new GamePiece(j.ToString().First(), i);
                    newPiece.Row        = pos.Row;
                    newPiece.Col        = pos.Col;
                    newPiece.SaveGameId = saveId;

                    context.Pieces.Add(newPiece);
                }

                context.Players.Add(newPlayer);
            }

            //}
            //else
            //{
            //    //TODO: Update existing
            //}
            context.SaveChanges();
        }
コード例 #2
0
        /// <summary>
        /// Move a GamePiece and check if the move was valid.
        /// </summary>
        /// <param name="piece">GamePiece to move</param>
        /// <param name="diceRoll">How far to move (dice)</param>
        /// <returns>True if the move succeeded, else false</returns>
        public static bool MovePiece(GamePiece piece, int diceRoll)
        {
            Position position    = GameBoardGenerator.FindObject(Game.GameBoard, piece);
            Position newPosition = new Position();

            int             traversablePos = -1;
            List <Position> currentPath;

            var onInnerPath = piece.OnInnerPath;

            currentPath = piece.OnInnerPath ? Player.GetAllInnerPaths() : Game.Traversable;

            traversablePos = FindOnPath(currentPath, position, piece);

            if (traversablePos == -1)
            {
                //Couldn't find position
                return(false);
            }

            for (int i = 0; i < diceRoll; i++)
            {
                //TODO: Inner path code
                int offset = 0;
                var currentDicePosition = 1 + i;

                if (traversablePos + currentDicePosition >= currentPath.Count)
                {
                    offset = 0 - currentPath.Count;
                }

                var nextStep = currentPath[traversablePos + currentDicePosition + offset];

                bool finalStep = i == diceRoll - 1;

                if (!Movement.TryMove(piece, nextStep, finalStep)) // Om TryMove returnerar false misslyckas move, GamePiece kan inte flyttas
                {
                    //Move failed

                    return(false);
                }

                if (piece.OnInnerPath && !onInnerPath)
                {
                    currentPath = Player.GetAllInnerPaths();

                    diceRoll           -= currentDicePosition;
                    currentDicePosition = 0;
                    offset = 0;
                    i      = -1;

                    traversablePos = (currentPath.Count / Game.Rules.NumberOfPlayers) * piece.PlayerId;

                    onInnerPath = true;
                }
                //TODO: If final move is not valid, don't move at all
                newPosition = currentPath[traversablePos + currentDicePosition + offset];
            }
            //Place piece on new position and set the old position to the original value when we created the board
            Game.GameBoard[newPosition.Row, newPosition.Col] = piece;
            Game.GameBoard[position.Row, position.Col]       = Game.OriginalGameBoard[position.Row, position.Col];
            return(true);
        }