 (defn add-san-move-sequence
   "Like add-san-move, but adds a sequence of moves rather than a single move. 
   This is faster than calling add-san-move multiple times, because we don't
-  have to unzip and zip the tree for each added move."
-  [game san-moves & {:keys [node-id]}]
+  have to unzip and zip the tree for each added move. If 
+  remove-existing-moves? is true, any previously existing moves at the point
+  of insertion are removed."
+  [game san-moves & {:keys [node-id remove-existing-moves?]}]
   (add-move-sequence game board/move-from-san san-moves
-                     (or node-id (-> game :current-node :node-id))))
+                     (or node-id (-> game :current-node :node-id))
+                     remove-existing-moves?))
 
 
 (defn add-uci-move-sequence
   "Like add-uci-move, but adds a sequence of moves rather than a single move. 
   This is faster than calling add-uci-move multiple times, because we don't
-  have to unzip and zip the tree for each added move."
-  [game uci-moves & {:keys [node-id]}]
+  have to unzip and zip the tree for each added move. If
+  remove-existing-moves? is true, any previously existing moves at the point
+  of insertion are removed."
+  [game uci-moves & {:keys [node-id remove-existing-moves?]}]
   (add-move-sequence game board/move-from-uci uci-moves
-                     (or node-id (-> game :current-node :node-id))))
+                     (or node-id (-> game :current-node :node-id))
+                     remove-existing-moves?))
 
 
 (defn add-key-value-pair

(defn add-san-move
  "Adds a move in short algebraic notation to a game at a given node
  id. The move is added as the last child. If no node id is supplied,
  the current node of the game is used."
  [game san-move & {:keys [node-id]}]
  (add-move game board/move-from-san san-move
            (or node-id (-> game :current-node :node-id))))


(defn add-uci-move
  "Adds a move in UCI notation to a game at a given node id. The move is
  added as the last child. If no node id is supplied, the current node of the
  game is used."
  [game uci-move & {:keys [node-id]}]
  (add-move game board/move-from-uci uci-move
            (or node-id (-> game :current-node :node-id))))


(defn add-map-move
  "Adds a map move (see documentation for clj-chess.board/board-to-map) to a
  game at a given node id. The move is added as the last child. If no node id
  is supplied, the current node of the game is used."
  [game map-move & {:keys [node-id]}]
  (add-move game board/move-from-map map-move
            (or node-id (-> game :current-node :node-id))))


(defn add-move-sequence
  "Like add-move, but adds a sequence of moves rather than a single move. This
  is faster than calling add-move multiple times, because we don't have to
  unzip and zip the tree for each added move."
  [game move-function moves node-id]
  (let [z (-> (game-zip game node-id)
              (zip-add-move-sequence move-function moves))]
    (assoc game :root-node (zip/root z)
                :current-node (zip/node z))))


(defn add-move
  "Takes a game, a move function (a function that, given a board and
  some other value, translates that value to a chess move), a
  a value representing a move, a node id, and a boolean that says whether the
  existing moves at the current nodes should be removed, and returns an
  updated game where the move has been added as the last child at the given
  node. If no node id is supplied, the current node of the game is used."
  [game move-function move node-id remove-existing-moves?]
  (let [z (-> (game-zip game node-id)
              (zip-add-move move-function move remove-existing-moves?)
              zip/down
              zip/rightmost)]
    (assoc game :root-node (zip/root z)
