Created
October 3, 2021 10:05
-
-
Save hakua-doublemoon/f0df1a081177342e4ddfc57f7beb7c8a 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
NUM_OF_TEAM = 5000 | |
MAX_OF_RANK = 4 | |
class Team | |
def initialize(strength) | |
@strength = strength | |
@rank = 0 | |
@win = 0 | |
@lose = 0 | |
end | |
attr_reader :rank | |
attr_reader :strength | |
def is_rank(rank) | |
@rank == rank ? true : false | |
end | |
def win | |
@win += 1 | |
if @win == 3 && @rank < MAX_OF_RANK then | |
@win = 0 | |
@lose = 0 | |
@rank += 1 | |
end | |
end | |
def lose | |
@lose += 1 | |
if @lose == 3 && @rank > 1 then | |
@win = 0 | |
@lose = 0 | |
@rank -= 1 | |
end | |
end | |
def show | |
puts "s=%d r=%d w=%d l=%d" % [@strength, @rank, @win, @lose] | |
end | |
end | |
ranks = Array::new(MAX_OF_RANK+1, 0) | |
(MAX_OF_RANK+1).times do |rank| | |
ranks[rank] = [] | |
end | |
teams = [] | |
NUM_OF_TEAM.times do |t_no| | |
teams << Team::new(t_no) | |
ranks[0] << t_no | |
end | |
turn = 0 | |
while true | |
#rank search | |
(MAX_OF_RANK+1).times do |rank| | |
ranks[rank].each { |t_no| | |
t_rank = teams[t_no].rank | |
if t_rank != rank then | |
ranks[rank].delete(t_no) | |
ranks[t_rank].push(t_no) | |
end | |
} | |
end | |
if turn == 0 then | |
(MAX_OF_RANK+1).times do |rank| | |
puts "%2d %4d" % [rank, ranks[rank].length] | |
end | |
puts "-----------" | |
#puts teams[5000-340].show | |
turn = (gets).to_i | |
if turn == 0 then | |
break | |
end | |
end | |
turn -= 1 | |
# matching | |
(MAX_OF_RANK+1).times do |rank| | |
ranks[rank].shuffle! | |
num_of_match = ranks[rank].length / 2 | |
num_of_match.times do |m_no| | |
t1 = ranks[rank][m_no * 2] | |
t2 = ranks[rank][m_no * 2 + 1] | |
if teams[t1].strength < teams[t2].strength then | |
teams[t1].lose | |
teams[t2].win | |
else | |
teams[t1].win | |
teams[t2].lose | |
end | |
end | |
end | |
#puts turn | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment