/// <summary> /// Creates the tile intersection /// </summary> /// <remarks>If there are no surrounding corporations, returns a CorpFoundation. /// If there is only one, returns a CorpExpansion. /// If there is more than one, returns a merger.</remarks> /// <param name="connector">The connecting tile</param> /// <param name="adjTiles">The adjacent tiles</param> /// <returns>TileIntersection object</returns> public TileIntersection determineIntersection(Tile connector, List <Tile> adjTiles) { int corpCount = 0; // count the corporations surrounding List <Corporation> corpsInTiles = new List <Corporation>(); foreach (Tile t in adjTiles) { Corporation corp = t.Corp; if (corp != null && !corpsInTiles.Contains(corp)) // Not null and not already in list { corpCount++; corpsInTiles.Add(corp); } } if (corpCount == 0) { return(new CorpFoundation(connector, adjTiles)); } else if (corpCount == 1) { return(new CorpExpansion(connector, adjTiles)); } else { return(new Merger(connector, adjTiles)); } }
/// <summary> /// Resolves the intersection by attempting to merge the corporations in the adjacent tiles. /// </summary> /// <param name="corporations">List of all the corporations</param> /// <exception cref="PermanentUnplayableTileException">Thrown when trying to merge multiple safe corporations.</exception> public override bool resolveIntersection(Corporation[] corporations) { // Check for multiple safe corporations if (mergerCorporations[0].IsSafe && mergerCorporations[1].IsSafe) { throw new PermanentUnplayableTileException(connector.getPosition()); } // Largest size int largestSize = mergerCorporations[0].Size; // Choose the corporation if the largest are the same size if (largestSize == mergerCorporations[1].Size) { // Gather corporation choices (could be more than 2) foreach (Corporation c in mergerCorporations) { if (c.Size == largestSize) { validCorporationChoices.Add(c.Name); } } handler.handleChooseCorp(validCorporationChoices); } else // different sizes { overtaker = mergerCorporations[0]; notifyDefunctStock(); } return(false); }
/// <summary> /// Tile Constructor /// </summary> /// <param name="position">string of the form '1A'</param> /// <exception cref="ArgumentException">Invalid position exception</exception> public Tile(string position) { if (!isValidPosition(position)) { throw new ArgumentException("String " + position + " is not a valid tile position."); } this.position = position; this.corp = null; }
/// <summary> /// Chooses the corporation that will survive in a tie between largest size merger /// </summary> /// <param name="corporation">The survivor or overtaker</param> /// <exception cref="InvalidProgramException">Thrown when making an illegal corporation choice</exception> public override void chooseCorporation(ECorporation corporation) { if (!validCorporationChoices.Contains(corporation)) // Illegal choice { throw new InvalidProgramException("Corporation is not a valid choice."); } foreach (Corporation corporation1 in mergerCorporations) { if (corporation1.Name == corporation) { overtaker = corporation1; notifyDefunctStock(); break; } } }
/// <summary> /// Expands the corporation to include the unincorporated tiles. /// </summary> /// <param name="corporations">A list of corporations</param> public override bool resolveIntersection(Corporation[] corporations) { List <Tile> unincorporated = new List <Tile>(); Corporation corporation = null; foreach (Tile t in adjacentTiles) { if (t.Corp != null) { corporation = t.Corp; } else { unincorporated.Add(t); } } unincorporated.Add(connector); corporation.expand(unincorporated); return(true); }
/// <summary> /// Performs the merging of corporations. /// </summary> private bool notifyDefunctStock() { // Find the next defunct corporation, and notify that its defunct bool foundDefunct = (defunct == null); foreach (Corporation c in mergerCorporations) { if (c == defunct) { foundDefunct = true; } else if (c != overtaker && foundDefunct) { defunct = c; // Notify it's defunct, to handle stocks. Pass in this to be able to return execution here. c.notifyDefunct((IMerger)this); return(true); } } // No corporations left to handle stocks return(false); }
/// <summary> /// Determine the merging corporations /// </summary> private void initializeMergingCorporations() { mergerCorporations = new List <Corporation>(); foreach (Tile t in adjacentTiles) { Corporation c = t.Corp; if (c != null && !mergerCorporations.Contains(c)) // not null and not already in list { int corpSize = c.Size; // insert into sorted list int i = 0; // determine index to insert at while (i < mergerCorporations.Count) { if (mergerCorporations[i].Size < corpSize) { break; } i++; } mergerCorporations.Insert(i, c); } } }