I'm (slowly) working out a 2D version of Battleship now that my 1D version is done. I wrote the following function to place a boat on the board given the length of the boat, the position of the boat, and the direction the boat is facing. However, the function is ugly. Very ugly. By which I mean, there is much in the way of code duplication. Could anyone point out some ways in which I could reduce the duplication in this code?
(defun place-boat (len pos dir)
(let ((offset 0))
(dotimes (i len)
(if (= dir 0)
(if (< pos 50)
(setf (aref *ans-board*
(+ (/ pos 10) offset)
(mod pos 10))
'#)
(setf (aref *ans-board*
(- (/ pos 10) offset)
(mod pos 10))
'#))
(if (< pos 50)
(setf (aref *ans-board*
(/ pos 10)
(+ (mod pos 10) offset))
'#)
(setf (aref *ans-board*
(/ pos 10)
(- (mod pos 10) offset))
'#)))
(incf offset))))
EDIT: For clarification, pos
is a number between 1 and 100, signifying a cell in a 10x10 2D array.