Skip to content

Instantly share code, notes, and snippets.

@ledsun
Last active December 13, 2015 23:49
Show Gist options
  • Save ledsun/4994052 to your computer and use it in GitHub Desktop.
Save ledsun/4994052 to your computer and use it in GitHub Desktop.
角にとって右は右斜め上で、上は左斜め上
# -*- 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
# -*- 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
@ledsun
Copy link
Author

ledsun commented Feb 20, 2013

とりあえず実装したけど、どうやってリファクタリングしようかな?

@ledsun
Copy link
Author

ledsun commented Feb 20, 2013

@x += 10 * @x_ratio * plusMinus がいっぱい出てくるのが嫌だなぁ。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment