Esempio n. 1
0
        public TurnResult playATurn(Board b, Tile t)
        {
            // Place tile on board
            SPlayer currentPlayer = b.getFirstActivePlayer();

            b.placeTile(currentPlayer, t);

            // Move active players if newly placed tile affects them
            b.movePlayers();
            bool isCurrentPlayerEliminated = b.isEliminated(currentPlayer.returnColor());

            // Check if game is over
            if (b.isGameOver())
            {
                return(b.GetTurnResult());
            }

            // Players draw
            if (b.existsDragonTileHolder())
            {
                b.drawTilesWithDragonHolder();
            }
            else
            {
                if (!isCurrentPlayerEliminated)
                {
                    if (b.isDrawPileEmpty())
                    {
                        b.setDragonTileHolder(currentPlayer);
                    }
                    else
                    {
                        currentPlayer.addTileToHand(b.drawATile());
                    }
                }
            }

            // Update game play order
            if (!isCurrentPlayerEliminated)
            {
                b.moveCurrentPlayerToEndOfPlayOrder();
            }

            // Compute turn result
            return(b.GetTurnResult());
        }
Esempio n. 2
0
        /*************** 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);
        }
Esempio n. 3
0
        public SPlayer getPlayer(string color)
        {
            // Look in active players
            SPlayer p = activePlayers.Find(x => color == x.getColor());

            // Look in eliminated players
            if (p == null)
            {
                p = eliminatedPlayers.Find(x => color == x.getColor());
            }

            // Could not find input color
            if (p == null)
            {
                throw new TsuroException("Could not find player with color " + color);
            }
            return(p);
        }
Esempio n. 4
0
        public bool isNotEliminationMove(SPlayer p, Tile t)
        {
            // Get next tile position
            Posn playerPosn = p.getPlayerPosn();

            int[] newGridLoc = nextTileCoord(playerPosn);

            // Put tile on mock board
            Board mockBoard = this.clone();

            mockBoard.grid[newGridLoc[0], newGridLoc[1]] = t;

            // Move player on fake board
            Posn endPos = mockBoard.moveMockPlayer(playerPosn);

            // See if elimination move
            return(!onEdge(endPos));
        }
Esempio n. 5
0
        public static XElement splayerToXML(SPlayer player, Board board)
        {
            XElement splayerXML;

            if (board.existsDragonTileHolder() &&
                board.isDragonTileHolder(player.returnColor()))
            {
                splayerXML = new XElement("splayer-dragon");
            }
            else
            {
                splayerXML = new XElement("splayer-nodragon");
            }


            XElement handTileXML = playerHandToXML(player.returnHand());

            splayerXML.Add(new XElement("color", player.returnColor()),
                           handTileXML);
            return(splayerXML);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        // Moves all players to the end of their path
        // Returns list of players who end up on the edge
        public void movePlayers()
        {
            List <SPlayer> onEdgePlayers = new List <SPlayer>();

            for (int i = 0; i < getNumActive(); i++)
            {
                SPlayer player = onBoard[i];

                Posn endPos = moveMockPlayer(player.getPlayerPosn());
                player.setPosn(endPos);

                if (onEdge(endPos))
                {
                    onEdgePlayers.Add(player);
                    eliminatePlayer(player);
                    i--;
                }
            }

            if (getNumActive() == 0)
            {
                eliminatedButWinners = onEdgePlayers;
            }
        }
Esempio n. 8
0
        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));
        }
Esempio n. 9
0
 public bool legalPlay(SPlayer p, Board b, Tile t)
 {
     return(!b.isEliminationMove(p.getColor(), t) ||
            (b.isEliminationMove(p.getColor(), t) &&
             (p.allMovesEliminatePlayer(b, t))));
 }
Esempio n. 10
0
        public override Tile playTurn(Board b, List <Tile> playerHand, int numTilesInDrawPile)
        {
            //ordered from least to most symmetric
            List <Tile> validMoves = new List <Tile>();

            if (playerHand.Count == 0)
            {
                throw new Exception("player hand is empty");
            }
            else
            {
                //order tiles from most to least symmetric
                List <Tile> orderedTiles = new List <Tile>();

                //list of all moves from most to least symmetric
                List <Tile> allMovesOrdered = new List <Tile>();

                SortedDictionary <int, List <Tile> > sortedTiles = new SortedDictionary <int, List <Tile> >();

                foreach (Tile t in playerHand)
                {
                    int symmetry = t.howSymmetric();
                    if (sortedTiles.ContainsKey(symmetry))
                    {
                        sortedTiles[symmetry].Add(t);
                    }
                    else
                    {
                        sortedTiles.Add(symmetry, new List <Tile> {
                            t
                        });
                    }
                }

                foreach (KeyValuePair <int, List <Tile> > pair in sortedTiles.Reverse())
                {
                    foreach (Tile t in pair.Value)
                    {
                        Tile checkTile    = t;
                        int  timesRotated = 0;
                        checkTile = checkTile.rotate();

                        while (timesRotated < 4)
                        {
                            SPlayer currPlayer = b.getActiveSPlayer(color);
                            if (currPlayer.returnColor() == null)
                            {
                                throw new Exception("Player not found on board!");
                            }
                            if (b.isNotEliminationMove(currPlayer, checkTile))
                            {
                                validMoves.Add(checkTile);
                                break;
                            }
                            else
                            {
                                checkTile    = checkTile.rotate();
                                timesRotated = timesRotated + 1;
                            }
                            allMovesOrdered.Add(checkTile);
                        }
                    }
                }


                //no valid moves, return the first tile
                if (validMoves.Count == 0)
                {
                    return(allMovesOrdered[0]);
                }

                return(validMoves[0]);
            }
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            while (true)
            {
                List <XElement> inputXML = new List <XElement>();

                string line;

                for (int i = 0; i < 5; i++)
                {
                    line = Console.ReadLine();

                    // End execution when game is over
                    if (line == null)
                    {
                        return;
                    }
                    inputXML.Add(XElement.Parse(line));
                }
                XElement drawTilesXml         = inputXML[0];
                XElement onBoardPlayersXml    = inputXML[1];
                XElement eliminatedPlayersXml = inputXML[2];
                XElement boardXml             = inputXML[3];
                XElement tileToPlayXml        = inputXML[4];

                List <Tile> drawPile   = XMLDecoder.xmlToListOfTiles(drawTilesXml);
                Board       b          = XMLDecoder.xmlToBoard(boardXml);
                Tile        tileToPlay = XMLDecoder.xmlToTile(tileToPlayXml);

                SPlayer        dragonTileHolder = null;
                List <SPlayer> activePlayers    = new List <SPlayer>();
                foreach (XElement splayerXml in onBoardPlayersXml.Elements())
                {
                    SPlayer tempPlayer = XMLDecoder.xmlToSplayer(splayerXml);
                    tempPlayer.playerState = SPlayer.State.Playing;

                    if (tempPlayer.isDragonHolder())
                    {
                        if (dragonTileHolder != null)
                        {
                            throw new TsuroException("Cannot set multiple dragon tile holders.");
                        }
                        dragonTileHolder = tempPlayer;
                    }
                    activePlayers.Add(tempPlayer);
                }

                List <SPlayer> eliminatedPlayers = new List <SPlayer>();
                foreach (XElement splayerXml in eliminatedPlayersXml.Elements())
                {
                    SPlayer tempPlayer = XMLDecoder.xmlToSplayer(splayerXml);
                    eliminatedPlayers.Add(tempPlayer);
                }

                // Run our version of play a turn
                Admin      admin = new Admin(activePlayers, eliminatedPlayers, dragonTileHolder, drawPile);
                TurnResult tr    = admin.playATurn(b, tileToPlay);

                //Convert our turn result into xml strings
                string outDrawPileXml          = XMLEncoder.listOfTilesToXML(tr.drawPile).ToString();
                string outActivePlayersXml     = XMLEncoder.listOfSPlayerToXML(tr.currentPlayers, admin).ToString();
                string outEliminatedPlayersXml = XMLEncoder.listOfSPlayerToXML(tr.eliminatedPlayers, admin).ToString();
                string outBoardXml             = XMLEncoder.boardToXML(tr.b).ToString();
                string outwinnersXML;

                if (tr.playResult == null)
                {
                    outwinnersXML = XMLEncoder.encodeFalse().ToString();
                }
                else
                {
                    outwinnersXML = XMLEncoder.listOfSPlayerToXML(tr.playResult, admin).ToString();
                }

                // Print XML Strings out through stdout
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outDrawPileXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outActivePlayersXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outEliminatedPlayersXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outBoardXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outwinnersXML));

                Console.Out.WriteLine();
            }
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            while (true)
            {
                List <XElement> inputXML = new List <XElement>();

                string line;

                for (int i = 0; i < 5; i++)
                {
                    line = Console.ReadLine();

                    // End execution when game is over
                    if (line == null)
                    {
                        return;
                    }
                    inputXML.Add(XElement.Parse(line));
                }
                XElement drawTilesXml         = inputXML[0];
                XElement onBoardPlayersXml    = inputXML[1];
                XElement eliminatedPlayersXml = inputXML[2];
                XElement boardXml             = inputXML[3];
                XElement tileToPlayXml        = inputXML[4];

                List <Tile> drawPile = XMLDecoder.xmlToListOfTiles(drawTilesXml);
                Tile[,] boardGrid = XMLDecoder.xmlBoardToGrid(boardXml);
                Dictionary <string, Posn> colorToPosnMap = XMLDecoder.xmlBoardToPlayerPosns(boardXml);
                Tile    tileToPlay       = XMLDecoder.xmlToTile(tileToPlayXml);
                SPlayer dragonTileHolder = null;

                // Create list of active players from onBoardXml
                List <SPlayer> activePlayers = new List <SPlayer>();
                foreach (XElement splayerXml in onBoardPlayersXml.Elements())
                {
                    string color = XMLDecoder.xmlSPlayerToColor(splayerXml);

                    List <Tile> playerHand = XMLDecoder.xmlSPlayerToHand(splayerXml);
                    SPlayer     tempPlayer = new SPlayer(color, playerHand);
                    tempPlayer.playerState = SPlayer.State.Playing;

                    if (colorToPosnMap[color] == null)
                    {
                        throw new Exception("Active player color was not found on Board player colors");
                    }
                    tempPlayer.setPosn(colorToPosnMap[color]);

                    if (XMLDecoder.SPlayerXmlIsDragonTileHolder(splayerXml))
                    {
                        dragonTileHolder = tempPlayer;
                    }

                    activePlayers.Add(tempPlayer);
                }

                // Create list of eliminated players from onBoardXml
                List <SPlayer> eliminatedPlayers = new List <SPlayer>();
                foreach (XElement splayerXml in eliminatedPlayersXml.Elements())
                {
                    string color = XMLDecoder.xmlSPlayerToColor(splayerXml);

                    List <Tile> playerHand = XMLDecoder.xmlSPlayerToHand(splayerXml);
                    SPlayer     tempPlayer = new SPlayer(color, playerHand);
                    if (colorToPosnMap[color] == null)
                    {
                        throw new Exception("Eliminated player color was not found on Board player colors");
                    }
                    tempPlayer.setPosn(colorToPosnMap[color]);

                    eliminatedPlayers.Add(tempPlayer);
                }

                Board boardWithAllInfo = new Board(drawPile, activePlayers, eliminatedPlayers, dragonTileHolder, boardGrid);

                // Run our version of play a turn
                Admin      admin = new Admin();
                TurnResult tr    = admin.playATurn(boardWithAllInfo, tileToPlay);

                //Convert our turn result into xml strings
                string outDrawPileXml          = XMLEncoder.listOfTilesToXML(tr.drawPile).ToString();
                string outActivePlayersXml     = XMLEncoder.listOfSPlayerToXML(tr.currentPlayers, tr.b).ToString();
                string outEliminatedPlayersXml = XMLEncoder.listOfSPlayerToXML(tr.eliminatedPlayers, tr.b).ToString();
                string outBoardXml             = XMLEncoder.boardToXML(tr.b).ToString();
                string outwinnersXML           = "";

                if (tr.playResult == null)
                {
                    outwinnersXML = XMLEncoder.encodeFalse().ToString();
                }
                else
                {
                    outwinnersXML = XMLEncoder.listOfSPlayerToXML(tr.playResult, tr.b).ToString();
                }

                // Print XML Strings out through stdout
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outDrawPileXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outActivePlayersXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outEliminatedPlayersXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outBoardXml));
                Console.WriteLine(XMLEncoder.RemoveWhitespace(outwinnersXML));

                Console.Out.WriteLine();
            }
        }
Esempio n. 13
0
 public void setStartPos(Board board, SPlayer player, Posn pos)
 {
     player.setPosn(pos);
     board.registerPlayer(player);
     player.playerState = SPlayer.State.Placed;
 }
Esempio n. 14
0
 public Board(List <Tile> drawPile, List <SPlayer> activePlayers, List <SPlayer> eliminatedPlayers, SPlayer dragonHolder, Tile[,] grid)
 {
     this.drawPile         = drawPile;
     this.onBoard          = activePlayers;
     this.eliminated       = eliminatedPlayers;
     this.dragonTileHolder = dragonHolder;
     this.grid             = grid;
 }
Esempio n. 15
0
 public void setDragonTileHolder(SPlayer p)
 {
     dragonTileHolder = p;
 }
Esempio n. 16
0
 public void setStartPos(Board board, SPlayer player, Posn pos)
 {
     board.addPlayerToBoard(player.getColor(), pos);
     player.playerState = SPlayer.State.Placed;
 }