【发布时间】:2016-07-05 02:02:07
【问题描述】:
在我的迷你游戏中,我有多个玩家,他们赌钱。然后这笔钱进入头奖,我使用地图获得头奖。在那之后,为了让他们中奖的百分比,我得到下注金额除以累积奖金。如何让程序根据给定的百分比选择球员?
示例:
玩家 1:下注 25 美元
头奖是:$25
玩家 2:下注 45 美元
头奖是:$70
玩家 3:下注 20 美元
头奖为:$90
玩家 4:下注 21 美元
头奖为:$111
这些玩家获得一定比例的获胜,但我如何编程让玩家 2 获胜的机会最高等
package dogboy602k.MassBet.Util;
import dogboy602k.MassBet.Main.MassBet;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import java.text.NumberFormat;
import java.util.*;
/**
* Created by dogboy on 6/18/2016.
*/
public class Manager {
private Economy economy = null;
private Player p;
private MassBet plugin;
private HashMap<UUID, Double> playerBet;
public Manager(MassBet plugin) {
this.plugin = plugin;
this.playerBet = new HashMap<>();
}
public void returninfo(Player player) {
UUID playerUUID = player.getUniqueId();
String Player = player.getName();
if (!playerBet.containsKey(playerUUID)) {
double jackpot = getTotalPotAmount();
int howManyBettors = getamountofPlayers();
Util.sendMsg(player, ChatColor.RED + "[ERROR] Seems to be you havent betted, use " + ChatColor.GREEN + "/mass bet " + player.getName() + " <amount>"+ChatColor.RED+" to bet and to be added to the Jack pot");
Util.sendMsg(player, ChatColor.AQUA + "[INFO] The Jackpot is " + ChatColor.GREEN + "$" + jackpot);
Util.sendMsg(player, ChatColor.AQUA + "[INFO] There are " + ChatColor.GREEN + howManyBettors);
return;
} else if (playerBet.containsKey(playerUUID)) {
NumberFormat defaultFormat = NumberFormat.getPercentInstance();
defaultFormat.setMinimumFractionDigits(2);
double bet = getPlayerBet(playerUUID);
double jackpot = getTotalPotAmount();
int howManyBettors = getamountofPlayers();
double chance = bet/jackpot;
Util.sendMsg(player, ChatColor.AQUA + "[INFO] Your chances are : " +defaultFormat.format(chance));
Util.sendMsg(player, ChatColor.AQUA + "[INFO] You have bet " + ChatColor.GREEN + "$" + bet);
Util.sendMsg(player, ChatColor.AQUA + "[INFO] The Jackpot is " + ChatColor.GREEN + "$" + jackpot);
Util.sendMsg(player, ChatColor.AQUA + "[INFO] There are " + ChatColor.GREEN + howManyBettors);
}
}
public void SendBet(Player player, double betAmount) {
UUID playerUUID = player.getUniqueId();
String Player = player.getName();
// check of the player got enough me money
if (!hasEnoughMoney(player, betAmount)) {
Util.sendMsg(player, ChatColor.RED + "Not Enough Money.");
}
if (hasEnoughMoney(player, betAmount)) {
/** To see the Map printed put
*
* for (Map.Entry<UUID, Double> entry : playerBet.entrySet()) {
* String key = entry.getKey().toString();
* Double value = entry.getValue();
* System.out.println("key, " + key + " value " + value);
* }
**/
if (playerBet.containsKey(playerUUID)) {
double bet = getPlayerBet(playerUUID);
Util.sendMsg(player, ChatColor.RED + "[ERROR] You have betted already");
Util.sendMsg(player, ChatColor.RED + "[ERROR] Use " + ChatColor.GREEN + "/mass retract " + player.getName() + ChatColor.RED + " to remove your bet");
Util.sendMsg(player, ChatColor.RED + "[ERROR] Your previous bet amount was: " + ChatColor.GREEN + "$" + bet);
return;
} else if (!playerBet.containsKey(playerUUID)) {
NumberFormat defaultFormat = NumberFormat.getPercentInstance();
defaultFormat.setMinimumFractionDigits(2);
addPlayerToPot(playerUUID, betAmount);
double bet = getPlayerBet(playerUUID);
double jackpot = getTotalPotAmount();
double chance = bet/jackpot;
Util.sendMsg(player, " Your chances are : " +defaultFormat.format(chance));
Util.sendMsg(player, " You have bet: " + ChatColor.GREEN + "$" + bet);
Util.sendMsg(player, getamountofPlayers() + " have bet, totaling :" + ChatColor.GREEN + "$" + jackpot);
plugin.getEconomy().withdrawPlayer(Player,betAmount);
}
}
}
public double getTotalPotAmount() {
double amount = 0;
for (Map.Entry<UUID, Double> values : playerBet.entrySet()) {
amount = values.getValue() + amount;
}
return amount;
}
public boolean hasEnoughMoney(Player player, double amount) {
if (plugin.getEconomy().getBalance(player.getName()) >= amount) {
return true;
}
return false;
}
public void addPlayerToPot(UUID playerUUID, double amount) {
this.playerBet.put(playerUUID, amount);
}
public void removePlayerFromPot(UUID playerUUID) {
this.playerBet.remove(playerUUID);
}
public Double getPlayerBet(UUID playerUUID) {
return this.playerBet.get(playerUUID);
}
public HashMap<UUID, Double > getPlayerBet() {
return this.playerBet;
}
public int getamountofPlayers() {
return this.playerBet.size();
}
public void returnTheCashInRetract(Player player){
UUID playerUUID = player.getUniqueId();
String Player = player.getName();
if (playerBet.containsKey(playerUUID)) {
double bet = getPlayerBet(playerUUID);
plugin.getEconomy().depositPlayer(player, bet);
Util.sendMsg(player, ChatColor.AQUA + "You have received " + ChatColor.GREEN + "$" + bet + ChatColor.AQUA + " due to your retract");
Util.sendMsg(player, "You have been removed from the Jackpot and game");
return;
}
else {
Util.sendMsg(player, ChatColor.RED + "[ERROR] Seems to be you havent betted, use " + ChatColor.GREEN + "/mass bet " + player.getName() + " <amount>"+ChatColor.RED+" to bet and to be added to the Jack pot");
return;
}
}
}
【问题讨论】:
-
你能发布一些关于你如何使用 hashmap 的代码吗?
-
您需要提供更多信息。头奖赢家通吃,还是像扑克一样有“边底池”?就目前而言,这个问题是无法回答的。
-
我认为你需要找到最大赌注并且你有玩家。就这么简单,为什么需要百分比?您需要在某处展示它吗?
-
@GauravAgarwal 是的学校项目
标签: java hashmap probability