示例#1
0
        public Tile playTurn(Board b, List <Tile> playerHand, int numTilesInDrawPile)
        {
            XElement xmlQuery = new XElement("play-turn",
                                             XMLEncoder.boardToXML(b),
                                             XMLEncoder.playerHandToXML(playerHand),
                                             new XElement("n", numTilesInDrawPile));
            XElement response     = sendQuery(xmlQuery);
            Tile     returnedTile = XMLDecoder.xmlToTile(response);

            return(returnedTile);
        }
示例#2
0
        public void endGame(Board b, List <string> allColors)
        {
            XElement winners  = XMLEncoder.listOfColorsToXML(allColors);
            XElement xmlQuery = new XElement("end-game",
                                             XMLEncoder.boardToXML(b),
                                             winners);
            XElement response = sendQuery(xmlQuery);

            if (response.Name != "void")
            {
                throw new Exception("Expected void from network player end game call.");
            }
        }
示例#3
0
        public void initialize(string playerColor, List <string> allColors)
        {
            XElement listOfColors = XMLEncoder.listOfColorsToXML(allColors);
            XElement xmlQuery     = new XElement("initialize",
                                                 new XElement("color", playerColor),
                                                 listOfColors);
            //Expect to receive a void
            XElement response = sendQuery(xmlQuery);

            //check if return value is void
            if (response.Name != "void")
            {
                throw new Exception("Expected void from network player initialize call.");
            }
        }
示例#4
0
        // Parses initialize XML and calls initialize on player
        // Returns void XML response
        public String initializeHandler(XElement initXML)
        {
            XMLDecoder.checkOrderOfTagsFromXML(new List <string> {
                "color", "list"
            },
                                               initXML.Elements().ToList());

            String        color       = XMLDecoder.xmlToColor(initXML.Element("color"));
            List <string> playerOrder = XMLDecoder.xmlToListOfColors(initXML.Element("list"));

            player.initialize(color, playerOrder);

            // Return void
            return(XMLEncoder.toString(XMLEncoder.encodeVoid()));
        }
示例#5
0
        // Parses play turn XML and calls play turn on player
        // Returns tile XML response
        public String playTurnHandler(XElement playTurnXML)
        {
            XMLDecoder.checkOrderOfTagsFromXML(new List <string> {
                "board", "set", "n"
            },
                                               playTurnXML.Elements().ToList());

            Board       b    = XMLDecoder.xmlToBoard(playTurnXML.Element("board"));
            List <Tile> hand = XMLDecoder.xmlToListOfTiles(playTurnXML.Element("set"));
            int         numTilesInDrawPile = XMLDecoder.xmlToNumber(playTurnXML.Element("n"));

            Tile   tileToPlay   = player.playTurn(b, hand, numTilesInDrawPile);
            String responseTile = XMLEncoder.toString(XMLEncoder.tileToXML(tileToPlay));

            return(responseTile);
        }
示例#6
0
        // Parses and responses to an XML query about the competitor
        // Returns XML string of response
        public String interpretQuery(String query)
        {
            XElement queryXML = XElement.Parse(query);
            String   command  = queryXML.Name.ToString();
            String   response = null;

            switch (command)
            {
            case "get-name":
                String name = player.getName();
                response = XMLEncoder.nameToXML(name).ToString();
                break;

            case "initialize":
                response = initializeHandler(queryXML);
                break;

            case "place-pawn":
                XElement boardXML = queryXML.Element("board");
                Board    board    = XMLDecoder.xmlToBoard(boardXML);
                Posn     posn     = player.placePawn(board);
                response = XMLEncoder.RemoveWhitespace(XMLEncoder.posnToPawnLocXML(posn).ToString());
                break;

            case "play-turn":
                response = playTurnHandler(queryXML);
                break;

            case "end-game":
                XElement      xmlBoardEndGame = queryXML.Element("board");
                Board         bEndGame        = XMLDecoder.xmlToBoard(xmlBoardEndGame);
                XElement      xmlPlayerColors = queryXML.Element("set");
                List <string> playerColors    = new List <string>();
                foreach (XElement playerColorXml in xmlPlayerColors.Descendants("color"))
                {
                    playerColors.Add(XMLDecoder.xmlToColor(playerColorXml));
                }
                response = XMLEncoder.encodeVoid().ToString();
                break;

            default:
                throw new Exception("Outgoing competitor command not understand " +
                                    "the command " + command);
            }

            return(response);
        }
示例#7
0
        public Posn placePawn(Board b)
        {
            XElement xmlQuery = new XElement("place-pawn",
                                             XMLEncoder.boardToXML(b));
            XElement    response      = sendQuery(xmlQuery);
            List <Posn> possiblePosns = XMLDecoder.xmlToPosn(response);

            foreach (Posn pos in possiblePosns)
            {
                // on edge returns true if posn is on the edge and NOT a phantom posn
                if (!b.onEdge(pos))
                {
                    return(pos);
                }
            }
            throw new Exception("Did not get an phantom starting position for network player from placePawn.");
        }
示例#8
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();
            }
        }
示例#9
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();
            }
        }