0

Updated 2.0

Currently updated it so every old "function" has been moved beneath my class. Now it seems like one of my only problems is figuring out all these drawBoard() takes 1 positional argument but 2 were given

This is just the start of my current class, just to show you what I am currently dealing with

Community
  • 1
  • 1
Pythongirl
  • 21
  • 2
  • 7
  • There are quite some issues with your code: 1. BoardHandler.__init__ takes `size` but does not use it. 2. BoardHandler.getNewBoard returns `board`, I guess it should write it back to the class member instead. 3. in `resetBoard` you try to access `BoardHandler().size`, however `size` is not a static or class member, so its instance-specific. – Johannes Jul 28 '17 at 19:15
  • @Johannes watch the updated version! – Pythongirl Jul 28 '17 at 19:20
  • The problem is that I need to use both the input value of size and both `getNewBoard` and `Board`, but I've been stuck with this problem for over 4 hours, just getting error after error – Pythongirl Jul 28 '17 at 19:22
  • one very often being `TypeError: __init__() missing 2 required positional arguments: 'size' and 'board` – Pythongirl Jul 28 '17 at 19:22
  • What you _really_ should do is make `resetBoard` and `drawBoard` methods of the class, then they can access the attribute with `self.board`. Otherwise, you'll need to modify them to accept a `BoardHandler` instance as an argument, for example named `board_handler` and then use `board_handler.board` inside of them. – martineau Jul 28 '17 at 19:24
  • @martineau yes but the problem is that, mainly most of my long code need to acces `Board` and `size` – Pythongirl Jul 28 '17 at 19:26
  • Well just make an example of what you just told me, using ex `resetboard` – Pythongirl Jul 28 '17 at 19:29
  • I think the real question here is whether all boards should have the same size (-> make size a class or static member) or if every board should have its own size (-> pass self to resetBoard). If you don't know the difference you should read about static members: https://stackoverflow.com/questions/27481116/how-to-declare-a-static-attribute-in-python – Johannes Jul 28 '17 at 19:30
  • Every run of the program will give the option which size the board should be (ex 4x4 which input becomes `4`) – Pythongirl Jul 28 '17 at 19:32
  • But yes every board in the code shall be the same size @Johannes – Pythongirl Jul 28 '17 at 19:32
  • @martineau check the new update! – Pythongirl Jul 28 '17 at 20:11
  • @Pythongirl: I think the general ideas in my answer are already good enough and still apply (for the most part) to your question, despite your 2.0 update. You also need to learn how to format the code you post in your question properly here on stackoverflow. – martineau Jul 28 '17 at 20:35

2 Answers2

0

As I said in a comment, I think you should make the two functions in methods of class BoardHandler. This will give you better encapsulation and it's fairly easy to do, here's how:

class BoardHandler:
    def __init__ (self, size, board):
        self.size = size
        self.board = board

    def ask_size(self):
        try:
            self.size = int(input("Which size would you want?"))
        except ValueError:
            print("Wrong! try again")
            ask_size()

    def getNewBoard(self):
        board=[]
        for i in range(self.size):
            board.append([' ']* self.size)
        self.board = board

    def resetBoard(self):
        for x in range(self.size):
            for y in range(self.size):
                self.board[x][y] = ' '

        n=int(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 = '  ----------------------------------------'

        for y in range(self.size):
            print(y+1, end=' ')
            for x in range(Q):
                print('| %s' % (self.board[x][y]), end=' ')
            print('|')
            print(HLINE)

If you want to keep them outside the class for some reason, you will need to modify each of them to accept a BoardHandler class instance as an argument:

def resetBoard(board_handler):
    for x in range(board_handler.size):
        for y in range(board_handler.size):
            board_handler.board[x][y] = ' '

    n=int(board_handler.size/2-1)
    board[n][n] = 'B'
    board[n+1][n] = 'W'
    board[n][n+1] = 'W'
    board[n+1][n+1] = 'B'

def drawBoard(board_handler):
    HLINE = '  ----------------------------------------'

    for y in range(board_handler.size):
        print(y+1, end=' ')
        for x in range(Q):
            print('| %s' % (board_handler.board[x][y]), end=' ')
        print('|')
        print(HLINE)

This assumes your program creates a single BoardHandler class instance, and does all operations to that object.

martineau
  • 119,623
  • 25
  • 170
  • 301
0

Update based on:

Every run of the program will give the option which size the board should be ... every board in the code shall be the same size

We make size a class variable and only prompt for a size if it isn't already set. All subsequent board creations will use the same size as the first board:

class BoardHandler:
    size = None

    def __init__(self):
        self.board = None

        if BoardHandler.size is None:
            self.ask_size()

        self.getNewBoard()

    def ask_size(self):  # no self use, method could be a function
        while True:
            try:
                BoardHandler.size = int(input("Which size would you want? "))
                break
            except ValueError:
                print("Wrong! try again")

    def getNewBoard(self):
        self.board = []

        for _ in range(BoardHandler.size):
            self.board.append([' '] * BoardHandler.size)

    def resetBoard(self):
        for x in range(BoardHandler.size):
            for 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)

        for y in range(BoardHandler.size):
            print("{:2d}".format(y + 1), end=' ')
            for 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 but same size as first
cdlane
  • 40,441
  • 5
  • 32
  • 81