Skip to content

Instantly share code, notes, and snippets.

@eggie5
Created November 4, 2016 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eggie5/e49d49c4188a3f5ebad2c9fc871aefca to your computer and use it in GitHub Desktop.
Save eggie5/e49d49c4188a3f5ebad2c9fc871aefca to your computer and use it in GitHub Desktop.
Pokemon Go
require "rspec/autorun"
require "singleton"
class Entity
attr_accessor :lat, :lng
def initialize(lat=0, lng=0)
self.lat = lat
self.lng = lng
end
def location
return [@lat, @lng]
end
#lets the object move in the world
def location=(tuple)
self.lat=tuple[0]
self.lng=tuple[1]
end
end
class User < Entity
attr_accessor :lat, :lng
attr_reader :pokedex
def initialize(lat=0, lng=0)
super
@pokedex=[]
end
def capture(pokemon)
if World.instance.capture(self, pokemon)
#add to my pokedex
self.pokedex.push pokemon
return true
else
return false
end
end
def to_s
self.object_id
end
end
class Pokemon < Entity
attr_reader :location
end
class World
include Singleton
THRESHOLD = 5 #capture threshold
attr_accessor :entities
def initialize
self.entities = []
end
#euclidian distance
def distance(a, b)
vec_x = (a.lat - b.lat).abs()
vec_y = (a.lng - b.lng).abs()
norm = Math.sqrt(vec_x**2+ vec_y**2)
norm.floor
end
#checks if a user and pokemon are in range
def proximity?(user, pokemon)
self.distance(user, pokemon) <= THRESHOLD
end
#what pokemon are close to a given user
def proximity(user)
pokemons = []
self.pokemons.each do |pokemon|
pokemons.push(pokemon) if proximity?(user,pokemon)
end
end
#for a given user, attempt to capture given pokemon IF in proximity
#return true/false
def capture(user, pokemon)
if proximity?(user, pokemon)
return true
else
return false
end
end
#Add entities to the game world
def add(entity)
self.entities << entity
end
#return all entities that are type Pokemon
def pokemons
self.entities.select{|entity| entity.class==Pokemon }
end
def users
self.entities.select{|entity| entity.class==User }
end
end
describe "Pokemon" do
it "should have a species" do
end
end
describe "User" do
it "should have many pokemons" do
user = User.new
pokemon = Pokemon.new
context = World.instance
context.add(user)
context.add(pokemon)
expect(user.capture(pokemon)).to eq(true)
expect(user.pokedex.length).to eq(1)
first_pokemon = user.pokedex.first
expect(first_pokemon == pokemon)
end
it "should capture a pokemon" do
context = World.instance #1 ref in memory!!
user = User.new
pokemon = Pokemon.new(3, 5)
context.add(user)
context.add(pokemon)
expect(user.capture(pokemon)).to be (true)
expect(user.pokedex.include?(pokemon)).to be(true)
end
it "can't capture pokemon that's far away" do
context = World.instance #1 ref in memory!!
user = User.new
pokemon = Pokemon.new(6, 6) #out of distance fro 0,0
context.add(user)
context.add(pokemon)
expect(user.capture(pokemon)).to be (false)
expect(user.pokedex.include?(pokemon)).to be(false)
end
it "can walk around" do
context = World.instance
user = context.users.first
old_location = user.location
expect(old_location).to eq([0,0])
#walk
user.location=[3,5]
expect(user.location).to eq([3,5])
end
end
describe "World" do
it "should have many pokemon" do
context = World.instance
user = User.new
pokemon = Pokemon.new(3, 5)
context.add(user)
context.add(pokemon)
#now get proximity
prox_pokemons = context.proximity(user)
expect(prox_pokemons.length).to eq(4)
end
it "should measure euclidian distance" do
context = World.instance
user1 = User.new
user2 = User.new(5, 5)
distance = context.distance(user1, user2)
expect(distance).to eq(7)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment