Python Forum
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GameBoard coding
#1
I'm have to create connect 4 as an assignment. We have been given code to tic tac toe to base it on. I have adjusted the board code but keep getting this error message


Error:
Traceback (most recent call last):   File "C:\Users\Brett Bullman\Desktop\Programming\Lab 6\Lab6_Python\tic_tack_toe_fixed\main.py", line 38, in <module>     main()   File "C:\Users\Brett Bullman\Desktop\Programming\Lab 6\Lab6_Python\tic_tack_toe_fixed\main.py", line 17, in main     gboard.show_board_fixed() # show empty grid at the beginning of the game   File "C:\Users\Brett Bullman\Desktop\Programming\Lab 6\Lab6_Python\tic_tack_toe_fixed\gameboardfixed.py", line 63, in show_board_fixed     print(' ' + self.__board[5][0] + ' | ' + self.__board[5][1] + ' | ' + self.__board[5][2]) IndexError: list index out of range
This is the code:

>>> def show_board_fixed(self):
  print('   |   |   |   |   |   |   ')
  print(' ' + self.__board[0][0] + ' | ' + self.__board[0][1] + ' | ' + self.__board[0][2])
  print('   |   |   |   |   |   |   ')
  print('---------------------------')
  print('   |   |   |   |   |   |   ')
  print(' ' + self.__board[1][0] + ' | ' + self.__board[1][1] + ' | ' + self.__board[1][2])
  print('   |   |   |   |   |   |   ')
  print('---------------------------')
  print('   |   |   |   |   |   |   ')
  print(' ' + self.__board[2][0] + ' | ' + self.__board[2][1] + ' | ' + self.__board[2][2])
  print('   |   |   |   |   |   |   ')
  print('---------------------------')
  print('   |   |   |   |   |   |   ')
  print(' ' + self.__board[3][0] + ' | ' + self.__board[3][1] + ' | ' + self.__board[3][2])
  print('   |   |   |   |   |   |   ')
  print('---------------------------')
  print('   |   |   |   |   |   |   ')
  print(' ' + self.__board[4][0] + ' | ' + self.__board[4][1] + ' | ' + self.__board[4][2])
  print('   |   |   |   |   |   |   ')
  print('---------------------------')
  print('   |   |   |   |   |   |   ')
  print(' ' + self.__board[5][0] + ' | ' + self.__board[5][1] + ' | ' + self.__board[5][2])
  print('   |   |   |   |   |   |   ')
also would i have to adjust the self.board to suit all spaces? Thanks
Reply
#2
Not knowing how board is created and processed it's hard to answer the question. I would use print statements before the show_board_fixed method, to see exactly what it is. Specifically, does board contain six lists, and does the sixth list contain three items.

Note that the format method of the string object and storing some of the repeated strings in variables would simplify your method considerably.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
It's acting like cells are missing from the board
Reply
#4
(Dec-20-2016, 01:22 PM)Bullman Wrote: I'm have to create connect 4 as an assignment. We have been given code to tic tac toe to base it on. I have adjusted the board code but keep getting this error message


Error:
Traceback (most recent call last): [...snip...] IndexError: list index out of range

Looks like your board isn't big enough.  If you don't believe it, add a print(self.__board) to that method.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020