Tic Tac Toe vs. AI

Hey Arduino forum,

As my first major Arduino project i am making a Tic Tac Toe game that allows the player to play against an AI. I’m using the mega 2560 with an Adafruit LCD tft shield. I have very little experience coding, and using functions. Currently i have written the code for a person to play against the computer who will randomly select an available section of the grid unless, A. The user has 2 pieces in a row or, B. The computer has two pieces in a row. While the code sorta works, i know with my insane amount of if statements that i am not writing the code nearly as efficiently or well as i could be, I will attach my code on to this post. Thanks very much, any suggestions would be greatly appreciated.

Also i am very sorry if this is the wrong place to post this type of question, if this is the case could you guys please direct me towards where it should be.

PrototypeDefAttack.ino (27.8 KB)

I wrote a Tic Tac Toe program a million years ago on an 8080 CPU-driven SBC. Mine was competitive and would never lose (tied a lot, though).

A thought -- there are only three first moves: corner, side, or center. You can write the code to "rotate" its thinking to adjust for which corner or side is first selected. This reduces the code when being competitive.

I await the code...

I had thought about that rotating strategy but didn’t really have an idea how to implement it (I’'m not really good at programming). That’s pretty awesome though that you wrote a program that couldn’t lose. Also I’m attaching a file with the code to this post, because the code itself is to long to be posted

PrototypeDefAttack.ino (27.8 KB)