コード例 #1
0
ファイル: GameLoop.cs プロジェクト: nanilevay/lp1_18ghosts
        /// <summary>
        /// this method gets a ghost, its position and a new position to where
        /// the player wants to move it, and updates the portals if there's
        /// been a battle between ghosts. it doesn't return anything.
        /// </summary>
        /// <param name="piece"> the current ghost type</param>
        /// <param name="newPos"> the position to which it's been moved</param>
        /// <param name="oldPos"> the position it was originally in</param>
        private static void MovePiece(BoardPiece piece, Position newPos, Position oldPos)
        {

            // placing the piece in the new position and updating the board
            board.pieces[newPos.Row, newPos.Col] = 
                GameBoard.GetBoardSettings(newPos);
            board.pieces[oldPos.Row, oldPos.Col] = piece;

            // send piece information to UpdatePortal to check if rotation is due

            board.pieces[newPos.Row, newPos.Col] = GameBoard.GetBoardSettings(newPos);
            board.pieces[oldPos.Row, oldPos.Col] = piece;

            board.UpdatePortal(piece.color);
        }
コード例 #2
0
ファイル: GameLoop.cs プロジェクト: nanilevay/lp1_18ghosts
        /// <summary>
        /// this method returns nothing. it's purpose is to update the board
        /// </summary>
        private static void Update()
        {

            // if the dungeon has any ghost belonging to this player
            if (board.CountdungeonGhostsForPlayer(currentPlayer) > 0)
            {
                Console.WriteLine("Do you wish to move(m) or revive(r) " +
                    "a ghost in the dungeon?");
                // if player chose to revive
                if (Console.ReadLine().ToUpper() == "R")
                {
                    // get the ghost's position from the dungeon
                    Console.WriteLine("Input ghost's dungeon coordinates");

            if (board.CountdungeonGhostsForPlayer(currentPlayer) > 0)
            {
                Console.WriteLine("Quer mover ou ressuscitar um fantasma? (R/F)");
                if (Console.ReadLine().ToUpper() == "R")
                {
                    Console.WriteLine("Que fantasma que ressuscitar?");

                    Ghosts ghost = board.GetDungeonGhost(Console.ReadLine());

                    if (ghost != null)
                    {
                        BoardPiece fPiece = null;
                        Position fPos = null;


                        // ask player to place ghost on the board from dungeon
                        do
                        {
                            // get input on board
                            Console.WriteLine("Input position on board");

                        do
                        {
                            Console.WriteLine("Que posição quer por o fantasma?");

                            fPos = Player.GetPosition(board);
                            fPiece = board.GetPiece(fPos);
                        }
                        while (fPiece is Ghosts || fPiece is Portals);


                        // place ghost back on board
                        board.pieces[fPos.Row, fPos.Col] = ghost;

                        // remove dungeon ghost

                        board.pieces[fPos.Row, fPos.Col] = ghost;

                        board.dungeonGhosts.Remove(ghost);

                        board.UpdatePortal(ghost.color);
                    }

                    return;
                }
            }
           
            // set the default pieces to null to use and replace them later
            BoardPiece piece = null;

            // get a variable of Position type to simplify use
            Position pos;

            // to ask the player which ghost they want to move and getting it
            do
            {
                Console.WriteLine("Que fantasma quer mover?");
                pos = Player.GetPosition(board);
                piece = board.GetPiece(pos);
            }
            // do while piece belongs to the player
            while (!(piece is Ghosts ghost && ghost.player == currentPlayer));

            // auxiliary piece to check types of corridors that aren't ghosts
            BoardPiece auxPiece;
            Position auxPosition; 
            bool isValidPosition;
            
            // declare the new piece's values as null to use for different types
            auxPiece = null;
            auxPosition = null;
            isValidPosition = false;

            // ask player where to move the piece and check for it's validity
            do
            {

                // get coordinates of new position


                Console.WriteLine("Where do you want to move it to?");

                auxPosition = Player.GetPosition(board);
                auxPiece = board.GetPiece(auxPosition);

                // check if the movement is within possible moves

                uint abs1 = (uint)Math.Abs(auxPosition.Row - pos.Row);
                uint abs2 = (uint)Math.Abs(auxPosition.Col - pos.Col);

                isValidPosition = (abs1 <= 1 && abs2 <= 1 && abs1 + abs2 <= 1);
            }
            while (auxPiece is Portals || !isValidPosition);


            // if the piece moved to is a ghost

            if (auxPiece is Ghosts ghosts)
            {
                if (((Ghosts)piece).checkWinner(ghosts))
                {
                    MovePiece(piece, pos, auxPosition);
                    board.OnPieceLost(auxPiece);
                }
                else
                {
                    board.pieces[pos.Row, pos.Col] = GameBoard.GetBoardSettings(pos);
                    board.OnPieceLost(piece);
                }
            }

            // if moved to mirror

            else if (auxPiece is Mirror mirror)
            {
                BoardPiece auxMirror = null;
                Position mirrorPos = null;

                uint mirrorCount = board.CountMirrors();

                
                // if player is in a mirror

                if (mirrorCount > 1)
                {
                    do
                    {

                        // ask what mirror to move to and check validity
                        Console.WriteLine("Which Mirror to teleport to?");

                        Console.WriteLine("Para que espelho que ir?");

                        mirrorPos = Player.GetPosition(board);
                        auxMirror = board.GetPiece(mirrorPos);

                    } while (!(auxMirror is Mirror));


                    // setting the new position
                    board.pieces[pos.Row, pos.Col] = 
                        GameBoard.GetBoardSettings(pos);
                    board.pieces[mirrorPos.Row, mirrorPos.Col] = piece;

                    // update portal rotation if necessary

                    board.pieces[pos.Row, pos.Col] = GameBoard.GetBoardSettings(pos);
                    board.pieces[mirrorPos.Row, mirrorPos.Col] = piece;


                    board.UpdatePortal(piece.color);
                }
                else
                    MovePiece(piece, pos, auxPosition);
            }
            else
                MovePiece(piece, pos, auxPosition);
        }
    }
}