コード例 #1
0
        // Static method for creating a piece (which is of course actually an integer)
        public static int createPiece(bool up, bool down, bool left, bool right, bool silver, bool gold, int pieceNumber, int personNumber, int houseNumber)
        {
            int p = 0;

            p  = Pieces.setDirections(p, up, down, left, right);
            p  = Pieces.setCoins(p, silver, gold);
            p |= (pieceNumber << 6);
            p |= (personNumber << 12);
            p |= (houseNumber << 15);
            return(p);
        }
コード例 #2
0
        public int playPieceAt(int p, int x, int y)
        {
            //  Plays the valid piece P at (x,y) on the board.  Returns the Success piece on regular success
            //  (with the board updated), the Failure piece if the move is invalid, or, if a person has
            //  begun moving, it returns a pure Person piece to indicate which person is moving.
            //  If you get a failure piece, you can get an error message using getLastErrorMessage.
            Coordinates c = new Coordinates(x, y);

            // check if there are no moves left
            if (this._movingPerson && this.personMovesLeft <= 0)
            {
                return(this.setFailure("You have no moves left for your person."));
            }
            if (!this.piecesLeftThisTurn())
            {
                return(this.setFailure("You have no pieces left for this turn."));
            }
            // check for valid coordinates, retrieve the current piece on the board.
            if (!this.currentBoard.isValidCoords(c))
            {
                return(this.setFailure("Internal Error: invalid coordinates"));
            }
            int currentPiece = this.currentBoard.getCell(c);

            // check game rules and place piece based on which kind it is.
            if (Pieces.isPerson(p) && this._movingPerson)
            {
                // Playing a single step in moving a person.  First, see if there is a path to move the person.
                // (The situation of having no moves left was already handled up top.)
                int  sourcePiece      = this.currentBoard.getCell(this.personCoordinates);
                int  thisPersonPiece  = Pieces.getPersonPieceFrom(sourcePiece);
                int  thisPersonNumber = Pieces.personNumber(thisPersonPiece);
                int  goalPiece        = this.currentBoard.createGoalPiece(thisPersonNumber, 0, c);
                bool reachedGoal      = (goalPiece == currentPiece);
                if (Pieces.personNumber(currentPiece) > 0)
                {
                    return(this.setFailure("There can be only one person in a square at a time."));
                }
                if (!Pieces.piecesConnect(this.currentBoard.getCell(this.personCoordinates), currentPiece, this.personCoordinates.x(), this.personCoordinates.y(), x, y))
                {
                    return(this.setFailure("There is no path for the person to get to that square.  Make sure to move your person only one square at a time."));
                }
                //-- it appears that we now have a valid move.
                //-- move the person
                sourcePiece  = Pieces.setPersonNumber(sourcePiece, 0);
                currentPiece = Pieces.setPersonNumber(currentPiece, thisPersonNumber);
                this.currentBoard.setCell(this.personCoordinates, sourcePiece);
                this.personMovesLeft--;
                this.personCoordinates = c;
                //-- check for coins
                if (Pieces.gold(currentPiece) || Pieces.silver(currentPiece))
                {
                    if (Pieces.gold(currentPiece))
                    {
                        this.incrementScore(this.pointsForGold());
                    }
                    if (Pieces.silver(currentPiece))
                    {
                        this.incrementScore(this.pointsForSilver());
                    }
                    currentPiece = Pieces.setCoins(currentPiece, false, false);
                    this.currentBoard.setCell(c, currentPiece);
                }
                //-- check for reaching goal
                if (reachedGoal)
                {
                    int thisZeroBasedPersonNumber = thisPersonNumber - 1;
                    this.incrementScore(this.personScores[thisZeroBasedPersonNumber]);   // DEBUG: line where Nancy's game crashed 9/1 8:20pm: probably due to the zero-based/one-based thing, probably she got her purple person to the goal which caused the crash.
                    this.personScores[thisZeroBasedPersonNumber]--;
                    if (this.personScores[thisZeroBasedPersonNumber] < 0)
                    {
                        this.personScores[thisZeroBasedPersonNumber] = 0;
                    }
                    //-- check: have we won by finding all the goals?
                    if (this.currentBoard.isWinningBoard())
                    {
                        this.determineWinners();
                    }
                }
                //-- now our piece is ready to store and we can return.
                this.currentBoard.setCell(c, currentPiece);
                return(Pieces.createSuccessPiece());
            }
            else if (Pieces.isPerson(p) || Pieces.isHouse(p))
            {
                // Playing a person piece or house piece when you're not moving a person.
                if (!this.currentBoard.isOnEdge(c))
                {
                    return(this.setFailure("Person and house pieces can only be placed on the edges."));                                 // person piece has to be placed on edge
                }
                if (this.currentBoard.isOnCorner(c))
                {
                    return(this.setFailure("You can't place person and house pieces on the corners."));
                }
                if (this.currentBoard.distanceToPartner(p, c) < 5)
                {
                    return(this.setFailure("The person piece and the house piece of each color have to be at least 5 spaces apart."));                                                // person piece can't be too close to house piece
                }
                if (!Pieces.isGreenGrassPiece(currentPiece))
                {
                    return(this.setFailure("Person and house pieces can only be placed on green grass."));                                          // have to put house or person on green grass.
                }
                // combine the pieces and store the result.  Also add a path for the person or house.
                int newPiece = Pieces.combinePieces(currentPiece, p);
                if (Pieces.isFailurePiece(newPiece))
                {
                    return(this.setFailure("There was an unexpected problem."));
                }
                newPiece = this.currentBoard.setGoalDirections(newPiece, c);
                this.currentBoard.setCell(c, newPiece);
                this.setCurrentPiecePlayed();
                return(Pieces.createSuccessPiece());
            }
            else
            {
                // Playing a regular piece.  If you play it against an existing piece with a person on it,
                // it starts person-moving mode.  If you play it on a blank square, it places the piece.
                // Other moves are invalid.
                if (Pieces.personNumber(currentPiece) > 0)
                {
                    // Begin person moving mode
                    this._movingPerson     = true;
                    this.personCoordinates = c;
                    this.personMovesLeft   = Pieces.numberMoves(p);
                    this.setCurrentPiecePlayed();
                    return(Pieces.createSuccessPiece());
                }
                else if (this.currentBoard.isOnEdge(c))     //-- note: you CAN be on an edge if you're moving a person, see above.
                {
                    return(this.setFailure("You can't play a path piece on the edge of the board."));
                }
                else if (Pieces.isBlank(currentPiece))
                {
                    // Placing tile in blank space
                    this.currentBoard.setCell(c, p);
                    this.setCurrentPiecePlayed();
                    return(Pieces.createSuccessPiece());
                }
                else
                {
                    return(this.setFailure("You can't move there."));
                }
            }
            // Shouldn't get here, and indeed, the compiler says you can't get here.
        }