This is not fully smart, it only tries not to lose. As a result it fails to see simple winning moves. I will try to add that part as well. The code is very verbose. It can be optimized and cleaned up more.