Created
October 8, 2018 14:03
-
-
Save Centaur/6622e4f529ed13b676c760e1c762d623 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.AbstractIterator | |
case class Player(name: String, rank: Int) | |
case class Position(group: Char, index: Int, player: Player) | |
val 老猪 = Player("老猪", 1719) | |
val 邢罡 = Player("邢罡(外卡)", 1500) | |
val a剑 = Player("a剑", 1528) | |
val 阮鸣强 = Player("阮鸣强", 1478) | |
val 大通 = Player("大通", 1392) | |
val 简 = Player("简", 1429) | |
val 孙刚 = Player("孙刚", 1583) | |
val 杨海 = Player("杨海", 1623) | |
val 小盛 = Player("小盛", 1564) | |
val 曹靖 = Player("曹靖", 1725) | |
val 铁道部长 = Player("铁道部长", 1524) | |
val 戴传明 = Player("戴传明", 1400) | |
val 忘找羊 = Player("忘找羊(王满银)", 1554) | |
val 凰翩 = Player("凰翩(外卡)", 1500) | |
val 淋漓尽致 = Player("淋漓尽致", 1400) | |
val Jacky = Player("Jacky", 1408) | |
val 飞鱼 = Player("飞鱼(外卡)", 1500) | |
val 袁卫强 = Player("袁卫强", 1537) | |
val 冬毅 = Player("冬毅(外卡)", 1500) | |
val 孙飞 = Player("孙飞(外卡)", 1500) | |
val NAP = Player("Not A Player", 0) | |
val PH = Position('-', 0, NAP) // 轮空位 | |
val players = List( | |
老猪, a剑, 淋漓尽致, 阮鸣强, 孙刚, | |
忘找羊, 袁卫强, 铁道部长, 杨海, 简, | |
小盛, Jacky, 戴传明, 曹靖, 冬毅, | |
孙飞, 凰翩, 飞鱼, 大通, 邢罡 | |
) | |
//players.sortBy(_.rank).reverse.grouped(4).zipWithIndex.map { case (four, i) => if ((i & 0x01) == 1) four.reverse else four }.toList.transpose.zipWithIndex.flatMap { case (players, i) => players.zipWithIndex.map { case (player, j) => Position(('A'.toInt + i).toChar, j + 1, player) } }.groupBy(_.group) | |
// | |
def padded(players: List[Player]): List[Player] = { | |
val len = players.length | |
players.padTo(len + (4 - (len % 4)) % 4, NAP) | |
} | |
def to_group(players: List[Player]): Map[Char, List[Position]] = { | |
val sorted_by_rank = padded(players).sortBy(_.rank).reverse | |
val windowed_by_4 = sorted_by_rank.grouped(4) | |
val transposed = windowed_by_4.zipWithIndex.map { case (four, i) => if ((i & 0x01) == 1) four.reverse else four }.toList.transpose | |
val all = for { | |
(ps, i) <- transposed.zipWithIndex | |
(p, j) <- ps.zipWithIndex if p != NAP | |
} yield Position(('A'.toInt + i).toChar, j + 1, p) | |
all.groupBy(_.group) | |
} | |
to_group(players) | |
def beiger(player_count: Int) = new AbstractIterator[(Array[Int], Array[Int])] { | |
private val odd = (player_count & 0x01) == 1 | |
private val per_round = (player_count + 1) >> 1 | |
private val rounds = if (odd) player_count else player_count - 1 | |
private var left = Array.range(1, per_round + 1) | |
private var right = Array.range(per_round + 1, player_count + 1).reverse | |
if (odd) right = 0 +: right | |
private var next_round = 1 | |
override def hasNext = next_round <= rounds | |
override def next() = { | |
if (hasNext && next_round > 1) { | |
val new_left = Array.ofDim[Int](per_round) | |
val new_right = Array.ofDim[Int](per_round) | |
if ((next_round & 0x01) == 0) { | |
new_left(0) = right(0) | |
new_right(0) = right(per_round-1) | |
var cursor = new_right(0) | |
for (i <- 1 until per_round) { | |
cursor -= 1 | |
if (cursor == 0) cursor = rounds | |
new_right(i) = cursor | |
} | |
cursor = new_right(0) | |
for (i <- 1 until per_round) { | |
cursor += 1 | |
if (cursor > rounds) cursor = 1 | |
new_left(i) = cursor | |
} | |
} else { | |
new_left(0) = right(per_round-1) | |
new_right(0) = left(0) | |
var cursor = new_left(0) | |
for (i <- 1 until per_round) { | |
cursor -= 1 | |
if (cursor == 0) cursor = rounds | |
new_right(i) = cursor | |
} | |
cursor = new_left(0) | |
for (i <- 1 until per_round) { | |
cursor += 1 | |
if (cursor > rounds) cursor = 1 | |
new_left(i) = cursor | |
} | |
} | |
left = new_left | |
right = new_right | |
} | |
next_round += 1 | |
(left, right) | |
} | |
} | |
for { | |
(l, r) <- beiger(8) | |
i <- l.indices | |
} { | |
println(s"${l(i)}-${r(i)}") | |
} | |
for { | |
(l, r) <- beiger(7) | |
i <- l.indices | |
} { | |
println(s"${l(i)}-${r(i)}") | |
} | |
for { | |
(l, r) <- beiger(5) | |
i <- l.indices if l(i) !=0 && r(i) !=0 | |
} { | |
println(s"${l(i)}-${r(i)}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment