Hola..
Israel




Wishful Thinking
O algo mas?
Diseñar programando
def suma_complicada
resultado_complicado + resultado_imposible
end
La pregunta clave: ******** *** **** ?
Empecemos por el final..O el principio?
TDD
TDD
Input y Output.
RSpec.describe GamesController, type: :controller do
render_views
describe '#create' do
it "should return the new game" do
post :create, format: :json
game = json.game
expect(game.id).to be_present
expect(game.over).to eq false
expect(game.warnings).to eq []
expect(game.mines).to be_nil
end
end
describe '#move' do
let(:game_mines) { [{"x" => 1, "y" => 1}, {"x" => 3, "y" => 4}, {"x" => 5, "y" => 2}] }
it "shold return game as over if a mine is marked and return all mines" do
post :create, format: :json
post :move, point: last_game_mine, format: :json
expect(json.game.over).to eq true
expect(json.game.mines.size).to eq 7
end
it "should not be over unless a mine explodes" do
post :create, format: :json
Game.last.update_attribute :mines, game_mines
post :move, point: {"x" => 3, "y" => 6}, format: :json
expect(json.game).not_to be_over
end
end
def last_game_mine
Game.find(json.game.id).mines.last
end
end
Controlador
def create
@game = Game.create
BuildGame.call(@game)
end
Vista
json.game do
json.call(game, :id, :over)
json.warnings game.warnings
json.revealed game.revealed
if game.over
json.mines game.mines
end
end
La pregunta.... De siempre.
Qué método o clase desearía que existiera aquí?

Cuando deseas algo con el corazón...
El universo no hace un carajo.

La Mejor pregunta...
Si solo fuera mi trabajo escribir esta parte de el código. Cómo lo escribiría?
class BuildGame
......
def call(game)
end
.......
La clave

class BuildGame
......
def call(game)
x = Settings.game_width
y = Settings.game_height
game.mines = make_mines(x, y)
game.save
end
.......
Pero siempre se puede ser mas...

class BuildGame
......
def call(game)
game.mines = generate_mines
game.save
end
.......
....
def x
Settings.game_width
end
def x
Settings.game_height
end
....
def generate_mines
mines = []
x.times do
mines << generate_mine(mines)
end
mines
end
Pero qué hace generate_mine ?

def generate_mine(existing_mines)
point = {"x" => Random.rand(x), "y" => Random.rand(y)}
point_exists?(existing_mines, point) ? generate_point(existing_mines) : point
end
def generate_mine(existing_mines)
point = {"x" => random_x, "y" => ramdon_y}
point_exists?(existing_mines, point) ? generate_point(existing_mines) : point
end
Y dónde está el resto?
No es mi trabajo..
Siempre se puede ser más perezoso.
RecordLog.logs_for(record).last(10)
record.last_10_logs
def invalid_point?(point)
x = Settings.game_width
y = Settings.game_height
return true if point["x"] < 1 || point["y"] < 1
return true if point["x"] > x || point["y"] > y
false
end
def invalid_point?(point)
return true if point_lower_than_board(point)
return true if point_higher_than_board(point)
false
end
def invalid_point?(point)
MineSweeperPoint.new(point).out_of_the_board?
end
O incluso mas...
Gracias!
Totales!
Wishful thinking
By israeldelahoz
Wishful thinking
- 250