Home > database >  how to solve out of range probleme in python
how to solve out of range probleme in python

Time:02-25

I'm trying to build 4 in row game so when I try to make the players have the same choice it give this error

list index out of range

this is an example of what happedenter image description here

it seems the probleme with winning condition but I don't understand why this happend it gives right answer when the input is correct like make the one player win but when make them have same choice it is break.

the code is

Width = 4
Height = 4

game_board = []
for x in range(Width): game_board.append(list(['a'] * Height))


def make_move(board, borad_row, board_col, piece):
    board[borad_row][board_col] = piece


# check if the slot is empty
def is_slot_empty(board, board_col):
    return board[Width - 1][board_col] == 'a'


# get the next available
def next_available_slot(board, board_col):
    for i in range(Width):
        if board[i][board_col] == 'a':
            return i


# method for winning conditions
def winning(board, piece):
    # check horizontally
    for i in range(Height):
        for j in range(Width):
            if board[j][i] == piece and board[j][i   1] == piece and board[j][i   2] == piece and board[j][
                i   3] == piece:
                return True
    # check vertically
    for i in range(Height):
        for j in range(Width):
            if board[j][i] == piece and board[j   1][i] == piece and board[j   2][i] == piece and board[j   3][
                i] == piece:
                return True
    # positive diagonal
    for i in range(Height):
        for j in range(Width):
            if board[j][i] == piece and board[j   1][i   1] == piece and board[j   2][i   2] == piece and board[j   3][
                i   3] == piece:
                return True

    # negative diagonal
    for i in range(Height):
        for j in range(Width):
            if board[j][i] == piece and board[j - 1][i   1] == piece and board[j - 2][i   2] == piece and board[j - 3][
                i   3] == piece:
                return True


game_end = False
turn_1 = 0

while not game_end:
    if turn_1 == 0:
        user_input = int(input("player_1:"))

        if is_slot_empty(game_board, user_input):
            user_input_row = next_available_slot(game_board, user_input)
            make_move(game_board, user_input_row, user_input, 'X')

            if winning(game_board, 'X'):
                print("player 1 wins")
                game_end = True

    else:
        user_input: int = int(input("player_2:"))
        if is_slot_empty(game_board, user_input):
            user_input_row = next_available_slot(game_board, user_input)
            make_move(game_board, user_input_row, user_input, 'Z')
            if winning(game_board, 'Z'):
                print("player_2 wins")
                game_end = True

    for row in reversed(game_board):
        print(row)

    # alternating between 2 users
    turn_1  = 1
    turn_1 = turn_1 % 2

CodePudding user response:

To fix winning(), try something like this:

        def winning(board, piece):
            # check horizontally
            for i in range(Height):
                if all(board[j][i] == piece for j in range(Width)):
                    return True
            # check vertically
            for j in range(Width):
                if all(board[j][i] == piece for i in range(Height)):
                    return True
            # positive diagonal
            if all(board[i][i] == piece for i in range(Width)):
                return True
            # negative diagonal
            if all(board[i][-(i 1)] == piece for i in range(Width)):
                return True

This is only designed to handle a board with equal values for Height and Width.

CodePudding user response:

When you do the checks, make sure to check only the indices that are within the board.

For example, for the horizontal check, both the left end (i) and the right end (i 3) need to be within the 0..Width-1 range. So you should limit the value of the i index so that i 3 is still less than Width:

...
    # check horizontally
    for i in range(Width - 3):
        for j in range(Height):
            if board[j][i] == piece and board[j][i   1] == piece and board[j][i   2] == piece and board[j][i   3] == piece:
                return True
...
  • Related