简体   繁体   English

修改自我内部功能(8益智游戏)是错误的做法吗?

[英]Is it wrong practice to modify self inside functions (8-puzzle game)?

I was trying to make my own solution for 8 puzzle game. 我正在尝试为8个益智游戏制定自己的解决方案。 Online I found mostly solutions with return statements. 在网上,我发现大多数带有return语句的解决方案。 Is it wrong practice to code this way ? 用这种方式编码是错误的做法吗?

import numpy as np


class Game(object):

    def __init__(self, n, config):
        array = np.array(config)
        self.board = array.reshape((n, n))
        blank = np.where(self.board == 0)
        self.blanky = int(blank[0])
        self.blankx = int(blank[1])
        self.n = n

    def move_up(self):
        if self.blanky - 1 < 0:
            return None
        ay, ax = self.blanky, self.blankx
        by, bx = self.blanky - 1, self.blankx
        self.board[ay, ax], self.board[by, bx] = self.board[by, bx], self.board[ay, ax]
        self.blanky = self.blanky - 1

    def move_down(self):
        if self.blanky + 1 > n:
            return None
        ay, ax = self.blanky, self.blankx
        by, bx = self.blanky + 1, self.blankx
        self.board[ay, ax], self.board[by, bx] = self.board[by, bx], self.board[ay, ax]
        self.blanky = self.blanky + 1

    def move_left(self):
        if self.blankx - 1 < 0:
            return None
        ay, ax = self.blanky, self.blankx
        by, bx = self.blanky, self.blankx - 1
        self.board[ay, ax], self.board[by, bx] = self.board[by, bx], self.board[ay, ax]
        self.blankx = self.blankx - 1

    def move_right(self):
        if self.blankx - 1 < 0:
            return None
        ay, ax = self.blanky, self.blankx
        by, bx = self.blanky, self.blankx + 1
        self.board[ay, ax], self.board[by, bx] = self.board[by, bx], self.board[ay, ax]
        self.blankx = self.blankx + 1

    def visualise(self):
        return(self.board)

Within the world of object-oriented programming, you are using classes as they are designed. 在面向对象编程的世界中,您正在使用设计的类。 All the instance variables ( self.blanky and so on) are there to keep track of the state of the class. 所有实例变量( self.blanky等)都可以用来跟踪类的状态。

However, you are on to something if you are thinking about other methodologies of coding, particularly functional coding. 但是,如果您正在考虑其他编码方法,尤其是功能性编码,那么您将有所作为。 In functional coding, we want to avoid all side effects. 在功能编码中,我们要避免所有副作用。 They can modify variables within the function and return a value, but the shouldn't modify anything from the outside world (so you wouldn't be using classes if you were doing functional programming). 他们可以在函数中修改变量并返回值,但不应修改外界的任何东西(因此,如果您在进行函数式编程,则不会使用类)。 See this article for some details: https://dzone.com/articles/side-effects-1 有关更多详细信息,请参见本文: https : //dzone.com/articles/side-effects-1

However, for the type of coding you are doing, there's no issue with a class method modifying the state of the class. 但是,对于您正在执行的编码类型,使用类方法修改类的状态没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM