Inspiration

Looking at all the games I have built before, none of them seem to be “classics.” What I mean by this is all of the games I have made before in the past are original ideas. Sometimes, they do go off of other games(such as similar concepts or game style), but I was the original creator of those games. I realized that I wanted to remake a classic and simple game. I immediately thought of all the games I had played in my life, and saw an opportunity in tic tac toe. Whenever I am bored, I find myself drawing a board and playing with whoever is near me. It gives players temporary satisfaction. I decided to choose this game to remake.

What it does

If it’s already not self explanatory, it is a tic tac toe AI. You don’t have to find another person to play with. All you do is hit run and you are in the game. The computer plays against you - it knows what boxes are full and which ones are empty. Then it fills the box with an “O” strategically. 

How I built it

I realized the best way to go about this was by using a nested array as the board. As I started to make a function to print out the UI of the board, and to make the board, I realized that I had to make a separate file for all the functions, because the main file would get too messy. I made a total of 6 functions, including the function for the AI(make the board, print the board, get user input, get computer input, check if the game ended, and the actual AI).Since the board was a nested loop, if I ever wanted to change or check something on it, all I had to do was make a for loop and go through the board and check or immediately just go to the box where I needed to go. It was really easy this way, and made the whole process a lot more simple. After making all the basic functions, it was time to make the AI. I first made an AI that chose random locations. This worked flawlessly, but it was still pretty stupid. So I planned out the AI. As I said before, the AI had to play strategically, and by that I meant it had to have a list of priorities. First, it checked if it could win right there. It did this by putting an “O” on the board, and checking if it won. If it did win, then it would keep it. If not, then it would change and move to the next available spot. If it could not win, then it would move on to choosing the center box. If that is full, then it will choose one of the 4 corner boxes, and then finally one of the 4 middle boxes. When the AI’s turn is done the game will check if anyone has won, and then it will be the user’s turn. This will keep going until someone wins, or there is a tie.

Challenges I ran into

Since I don’t have much experience with nested loops, I ran into a couple challenges along the way. First, when making the check function to see if anyone had one, I tried in many ways to shorten it, but I found that I had to use all those if statements, no matter what. I also ran into basic syntax problems, but I was able to figure those out pretty quickly. The major challenge I faced was writing some code and then trying to connect it to the board. In other words, when I made the check function for example, I knew how it was going to work, but not how I was going to implement it. I went through a lot of trial and error until finally I figured it out.

What I learned

As I mentioned before, I was not very good with nested lists. After this project however, I feel that I have a very good grasp on how they work, and how I can use them. I also learned that although some things may seem like they have an easy answer, sometimes you have to go the hard way to solve it.

What's next for Tic Tac Toe AI

Honestly, I’m not sure. I feel that all I might do is improve the UI a little bit. However, for the AI, I have a lot of plans in mind. Since this is my first real “AI”, I plan on making more AIs and using this as a role model, or a goal to meet when making them. I used a lot of logical thinking when making this, and looking at this when doing future projects will really help me out. When I learn more about AI in the future, I will definitely revisit this project and make my AI more hard to beat. But for now, this is all my feeble fifteen year old mind can do.

Built With

Share this project:

Updates