+ # This tries to avoid a human-human match
+ #
+ class StartGameWithoutHumans < AbstractStartGame
+ def match(players)
+ super
+ log_players(players)
+ if players.size < 2
+ log_warning("Floodgate: There should be more than one player (%d)." % [players.size])
+ return
+ elsif players.size == 2
+ start_game_shuffle(players)
+ return
+ end
+
+ loop do
+ humans = get_human_indexes(players)
+ log_message("Floodgate: There are (still) %d humans." % [humans.size])
+ break if humans.size < 2
+
+ pairing_possible = false
+ for i in 0..(humans.size-2) # -2
+ next if humans[i].odd?
+ if humans[i]+1 == humans[i+1]
+ pairing_possible = humans[i]
+ break
+ end
+ end
+ unless pairing_possible
+ log_message("Floodgate: No possible human-human match found")
+ break
+ end
+
+ current_index = pairing_possible
+ j = [0, current_index - 2].max
+ while j < players.size
+ break if players[j].is_computer?
+ j += 1
+ end
+
+ pairing_indexes = []
+ if j == players.size
+ # no computer player found
+ pairing_indexes << current_index << current_index+1
+ else
+ # a comupter player found
+ pairing_indexes << current_index << j
+ end
+
+ pair = []
+ pair << players.delete_at(pairing_indexes.max)
+ pair << players.delete_at(pairing_indexes.min)
+ start_game_shuffle(pair)
+ end # loop
+
+ while (players.size >= 2) do
+ pair = players.shift(2)
+ start_game_shuffle(pair)
+ end
+ end
+
+ private
+
+ def get_human_indexes(players)
+ ret = []
+ for i in 0..(players.size-1)
+ ret << i if players[i].is_human?
+ end
+ return ret