public static int[] pieces(int numPieces) { //--- New version of pieces, which generates the shuffled bag to use in the game; it is //--- randomly generated instead of static, and includes the new diagonal directions. int[] a = new int[numPieces]; int randomPiece = 0; int maxBitPattern = (int)Math.Pow(2, 22) - 1; //by getting random bits of this size, we can make a whole mess of weird pieces. for (int i = 0; i < numPieces; i++) { bool pieceWanted = false; while (!pieceWanted) { // Make a random, but valid, path piece. Its piece number is the array index. randomPiece = Pieces.randomInt(0, maxBitPattern); randomPiece &= 0b1111000000000000111111; // zero out house #, person #, piece ID, etc. randomPiece = Pieces.setPieceNumber(randomPiece, i); // Throw out pieces that have various non-game-preferred attributes. These are aspects of game play that I might adjust after playing. int nm = Pieces.numberMoves(randomPiece); if (nm > 4) { continue; // no more than 4 directions } if (nm < 2) { continue; // No dead-end pieces } //-- Some code to alter proportions of pieces.... if (i < 3 * numPieces / 40) { randomPiece = Pieces.setAsLake(randomPiece); // 3 in every game are lakes. } else if (i < 20 * numPieces / 40) { if (nm != 2) { continue; } } // then pieces 4 through 20 are two-roads else if (i < 30 * numPieces / 40) { if (nm != 3) { continue; } } // 20-30 are three-roads else { if (nm != 4) { continue; } } // rest are 4-roads //-- not too many coins if (Pieces.rnd.NextDouble() < 0.3) { randomPiece = Pieces.takeCoins(randomPiece); } // 30% of the time coins get buried... //-- Below, we don't want very many pieces with, say, 3 straight lines and 1 diagonal. Two-move pieces with one straight and one diagonal are fine, and can be used to convert straight to diagonal. if (Pieces.numberMovesHV(randomPiece) == 1 && Pieces.numberMoves(randomPiece) > 2 && Pieces.rnd.NextDouble() > 0.75) { continue; } if (Pieces.numberMovesDiag(randomPiece) == 1 && Pieces.numberMoves(randomPiece) > 2 && Pieces.rnd.NextDouble() > 0.75) { continue; } //-- not too scrunched up of a piece int[] maxmin = Pieces.minMaxRoads(randomPiece); int mmDiff = Math.Abs(maxmin[0] - maxmin[1]); if (nm == 2 && (mmDiff < 2 || (maxmin[0] == 0 && maxmin[1] == 7))) { continue; } if (nm == 3 && (mmDiff < 5)) { continue; } // We like this piece pieceWanted = true; } a[i] = randomPiece; } Pieces.shuffleArray(a); return(a); }
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. }