Exemplo n.º 1
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);
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
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();
            }
        }