// Eliminates a player // // Removes from active player list, adds to eliminated player list, // returns all tiles to the draw pile public void eliminatePlayer(string color) { SPlayer p = activePlayers.Find(x => color == x.getColor()); if (p == null) { throw new TsuroException("Cannot find an active player with color " + color + " to eliminate."); } if ((p.playerState != SPlayer.State.Placed) && (p.playerState != SPlayer.State.Playing)) { throw new TsuroException("Player is being eliminated before having placed a start pawn."); } // Add eliminated player's tiles to draw pile, and remove from his/her hand if (p.getHandSize() != 0) { List <Tile> hand = p.getHand(); for (int i = 0; i < hand.Count; i++) { Tile tempTile = hand[i]; p.removeTileFromHand(tempTile); addTileToDrawPile(tempTile); i--; } } // Eliminated player is dragon tile holder if (dragonTileHolder != null && dragonTileHolder.getColor() == p.getColor()) { // Get index of eliminated player in active players list int currIndex = activePlayers.FindIndex(x => p.getColor() == x.getColor());; // Pass dragon tile to next player with less than 3 tiles in hand SPlayer nextPlayer; do { currIndex += 1; nextPlayer = activePlayers[(currIndex) % activePlayers.Count]; } while (nextPlayer.getHandSize() >= 3); if (nextPlayer.getColor() == p.getColor()) { // Cannot find player with fewer than 3 tiles in hand dragonTileHolder = null; } else { dragonTileHolder = nextPlayer; } } eliminatedPlayers.Add(p); activePlayers.Remove(p); p.eliminate(); }
public void setStartPos00(Board board, SPlayer player) { Posn startPos = new Posn(-1, 0, 5); board.addPlayerToBoard(player.getColor(), startPos); player.playerState = SPlayer.State.Placed; }
/*************** GAME PLAY FUNCTIONS ****************************/ // Plays a game and returns a list of the winners // // Assumes all players have already been initialized public List <SPlayer> play(List <SPlayer> players) { initializeDrawPile("drawPilepaths.txt"); Board b = new Board(); activePlayers = players; // Set start positions foreach (SPlayer p in players) { p.placePawn(b); } dealTiles(players, b); // Continue game play until there are winners List <SPlayer> winners = null; while (winners == null) { SPlayer p = activePlayers[0]; Tile tileToPlay = p.playTurn(b, drawPile.Count); // Check for cheating player bool cheating = isCheating(p, b, tileToPlay); if (cheating) { // Replace cheating player with random player Console.WriteLine(p.getColor() + " was cheating - being replaced with random player."); RandomPlayer replacementPlayer = new RandomPlayer(); replacementPlayer.initialize(p.getColor(), getActivePlayerColors()); p.setStrategy(replacementPlayer); tileToPlay = p.playTurn(b, drawPile.Count); } TurnResult tr = playATurn(b, tileToPlay); // Update status of game, based on turn winners = tr.playResult; b = tr.b; } return(winners); }
public static XElement splayerToXML(SPlayer player, Admin a) { XElement splayerXML; if (a.getDragonTileHolder() != null && a.getDragonTileHolder().getColor() == player.getColor()) { splayerXML = new XElement("splayer-dragon"); } else { splayerXML = new XElement("splayer-nodragon"); } XElement handTileXML = playerHandToXML(player.getHand()); splayerXML.Add(new XElement("color", player.getColor()), handTileXML); return(splayerXML); }
public TurnResult playATurn(Board b, Tile t) { if (activePlayers.Count == 0) { throw new TsuroException("Cannot play turn - No more active players on the board"); } List <SPlayer> winners = null; SPlayer currentPlayer = activePlayers[0]; b.placeTile(currentPlayer.getColor(), t); // Move active players if newly placed tile affects them List <string> onEdgeColors = b.moveActivePlayers(getActivePlayerColors()); // Eliminate on edge players bool isCurrentPlayerEliminated = false; List <SPlayer> onEdgePlayers = new List <SPlayer>(); foreach (string playerColor in onEdgeColors) { eliminatePlayer(playerColor); onEdgePlayers.Add(getPlayer(playerColor)); if (playerColor == currentPlayer.getColor()) { isCurrentPlayerEliminated = true; } } // Check if game is over bool gameOver = false; if (activePlayers.Count == 0) { // If all active players eliminated in the same turn, they all are winners winners = onEdgePlayers; gameOver = true; } else if (b.getNumTilesOnBoard() == 35 || activePlayers.Count == 1) { // If all tiles played, all remaining players tie as winners // If only one active player left, he/she wins winners = activePlayers; gameOver = true; } if (gameOver) { return(new TurnResult(drawPile, activePlayers, eliminatedPlayers, b, winners)); } // Players draw if (dragonTileHolder != null) { drawTilesWithDragonHolder(); } else { if (!isCurrentPlayerEliminated) { if (drawPile.Count == 0) { dragonTileHolder = currentPlayer; } else { currentPlayer.addTileToHand(drawTile()); } } } // Update game play order if (!isCurrentPlayerEliminated) { activePlayers.RemoveAt(0); activePlayers.Add(currentPlayer); } // Compute turn result return(new TurnResult(drawPile, activePlayers, eliminatedPlayers, b, winners)); }
public bool legalPlay(SPlayer p, Board b, Tile t) { return(!b.isEliminationMove(p.getColor(), t) || (b.isEliminationMove(p.getColor(), t) && (p.allMovesEliminatePlayer(b, t)))); }
public bool isDragonHolder(string color) { return(dragonTileHolder.getColor() == color); }
public void setStartPos(Board board, SPlayer player, Posn pos) { board.addPlayerToBoard(player.getColor(), pos); player.playerState = SPlayer.State.Placed; }