简体   繁体   English

如何随机化然后拆分 ArrayList<string> 分成两个偶数 ArrayList</string>

[英]How to randomize and then split an ArrayList<String> into two even ArrayLists

i know virtually no java but i'm trying to learn some for this project.我几乎不知道 java 但我正在尝试为这个项目学习一些。 I am trying to modify a program called GyaPickupBot which is basically a "pick up game" bot on IRC where players can type: .add in order to get added to a list of players who want to play a game and once enough players.add the bot announces the ip of the game server (ie, quake server) that the game is to be played on, right now.我正在尝试修改一个名为 GyaPickupBot 的程序,它基本上是 IRC 上的“拾取游戏”机器人,玩家可以在其中键入: .add 以便添加到想要玩游戏的玩家列表中,并且一旦有足够的玩家。添加机器人现在宣布游戏服务器(即地震服务器)的 ip。 when the specified number of players.add and the game launches.当指定数量的 player.add 和游戏启动时。 the bot only lists the players who previously.add'ed before the max number of players was reached: I would like for it to divide all of the players who have !add'ed up into 2 random teams of equal size.该机器人仅列出在达到最大玩家数量之前之前添加的玩家:我希望它将所有已添加的玩家分成 2 个大小相等的随机团队。 I have already done the random part using Collections.shuffle but I have no idea how to divide the players into 2 equally sized teams.我已经使用 Collections.shuffle 完成了随机部分,但我不知道如何将玩家分成 2 个大小相等的团队。 I emailed the author who is based in Japan a few weeks ago and he finally replied this morning with some very vague hints on how to do this:几周前我给住在日本的作者发了电子邮件,他今天早上终于回复了一些关于如何做到这一点的非常模糊的提示:

Well... at this time, I don't have much motivation to maintain this code.嗯……这个时候,我没有太多的动力去维护这段代码。 I can only suggest you some hints.我只能给你一些提示。

private boolean handleReady(String channel, String sender, String login, String hostname, String message) {
boolean isUpdate = false;
String readyGameID = mgr.getReadyGameID();
if (null != readyGameID) {
// *** add some code here to choose teams and store that result to string variable. something like: "team1: ,,,, team2: ,,,,,"
// *** you can get players list by mgr.getPlayers(readyGameID) in order to divide players to 2 teams randomly 

for (String ch : getChannels()) {
sendMessage(ch, mgr.getPickupReadyString(readyGameID));
// sendNotice(ch, mgr.getPickupReadyString(readyGameID));
// *** then, send that string to channel
}
mgr.setLastGame(Calendar.getInstance().getTimeInMillis(), mgr.getPickupReadyString(readyGameID));
ArrayList<String> players = mgr.getPlayers(readyGameID);
for (String nick : players) {
sendNotice(nick, mgr.getPickupReadyPMString(readyGameID));
// *** and send that string to players too
}
isUpdate = mgr.clearPlayers(players);
}
return isUpdate;

I know this is probably more than what is asked on here, but I'm really trying to learn this but I can't figure it out and any help would be appreciated我知道这可能比这里要求的要多,但我真的很想学习这个,但我无法弄清楚,任何帮助将不胜感激

The code below is untested, but should give you an idea on how to proceed.下面的代码未经测试,但应该让您了解如何继续。 Good luck.祝你好运。

// retrieve all players
ArrayList<String> players = mgr.getPlayers(readyGameID);
// randomize the list
Collections.shuffle(players);
// instantiate two arraylists for the teams
ArrayList<String> teamRed = new ArrayList<String>();
ArrayList<String> teamBlue = new ArrayList<String>();

// add the first half of players to teamRed
teamRed.addAll(players.subList(0, players.size() / 2 + players.size()%2));
// and the second half to teamBlue
teamBlue.addAll(players.subList(players.size() / 2 + players.size()%2, players.size()));

// now do whatever you need to do with the two teams

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM