0

I am a beginner programmer. I'm attempting to make an object-oriented chess game in Python. This is my first step, laying out a chess board. I have written:

  #this is board whick is necessery to run a class
board_for_start=[]
for x in range(8):
   for y in range(8):
      board_for_start.append('.')

class game:

#this is a setting board in class
    def __init__(self,board):
        self.board=board

# displaying board
    def display_board(self):
        for i in range (8):
            for j in range (8):
                print (self.board[i][j])
game_board=game(board_for_start)
game_board.display_board()

Traceback (most recent call last): File "C:/Users/Goldsmitd/PycharmProjects/CHESS/chees_ver0.02.py", line 22, in game_board.display_board() File "C:/Users/Goldsmitd/PycharmProjects/CHESS/chees_ver0.02.py", line 18, in display_board print (self.board[i][j]) IndexError: string index out of range

Why am I getting this error?

Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
Rafail
  • 149
  • 1
  • 1
  • 4
  • 2
    You do not have an 8x8 list of lists. You have *one* list of 64 '.' characters. – Kevin Aug 13 '15 at 20:08
  • You don't have to iterate over the indices when you want the content anyway. You're just begging for [off-by-one errors](https://en.wikipedia.org/wiki/Off-by-one_error). If you _did_ have an 8x8 list, you should say `for row in self.board: for square in row: print(square)` or similar. Also, that would have prevented this because you would have instantly realized you only have one list to iterate. – Two-Bit Alchemist Aug 13 '15 at 20:11

4 Answers4

2

Here you are initializing a one dimensional list but what you really are looking for is a list of list, here is the bug free code and do see the below mentioned link for initializing a list of list. Also, you can learn list comprehension if this seems a long process.

How to initialize a two-dimensional array in Python?

List comprehension

class Chess:
    def __init__ (self):
        self.board = self.create_board ()

    def create_board (self):
        board_x = []
        for x in range (8):
            board_y = []
            for y in range (8):
                board_y.append ('.')
            board_x.append (board_y)

        return board_x

    def display_board (self):
        for i in range (8):
            for j in range (8):
                print (self.board[i][j], end="")
            print()

game = Chess()
game.display_board()
Community
  • 1
  • 1
Vivek Anand
  • 621
  • 1
  • 7
  • 15
  • 1
    I recommend putting parentheses around your print statements, as the question seems to use Python 3. Additionally, it would be nice if you would follow well-recognized standards in your answer, such as [PEP8](https://www.python.org/dev/peps/pep-0008/) for formatting and having the board creation logic _inside_ the class, where it belongs. – Two-Bit Alchemist Aug 13 '15 at 20:37
1

I made some small adjustments to your code that should help you get started. I renamed your class to something slightly less generic, though I think you will end up discarding this class as your game grows, because you will realize you need several discrete objects, not one big thing called "Game" or "ChessGame".

All the board initialization takes place in __init__ for convenience, rather than outside the class. The display_board function has been rewritten to prevent off-by-one errors as discussed in my comment.

class ChessGame:
    def __init__(self):
        self.board = [list('........') for i in range(8)]

    def display_board(self):
        for row in self.board:
            for square in row:
                print(square, end="")
            print()

Example Output:

In [4]: game = ChessGame()

In [5]: game.board
Out[5]: 
[['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '.']]

In [6]: game.display_board()
........
........
........
........
........
........
........
........
Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
0

At the moment board_for_start is a 1D list not a 2D list, and print self.board is trying to access it using two indexes i, j.

kezzos
  • 3,023
  • 3
  • 20
  • 37
0

Try to use list comprehension for your initialization of your game board. Something like this

board_for_start = [['.' for x in range(8)] for x in range(8)]
wpercy
  • 9,636
  • 4
  • 33
  • 45