Inspiration
The inspiration for this project came from an interest in the game Tic-Tac-Toe. I knew that Tic-Tac-Toe could always be drawn if the players play optimally, and I figured it would be fun to program. I successfully made Tic-Tac-Toe, but I felt it was too simple. I've been a lifelong chess player, so I tried my hand at coding chess from scratch. However, I realized just how difficult this would be, and I looked for a game a bit closer to the level of Tic-Tac-Toe. After some research, I fell upon Connect 4, which wasn't a new game to me, but also not something that came to mind.
What it does
This Connect 4 AI uses minimax to make its move. Minimax is a recursive algorithm that finds the best move for a player, assuming that the opponent is playing optimally. There are two components: the maximizer and the minimizer. The maximizer is used on the computer's turn in this case. It tries to get the highest possible score, with scores being given based on if the computer wins (20 points for example) and then subtracting the amount of moves it took (to finish the game more efficiently and effectively). On the player's turn, the algorithm tries to find the minimal possible score to demonstrate an optimal move for a player (-20 would be the score if the player wins). By combining the minimizer and maximizer, the algorithm can find best-case scenarios to either win or draw.
How we built it
Built on VS Code with Java. I used JPanel and Graphics2D to display the board. I had 4 classes: a Main class, a GamePanel class, where the main game loop was, a Board class, where the board's properties were stored, and a KeyHandler class, which was used to record when the user hit 0-6.
Challenges we ran into
There were quite a few challenges with this project. The first problem was that minimax is extremely time inefficient due to its recursive nature. For each turn, there are 7 possible moves at most, so if the depth (number of moves the algorithm calculates) is 8, then there would be over 5 million cases. Figuring out ways to speed up the process was something that took a decent amount of time. Another problem I ran into was that, since I ended up using a depth of 6 moves, the algorithm couldn't find a win (which returned a score of 0), and it played the first move it checked, which is playing the far-left column. What ended up happening was that the computer would only play on the left column unless it was in danger of losing, which is suboptimal. I ended up fixing this by creating a method that placed moves in the middle 3 columns.
Accomplishments that we're proud of
I'm proud of the fact that the AI is quite difficult to beat, and the fact that everything works as it should. I encountered an unbelievable number of bugs when coding this, so I'm very happy that the game is running smoothly.
What we learned
I learned a lot about using JPanel and Graphics to make games, as well as what the Minimax algorithm is and how it can be used. Furthermore, I feel like I gained a lot of perseverance. There were many times when I felt frustrated, but I continued to program until I was successful, which I think is a very valuable lesson.
What's next for Connect 4 AI
I want to make this Connect 4 AI even better and faster, making it impossible to beat. Also, I'd like to improve the UI to make the game look very sleek and beautiful. Once I'm unable to get a single win and create a professional game, I would like to make a chess bot.
Log in or sign up for Devpost to join the conversation.