Hello, I'm working on a Connect 4 evaluation function, and this is the only code we have left. This code WORKS just fine, however, I am wondering if there is a more efficient way to write this code.
Basically, this code takes in a game board that is 6x7 and when you insert a piece in a column, it look at the surrounding pieces and adds points accordingly to pieces it detects around it.
However, this can create a whole bunch of out of bounds errors so we decided to specifically look at all 9 scenarios, 4 corners, 4 sides, and if its in the middle
Is there a more efficient way to do this code? because it is ridiculously long and clustered.
Don't be intimidated by the code. It LOOKS extremely long, but it's just a bunch of if/else if statements that do the same thing for each position
Code:int evaluate(struct connect4 game, int row, int col) { int points = 0, baseVals[NUM_COLS] = {100, 300, 400, 500, 400, 300, 100}; points += baseVals[col]; if (row == 5 && col == 0) { if (game.board[4][1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[4][1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[5][1] == game.whoseTurn) points += ROW_WIN; else if (game.board[5][1] == other(game.whoseTurn)) points += ROW_BLOCK; } else if (row == 5 && col == 6) { if (game.board[4][5] == game.whoseTurn) points += DIAG_WIN; else if (game.board[4][5] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[5][5] == game.whoseTurn) points += ROW_WIN; else if (game.board[5][5] == other(game.whoseTurn)) points += ROW_BLOCK; } else if (row == 0 && col == 0) { if (game.board[1][1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[1][1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[0][1] == game.whoseTurn) points += ROW_WIN; else if (game.board[0][1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[1][0] == game.whoseTurn) points += COL_WIN; else if (game.board[1][0] == other(game.whoseTurn)) points += COL_BLOCK; } else if (row == 0 && col == 6) { if (game.board[1][5] == game.whoseTurn) points += DIAG_WIN; else if (game.board[1][5] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[0][5] == game.whoseTurn) points += ROW_WIN; else if (game.board[0][5] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[1][6] == game.whoseTurn) points += COL_WIN; else if (game.board[1][6] == other(game.whoseTurn)) points += COL_BLOCK; } else if (row < 5 && row > 0 && col == 0) { if (game.board[row-1][col+1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row-1][col+1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row+1][col+1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row+1][col+1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row][col+1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col+1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row+1][col] == game.whoseTurn) points += COL_WIN; else if (game.board[row+1][col] == other(game.whoseTurn)) points += COL_BLOCK; } else if (row < 5 && row > 0 && col == 6) { if (game.board[row-1][col-1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row-1][col-1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row+1][col-1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row+1][col-1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row-1][col] == game.whoseTurn) points += ROW_WIN; else if (game.board[row-1][col] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row+1][col] == game.whoseTurn) points += COL_WIN; else if (game.board[row+1][col] == other(game.whoseTurn)) points += COL_BLOCK; } else if (col < 6 && col > 0 && row == 0) { if (game.board[row-1][col+1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row-1][col+1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row+1][col+1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row+1][col+1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row][col-1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col-1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row][col+1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col+1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row+1][col] == game.whoseTurn) points += COL_WIN; else if (game.board[row+1][col] == other(game.whoseTurn)) points += COL_BLOCK; } else if (col < 6 && col > 0 && row == 5) { if (game.board[row-1][col-1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row-1][col-1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row+1][col-1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row+1][col-1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row][col-1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col-1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row][col+1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col+1] == other(game.whoseTurn)) points += ROW_BLOCK; } else { if (game.board[row-1][col-1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row-1][col-1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row+1][col-1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row+1][col-1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row-1][col+1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row-1][col+1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row+1][col+1] == game.whoseTurn) points += DIAG_WIN; else if (game.board[row+1][col+1] == other(game.whoseTurn)) points += DIAG_BLOCK; if (game.board[row][col-1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col-1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row][col+1] == game.whoseTurn) points += ROW_WIN; else if (game.board[row][col+1] == other(game.whoseTurn)) points += ROW_BLOCK; if (game.board[row+1][col] == game.whoseTurn) points += COL_WIN; else if (game.board[row+1][col] == other(game.whoseTurn)) points += COL_BLOCK; } return points; }