最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

用Python簡陋模擬n階魔方

 更新時間:2021年04月16日 17:12:50   作者:Eyizoha  
這篇文章主要介紹了用Python簡陋模擬n階魔方,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴呢有一定的幫助,需要的朋友可以參考下

一、前言

終于整完了畢業(yè)論文,忙里偷閑半小時摸了個魔方模擬程序,支持模擬任意階魔方,自動打亂,輸入指令旋轉(zhuǎn)。顯示方面不會弄3D的,用opencv整了個展開圖。

展開示意圖

二、效果

五階魔方打亂20步

在這里插入圖片描述

震撼人心50階,打亂100步

在這里插入圖片描述

三、代碼

import cv2
import numpy as np
from random import randint


class Cube:
    def __init__(self, order=3, size=50):  # 魔方階數(shù)、顯示尺寸
        self.img = np.zeros((4 * size * order, 3 * size * order, 3), dtype=np.uint8)
        self.order = order
        self.size = size
        self.len = size * order
        self.top = [['y'] * order for _ in range(order)]
        self.front = [['r'] * order for _ in range(order)]
        self.left = [['b'] * order for _ in range(order)]
        self.right = [['g'] * order for _ in range(order)]
        self.back = [['o'] * order for _ in range(order)]
        self.bottom = [['w'] * order for _ in range(order)]
        self.axis_rotate = (self.base_rotate_x, self.base_rotate_y, self.base_rotate_z)
        self.color = {'y': (0, 255, 255), 'r': (0, 0, 255), 'b': (255, 0, 0),
                      'g': (0, 255, 0), 'o': (0, 128, 255), 'w': (255, 255, 255)}

    def check(self):  # 檢測魔方是否還原
        for i in range(self.order):
            for j in range(self.order):
                if self.top[i][j] != self.top[0][0]:
                    return False
                if self.back[i][j] != self.back[0][0]:
                    return False
                if self.front[i][j] != self.front[0][0]:
                    return False
                if self.left[i][j] != self.left[0][0]:
                    return False
                if self.right[i][j] != self.right[0][0]:
                    return False
                if self.bottom[i][j] != self.bottom[0][0]:
                    return False
        return True

    def show(self, wait=0):  # 顯示魔方展開圖
        for i in range(self.order):
            for j in range(self.order):
                # back
                x, y = self.len + i * self.size, j * self.size
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), self.color[self.back[j][i]], -1)
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), (10, 10, 10), 1)
                # left
                x, y = i * self.size, self.len + j * self.size
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), self.color[self.left[j][i]], -1)
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), (10, 10, 10), 1)
                # top
                x, y = self.len + i * self.size, self.len + j * self.size
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), self.color[self.top[j][i]], -1)
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), (10, 10, 10), 1)
                # right
                x, y = 2 * self.len + i * self.size, self.len + j * self.size
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), self.color[self.right[j][i]], -1)
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), (10, 10, 10), 1)
                # front
                x, y = self.len + i * self.size, 2 * self.len + j * self.size
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), self.color[self.front[j][i]], -1)
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), (10, 10, 10), 1)
                # bottom
                x, y = self.len + i * self.size, 3 * self.len + j * self.size
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), self.color[self.bottom[j][i]], -1)
                cv2.rectangle(self.img, (x, y), (x + self.size, y + self.size), (10, 10, 10), 1)
        cv2.imshow('cube', self.img)
        cv2.waitKey(wait)

    def shuffle(self, times):  # 打亂魔方
        for _ in range(times):
            self.rotate(randint(0, 2), randint(0, self.order - 1), randint(0, 3))

    def rotate(self, axis, index, times):  # 旋轉(zhuǎn)魔方:axis軸,第index層,逆時針times次
        for _ in range(times):
            self.axis_rotate[axis](index)

    def count(self, color='y'):
        count = 0
        for i in range(self.order):
            for j in range(self.order):
                if self.top[i][j] == color:
                    count += 1
        return count

    @staticmethod
    def _column_trans(surface, index, col):
        for i, r in enumerate(surface):
            r[index] = col[i]

    def base_rotate_x(self, index):
        if index == 0:
            self.left = [list(c) for c in zip(*self.left)][::-1]
        elif index == self.order - 1:
            self.right = [list(c)[::-1] for c in zip(*self.right)]
        temp = [r[index] for r in self.top]
        self._column_trans(self.top, index, [r[index] for r in self.front])
        self._column_trans(self.front, index, [r[index] for r in self.bottom])
        self._column_trans(self.bottom, index, [r[index] for r in self.back])
        self._column_trans(self.back, index, temp)

    def base_rotate_y(self, index):
        if index == 0:
            self.back = [list(c)[::-1] for c in zip(*self.back)]
        elif index == self.order - 1:
            self.front = [list(c) for c in zip(*self.front)][::-1]
        temp = self.left[index][::-1]
        self.left[index] = self.top[index]
        self.top[index] = self.right[index]
        self.right[index] = self.bottom[self.order - index - 1][::-1]
        self.bottom[self.order - index - 1] = temp

    def base_rotate_z(self, index):
        if index == 0:
            self.top = [list(c) for c in zip(*self.top)][::-1]
        elif index == self.order - 1:
            self.bottom = [list(c)[::-1] for c in zip(*self.bottom)]
        temp = self.front[index][::-1]
        self.front[index] = [r[self.order - index - 1] for r in self.left]
        self._column_trans(self.left, self.order - index - 1, self.back[self.order - index - 1][::-1])
        self.back[self.order - index - 1] = [r[index] for r in self.right]
        self._column_trans(self.right, index, temp)


cube = Cube(3, 50)
cube.shuffle(100)
while True:
    cube.show(1)
    cube.rotate(*(int(c) for c in input('axis,index,times:').split()))
    if cube.check():
        break
print('Congratulations')
cube.show(0)

到此這篇關(guān)于用Python簡陋模擬n階魔方的文章就介紹到這了,更多相關(guān)pytho模擬魔方內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 使用Numpy打亂數(shù)組或打亂矩陣行

    使用Numpy打亂數(shù)組或打亂矩陣行

    這篇文章主要介紹了使用Numpy打亂數(shù)組或打亂矩陣行問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Python中類變量和實例變量的區(qū)別

    Python中類變量和實例變量的區(qū)別

    這篇文章主要介紹了Python中類變量和實例變量的區(qū)別,文章針對Python類變量和實例變量的問題,給出了具體說明和演示,需要的小伙伴可以參考一下
    2022-02-02
  • PyTorch張量拼接、切分、索引的實現(xiàn)

    PyTorch張量拼接、切分、索引的實現(xiàn)

    在學(xué)習(xí)深度學(xué)習(xí)的過程中,遇到的第一個概念就是張量,張量在pytorch中的計算十分重要,本文主要介紹了PyTorch張量拼接、切分、索引的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • Python?NumPy科學(xué)計算庫的高級應(yīng)用

    Python?NumPy科學(xué)計算庫的高級應(yīng)用

    這篇文章主要為大家介紹了Python?NumPy科學(xué)計算庫的高級應(yīng)用深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • python中xrange和range的區(qū)別

    python中xrange和range的區(qū)別

    這篇文章主要介紹了python中xrange和range的區(qū)別,需要的朋友可以參考下
    2014-05-05
  • python上下文管理器異常問題解決方法

    python上下文管理器異常問題解決方法

    在本篇文章里小編給大家整理的是一篇關(guān)于python上下文管理器異常問題解決方法,對此有興趣的朋友們可以學(xué)習(xí)參考下。
    2021-02-02
  • python webp圖片格式轉(zhuǎn)化的方法

    python webp圖片格式轉(zhuǎn)化的方法

    這篇文章主要為大家詳細(xì)介紹了python webp圖片格式轉(zhuǎn)化的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • exe反編譯為.py文件的方法

    exe反編譯為.py文件的方法

    本文主要介紹了exe反編譯為.py文件的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • python中Array和DataFrame相互轉(zhuǎn)換的實例講解

    python中Array和DataFrame相互轉(zhuǎn)換的實例講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python中Array和DataFrame相互轉(zhuǎn)換的實例講解內(nèi)容,對此有需要的朋友們可以學(xué)參考下。
    2021-02-02
  • python 3.0 模擬用戶登錄功能并實現(xiàn)三次錯誤鎖定

    python 3.0 模擬用戶登錄功能并實現(xiàn)三次錯誤鎖定

    Python的3.0版本,常被稱為Python 3000,或簡稱Py3k。這篇文章主要介紹了python 3.0 模擬用戶登錄功能并實現(xiàn)三次錯誤鎖定,需要的朋友可以參考下
    2017-11-11

最新評論

宁蒗| 木里| 广平县| 潼南县| 平谷区| 泰顺县| 饶平县| 阳春市| 遂溪县| 乐陵市| 台前县| 邵武市| 诸暨市| 客服| 哈密市| 新绛县| 伽师县| 松潘县| 肇庆市| 凌海市| 麟游县| 沙坪坝区| 徐闻县| 额尔古纳市| 湖南省| 新沂市| 库尔勒市| 兴仁县| 木兰县| 仁布县| 湘乡市| 大埔区| 德惠市| 汉川市| 青海省| 阳东县| 清涧县| 江都市| 柳江县| 兴海县| 红桥区|