AI for a Connect 4 game
CS4523 – Artificial Intelligence Project 2 Goal Utilize min-max search to program an AI agent that can play a variant of the popular “Connect 4” game. The program will implement min-max and move selection within a specified amount of time so you can have humans play each other, a human play against the AI, and have two AI players compete. Student Outcomes Addressed 4. Demonstrate ability for reasoning with uncertainty and decision making, machine learning with various learning models. Details Your task is to develop a program that handles the data structures and algorithms needed to play a variant of the “Connect 4” game. “Connect 4” assumes a 7×6 board (shown below) with two players – red and black. Players take turns placing a disc of their color into a column until the board fills (a tie) or until one player is able to get 4 discs in a row (horizontally, vertically, or along a diagonal). You’ll notice in the image above, black has won. You’re an epic student who knows that if you can solve the problem of a 7×6 grid that requires 4 in a row to win, you can certainly solve the problem of an NxM grid that requires R in a row to win. Thus, your program will • Prompt the user for width and height and win-requirement input at the start of the program. • Base all of your algorithms and data structures on this input. Next, your program should implement the min-max algorithm (with alpha-beta pruning as an optional, extra-credit choice). This algorithm is fairly well defined, so you hopefully shouldn’t struggle to design it; rather, your time should be focused on understanding it and then implementing it into your desired programming language. Recall that min-max requires you implement the following: • A function that takes in a state and generates all possible states given the valid moves/ rules of the game. Hopefully you see that this will be relatively easy given the nature of the “NxM Connect-R” game we’re using in this project. ☺ • A utility function that takes in a state and player and generates a numeric value denoting the quality of the state for the player. For example, a -1 indicates a loss, a +1 indicates a win, and every value in between is a qualitative judgment on the state of the board. • A terminal test function that takes in a state and returns whether the game is over or not. Notice that if you’ve written the utility function above, the terminal test function becomes trivial. • The recursive min-max function itself. See page 166 in the course text. Code the above and have your program • Display the board state • Prompt the user for their move and make this move • Run the min-max function to select the move for the AI and then make that move for the AI • Repeat until the game is over (displaying tie/win/loss upon conclusion) Once you’ve achieved the above, the fun begins. ☺ While it’s fun to have the human play against your AI, being able to have humans play against humans and AI play against AI is even better. You will play a one-player game against your AI and the other player will do the same. You will take the AI inputs from the other player as the inputs for your game, then take your AI output and tell the other player who will do the same. You must agree on who will go first before you begin so that the turns are in sync. Note that while a more robust game would manage the game setup by ensuring both clients’ board sizes and connect requirement match and that the players cannot be of the same color, we’ll assume both clients are coordinating such that they both specify the same board parameters and also select different colors. We will assume the RED player goes first. Make sure your program prompts the user for whether they’ll be human or AI, BLACK or RED, and the board setup information when it begins. Thereafter, if it’s an AI player, when it’s their turn, the min-max algorithm should be invoked and the move selected should be sent to the other player; otherwise, if it’s a human player, prompt the user for their move and send this to the other player (you switch roles, essentially). One final requirement… it’s typically bad form for one player to take too long to make a move, and often we don’t want the AI to search too long for a move. So you should now modify your program to be multithreaded such that when a timer goes off after T time, the game sends the best move seen by the AI so far after time T to the server regardless of what the AI agent is doing. One thread in your program should track time, and the other thread should min-max search for a move; when the time is up, the timer thread reads the “best move seen so far” from the min-max worker thread and sends the move to the server. This means that you should be storing the best move seen thus far as your algorithm progresses. If the player is a human, you can assume no time limit is required, so you don’t need to multithread when a human player is playing. Your program can be written in any programming language you desire. Grading • 20% – overall structure of the program and style/clarity of code • 65% – correct implementation of min-max o 10% – generate child states based upon valid moves o 25% – recursive function correctly defined o 25%- utility evaluation function defined o 5% – terminal test functions defined • 15% – threaded agent that responds within T milliseconds +10% extra credit for alpha-beta pruning We’ll have a contest at the end wherein we’ll pit your AIs against each other. Whichever student’s AI wins the tournament is exempted from the final exam and earns a 100% on the final. Deadlines 3/21 Complete data structures & min-max so game plays with human-vs-AI 3/28 Finalize gameplay with heuristic 3/31 Game finished including multithreading We’ll grade each of these deliverables/milestones on the dates above, so you are required to submit your progress by 6am on these dates via D2L. If you miss the deadline, you can still get credit for the first two tasks, but you’ll incur a 5% penalty each; thus if you wait until the end of the project and have a perfect project, you’ll be graded out of 90% (-5% for each of the two earlier deadlines missed). Sample output (human playing AI) Board Width? 7 Board Height? 6 Connect? 4 Board state value = 0 ……. ……. ……. ……. ……. ……. Move? (0-6) – 4 Board state value = -0.0238095238095238 ……. ……. ……. ……. ……. …OX.. Move? (0-6) – 4 Board state value = -0.0238095238095238 ……. ……. ……. ……. ….X.. ..OOX.. Move? (0-6) – 0 Board state value = -0.0952380952380952 ……. ……. ……. ……. …OX.. X.OOX.. Move? (0-6) – 1 Board state value = -0.119047619047619 ……. ……. ……. ……. ..OOX.. XXOOX.. Move? (0-6) – 6 Board state value = -0.142857142857143 ……. ……. ……. ……. .OOOX.. XXOOX.X Etc.
Is this the question you were looking for? If so, place your order here to get started!