Last active
December 13, 2015 23:49
-
-
Save ledsun/4994052 to your computer and use it in GitHub Desktop.
角にとって右は右斜め上で、上は左斜め上
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
module Movable | |
def position(position=nil) | |
return {:x=>@x, :y=>@y} unless position | |
@x = position[:x] | |
@y = position[:y] | |
end | |
def set_position_or_player(position) | |
position (position.is_a?(Movable) ? position.position : position) | |
end | |
def set_ratio(x, y) | |
@x_ratio = x | |
@y_ratio = y | |
end | |
def move_x(plusMinus) | |
@x += 10 * @x_ratio * plusMinus | |
end | |
def move_y(plusMinus) | |
@y += 10 * @y_ratio * plusMinus | |
end | |
def move(direction) | |
case direction | |
when :up | |
move_y -1 | |
when :down | |
move_y +1 | |
when :left | |
move_x -1 | |
when :right | |
move_x +1 | |
end | |
end | |
end | |
class NormalPlayer | |
include Movable | |
def initialize(position) | |
set_position_or_player position | |
set_ratio 1, 1 | |
end | |
end | |
class FastPlayer < NormalPlayer | |
include Movable | |
def initialize(position) | |
super | |
set_ratio 2, 2 | |
end | |
end | |
class KaniPlayer < NormalPlayer | |
include Movable | |
def initialize(position) | |
super | |
set_ratio 4, 0.5 | |
end | |
end | |
class NechigaePlayer < NormalPlayer | |
include Movable | |
def position(position=nil) | |
return {:x=>@y, :y=>@x} unless position | |
@x = position[:y] | |
@y = position[:x] | |
end | |
end | |
class KakuPlayer < NormalPlayer | |
include Movable | |
def move_x(plusMinus) | |
@x += 10 * @x_ratio * plusMinus | |
@y += 10 * @y_ratio * plusMinus | |
end | |
def move_y(plusMinus) | |
@x -= 10 * @x_ratio * plusMinus | |
@y += 10 * @y_ratio * plusMinus | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
$: << File.expand_path(File.dirname(__FILE__)) | |
require 'player' | |
describe NormalPlayer do | |
let (:player) { NormalPlayer.new(x: 100, y: 100) } | |
it "通常 モードで正しい動きをすること" do | |
player.tap{|p| p.move(:up) }.position.should == {x: 100, y: 90} | |
player.tap{|p| p.move(:left) }.position.should == {x: 90, y: 90} | |
player.tap{|p| p.move(:down) }.position.should == {x: 90, y: 100} | |
player.tap{|p| p.move(:right) }.position.should == {x: 100, y: 100} | |
end | |
end | |
describe FastPlayer do | |
let (:player) { FastPlayer.new(x: 100, y: 100) } | |
it "倍速 モードで正しい動きをすること" do | |
player.tap{|p| p.move(:up) }.position.should == {x: 100, y: 80} | |
player.tap{|p| p.move(:left) }.position.should == {x: 80, y: 80} | |
player.tap{|p| p.move(:down) }.position.should == {x: 80, y: 100} | |
player.tap{|p| p.move(:right) }.position.should == {x: 100, y: 100} | |
end | |
end | |
describe KaniPlayer do | |
let (:player) { KaniPlayer.new(x: 100, y: 100) } | |
it "カニ モードで正しい動きをすること" do | |
player.tap{|p| p.move(:up) }.position.should == {x: 100, y: 95} | |
player.tap{|p| p.move(:left) }.position.should == {x: 60, y: 95} | |
player.tap{|p| p.move(:down) }.position.should == {x: 60, y: 100} | |
player.tap{|p| p.move(:right) }.position.should == {x: 100, y: 100} | |
end | |
end | |
describe NechigaePlayer do | |
let (:player) { NechigaePlayer.new(x: 100, y: 100) } | |
it "寝違え モードで正しい動きをすること" do | |
player.tap{|p| p.move(:up) }.position.should == {x: 90, y: 100} | |
player.tap{|p| p.move(:left) }.position.should == {x: 90, y: 90} | |
player.tap{|p| p.move(:down) }.position.should == {x: 100, y: 90} | |
player.tap{|p| p.move(:right) }.position.should == {x: 100, y: 100} | |
end | |
end | |
describe KakuPlayer do | |
let (:player) { KakuPlayer.new(x: 100, y: 100) } | |
it "角 モードで正しい動きをすること" do | |
player.tap{|p| p.move(:up) }.position.should == {x: 110, y: 90} | |
player.tap{|p| p.move(:left) }.position.should == {x: 100, y: 80} | |
player.tap{|p| p.move(:down) }.position.should == {x: 90, y: 90} | |
player.tap{|p| p.move(:right) }.position.should == {x: 100, y: 100} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
とりあえず実装したけど、どうやってリファクタリングしようかな?