Since chessboard is of size 8 X8 so if any coordinate is less than 1 or greater than 8 make it 1 or 8.
if ( $ a < 1 ) $ a = 1 ; if ( $ c < 1 ) $ c = 1 ; if ( $ b > 8 ) $ b = 8 ; if ( $ d > 8 ) $ d = 8 ;