【问题标题】:Choosing players based on percentage根据百分比选择球员
【发布时间】: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


【解决方案1】:

您需要维护一个机会地图作为键和 UUID 作为值。

 private HashMap<Double, UUID> chancesOfPlayer=new HashMap<Double, UUID>();;

在sendBet函数中,创建一个本地地图并用localMap替换原始地图

     HashMap<Double, UUID> cop = new HashMap<Double, UUID>();
            for (Map.Entry<UUID,Double> e : playerBet.entrySet()) {
                UUID uid=e.getKey();
                Double userBet=e.getValue();
                Double userChance=userBet/jackpot;
                cop.put(userChance, uid);
            }
            chancesOfPlayer=cop;

那你就可以了

     chancesOfPlayer.get(chance); to get the player UUID given percentage.

在访问这个 hashMap 的过程中仍然多线程留下了复杂性,每次有新的 Bet 时都需要重新计算。

【讨论】:

  • 我已经提到了,你可以调用 chanceOfPlayer.get(chance);当您有机会需要获得玩家 uuid 时
  • 你想如何使用它
  • @mike,你有解决办法吗?
  • 是的,我使用了哈希映射,并将映射的总和除以下注,即放入另一个哈希映射中,并四舍五入并设置为整数。然后在 for 循环中将其添加到数组中,然后我进行数学运算。从数组中随机选择一个随机“盒子”,这就是赢家
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
相关资源
最近更新 更多