2019独角兽企业重金招聘Python工程师标准>>>
假设有100000个人参与游戏,每次按级别匹配,输赢随机,大概400局出至尊星耀I
//author:zbg
//2018-12-12 10:58
print = console.log;
function Player()
{
this.grade = 1; //段位
this.level = 0; //星星
this.score = 0; //勇者积分。简化起见,每参赛一次1次加1分,20分可换星,10分可保护段位不掉。
if (typeof Player._initialized == "undefined")
{
Player.grade_max_level =
{
1: {grade: "倔强青铜III", max_level: 3},
2: {grade: "倔强青铜II", max_level: 3},
3: {grade: "倔强青铜I", max_level: 3},
4: {grade: "秩序白银III", max_level: 3},
5: {grade: "秩序白银II", max_level: 3},
6: {grade: "秩序白银I", max_level: 3},
7: {grade: "荣耀黄金III", max_level: 4},
8: {grade: "荣耀黄金II", max_level: 4},
9: {grade: "荣耀黄金I", max_level: 4},
10: {grade: "尊贵铂金III", max_level: 4},
11: {grade: "尊贵铂金II", max_level: 4},
12: {grade: "尊贵铂金I", max_level: 4},
13: {grade: "永恒钻石V", max_level: 5},
14: {grade: "永恒钻石IV", max_level: 5},
15: {grade: "永恒钻石III", max_level: 5},
16: {grade: "永恒钻石II", max_level: 5},
17: {grade: "永恒钻石I", max_level: 5},
18: {grade: "至尊星耀V", max_level: 5},
19: {grade: "至尊星耀IV", max_level: 5},
20: {grade: "至尊星耀III", max_level: 5},
21: {grade: "至尊星耀II", max_level: 5},
22: {grade: "至尊星耀I", max_level: 5}
};
Player.cmp = function(p1, p2)
{
if(p1.grade > p2.grade)
return 1;
else if(p1.grade < p2.grade)
return -1;
if(p1.level > p2.level)
return 1;
else if(p1.level < p2.level)
return -1;
return 0;
};
Player.prototype.level_up = function()
{
this.level++;
if(this.level > Player.grade_max_level[this.grade].max_level)
{
this.grade++;
this.level = 0;
if(this.grade > 22)
{
this.grade = 22;
this.level = 5;
}
}
}
Player.prototype.level_down = function()
{
this.level--;
if(this.level < 0)
{
this.grade--;
if(this.grade < 1 )
{
this.grade = 1;
this.level = 0;
}
else
{
this.level = Player.grade_max_level[this.grade].max_level;
}
}
}
Player.prototype.win = function()
{
this.level_up();
this.score++;
if(this.score > 20)//勇者积分加星
{
this.score = 0;
this.level_up();
}
}
Player.prototype.lose = function()
{
this.score++;
if(this.level == 0 && this.score >= 10)//段位保护
this.score -= 10;
else
this.level_down();
}
}
Player._initialized = true;
}
players = new Array(100000);
for(var i = 0; i < 100000; i++)
{
players[i] = new Player();
}
print("ok");
function print_state()
{
d = {};
for(var i = 1; i <= 22; i++)
{
d[i] = {};
for(var j = 0; j <= Player.grade_max_level[i].max_level; j++)
{
d[i][j] = 0;
}
}
for(var i = 0; i < 100000; i++)
{
d[players[i].grade][players[i].level]++;
}
for(var i = 1; i <= 22; i++)
{
s = Player.grade_max_level[i].grade;
ss = 0;
sss = "";
for(var j = 0; j <= Player.grade_max_level[i].max_level; j++)
{
sss +=j + ": " + d[i][j] + ", ";
ss += d[i][j];
}
print(s + ": " + ss + ": " + sss);
}
}
function battle(n)
{
for(var i = 0; i < n; i++)
{
players.sort(Player.cmp);//按级别排序后,相邻匹配
for(var j = 0; j < 100000; j += 2)
{
if(Math.random() > .5)
{
players[j].win();
players[j + 1].lose();
}
else
{
players[j + 1].win();
players[j].lose();
}
}
}
}
battle(400);
print_state();
输出:
ok
倔强青铜III: 942: 0: 239, 1: 233, 2: 232, 3: 238,
倔强青铜II: 1615: 0: 412, 1: 417, 2: 397, 3: 389,
倔强青铜I: 2559: 0: 678, 1: 659, 2: 612, 3: 610,
秩序白银III: 3630: 0: 995, 1: 953, 2: 882, 3: 800,
秩序白银II: 5017: 0: 1421, 1: 1304, 2: 1191, 3: 1101,
秩序白银I: 6407: 0: 1824, 1: 1672, 2: 1512, 3: 1399,
荣耀黄金III: 9934: 0: 2349, 1: 2165, 2: 1976, 3: 1803, 4: 1641,
荣耀黄金II: 11189: 0: 2721, 1: 2462, 2: 2237, 3: 2004, 4: 1765,
荣耀黄金I: 11632: 0: 2934, 1: 2576, 2: 2297, 3: 2066, 4: 1759,
尊贵铂金III: 11175: 0: 2871, 1: 2488, 2: 2254, 3: 1908, 4: 1654,
尊贵铂金II: 9922: 0: 2586, 1: 2267, 2: 1932, 3: 1710, 4: 1427,
尊贵铂金I: 8225: 0: 2210, 1: 1871, 2: 1623, 3: 1408, 4: 1113,
永恒钻石V: 7466: 0: 1802, 1: 1466, 2: 1340, 3: 1105, 4: 975, 5: 778,
永恒钻石IV: 4823: 0: 1183, 1: 993, 2: 829, 3: 734, 4: 593, 5: 491,
永恒钻石III: 2804: 0: 704, 1: 589, 2: 496, 3: 400, 4: 356, 5: 259,
永恒钻石II: 1472: 0: 379, 1: 321, 2: 248, 3: 221, 4: 171, 5: 132,
永恒钻石I: 706: 0: 191, 1: 151, 2: 122, 3: 98, 4: 84, 5: 60,
至尊星耀V: 303: 0: 86, 1: 62, 2: 57, 3: 39, 4: 32, 5: 27,
至尊星耀IV: 120: 0: 33, 1: 22, 2: 26, 3: 15, 4: 13, 5: 11,
至尊星耀III: 41: 0: 10, 1: 10, 2: 7, 3: 6, 4: 6, 5: 2,
至尊星耀II: 12: 0: 4, 1: 4, 2: 0, 3: 2, 4: 2, 5: 0,
至尊星耀I: 6: 0: 2, 1: 2, 2: 0, 3: 1, 4: 0, 5: 1,