Created
December 4, 2012 19:37
-
-
Save uu59/4207850 to your computer and use it in GitHub Desktop.
Gacha Game http://makebooth.com/i/fMO1N
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
# -- coding: utf-8 | |
require "rubygems" | |
require "pry" | |
@inventory = [] | |
class Gacha | |
def initialize | |
ascii = ("\x20".."\x7e").to_a | |
@common = ["a".."z", "A".."Z", "0".."9"].map(&:to_a).flatten | |
@rare = ascii - @common - %W!( ) { } [ ] < >! | |
@srare = %W!❤ ♨ ⚡ ⚝! | |
@all = @common + @rare + @srare | |
@inventory = [] | |
@gacha_count = 0 | |
@start_at = Time.now | |
end | |
def howtoplay | |
puts <<HELP | |
> gacha [n] | |
Get new item [n] times. | |
> inventory | |
Display what you have | |
> result | |
Give up this game, display your result. | |
HELP | |
end | |
def gacha(times = 1) | |
times = [times, 10].min | |
return if times < 1 | |
@gacha_count += times | |
set = case @inventory.size | |
when 0..10 | |
@common | |
when 10..40 | |
@common + @rare | |
else | |
@common + @common + @common + @common + @rare + @rare + @srare | |
end | |
role_length = times | |
tpl = "\r %s" | |
times.times do | |
30.times do | |
role = role_length.times.map{ set.shuffle.last }.join | |
print "#{tpl}" % role | |
sleep 0.05 | |
end | |
got = set.shuffle.last | |
@inventory << got | |
print "\r" | |
print " " * 40 | |
case rank(got) | |
when :common | |
puts tpl % "#{got}" | |
when :rare | |
puts tpl % "[RARE] #{got}" | |
when :srare | |
puts "\r☆**‥…★…‥**☆◆☆**‥…★…‥**☆◆☆**‥…★…‥**☆" | |
puts " C o n g r a t u l a t i o n " | |
puts "☆**‥…★…‥**☆◆☆**‥…★…‥**☆◆☆**‥…★…‥**☆" | |
puts " [S RARE] #{got}" | |
puts | |
end | |
role_length -= 1 | |
end | |
if @inventory.uniq.length == @all.length | |
end | |
end | |
def inventory | |
count = {} | |
count.default = 0 | |
@inventory.each do |char| | |
count[char] += 1 | |
end | |
items = count.sort_by{|k,v| -v}.group_by{|item, cnt| rank(item)} | |
[:srare, :rare, :common].each do |rank| | |
puts "-"*40 | |
puts "#{rank} (remaining #{instance_variable_get(:"@#{rank}").length - (items[rank] || []).length})" | |
puts | |
next if items[rank].nil? | |
puts items[rank].uniq.map{|item, cnt| "#{item}(#{cnt})"}.join(" ") | |
puts | |
end | |
nil | |
end | |
def result | |
inventory | |
puts ".. and you lost #{calc_spend} of your life." | |
exit | |
end | |
private | |
def rank(char) | |
ranks = [:common, :rare, :srare] | |
[@common, @rare, @srare].each_with_index do |collection, rank| | |
if collection.include?(char) | |
return ranks[rank] | |
end | |
end | |
end | |
def calc_spend | |
spend = (Time.now - @start_at).to_i | |
case spend | |
when 0..60 | |
msg = "#{spend.to_i} seconds" | |
when 60..3600 | |
msg = "#{spend / 60} minutes" | |
when 3600..86400 | |
msg = "#{spend / 3600} hours" | |
else | |
msg = "#{spend / 86400} days" | |
end | |
end | |
def complete | |
if @inventory.uniq.length == @all.length | |
puts "You are complete all of items!!" | |
result | |
else | |
puts <<-MSG | |
DON'T CHEAT. YOU'VE LOST ALL ITEMS FOR THE PENALTY. | |
YOUR #{calc_spend.upcase} WAS GONE. | |
MSG | |
exit | |
end | |
end | |
end | |
g = Gacha.new | |
g.instance_eval do | |
"Welcome to this game!" | |
"Type 'howtoplay' your console if you need." | |
"Good luck!" | |
binding.pry | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment