I'm just starting to learn Go, and I implemented an algorithm that checks four consecutive array indexes for an equal value in both diagonal directions, vertical, and horizontal. I designed it so that there is a function for each direction, and I'm wondering if there is a way for me to catch index errors without having to do the overhead to check if each index is in the range of index constraints.
func (state *State) checkTopLeftDiagonal(row, col int) bool {
token := state.Board[row][col]
b := token == state.Board[row-3][col-3] && token == state.Board[row-2][col-2]
&& token == state.Board[row-1][col-1]
return b
}
func (state *State) CheckWinner(row, col int) bool {
for i := 0; i < 4; i++ {
if state.checkTopLeftDiagonal(row+i, col+i) == true {
return true
}
}
return false
}