游戏竞技对抗赛排队分组问题
2012-09-11 17:29
211 查看
游戏中要排一个对抗赛, 可以是nVSn, 如3v3, 5v5, 20v20, 玩家可以以小队形式参加排队, 也可以个人参加排队.
用一个数据记录每种队的的个数, 没有这种类型的队, 用0表示, 下面是一个11v11的组合. 如下
local strData = "5,8,7,3,1,0,0,1,0,1,0";
表示1个人的队伍为5个, 2人的队伍为8个, ...11人的队伍为0个.
============================
function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
function GetList(list)
local usedlist = {};
i = #list;
total = 0;
repeat
local num = tonumber(list[i]);
if num > 0 and total + i <= #list then
usedlist[#usedlist + 1] = i;
total = total + i;
list[i] = tostring(num - 1);
if(total == #list) then
return usedlist;
end
else
i = i - 1;
end
until i < 1
return nil;
end
function PrintTable(list)
for i = 1, #list
do
str = string.format("index %d: value = %s", i, list[i]);
print(str);
end
end
--1人队伍有5个, 2人队伍有8个,..11人队伍有0个.
local strData = "5,8,7,3,1,0,0,1,0,1,0";
local list = Split(strData, ",");
--GetList会修改list的值, 移除已经用过的值
local usedlist = GetList(list);
local usedlist2 = GetList(list);
PrintTable(usedlist);
PrintTable(usedlist2);
上面可以找出另几个队伍参加对抗赛, 一组为10人队与一个1人队合成一方, 另一组是一个8人队与另外一个3人队合成一方.
另外还要一个表格来记录, 1人队具体玩家的id, 2人队具体玩家的id, 3人队具体玩家的id...
...
===================
新版本:
--分隔字符串变为表格
function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
--从队列中取得一种队伍分法
--list表如果长度为n, 就会尝试找出一种n人的方案
function GetList(list)
local usedlist = {};
i = #list;
total = 0;
repeat
local num = tonumber(list[i]);
if num > 0 and total + i <= #list then
usedlist[#usedlist + 1] = i;
total = total + i;
list[i] = tostring(num - 1);
if(total == #list) then
return usedlist;
end
else
i = i - 1;
end
until i < 1
return nil;
end
--打印表格中的数据
function PrintTable(list)
for i = 1, #list
do
str = string.format("index %d: value = %s", i, list[i]);
print(str);
end
end
--表格转成字符串, 用seperator相间隔
function List2Str(list, seperator)
local str = "";
for i = 1, #list do
str = str..tostring(list[i])..seperator
end
return TrimEnd(str, seperator);
end
--去除结尾指定一个字符
function TrimEnd(str, seperator)
if(string.sub(str, -1) == seperator) then
return string.sub(str, 0, -2);
end
return str;
end
--在players列表中增加players
function AddPalyers(strPlayers, players)
--修改strPlayers中的players
local list = Split(strPlayers, "|");
local count = table.getn(Split(players, ","));
if(list[count] == "") then
list[count] = players;
else
list[count] = list[count]..","..players;
end
--重组字符串
return List2Str(list, "|");
end
--移除players列表中的player count为位置, 也是移除的个数
function RemovePlayers(strPlayers, count)
local list = Split(strPlayers, "|");
local players = Split(list[count], ",");
if(#players <= count) then
list[count] = "";
else
local str = "";
for i = count + 1, #players do
str = str..tostring(players[i])..","
end
list[count] = TrimEnd(str, ",");
end
return List2Str(list, "|");
end
--得到队列中玩家总人数
function GetPlayerCount(strQueue)
local list = Split(strQueue, ",");
local total = 0;
for i = 1, #list do
total = total + tonumber(list[i]) * i;
end
return total;
end
--检查玩家是否在列表中
function PlayerIsInList(strPlayers, playerId)
local strPlayerId = tostring(playerId);
local list = Split(strPlayers, "|");
for i = 1, #list do
if list[i] ~= nil and list[i] ~= "" then
local players = Split(list[i], ",");
for j= 1, #players do
if players[j] == strPlayerId then
return true;
end
end
end
end
return false;
end
--1人队伍有5个, 2人队伍有8个,..11人队伍有0个.
local strQueue = "5,8,7,3,1,0,0,1,0,1,0";
local list = Split(strQueue, ",");
--GetList会修改list的值, 移除已经用过的值
local usedlist = GetList(list);
if(usedlist ~= nil) then
PrintTable(usedlist);
local usedlist2 = GetList(list);
if(usedlist2 ~= nil) then
PrintTable(usedlist2);
end
end
print(List2Str(list, ","));
--strPlayers各队类型之间用|相隔, 各player之间用, 相隔. 同一类型的队在一起, 用,相隔
local strPlayers = "123,123,213|213,213,123,213|||||||||";
local result = AddPalyers(strPlayers, "444,4444,444,4444");
print(result);
result = RemovePlayers(strPlayers, 2);
print(result);
print(tostring(GetPlayerCount(strQueue)));
print(PlayerIsInList(strPlayers, 723));
用一个数据记录每种队的的个数, 没有这种类型的队, 用0表示, 下面是一个11v11的组合. 如下
local strData = "5,8,7,3,1,0,0,1,0,1,0";
表示1个人的队伍为5个, 2人的队伍为8个, ...11人的队伍为0个.
============================
function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
function GetList(list)
local usedlist = {};
i = #list;
total = 0;
repeat
local num = tonumber(list[i]);
if num > 0 and total + i <= #list then
usedlist[#usedlist + 1] = i;
total = total + i;
list[i] = tostring(num - 1);
if(total == #list) then
return usedlist;
end
else
i = i - 1;
end
until i < 1
return nil;
end
function PrintTable(list)
for i = 1, #list
do
str = string.format("index %d: value = %s", i, list[i]);
print(str);
end
end
--1人队伍有5个, 2人队伍有8个,..11人队伍有0个.
local strData = "5,8,7,3,1,0,0,1,0,1,0";
local list = Split(strData, ",");
--GetList会修改list的值, 移除已经用过的值
local usedlist = GetList(list);
local usedlist2 = GetList(list);
PrintTable(usedlist);
PrintTable(usedlist2);
上面可以找出另几个队伍参加对抗赛, 一组为10人队与一个1人队合成一方, 另一组是一个8人队与另外一个3人队合成一方.
另外还要一个表格来记录, 1人队具体玩家的id, 2人队具体玩家的id, 3人队具体玩家的id...
...
===================
新版本:
--分隔字符串变为表格
function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
--从队列中取得一种队伍分法
--list表如果长度为n, 就会尝试找出一种n人的方案
function GetList(list)
local usedlist = {};
i = #list;
total = 0;
repeat
local num = tonumber(list[i]);
if num > 0 and total + i <= #list then
usedlist[#usedlist + 1] = i;
total = total + i;
list[i] = tostring(num - 1);
if(total == #list) then
return usedlist;
end
else
i = i - 1;
end
until i < 1
return nil;
end
--打印表格中的数据
function PrintTable(list)
for i = 1, #list
do
str = string.format("index %d: value = %s", i, list[i]);
print(str);
end
end
--表格转成字符串, 用seperator相间隔
function List2Str(list, seperator)
local str = "";
for i = 1, #list do
str = str..tostring(list[i])..seperator
end
return TrimEnd(str, seperator);
end
--去除结尾指定一个字符
function TrimEnd(str, seperator)
if(string.sub(str, -1) == seperator) then
return string.sub(str, 0, -2);
end
return str;
end
--在players列表中增加players
function AddPalyers(strPlayers, players)
--修改strPlayers中的players
local list = Split(strPlayers, "|");
local count = table.getn(Split(players, ","));
if(list[count] == "") then
list[count] = players;
else
list[count] = list[count]..","..players;
end
--重组字符串
return List2Str(list, "|");
end
--移除players列表中的player count为位置, 也是移除的个数
function RemovePlayers(strPlayers, count)
local list = Split(strPlayers, "|");
local players = Split(list[count], ",");
if(#players <= count) then
list[count] = "";
else
local str = "";
for i = count + 1, #players do
str = str..tostring(players[i])..","
end
list[count] = TrimEnd(str, ",");
end
return List2Str(list, "|");
end
--得到队列中玩家总人数
function GetPlayerCount(strQueue)
local list = Split(strQueue, ",");
local total = 0;
for i = 1, #list do
total = total + tonumber(list[i]) * i;
end
return total;
end
--检查玩家是否在列表中
function PlayerIsInList(strPlayers, playerId)
local strPlayerId = tostring(playerId);
local list = Split(strPlayers, "|");
for i = 1, #list do
if list[i] ~= nil and list[i] ~= "" then
local players = Split(list[i], ",");
for j= 1, #players do
if players[j] == strPlayerId then
return true;
end
end
end
end
return false;
end
--1人队伍有5个, 2人队伍有8个,..11人队伍有0个.
local strQueue = "5,8,7,3,1,0,0,1,0,1,0";
local list = Split(strQueue, ",");
--GetList会修改list的值, 移除已经用过的值
local usedlist = GetList(list);
if(usedlist ~= nil) then
PrintTable(usedlist);
local usedlist2 = GetList(list);
if(usedlist2 ~= nil) then
PrintTable(usedlist2);
end
end
print(List2Str(list, ","));
--strPlayers各队类型之间用|相隔, 各player之间用, 相隔. 同一类型的队在一起, 用,相隔
local strPlayers = "123,123,213|213,213,123,213|||||||||";
local result = AddPalyers(strPlayers, "444,4444,444,4444");
print(result);
result = RemovePlayers(strPlayers, 2);
print(result);
print(tostring(GetPlayerCount(strQueue)));
print(PlayerIsInList(strPlayers, 723));
相关文章推荐
- 排队游戏(括号匹配问题2)
- !HDU 4293 排队分组说谎问题-dp-(不相交区间的最大个数)
- 周赛问题 福州 竞技游戏
- 关于分组后字段拼接的问题
- POJ 2828 Buy Tickets(排队问题,线段树应用)
- 3.mysql的中文问题,database级操作,表级操作,数据CRUD,分组操作,时间和日期,字符串相关函数,表的约束
- 拇指接龙游戏从WIN32向Android移植过程问题记录(2)
- 使用dataframe解决spark TopN问题:分组、排序、取TopN和join相关问题
- PTA 7-6(队列) 银行排队问题之单队列多窗口加VIP服务(30 分) 30分代码 大模拟
- 游戏中帧函数调用的顺序问题——基于HGE引擎的打砖块游戏
- Unity 手机游戏屏幕翻转问题
- 关于分组后字段拼接的问题
- Codevs_P1033 蚯蚓的游戏问题(拆点网络流+最小费用流)
- HDU 2191汶川大地震(分组背包问题)
- 扑克牌游戏问题
- 20160324 codevs 2956 排队问题,poj Ancient Cipher,uva 253 - Cube painting
- 一个排列组合的分组问题
- Python利用带权重随机数解决抽奖和游戏爆装备问题
- Android游戏制作退出框遇到的问题:object not locked by thread before wait()和Can't create handler inside thread
- 【iOS-Cocos2d游戏开发之七】在cocos2d中添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题!【11月28日更新解决添加组件Cocos2d动画停止播放的BUG】