Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grupo 2 #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 37 additions & 41 deletions main.rb
Original file line number Diff line number Diff line change
@@ -1,68 +1,64 @@
class Pokemon
attr_reader :nombre, :ataque, :defensa
def initialize(nombre, ataque, defensa)
@nombre = nombre
@ataque = ataque
@defensa = defensa
end

def increase_atk(number=1)
@ataque += number
end

def increase_def(number=1)
@defensa += number
end

def get_stats()
"#{@ataque} / #{@defensa}"
end

end

class Pokedex
def initialize
# Lista para guardar pokemones
@pl = []
end

# Agrega nombre, ataque y defensa de un pokémon a la lista
def add_pokemon(n, a, d)
@pl << [n, a, d]
end

# Para aumentar el ataque de un pokémon
def increase_attack(n)
# Guarda si el pokémon fue encontrado
f = false
for pk in @pl
if pk[0] == n
pk[1] += 1
f = true
end
end
if !f
puts n + " no encontrado"
end
def add_pokemon(poke)
@pl << poke
end

# Para aumentar la defensa de un pokémon
def increase_defense(n)
# Guarda si el pokémon fue encontrado
f = false
for pk in @pl
if pk[0] == n
pk[2] += 1
f = true
def find_pokemon(_name)
@pl.each do |pk|
if pk.nombre == _name
return pk
else
puts _name + " no encontrado"
end
end
if !f
puts n + " no encontrado"
end
end

# Para obtener los atributos de un pokémon
def get_stats(n)
for pk in @pl
if pk[0] == n
puts pk[1].to_s + "/" + pk[2].to_s
end
end
end

# Se llama al invocar puts sobre una instancia
def to_s
i = 0
temp = ""
for pk in @pl
i += 1
temp += i.to_s + ". " + pk[0] + "\n"
temp += i.to_s + ". " + pk.nombre + "\n"
end
temp
end
end

pokedex = Pokedex.new
pokedex.add_pokemon('Pikachu', 12, 10)
pokedex.add_pokemon('Cubone', 8, 12)
pokedex.increase_attack('Pikachu')
pokedex.get_stats('Pikachu')
pikachu = Pokemon.new('Pikachu', 12 , 10)
cubone = Pokemon.new('Cubone', 8, 12)
pokedex.add_pokemon(pikachu)
pokedex.add_pokemon(cubone)
pokedex.find_pokemon("Pikachu").increase_atk(1)
puts pokedex.find_pokemon('Pikachu').get_stats()
puts pokedex
19 changes: 19 additions & 0 deletions pokedex_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative 'main.rb'
require 'minitest/autorun'


describe 'Pokedex test' do

def setup
@pokedex = Pokedex.new
pikachu = Pokemon.new('Pikachu', 12, 10)
cubone = Pokemon.new('Cubone', 8, 12)
@pokedex.add_pokemon(pikachu)
@pokedex.add_pokemon(cubone)
end

it 'should have Pikachu and Cubone' do
@pokedex.to_s.must_equal("1. Pikachu\n2. Cubone\n")
end

end
32 changes: 32 additions & 0 deletions pokemon_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require_relative 'main.rb'
require 'minitest/autorun'


describe 'Pokemon test' do

def setup
@pikachu = Pokemon.new('Pikachu', 5, 10) # Pikachu, 5 atk, 10 def.
end

it 'should be named Pikachu' do
@pikachu.nombre.must_equal('Pikachu')
end

it 'should have 5 atk and 10 def' do
@pikachu.get_stats.must_equal('5 / 10')
end

it 'should increase atk' do
@pikachu.ataque.must_equal(5)
@pikachu.increase_atk
@pikachu.ataque.must_equal(6)
end

it 'should increase def' do
@pikachu.defensa.must_equal(10)
@pikachu.increase_def
@pikachu.defensa.must_equal(11)
end

end

24 changes: 24 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Grupo 2

## Integrantes

Nombre | GitHub | Email
------------------ | --------------- | ----------------
Benjamin Ibarra | [@btibarra] | [btibarra@uc.cl]
Sebastian Butorovic | [@sibutorovic]  | [sibutorovic@uc.cl]
Cristobal Martinez | [@cnmartinez] | [cnmartinez@uc.cl]


## Code Smells

### Primitive Obssesion

Se puede ver este code smell en que los pokemones son guardados en arreglos, esto se arregló creando una clase que agrupa las características de un pokemon.

### Duplicated Code

Se puede encontrar este code smell cuando se busca un pokemón en los métodos de aumentar ataque y defensa, para solucionarlo se crea un método find_pokemon(pokemon) para buscar los pokemones.

### Inappropriate

Se puede ver un acceso inapropiado (leer y modificar) a los datos de los pokemones por parte del pokedex, para solucionarlo se agrega que solo se puedan leer los atributos de los pokemones en la clase.