[Ruby] Pokemon Go made with FakerGem

1 minute read

Background

Use various gems in the process of programming learning. Looking at the source data on GitHub to get a better understanding of the Gem, I found something interesting.

Faker::Games::Pokemon

Faker::Games::Pokemon.name #=> “Pikachu” Faker::Games::Pokemon.location #=> “Pallet Town” Faker::Games::Pokemon.move #=> “Thunder Shock”

There is a function to randomly display the names of Pokemon. The best way to understand FakerGem is to try it out! So, I made an application to encounter and capture Pokemon.

Code

application.rb


# Loading FakerGem and setting Japanese
require "Faker"
Faker::Config.locale = :ja

# Pokemon class and player class file loading
require "./pokemon"
require "./player"

# Generate Pokemon
pokemon_name = Faker::Games::Pokemon.name
pokemon = Pokemon.new(pokemon_name)

# Create player
player = Player.new()

# Pokemon appears!
pokemon.appear

# Player behavior
player.command(pokemon_name)

pokemon.rb


class Pokemon

  def initialize(name)
    @name = name
  end

  def appear
    puts "Oh! The wild #{@name} is popping out!"
  end

end

player.rb


class Player

  def command(pokemon)
    puts "What do you do?"
    puts "[0] Throw a monster ball\n[1] Run away"
    input = gets.to_i
      if input == 0
        puts "Done! I caught #{pokemon}!"
      elsif input == 1
        puts "I was able to do my best!"
      else
        puts "wrong number"
      end
  end

end

terminal

Summary

Since the Pokemon that appear are random, there is “unpredictable fun”. Not to mention FakerGem, it is a great learning to make even a small amount of code such as “object-oriented” and “method thinking” from scratch. There are various options such as “capture rate fluctuation” and “town movement”, so let’s find time and play with it.

Tags: ,

Updated: