Skip to content

Instantly share code, notes, and snippets.

@korutsuru
Forked from ovargas27/csv_user.rb
Created March 4, 2011 20:02
Show Gist options
  • Save korutsuru/855618 to your computer and use it in GitHub Desktop.
Save korutsuru/855618 to your computer and use it in GitHub Desktop.
Das de alta personajes con nombre y arma y los puedes poner a pelear, si uno se muere se borra del csv,
tulio sword
javier bat
Fermin Knife
Pancho bat
Ismael bat
alvaro axe
julio club
require './Gladiator.rb'
option=nil
#methods---------------------------------------------------------------------------------------------------------
def initialize_menu
puts "1) new character \n2) combat \n3) exit \none, two or three?"
option=gets.chomp
choose = case option
when '1' then new_character
when '2' then combat_menu
when '3' then Process.exit
end
end
def new_character
gladiator=Gladiator.new
print 'name of the character? '
gladiator.name=gets.chomp
print 'weapon of the character? '
gladiator.weapon=gets.chomp
gladiator.save
initialize_menu
end
def combat_menu
Gladiator.show_all
puts "who?(select a number) "
option=gets.chomp
puts "against?(select another number) "
combat(option.to_i,gets.chomp.to_i)
end
def combat(numa,numb)
gladiator1,gladiator2=Gladiator.find_by_index(numa), Gladiator.find_by_index(numb)
gladiators=[gladiator1,gladiator2]
states,combat_string=['','is injured','looks bad','is death'],''
counts=[0,0]
puts "#{gladiator1.name} VS #{gladiator2.name}\n"
loop{
turn=rand(2)
turn.zero?? enemy = 1 : enemy =0
rand(2).zero?? hits=true : hits=false
combat_string << "#{gladiators[turn].name} attack with his #{gladiators[turn].weapon} "
if hits then
counts[enemy]+=1
combat_string << " and hits! #{gladiators[enemy].name} #{states[counts[enemy]]} "
elsif
combat_string << "but fails"
end
combat_string << "\n"
if counts.include?(3)
gladiators[counts.index(3)].delete
break
end
}
puts combat_string
initialize_menu
end
#----------------------------------------------------------------------------------------------------------------
initialize_menu
#----------------------------------------------------------------------------------------------------------------
class Gladiator
attr_accessor :name, :weapon
def self.filepath
'combat.csv'
end
def filepath
Gladiator.filepath
end
def self.new(args=nil)
gladiator = super
unless args.nil? or args.empty?
gladiator.name, gladiator.weapon = args[0], args[1]
end
gladiator
end
def self.show_all
c=1
File.open(filepath,'r') do |file|
file.each{|line|
puts "#{c}) #{line}" if not (line.start_with?("\n"))
c+=1
}
end
end
def save
File.open(filepath,'a') do |file|
file.puts("#{self.name}, #{self.weapon}")
end
end
def self.find_by_index(index)
char_line=String.new
char_array=[]
char_line = File.readlines(filepath)[index-1] # 1
char_array = char_line.chop.split(', ') if char_line
Gladiator.new(char_array) if char_array
# 1) readlines Reads the entire file specified by name as individual lines, and returns those lines in an array.
end
def delete
file_content =[]
File.open(filepath,'r') do |file|
file_content = file.select{|line| !line.start_with?(@name) }.collect{|line| line.chop }
end
File.open(filepath,'w') do |file|
file_content.each{|line| file.puts line }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment