private void BuyButton_Click(object sender, RoutedEventArgs e) { CorpNames[] corps = new CorpNames[AcquireConstants.MaxSharePurchase]; // collect up purchaes for (int i = 0; i < AcquireConstants.MaxSharePurchase; i++) { if (null == GetItem <ComboBox>(Lbuycorp[i]).SelectedItem) { corps[i] = CorpNames.NA; } else { corps[i] = (CorpNames)GetItem <ComboBox>(Lbuycorp[i]).SelectedItem; } } BuyShares(corps); }
private void ChooseCorp(CorpNames corp) { if (game.State == AcquireGameStates.ChooseCorp) { // choose a corporation game.ChooseCorp(corp); } else if (game.State == AcquireGameStates.ChooseParentCorp) { // choose a corporation game.ChooseParentCorp(corp); } else { return; } // move ahead AdvanceUI(); }
// buy shares public CorpNames[] BuyShares(List <CorpNames>[] corps) { CorpNames[] purchase; int cnt; purchase = new CorpNames[corps.Length]; cnt = 0; foreach (List <CorpNames> choice in corps) { if (choice.Count == 0) { purchase[cnt] = CorpNames.NA; } else { purchase[cnt] = choice[rand.Next() % choice.Count]; } cnt++; } return(purchase); }
// decision tree // // nextBuyShares.Count - 1+ -> BUY_SPECIFIC_LOGIC (round robin on list and buy as much as you can) // | // ==0 // \|/ // corpSize - 11+ -> LONGTERM_LOGIC (ensure that we are ahead in large corps [shares > (boughtShares/NumberPlayers)]) // | // all <11 // \|/ // SHORTTERM_LOGIC (buy stock in corps in the middle) // buy shares public CorpNames[] BuyShares(List <CorpNames>[] corps) { CorpNames[] shares = new CorpNames[corps.Length]; int[] remaining = new int[AcquireConstants.CorpCount]; // track how many of each corp remain int cash; int index; decsions.Add("BuyShares"); // init for (int i = 0; i < shares.Length; i++) { shares[i] = CorpNames.NA; } cash = Player.Cash; index = shares.Length - 1; for (int i = 0; i < remaining.Length; i++) { remaining[i] = Corporations[i].Shares; } // buy what we were told to buy if (nextBuyShares.Count > 0) { // round robin through the names and buy as much as you can for (int i = 0; i < shares.Length; i++) { CorpNames corp = nextBuyShares[i % nextBuyShares.Count]; shares[i] = CorpNames.NA; if (cash > (Corporations[(int)corp].Price) && remaining[(int)corp] > 0) { decsions.Add(" - Buy predetermined " + corp); cash -= Corporations[(int)corp].Price; shares[i] = corp; remaining[(int)corp]--; } } return(shares); } else { for (int tries = 0; tries < corps.Length; tries++) { // check if there is a large corp, ensure we are the majority foreach (CorpStats corp in Corporations) { if (corp.Size > AcquireConstants.NonMergableCorp) { // check that we have a reasonable chance of being the majority if (index >= 0 && remaining[(int)corp.Name] > 0 && Player.Shares(corp.Name) < ((AcquireConstants.MaxShares - corp.Shares) / NumberPlayers) && cash >= corp.Price) { decsions.Add(" - Buy " + corp.Name + " to remain majority"); shares[index--] = corp.Name; cash -= corp.Price; remaining[(int)corp.Name]--; } } } // 0 1 2 3 4 5 6 7 8 9 10 11 // 0 // 1 // 2 // 3 x x x x x x // 4 x x x x x x // 5 x x x x x x // 6 x x x x x x // 7 x x x x x x // 8 x x x x x x // 9 // 10 // 11 // find coporations that are near the middle and buy stock for (int dim0 = AcquireConstants.BoardHeight / 4; dim0 < (AcquireConstants.BoardHeight * 3) / 4 && index >= 0; dim0++) { for (int dim1 = AcquireConstants.BoardWidth / 4; dim1 < (AcquireConstants.BoardWidth * 3) / 4 && index >= 0; dim1++) { CorpNames corp = Board[dim0, dim1]; if (index >= 0 && corp != CorpNames.NA && corp != CorpNames.UnClaimed && remaining[(int)corp] > 0 && Corporations[(int)corp].Size > 0 && Corporations[(int)corp].Shares >= 1 && cash >= Corporations[(int)corp].Price) { decsions.Add(" - Buy " + corp + " since it is in the middle"); shares[index--] = corp; cash -= Corporations[(int)corp].Price; remaining[(int)corp]--; } } } // buy stock in the smaller corporations // TODO: this is a brain dead algorithm... it has a lot of room for improvement for (int size = 2; size <= AcquireConstants.NonMergableCorp && index >= 0; size++) { foreach (CorpStats corp in Corporations) { if (index >= 0 && remaining[(int)corp.Name] > 0 && corp.Size == size && corp.Shares >= 1 && cash >= corp.Price) { decsions.Add(" - Buy " + corp.Name + " since it is small"); shares[index--] = corp.Name; cash -= corp.Price; remaining[(int)corp.Name]--; } } } } decsions.Add(" - Bought " + (shares.Length - 1 - index) + " shares"); return(shares); } }
// decision tree // // hasMerger - YES -> | // | | // NO | // | \|/ // | Buy 3 shares? - NO -> MERGE_LOGIC (pick corp with largest shares) // | | // | YES *(Note with share to buy, DONT TAKE THIS BRANCH NEXT TIME) // | \|/ // |----------> hasNewCorp - YES -> NEWCORP_LOGIC (pick cheapest) // | // NO || none avaliable // \|/ // 0 Weight tiles (!new && !merge && !expand) -YES-> CHOOSE_0_WEIGHT_LOGIC // | // NO // \|/ // hasCorpExpand - YES -> money - Buy stock? - NO -> EXPAND_LOGIC (pick corp with largest shares) // | | // NO YES *(Note which share to buy, DONT TAKE THIS BRANCH NEXT TIME) // \|/ \|/ // CHOOSETILE_LOGIC CHOOSETILE_LOGIC (don't merge, don't create corp, don't expand, find tile by others, or by largest corp) // choose a tile public Square SelectSquare(Square[] tiles) { TileOption[] options; bool hasNewCorp; bool hasMerger; bool hasCorpExpand; decsions.Add("SelectSquare"); // init nextBuyShares.Clear(); newCorporation = CorpNames.NA; mergedCorporation = CorpNames.NA; // preparse tiles options = CategorizeTiles(tiles, out hasNewCorp, out hasMerger, out hasCorpExpand); // try merger if (hasMerger) { CorpNames largestCorp = CorpNames.NA; Square largestCorpTile = tiles[0]; // find the largest corp int maxShares = -1; foreach (TileOption option in options) { if (option.TType == TileType.MergeCorp) { // find the corp with the largest number of shares foreach (CorpNames corp in option.Merger) { if (Player.Shares(corp) > maxShares) { maxShares = Player.Shares(corp); largestCorp = corp; largestCorpTile = option.Tile; } } } } // set the corp that we want to be merged mergedCorporation = largestCorp; // try to merge first if (Corporations[(int)largestCorp].Shares == 0 || Player.Cash < ((Corporations[(int)largestCorp].Shares > AcquireConstants.MaxSharePurchase)? (Corporations[(int)largestCorp].Price * AcquireConstants.MaxSharePurchase) : (Corporations[(int)largestCorp].Price * Corporations[(int)largestCorp].Shares)) || lastSkippedMerger) { decsions.Add(" - skip(" + lastSkippedMerger + ") merge " + largestCorp + " now"); lastSkippedMerger = false; // make the merger return(largestCorpTile); } else { decsions.Add(" - merge " + largestCorp + " later"); // buy shares then merge nextBuyShares.Add(largestCorp); lastSkippedMerger = true; } } // try new corp if (hasNewCorp) { Square selectedTile = tiles[0]; // find the tile which creates a corp foreach (TileOption option in options) { if (option.TType == TileType.NewCorp) { selectedTile = option.Tile; break; } } // pick the corp with the highest price that we can buy 3 shares in // find if any corps are avaliable foreach (CorpStats corp in Corporations) { if (corp.Size == 0) { decsions.Add(" - New most expensive affordable " + corp.Name); // choose the most expensive affordable corp nextBuyShares.Add(corp.Name); newCorporation = corp.Name; return(selectedTile); } } throw new ArgumentException("A new corporation should have been choosen"); } // find 0 weight items that are not a new corp, or merger, or expansion foreach (TileOption option in options) { if (option.TType == TileType.StandAlone && option.Weight == 0) { decsions.Add(" - zero weight tile"); return(option.Tile); } } // expansion if (hasCorpExpand) { CorpNames largestCorp = CorpNames.NA; Square largestCorpTile = tiles[0]; // find the largest corp int maxShares = -1; foreach (TileOption option in options) { if (option.TType == TileType.Expansion) { if (Player.Shares(option.ExpanedCorp) > maxShares) { maxShares = Player.Shares(option.ExpanedCorp); largestCorp = option.ExpanedCorp; largestCorpTile = option.Tile; } } } // see if we can buy shares if (Corporations[(int)largestCorp].Shares == 0 || Player.Cash < ((Corporations[(int)largestCorp].Shares > AcquireConstants.MaxSharePurchase) ? (Corporations[(int)largestCorp].Price * AcquireConstants.MaxSharePurchase) : (Corporations[(int)largestCorp].Price * Corporations[(int)largestCorp].Shares)) || lastSkippedExpansion) { decsions.Add(" - skip(" + lastSkippedExpansion + ") expand " + largestCorp + " now"); lastSkippedExpansion = false; // make the merger return(largestCorpTile); } else { decsions.Add(" - Expand " + largestCorp + " later"); // buy shares then merge nextBuyShares.Add(largestCorp); lastSkippedExpansion = true; } } // find the tile which is standalone which are close foreach (TileOption option in options) { if (option.TType == TileType.StandAlone && option.Weight == 1) { decsions.Add(" - Return 1 weight tile"); return(option.Tile); } } // find the tile which is standalone and not necessarily close foreach (TileOption option in options) { if (option.TType == TileType.StandAlone && option.Weight > 1) { decsions.Add(" - Return 2+ weight tile"); return(option.Tile); } } // hmm... that means that we can't find a perferred tile foreach (TileOption option in options) { if (option.TType == TileType.MergeCorp) { decsions.Add(" - Return merger tile"); return(option.Tile); } } foreach (TileOption option in options) { if (option.TType == TileType.Expansion) { decsions.Add(" - Return expansion tile"); return(option.Tile); } } decsions.Add(" - Return invalid"); // all else just return the first, it will be illegal return(tiles[0]); }
private void Refresh(PlayerStats player) { CorpNames[,] board; int networth; // debug only //DisplayFinalResults(game.Players, game.Corporations); // update status GetItem <Label>(Lstatus).Content = GetItem <Label>(game.State + Lstatustag).Content; if (game.Message == Acquire.Engine.AdditionalMessage.Blank) { GetItem <Label>(Laddmsg).Visibility = Visibility.Collapsed; } else { var label = GetItem <Label>(Laddmsg); label.Content = GetItem <Label>(game.Message + Lstatustag).Content; label.Visibility = Visibility.Visible; } // update corp stats for (int c = 0; c < AcquireConstants.CorpCount; c++) { CorpNames corp = (CorpNames)c; CorpStats stats = game[corp]; GetItem <Label>(corp, Lshares).Content = stats.Shares; GetItem <Label>(corp, Lprice).Content = "$" + stats.Price; GetItem <Label>(corp, Lsize).Content = stats.Size; GetItem <Label>(corp, Lbonus).Content = "$" + stats.BonusMax + "/$" + stats.BonusMin; } // update player stats PlayerStats pstats = player; GetItem <Label>(Lname).Content = pstats.Name; GetItem <Label>(Lcash).Content = pstats.IsComputer ? "$???" : "$" + pstats.Cash; for (int c = 0; c < AcquireConstants.CorpCount; c++) { CorpNames corp = (CorpNames)c; GetItem <Label>(corp, Lcount).Content = pstats.IsComputer ? "?" : pstats.Shares(corp).ToString(); } // calculate the networth networth = 0; foreach (CorpStats corp in game.Corporations) { networth += player.Shares(corp.Name) * corp.Price; } networth += player.Cash; GetItem <Label>(Lnetworth).Content = pstats.IsComputer ? "$???" : "$" + networth; // paint the board board = game.RawBoard; for (int dim0 = 0; dim0 < board.GetLength(0); dim0++) { for (int dim1 = 0; dim1 < board.GetLength(1); dim1++) { squares[dim0, dim1].Fill = GetItem <Rectangle>(board[dim0, dim1], Lcolor).Fill; } } // display the possible squares if (!game.CurrentPlayer.IsComputer) { foreach (Square tile in game.CurrentPlayer.Tiles) { squares[tile.Dim0, tile.Dim1].Fill = GetItem <Rectangle>(Lselection, Lcolor).Fill; } } // display the selected tile try { squares[game.CurrentTile.Dim0, game.CurrentTile.Dim1].Fill = GetItem <Rectangle>(Lselected, Lcolor).Fill; } catch (ArgumentException) { } }
private T GetItem <T>(CorpNames corp, string type) { return((T)this.FindName(corp.ToString() + type)); }