2016-07-05 117 views
-3

在我的小遊戲中,我有多個玩家,他們賭錢。這筆錢然後進入一個大獎,我用一個地圖來獲得大獎。之後,爲了讓他們獲得一定比例的贏利,我得到了下注金額除以中獎。我該如何讓程序根據給定的百分比選擇一名玩家?根據百分比選擇球員

實施例:

播放器1:下注$ 25

JACKPOT IS:$ 25

播放器2:下注$ 45

JACKPOT IS:$ 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; 
    } 
} 

} 
+1

你可以發表一些關於你如何使用散列表的代碼嗎? –

+0

您需要提供更多信息。中獎者是否勝利,或者撲克中是否有「邊盆」?就目前來看,這個問題無法回答。 –

+0

我認爲你需要找到最大的賭注,你有球員。這很簡單,爲什麼你需要百分比?你需要在某個地方展示它嗎? –

回答

0

您需要維護一個作爲鍵值和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. 

不過存在時的複雜性留給多線程每次有新的Bet時都需要重新計算這個hashMap。

+0

好的我已經這麼做了,現在該怎麼辦? – mike

+0

我已經提到過,你可以打電話給chancesOfPlayer.get(機會);當你需要得到一個球員uuid給你有機會 –

+0

是的,但我的程序將如何選擇它,球員 – mike