If the engine can choose between getting mated in 2 or mated in 3, it'll choose the line where it is mated in 3 (even though the mate in 2 might be 'more difficult' to spot for humans). It can't really set traps, because it doesn't know what things might be difficult to spot for a human (or other engine) opponent. It just evaluates the position, without really knowing it's playing a game against somebody; a notable exception is the implementation of [contempt](https://chess.stackexchange.com/a/26511/7297), basically a slight modification of the evaluation in order to prevent drawing too early. For example, in Stockfish, scores for won or lost positions are computed with the function `mated_in` (and, more rarely, its negated form `mate_in`). As you can see in [the code](https://github.com/official-stockfish/Stockfish/blob/221893bf679f70098e6f751fded2fe843471c6be/src/types.h#L381), a loss is scored as a large negative value plus the number of ply until the loss, so the engine favors winning lines that are fast and losing lines that are slow. The tie-breaking for draws is usually arbitrary. For instance, in Stockfish, scores for drawn positions are either the constant `VALUE_DRAW` or computed by the function [`value_draw`](https://github.com/official-stockfish/Stockfish/blob/221893bf679f70098e6f751fded2fe843471c6be/src/search.cpp#L91) to be `VALUE_DRAW` plus or minus one. The bonus depends on the thread's node counter, which is essentially up to luck, so the variations in scores for drawn positions are not really based on the positions themselves. This small bit of noise in draw scoring is more to [keep the search from fixating on certain three-fold repetitions](https://github.com/official-stockfish/Stockfish/commit/97d2cc9a9c1c4b6ff1b470676fa18c7fc6509886) than to help it choose among lines.