Skip to content

Instantly share code, notes, and snippets.

@missingno15
Created October 2, 2014 01:10
Show Gist options
  • Save missingno15/66f70bf14cae717a4f97 to your computer and use it in GitHub Desktop.
Save missingno15/66f70bf14cae717a4f97 to your computer and use it in GitHub Desktop.
require 'csv'
require 'pry'
// external csv file has headers: name_eng, name_jpn, team
// EXAMPLE:
// name_jpn | name_eng | team
// 宮脇咲良 | Miyawaki Sakura | KIV
module Janken
def self.start(roster_file, options = {})
@roster = Roster.new(roster_file)
@brackets = Bracket.new(@roster.list).set_up_brackets
@tournament = Tournament.new(@brackets)
@tournament.begin_tournament
end
class Bracket
attr_accessor :roster, :brackets
def initialize(roster)
@roster = roster
@brackets = []
randomize!
end
def set_up_brackets
while roster.size != 1
brackets << split_into(7)
end
extra_person = roster.pop
brackets[rand(16)] << extra_person
brackets
end
private
def randomize!
1000.times { roster.shuffle! }
end
def split_into(max_range)
roster.slice!(0...max_range)
end
end
class Roster
attr_reader :list
def initialize(list)
@list = load_participants(list)
end
def load_participants(filename)
list = CSV.read(filename)
header_keys = list.shift
list = list.map { |row| Hash[ header_keys.zip(row) ] }
list.map { |args| Participant.new(args) }
end
end
class Participant
attr_reader :name_eng,
:name_jpn,
:team,
:choices
def initialize(args)
@name_eng = args['name_eng']
@name_jpn = args['name_jpn']
@team = args['team']
@choices = ['ROCK', 'PAPER', 'SCISSORS']
end
def throw_choice
choice = choices.sample
puts "#{name_eng} used #{choice}!"
choice
end
end
# TODO: Find out how to make sense of these names
class Tournament
attr_accessor :brackets,
:block_winners,
:quarterfinalists
:match
def initialize(brackets)
@brackets = brackets
@block_winners = []
@quarterfinalists = []
end
def begin_tournament
decide_block_winners
until block_winners.count == 1
block_winners << normal_round(block_winners.shift, block_winners.shift)
end
puts "winner: #{block_winners.first.name_eng}"
end
private
def decide_block_winners
brackets.each do |bracket|
case bracket.count
when 5
set_of_5(bracket)
when 6
set_of_6(bracket)
when 7
set_of_7(bracket)
when 8
set_of_8(bracket)
else
raise "Haven't figured out the rest yet."
end
end
end
def set_of_5(sub_bracket)
remaining = []
remaining << bye_round(sub_bracket.shift(3))
remaining << normal_round(sub_bracket[0], sub_bracket[1])
block_winners << normal_round(remaining[0], remaining[1])
end
def set_of_6(sub_bracket)
remaining = []
2.times { remaining << bye_round(sub_bracket.shift(3)) }
block_winners << normal_round(remaining[0], remaining[1])
end
def set_of_7(sub_bracket)
remaining = []
bye_winner = bye_round(sub_bracket.shift(3))
while sub_bracket.count >= 2
left, right = sub_bracket.shift, sub_bracket.shift
sub_bracket << normal_round(left, right)
end
block_winners << normal_round(sub_bracket[0], bye_winner)
end
def set_of_8(sub_bracket)
while sub_bracket.count >= 2
left, right = sub_bracket.shift, sub_bracket.shift
sub_bracket << normal_round(left, right)
end
block_winners << sub_bracket.first
end
def normal_round(p_one, p_two)
match = Match.new(player1: p_one, player2: p_two)
match.determine_winner
end
def bye_round(participants)
bye = participants.pop
round1_winner = Match.new(player1: participants[0],
player2: participants[1]).determine_winner
Match.new(player1: round1_winner,
player2: bye).determine_winner
end
end
class Match
attr_reader :player1, :player2,
:player1choice, :player2choice,
:logic, :retire
def initialize(args)
@player1 = args[:player1]
@player2 = args[:player2]
@retire = args.fetch(:retire, [])
@logic = {
'ROCK' => 'SCISSORS',
'SCISSORS' => 'PAPER',
'PAPER' => 'ROCK'
}
save_choices
end
def determine_winner
case
when both_players_retired?
""
when player1_retired?
player2
when player2_retired?
player1
when draw?
puts "Draw"
save_choices
determine_winner
when player1_wins?
puts "#{player1.name_eng} wins"
player1
when player2_wins?
puts "#{player2.name_eng} wins"
player2
else
raise "Something broke in the determine_winner method"
end
end
private
def save_choices
binding.pry if player1.is_a? Array
binding.pry if player2.is_a? Array
@player1choice = player1.throw_choice
@player2choice = player2.throw_choice
end
def draw?
player1choice == player2choice
end
def player1_wins?
logic[player1choice] == player2choice
end
def player2_wins?
logic[player2choice] == player1choice
end
def player1_retired?
retire.include?(player1.name_eng) ||
retire.include?(player1.name_jpn)
end
def player2_retired?
retire.include?(player2.name_eng) ||
retire.include?(player2.name_jpn)
end
def both_players_retired?
retire.include?(player1.name_eng) && retire.include?(player2.name_eng) ||
retire.include?(player1.name_jpn) && retire.include?(player2.name_jpn)
end
end
end
Janken.start('janken_roster_2014.csv')
#############################################################################################################################
#
# Some helpful links that I found and used to help me build this program
#
# http://andrewcek.wordpress.com/2013/07/27/more-refactoring-this-time-with-hashes/
# http://stackoverflow.com/questions/4855926/in-ruby-how-does-a-class-defined-in-a-module-know-the-modules-constants
#
############################################################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment