Skip to content

Instantly share code, notes, and snippets.

@KevinMcHugh
Created February 9, 2016 04:36
Show Gist options
  • Save KevinMcHugh/5a9e03c9357fa1ca3ec8 to your computer and use it in GitHub Desktop.
Save KevinMcHugh/5a9e03c9357fa1ca3ec8 to your computer and use it in GitHub Desktop.
a small game that asks you to guess the author of a line of code.
# Usage: ruby code_author.rb in a git repo.
score_file = "#{ENV['HOME']}/.code_author_guessing_game_score"
score = File.new(score_file, "a+").read.to_i
puts "Current score is: #{score}!"
files = `git ls-files`.split("\n")
code = ""
until code.length > 10
filename = files.sample
line = `git blame #{filename}`.split("\n").sample
name = line.match(/\((\w+ \w+)/)[1]
code = line.split(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} -\d{4}\W+\d+\)/).last
end
puts "filename: #{filename}"
puts "Identify this code snippet: #{code}"
committers = `git shortlog -sn`.split("\n").map { |l| l.split(" ").last(2).join(" ") }
committers.each_with_index do |committer, index|
puts "#{index}: #{committer}"
end
guess = gets.to_i
correct = committers[guess] == name
puts correct ? "Correct!" : "Incorrect!"
score += correct ? 1 : -1
File.write(score_file, score)
puts "New score is: #{score}!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment