Its me
Its me

Reputation: 121

Is there a way to visual a grid?

I have a list of 8 list with 8 None (making like a grid of 8x8). I filled the board with a few piece and i now want to see the board in a friendly way.

Right now I'm using this method:

class ChessBoard():
      def __init__(self):
           self.board = [[None for x in range(0, 8)] for y in range(0, 8)]

      def Show(self):
           for line in self.board:
                  new_line = [None for x in range(0, 8)]
                  for i in range(0, 8):
                        if line[i] is not None:
                             new_line[i] = line[i].kind
                  print(new_line)

Which print it like this:

['White Rook', None, None, None, None, None, None, None]
['White Rook', None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None]
[None, None, None, None, None, None, None, None]
[None, None, None, None, 'Black King', None, None, None]
[None, None, None, None, None, None, 'White King', None]
[None, None, None, None, None, None, None, None]

I'm new to python so I don't know much function. Is there an easy way to show it as a grid or in any other way. Can I make a grid and just write the pieces name in it? I can download images of a board and piece and load them in specific places?

Upvotes: 0

Views: 317

Answers (3)

Catamondium
Catamondium

Reputation: 393

Presuming you mean to get a printed debugging representation of the board, I've broadly implemented your desired output by padding for the maximum length string of the pieces on the current board.

Ignore that I added a knight to [0][0] on the board, it was for testing.

Also, if every column has a piece it'll occupy most of the screen. You may also wish to add \n above to the __repr__ output for ChessBoard so the grid is printed on a fresh line.

# Python 3

class Piece():
    def __init__(self, _kind):
        self.kind = _kind

    def __repr__(self):
        return self.kind

class ChessBoard():
    def __init__(self):
        self.board = [[None for x in range(0, 8)] for y in range(0, 8)]
        self.board[0][0] = Piece("Black knight")
        self.board[1][0] = Piece("White Rook")

    def __repr__(self):
        strings = [["%s" % x for x in y] for y in self.board]
        maxlens = [len(max(strings[x], key=len))
                   for x in range(0, 8)]
        padded = [["%-*s" % (maxlens[x], strings[x][y])
                   for x in range(0, 8)] for y in range(0, 8)]

        rowed = ["[%s]" % ", ".join(x).strip() for x in padded]
        return "\n".join(rowed)

On a side note, see Prune's response about asking questions, you want to look into things like either printing a matrix(list of lists) of strings in python or refer to the pygame answers and similar for graphical representations.

Upvotes: 0

Rockybilly
Rockybilly

Reputation: 4510

There are numerous visualization tools that use Python, which one you should employ. For this task, I suggest Pygame rather that GUI libraries because of the learning curves and your requirements.

Pygame mostly supplies drawing functions, direct screen interaction. GUI modules does so as well, however needs some learning of widgets and parent-children relationship between canvas' etc.

But either way, you will need to learn some concepts before continuing with your work, such as, how to display the board, how to store and show the pieces, basic coordinate system understanding, Pygame (generally any application) running mechanics, etc.

There are many examples out there. In fact I am also interested in chess as a hobby so I did write a chess program (uses any third-party engine for the AI). Visualization is indeed was a hard task (if never did a similar work before), however I did have fun implementing the chess rules (en passant, pawn promotion, castling, etc), which is not what you are asking but worth mentioning.

https://github.com/Rockybilly/ITUChess

You can take a look if it helps you. I have written it just for fun, but I remember it to be documented enough to be understood.

Upvotes: 0

Bill K
Bill K

Reputation: 62759

You can do it by hand, it's not all that hard. Iterate over the array and figure out the longest item in each column, then iterate over it again and print each cell padding each item out to the max width for that column.

It's even pretty easy to implement wrapping within a cell if it comes to that.

For me it was rewarding and I use it all the time. Since it's simply displaying a list of maps (or a list of objects) it's infinitely reusable. Since I did it in Groovy I don't really have python source code to show you.

Upvotes: 1

Related Questions