美文网首页
(挖坑)從零開始解決二階魔方

(挖坑)從零開始解決二階魔方

作者: Pope怯懦懦地 | 来源:发表于2021-11-28 14:26 被阅读0次

昨天聚餐的時候,七舅叫 @凡凡妹 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

現在,編碼 & 操作都搞定了,接下來是找一個距離評估函數,計算到終點的距離。

相关文章

  • (挖坑)從零開始解決二階魔方

    昨天聚餐的時候,七舅叫 @凡凡妹 show 了把二階魔方。哇~~我也要 show 。先從教會機器開始吧。 現在,編...

  • 九月二十五日

    都從心開始,都在重新開始 想長大的人,不停邁著自己腳步 就算內心住著孩子,決然牽手,邁向明日 享受二人世界的夫婦 ...

  • 未來五年的電商趨勢:取代 APP 的微信「小程序」,將改變新零售

    第一階段:PC 電商平台 ─ 電商的起步 最早期接觸線上購物是從拍賣開始,一開始是 Yahoo 拍賣(畢竟早期 Y...

  • 文字學

    從零開始重新,再出發。漢字,漢人,漢語⋯⋯

  • ● 品牌是什麼?

    edenmandom 職業生涯感悟 ① 如果你想學現在成功的案例,你應該從該成功案例的最早創始階段開始,而不是模仿...

  • 成功的創業項目來自於心

    最近的項目進展快速地不敢想像,從開始決定做這件事就一直有人幫忙,然後開始決定往這個方向走的時候所有的藍圖都自動向前...

  • 不歸路

    不知道從何時開始?迷上了文字。不知道從何時開始?分享了所有。鍵盤敲打在無聲的夜裡,聽起來是多麼清脆,就像曾經解開數...

  • 小馬進階一課

    小馬哥的進階之路由人物開始。男女老少,髙矮胖瘦,都要一一掌握。伴隨學校的設計作業,有針對性的去解決問題。談構想,講...

  • 從今天開始

    Christine 你跟我說你想做一個優雅和有修養的人 那從今天開始說好話 不要在委屈和被誤會的時候就衝動一時說出...

  • 尋找生命中的熱情

    從知道格西老師會來香港教二階,就下定決定要上老師的課,期待二階的來臨,親自聽老師講筆的故事,接受老師的加持。 這次...

网友评论

      本文标题:(挖坑)從零開始解決二階魔方

      本文链接:https://www.haomeiwen.com/subject/urtoeltx.html