Code inspired by the book Programming Game AI by Example