Python connect 4 check win function -


i writing connect 4 game in can choose size of board. game works flawlessly board sizes gives me problems when board taller wide. keep getting index out of range errors , im not sure have done wrong. have right in terms of check function part giving me issues.

def checkowin(board):      boardheight = len(board)     boardwidth = len(board[0])     tile = 'o'     # check horizontal spaces     y in range(boardheight):         x in range(boardwidth - 3):             if board[x][y] == tile , board[x+1][y] == tile , board[x+2][y] == tile , board[x+3][y] == tile:                 return true      # check vertical spaces     x in range(boardwidth):         y in range(boardheight - 3):             if board[x][y] == tile , board[x][y+1] == tile , board[x][y+2] == tile , board[x][y+3] == tile:                 return true      # check / diagonal spaces     x in range(boardwidth - 3):         y in range(3, boardheight):             if board[x][y] == tile , board[x+1][y-1] == tile , board[x+2][y-2] == tile , board[x+3][y-3] == tile:                 return true      # check \ diagonal spaces     x in range(boardwidth - 3):         y in range(boardheight - 3):             if board[x][y] == tile , board[x+1][y+1] == tile , board[x+2][y+2] == tile , board[x+3][y+3] == tile:                 return true      return false 

any or suggestions appreciated. in advance!

you've mixed dimensions, should set them way:

def checkowin(board):     boardheight = len(board[0])     boardwidth = len(board) 

because when refer board[x], that's counting number of lists in board, , when refer board[x][y] that's referring length of 1 specific row in board.

if board[x][y] == tile , board[x+1][y] == tile , board[x+2][y] == tile , board[x+3][y] == tile: 

when flipped values function ran without errors.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -