python - How to call attributes from outside classes? -
updated 2.0
currently updated every old "function" has been moved beneath class. seems 1 of problems figuring out these drawboard() takes 1 positional argument 2 given
this start of current class, show dealing
update based on:
every run of program give option size board should ... every board in code shall same size
we make size class variable , prompt size if isn't set. subsequent board creations use same size first board:
class boardhandler: size = none def __init__(self): self.board = none if boardhandler.size none: self.ask_size() self.getnewboard() def ask_size(self): # no self use, method function while true: try: boardhandler.size = int(input("which size want? ")) break except valueerror: print("wrong! try again") def getnewboard(self): self.board = [] _ in range(boardhandler.size): self.board.append([' '] * boardhandler.size) def resetboard(self): x in range(boardhandler.size): y in range(boardhandler.size): self.board[x][y] = ' ' n = int(boardhandler.size / 2 - 1) self.board[n][n] = 'b' self.board[n + 1][n] = 'w' self.board[n][n + 1] = 'w' self.board[n + 1][n + 1] = 'b' def drawboard(self): hline = ' ' + '-' * (4 * boardhandler.size) + '-' print(hline) y in range(boardhandler.size): print("{:2d}".format(y + 1), end=' ') x in range(boardhandler.size): print('| {}'.format(self.board[x][y]), end=' ') print('|') print(hline) handler = boardhandler() handler.resetboard() handler.board[0][0] = 'w' handler.drawboard() print(boardhandler.size) handler2 = boardhandler() handler2.drawboard() # empty board same size first
Comments
Post a Comment