/*************** GAME PLAY FUNCTIONS ****************************/ // The player with the input color places tile t at the grid position they // are about to move to // // Returns the end position of the given player given the tile placement, // but does not actually move him/her public Posn placeTile(String color, Tile t) { int[] newGridLoc = new int[2]; Posn playerPosn = colorToPosnMap[color]; // if player is not on the edge, if it is not the players first turn anymore // set new grid location to be the next location that player can place tile in newGridLoc = nextTileCoord(playerPosn); // get the current player location on their current tile int currentTilePosn = playerPosn.returnLocationOnTile(); // get the new player location on the next tile int newTilePosn = getEndOfPathOnTile(t, currentTilePosn); int newRow = newGridLoc[0]; int newCol = newGridLoc[1]; // set the next grid location on the board to be the tile grid[newRow, newCol] = t; // Calculate end position of player on new tile Posn endPos = new Posn(newRow, newCol, newTilePosn); // Calculate end position of player if additional tiles to move across endPos = followPathsMock(endPos); return(endPos); }
public Posn moveMockPlayer(Posn startPos) { if (onEdge(startPos)) { return(startPos); } int[] nextCoord = nextTileCoord(startPos); int nextRow = nextCoord[0]; int nextCol = nextCoord[1]; // End recursion if no more tiles along path if (!occupied(nextRow, nextCol)) { return(startPos); } else // Recursively follow path { // set the current location of the player to be the the next grid location Tile nextTile = grid[nextRow, nextCol]; int endPosn = getEndOfPathOnTile(nextTile, startPos.returnLocationOnTile()); Posn newPosn = new Posn(nextRow, nextCol, endPosn); return(moveMockPlayer(newPosn)); } }
// Returns end position for the given start position, after following all // on board paths // // Does not actually move player on board public Posn followPathsMock(Posn startPos) { if (isElimPosn(startPos)) { return(startPos); } int[] nextCoord = nextTileCoord(startPos); int nextRow = nextCoord[0]; int nextCol = nextCoord[1]; // End recursion if no more tiles along path if (grid[nextRow, nextCol] == null) { return(startPos); } else // Recursively follow path { // set the current location of the player to be the the next grid location Tile nextTile = grid[nextRow, nextCol]; int endPosn = getEndOfPathOnTile(nextTile, startPos.returnLocationOnTile()); Posn newPosn = new Posn(nextRow, nextCol, endPosn); return(followPathsMock(newPosn)); } }
/*************** HELPER FUNCTIONS ****************************/ // Returns the [row, col] of the next grid position a player at position p // will move to public int[] nextTileCoord(Posn p) { int currentRow = p.returnRow(); int currentCol = p.returnCol(); int currentTilePosn = p.returnLocationOnTile(); int[] nextCoord = new int[] { currentRow, currentCol }; switch (currentTilePosn) { case 0: case 1: nextCoord[0] = currentRow - 1; break; case 2: case 3: nextCoord[1] = currentCol + 1; break; case 4: case 5: nextCoord[0] = currentRow + 1; break; case 6: case 7: nextCoord[1] = currentCol - 1; break; } return(nextCoord); }
public void setStartPos00(Board board, SPlayer player) { Posn startPos = new Posn(-1, 0, 5); board.addPlayerToBoard(player.getColor(), startPos); player.playerState = SPlayer.State.Placed; }
public void setStartPos00(Board board, SPlayer player) { //public void setStartPos00(Board board, SPlayer player, string color, List<string> playerOrder) { Posn startPos = new Posn(-1, 0, 5); player.setPosn(startPos); board.registerPlayer(player); player.playerState = SPlayer.State.Placed; }
public SPlayer createPlayerAtPos(String color, List <Tile> hand, IPlayer iplayer, Posn posn, Board board) { SPlayer player = new SPlayer(color, hand, iplayer); player.playerState = SPlayer.State.Playing; board.addPlayerToBoard(color, posn); return(player); }
public virtual Posn placePawn(Board b) { Posn randomStartPos = generateRandomStartPosn(); while (b.locationOccupied(randomStartPos)) { randomStartPos = generateRandomStartPosn(); } return(randomStartPos); }
public SPlayer createPlayerAtPos(String name, List <Tile> hand, IPlayer iplayer, Posn posn, Board board) { SPlayer player = new SPlayer(name, hand, iplayer); player.setPosn(posn); player.playerState = SPlayer.State.Playing; board.registerPlayer(player); return(player); }
// Returns true if there is already a player at the input position public bool locationOccupied(Posn inputPosn) { foreach (Posn p in colorToPosnMap.Values.ToList()) { if (p.isEqual(inputPosn)) { return(true); } } return(false); }
public bool isEqual(Posn checkP) { if ((checkP.returnRow() == row) && (checkP.returnCol() == col) && (checkP.returnLocationOnTile() == locOnTile)) { return(true); } else { return(false); } }
// Returns a position on the board from the 2 options, after parsing pawn loc XML // Needed because board has 2 positions per location public static Posn pawnLocToPosn(List <Posn> possiblePosns, Board board) { if (possiblePosns.Count != 2) { throw new Exception("There should only be two possible pawn locations."); } // if on edge // start game or eliminated // check if there is a tile in the onedge position //not on edge // check if there is a tile on a posn and choose that one Posn phantomPosn = null; Posn edgePosn = null; if (board.isElimPosn(possiblePosns[0])) { phantomPosn = possiblePosns[1]; edgePosn = possiblePosns[0]; } else if (board.isElimPosn(possiblePosns[1])) { phantomPosn = possiblePosns[0]; edgePosn = possiblePosns[1]; } if (phantomPosn != null) { if (board.getTileAt(edgePosn.returnRow(), edgePosn.returnCol()) != null) { return(edgePosn); } return(phantomPosn); } // Player is always on a tile, about to move to an empty space // Valid positions must be on a tile Posn posn1 = possiblePosns[0]; Posn posn2 = possiblePosns[1]; if (board.getTileAt(posn1.returnRow(), posn1.returnCol()) != null) { return(posn1); } else if (board.getTileAt(posn2.returnRow(), posn2.returnCol()) != null) { return(posn2); } else { throw new Exception("Invalid posn of player (not on edge and don't have tiles anywhere around it)."); } }
public Board placePawn(Board b) { if (playerState != State.Init) { throw new Exception("player pawn is being placed but" + "player is not in initialized state"); } playerPosn = playerStrategy.placePawn(b); b.registerPlayer(this); playerState = State.Placed; return(b); }
public static XElement pawnsToXML(List <string> playerColorList, Board b) { XElement pawnXML = new XElement("map"); foreach (string color in playerColorList) { Posn tempPosn = b.getPlayerPosn(color); pawnXML.Add(new XElement("ent", new XElement("color", color), posnToPawnLocXML(tempPosn))); } return(pawnXML); }
public bool locationOccupied(Posn inputPosn) { foreach (SPlayer p in onBoard) { Posn playerPosn = p.getPlayerPosn(); if (playerPosn.isEqual(inputPosn)) { return(true); } } return(false); }
public static Board xmlToBoard(XElement boardXML) { checkOrderOfTagsFromXML(new List <string> { "board" }, new List <XElement> { boardXML }); checkOrderOfTagsFromXML(new List <string> { "map", "map" }, boardXML.Elements().ToList()); Board board = new Board(); int col = -1; int row = -1; XElement tilesXML = boardXML.Elements("map").ElementAt(0); XElement pawnsXML = boardXML.Elements("map").ElementAt(1); //create board with tiles placed in correct grid position foreach (XElement ent in tilesXML.Elements("ent")) { try { col = Int32.Parse(ent.Descendants("x").ElementAt(0).Value); row = Int32.Parse(ent.Descendants("y").ElementAt(0).Value); } catch (FormatException e) { Console.WriteLine(e.Message); } Tile tile = xmlToTile(ent.Descendants("tile").ElementAt(0)); board.placeTileAt(tile, row, col); } // create pawns (aka onboard players) foreach (XElement ent in pawnsXML.Elements("ent")) { checkOrderOfTagsFromXML(new List <string> { "color", "pawn-loc" }, ent.Elements().ToList()); string color = ent.Element("color").Value; List <Posn> possiblePosns = xmlToPosn(ent.Element("pawn-loc")); Posn startPos = pawnLocToPosn(possiblePosns, board); board.addPlayerToBoard(color, startPos); } return(board); }
public Board placePawn(Board b) { if (playerState != State.Init) { throw new Exception("player pawn is being placed but" + "player is not in initialized state"); } Posn startPos = playerStrategy.placePawn(b); b.addPlayerToBoard(color, startPos); playerState = State.Placed; return(b); }
// 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); }
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)); }
public bool isEliminationMove(string color, Tile t) { // Get next tile position Posn playerPosn = colorToPosnMap[color]; 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.followPathsMock(playerPosn); // See if elimination move return(isElimPosn(endPos)); }
// Moves all active players to the end of their path // Returns list of colors of players who end up on the edge // // Actually moves players' positions on board public List <string> moveActivePlayers(List <string> activePlayerColors) { List <string> onEdgePlayerColors = new List <string>(); for (int i = 0; i < activePlayerColors.Count; i++) { String color = activePlayerColors[i]; Posn startPosn = colorToPosnMap[color]; Posn endPos = followPathsMock(startPosn); // Update player position map colorToPosnMap[color] = endPos; if (isElimPosn(endPos)) { onEdgePlayerColors.Add(color); } } return(onEdgePlayerColors); }
// Returns true if a position is on the edge of the board, due to elimination public bool isElimPosn(Posn p) { int row = p.returnRow(); int col = p.returnCol(); int tilePos = p.returnLocationOnTile(); // Phantom tile positions are valid start positions on the board edge, and don't cause elimination bool phantomTile = col == -1 || col == 6 || row == -1 || row == 6; if (phantomTile) { return(false); } bool topEdgeElim = row == 0 && (tilePos == 0 || tilePos == 1); bool bottomEdgeElim = row == 5 && (tilePos == 4 || tilePos == 5); bool leftEdgeElim = col == 0 && (tilePos == 6 || tilePos == 7); bool rightEdgeElim = col == 5 && (tilePos == 2 || tilePos == 3); return(topEdgeElim || bottomEdgeElim || leftEdgeElim || rightEdgeElim); }
public static Dictionary <string, Posn> xmlBoardToPlayerPosns(XElement boardXml) { Tile[,] grid = xmlBoardToGrid(boardXml); Board boardWithGridOnly = new Board(grid); Dictionary <string, Posn> colortoPosnMap = new Dictionary <string, Posn>(); XElement pawnsXML = boardXml.Elements("map").ElementAt(1); // create pawns (aka onboard players) foreach (XElement ent in pawnsXML.Elements("ent")) { checkOrderOfTagsFromXML(new List <string> { "color", "pawn-loc" }, ent.Elements().ToList()); string color = ent.Element("color").Value; List <Posn> possiblePosns = xmlToPosn(ent.Element("pawn-loc")); Posn startPos = pawnLocToPosn(possiblePosns, boardWithGridOnly); colortoPosnMap[color] = startPos; } return(colortoPosnMap); }
public bool onEdge(Posn p) { int row = p.returnRow(); int col = p.returnCol(); int tilePos = p.returnLocationOnTile(); if (row == 0 && (col != -1 && col != 6)) { if (tilePos == 0 || tilePos == 1) { return(true); } } if (row == 5 && (col != -1 && col != 6)) { if (tilePos == 4 || tilePos == 5) { return(true); } } if (col == 0 && (row != -1 && row != 6)) { if (tilePos == 6 || tilePos == 7) { return(true); } } if (col == 5 && (row != -1 && row != 6)) { if (tilePos == 2 || tilePos == 3) { return(true); } } return(false); }
// 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; } }
public static List <Posn> xmlToPosn(XElement posnXML) { List <XElement> posnXMLTree = posnXML.Descendants().ToList(); // Check XML tags // If not a horizontal pawn-loc, must be a vertical pawn-loc bool horizontalPosn = false; try { checkOrderOfTagsFromXML(new List <string> { "h", "n", "n" }, posnXMLTree); horizontalPosn = true; } catch (XMLTagOrderException) { checkOrderOfTagsFromXML(new List <string> { "v", "n", "n" }, posnXMLTree); } int row1; int row2; int col1; int col2; int tilePos; int tilePos2; XElement edgeXML = posnXML.Elements("n").ElementAt(0); int edge; int.TryParse(edgeXML.Value, out edge); XElement locOnEdgeXML = posnXML.Elements("n").ElementAt(1); int locOnEdge; int.TryParse(locOnEdgeXML.Value, out locOnEdge); if (horizontalPosn) { row1 = edge; row2 = edge - 1; col1 = locOnEdge / 2; col2 = col1; tilePos = locOnEdge % 2; if (tilePos == 0) { tilePos2 = 5; } else { tilePos2 = 4; } } else { col1 = edge - 1; col2 = edge; row1 = locOnEdge / 2; row2 = row1; tilePos = locOnEdge % 2 + 2; if (tilePos == 2) { tilePos2 = 7; } else { tilePos2 = 6; } } Posn p1 = new Posn(row1, col1, tilePos); Posn p2 = new Posn(row2, col2, tilePos2); List <Posn> posnList = new List <Posn> { p1, p2 }; return(posnList); }
/*************** SETTERS ****************************/ public void addPlayerToBoard(string color, Posn posn) { colorToPosnMap.Add(color, posn); }
// set the position and location of a player public void setPosn(Posn p) { playerPosn = p; }
public static XElement posnToPawnLocXML(Posn p) { XElement hv; int loc = p.returnLocationOnTile(); switch (loc) { case 0: case 1: case 4: case 5: hv = new XElement("h", ""); break; case 2: case 3: case 6: case 7: hv = new XElement("v", ""); break; default: hv = null; break; } XElement edge; switch (loc) { case 0: case 1: edge = new XElement("n", p.returnRow()); break; case 5: case 4: edge = new XElement("n", p.returnRow() + 1); break; case 2: case 3: edge = new XElement("n", p.returnCol() + 1); break; case 6: case 7: edge = new XElement("n", p.returnCol()); break; default: edge = null; break; } XElement locOnEdge; switch (loc) { case 0: case 5: locOnEdge = new XElement("n", p.returnCol() * 2); break; case 1: case 4: locOnEdge = new XElement("n", p.returnCol() * 2 + 1); break; case 2: case 7: locOnEdge = new XElement("n", p.returnRow() * 2); break; case 3: case 6: locOnEdge = new XElement("n", p.returnRow() * 2 + 1); break; default: locOnEdge = null; break; } if (hv == null || edge == null || locOnEdge == null) { throw new Exception("Invalid position input to posnToPawnLocXML!!!!!"); } return(new XElement("pawn-loc", hv, edge, locOnEdge)); }
public void setStartPos(Board board, SPlayer player, Posn pos) { player.setPosn(pos); board.registerPlayer(player); player.playerState = SPlayer.State.Placed; }