昨天聚餐的時候,七舅叫 @凡凡妹 show 了把二階魔方。哇~~我也要 show 。先從教會機器開始吧。
class Rubik_Cube
attr_accessor :cube
def initialize()
@cube = [
%w[- - r r - - - -],
%w[- - r r - - - -],
%w[b b w w g g y y],
%w[b b w w g g y y],
%w[- - o o - - - -],
%w[- - o o - - - -]
]
@face_id = {
u: [2, 0],
l: [0, 2], f: [2, 2], r: [4, 2], b: [6, 2],
d: [2, 4]
}
end
def rotate_face_in_counterclockwise(fi)
x, y = @face_id[fi]
face = [@cube[y][x + 0], @cube[y][x + 1], @cube[y + 1][x + 1], @cube[y + 1][x + 0]]
face << face.shift
@cube[y][x + 0], @cube[y][x + 1], @cube[y + 1][x + 1], @cube[y + 1][x + 0] = face
@cube
end
def rotate_z
u
d_r
@cube
end
def rotate_front_face_in_counterclockwise()
rotate_face_in_counterclockwise(:f)
x, y = @face_id[:f]
outer_loop = [
@cube[y - 1][x + 0], @cube[y - 1][x + 1],
@cube[y][x + 2], @cube[y + 1][x + 2],
@cube[y + 2][x + 1], @cube[y + 2][x + 0],
@cube[y + 1][x - 1], @cube[y][x - 1]
]
outer_loop << outer_loop.shift
outer_loop << outer_loop.shift
@cube[y - 1][x + 0], @cube[y - 1][x + 1],
@cube[y][x + 2], @cube[y + 1][x + 2],
@cube[y + 2][x + 1], @cube[y + 2][x + 0],
@cube[y + 1][x - 1], @cube[y][x - 1] = outer_loop
@cube
end
def u_r
(3 * 2).times { @cube[2] << @cube[2].shift }
rotate_face_in_counterclockwise(:u)
self
end
def u
3.times { u_r }
self
end
def l_r
3.times { rotate_z }
f_r
rotate_z
self
end
def l
3.times { rotate_z }
f
rotate_z
self
end
def f_r
rotate_front_face_in_counterclockwise
self
end
def f
3.times { rotate_front_face_in_counterclockwise }
self
end
def r_r
rotate_z
f_r
3.times { rotate_z }
self
end
def r
rotate_z
f
3.times { rotate_z }
self
end
def b_r
2.times { rotate_z }
f_r
2.times { rotate_z }
self
end
def b
2.times { rotate_z }
f
2.times { rotate_z }
self
end
def d_r
@cube[3] << @cube[3].shift
@cube[3] << @cube[3].shift
rotate_face_in_counterclockwise(:d)
self
end
def d
3.times { d_r }
self
end
end
rc = Rubik_Cube.new
rc.u_r.u_r
.r
.f
.u_r
.f
.u_r.u_r
.f_r.f_r
.r_r.r_r
.u_r
pp rc.cube
現在,編碼 & 操作都搞定了,接下來是找一個距離評估函數,計算到終點的距離。
网友评论