DEV Community

Cover image for MarvelBattle#ruby
Cicada0315
Cicada0315

Posted on

MarvelBattle#ruby

Keep thinking then you can find a better way to do things. But, if you stuck more than an hour for one thing, stop! You are not going to get anywhere. Try to rest for a while and try again. Fresh view will help you figure out. -Cicada

Day1

Step1: Pick an API

Deciding which API you are going to use is not esay yet, most important step. Try to see what is available to you and match with what you want to make. I want my program to be fun and efficient so I made my decision with Marvel API!

Marvel API: https://developer.marvel.com/
API list link: https://apilist.fun/

NOTE: Some API is easy to access but some of them you need to sign-up

Step2: Make connection to Marvel API

1. Get an API Key:

In order to use marvel API, you have to sign up and get a key

2. Creating url that can connect to marvel API:

I want to get the list of all the marvel characters and this was what I used.

base_endpoint= "https://gateway.marvel.com"
ts=Time.now.to_i.to_s
url_hash=Digest::MD5.hexdigest("#{ts}#{PRIVATEKEY}#{PUBLICKEY}"
url=base_endpoint+"/v1/public/characters?ts=#{ts}&apikey=#{PUBLICKEY}&hash=#{url_hash}"
Enter fullscreen mode Exit fullscreen mode
More info: https://developer.marvel.com/documentation/generalinfo

Day2 & 3

Feature1: Display all the characters by it's name

Using connection with marvel api, I made each character's object that contains below information.

Character {
id (int, optional): The unique ID of the character resource.,
name (string, optional): The name of the character.,
description (string, optional): A short bio or description of the character.,
modified (Date, optional): The date the resource was most recently modified.,
resourceURI (string, optional): The canonical URL identifier for this resource.,
urls (Array[Url], optional): A set of public web site URLs for the resource.,
thumbnail (Image, optional): The representative image for this character.,
comics (ComicList, optional): A resource list containing comics which feature this character.,
stories (StoryList, optional): A resource list of stories in which this character appears.,
events (EventList, optional): A resource list of events in which this character appears.,
series (SeriesList, optional): A resource list of series in which this character appears.

}

link: https://developer.marvel.com/docs#!/public/getCreatorCollection_get_0

Once you made character objects then simply calling down code will give you list of marvel characters

def display_characters
  Marvel_Characters.all.each.with_index(1) do |character, index|
    puts "#{index}. #{character.name}"
  end
end

#Welcome to MarvelBattle
#We always fight for justice!
#1. 3-D Man
#2. A-Bomb (HAS)
#3. A.I.M.
#4. Aaron Stack
#5. Abomination (Emil Blonsky)
#6. Abomination (Ultimate)
#7. Absorbing Man
#8. Abyss
#9. Abyss (Age of Apocalypse)
#10. Adam Destine
...
Enter fullscreen mode Exit fullscreen mode

NOTE: when getting characters the default is only 20 but there is total of 1493 characters so if you want to get more characters you have to change offset.

Feature2: Show user choice of character's information

Interact with user via termial. I gave options to user so that they can see detailed information of each of the character that they choose.

Character_information option:
Enter 1: Image
Enter 2: Abilities
Enter 3: Comics
Enter 4: Series
Enter 5: Description

Feature3: MarvelBattle (User vs Computer)

Step1: Geting each of the character's ability.

This was really tricky since I cannot get this information directly. I used 'nokogiri' to able to make it work (scraping). I tried add this ability to each of the object when I make the character's object for the first time but it took so long since I have total of 1493 characters. I end up deceide add ability only when user want to see the ability of the character or when they are ready to play. (which gives me 1 vs 1493 or 6 vs 1493)

Step2: Play setting

When user select three character, computer will choose ramdom three characters as well. then user is ready to play!

def initialize(player, computer)
  puts "#{player.name} VS Computer"
  puts "Match1: #{player.my_characters_arr[0].name} VS #{computer.character_arr[0].name}"
  puts "----------------------------------"
  puts "Match2: #{player.my_characters_arr[1].name} VS #{computer.character_arr[1].name}"
  puts "----------------------------------"
  puts "Match3: #{player.my_characters_arr[2].name} VS #{computer.character_arr[2].name}"
  puts "----------------------------------"
end
#Username VS Computer
#Match1: A-Bomb (HAS) VS Agent Brand
#----------------------------------
#Match2: 3-D Man VS Air-Walker (Gabriel Lan)
#----------------------------------
#Match3: A.I.M. VS 3-D Man
#----------------------------------
Enter fullscreen mode Exit fullscreen mode

Day4

Play

Each of the marvel character has six abilities with rate.
[durability, energy, fighting_skills, intelligence, speed, strength]
Each match, program will compute each character's ability and tells you which character won for that match. When you enter 4 will tells you who won the battle.

Enter 1: Match1
Enter 2: Match2
Enter 3: Match3
Enter 4: Winner of this battle
Enter 5: Go back to option
Enter 6: To end the program

Future

Impovement1: Able to show image in new browser when using wsl

For now I made error handling block for the image, it's not working with wsl. I will keep working on that to able to show image in new browser even when using wsl.

Improvement2: Make a game more interactive

For now porgram jsut computes each of the character's each of the character's ability and show the result. In the future I want to make it more interactive like give option to user so that they can choose ability they want to use for attack or defends.

This was my first project using ruby. Some of the part was hard but most of time it was fun. I hope this blog helps when you are acutally runing my program or trying to make new program with Marvel API.

Top comments (0)