0

I made connect 4, and Im trying to work out an algorithm to determine the winner. The one below determines the horizontal winner although for some reason an error occurs when the counters are positioned vertically like this. What causes this error and how do I fix it?

board[5][5] == 1 (red chip)

board[4][5] == 1 (red chip)

board[3][5] == 1 (red chip) WHEN THIS IS PLACED ERROR CAUSED

#Check for horizontal win   
 for y in range(6):
        for x in range(7 - 3):
            if board[x][y] == 1 and board[x+1][y] == 1 and board[x+2][y] == 1 and board[x+3][y] == 1:
                return True

Game ImageERROR:

  if board[x][y] == 1 and board[x+1][y] == 1 and board[x+2][y] == 1 and board[x+3][y] == 1:
    IndexError: list index out of range

UPDATE: That worked but now the vertical test isn't working, what have I done?

# check vertical spaces
for x in range(6):
    for y in range(7 - 3):
        if board[x][y] == 1 and board[x][y+1] == 1 and board[x][y+2] == 1 and board[x][y+3] == 1:
            return True

UPDATE 2: Index error when I arranaged vertical, occurs when chips are in same position as below

check vertical spaces

for x in range(6):

    for y in range(7 - 3):

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

            return True
6
  • So how do i resolve this? Commented Jul 17, 2016 at 15:37
  • but then checking for horizontal wins wont work? Commented Jul 17, 2016 at 15:39
  • @Rawing I didn't see the entire board, you're right Commented Jul 17, 2016 at 15:41
  • @AngusRyan Are you sure your board has the correct dimensions? Maybe you have to index it as board[y][x] instead of board[x][y]? Commented Jul 17, 2016 at 15:42
  • x,, strangely, seems to indicate the vertical axis. That said: you should find the lowest red disk and then check up, not down. Change your tests to x-1 and so on. Commented Jul 17, 2016 at 15:44

1 Answer 1

1

It seems like the dimensions of your board is mixed. The picture you posted gives the inserted chips inserted in a vertical order with changing y.

board[5][5] == 1 (red chip)

board[4][5] == 1 (red chip)

board[3][5] == 1 (red chip) # Like so

So your board layout should actually be accessed with Board[y][x].

But when you are searching the horizontal space, you look the Board up with Board[x][y]. Try changing that to

#Check for horizontal win   
 for y in range(6):
        for x in range(7 - 3):
            if board[y][x] == 1 and board[y][x + 1] == 1 and board[y][x + 2] == 1 and board[y][x + 3] == 1:
                return True

Edit: To fix vertical, just switch x and y as we solved horizontal check problem

Edit2: Make sure to print and check the indices while using negative terms, too big numbers would give you index error but negative numbers would not give you any errors in list indices. And you would not realize the problem until your win check fails.

Sign up to request clarification or add additional context in comments.

3 Comments

Switch the search layout to be Board[y][x], your's is Board[x][y] as you see. This can happen to many programmers new to gaming, I have some experience with pygame and even I had a very hard time trying not to confuse the Board access order while I was writing a chess game.
thanks for your help I appreciate it, but when I did what we did above the index error arose again. Read update
@AngusRyan If you are checking the chips from bottom to top, you should use y, y-1, y-2, y-3, instead of y y+1 y+2 etc.. Because the most bottom row is actually Board[6][x] not Board[0][x]. And don't forget to accept and upvote the answer if it helped you, good luck in your game.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.