Starting from:
$35

$29

Connect-Four Solution

Description-

The program declares a board in the main method and then all the functions basically fill out and check from the same board. makeBoard, print and playTurn are straightforward and do as the name suggests. fourInBoard is the main checking function which uses checkHorizontal, checkVertical and checkDiagonal. MainAI is the method which deals with running the AI and returns an int which corresponds to the column it wants to play in. It has 3 building and 3 blocking moves.

They are arranged in a specific way based on game theory to make the AI more competitive.




The almostFour method is a brute force on the board and checks if there is a senerio where for the given token, there is a possibility to have a connect four. It uses this offensively and first checks if it is 1 move away from winning, it then uses it defensively and checks if it can block the opponent who is 1 move away from winning.




The hasFourEmptyHorizontally and hasFourEmptyVertically are 2 other ways the AI can check if it is possible for itself or the opponent to get a connect four horizontally or vertically based on blank spots and the respective tokens. It uses both these methods offensively and defensively to build four for itself and block the opponent.




Functions-

1. General Functions (Functions that run the game such as making and printing the board etc)

- makeBoard - Populates the board with row and col headers and blank dots.

- print - Prints the board in proper format

- playTurn - Inserts the token in respective col of the board. Also acts as a function to check if a col is full or not

- copyArray - Copys the board onto another 2D array




2. Checking Functions (These functions are basically the rule set of the game and are used for checking if someone won or not or if its a draw)

- checkVertical - checks if there are 4 same tokens vertically in the board

- checkHorizontal - checks if there are 4 same tokens horizontally in the board

- checkDiagonal - checks if there are 4 same tokens diagonally in the board

- fourInBoard - is the higher order function that use the previous three functions to see if there are four tokens inline on the board

- fullBoard - a function to check if the board in full of tokens (in case of a draw)




3. AI Functions (These functions are what runs the AI, some of them are more specific checking functions that help the AI decide its move)

- mainAI - the main driver for the AI (makes use of the 3 functions below)

- hasFourEmptyHorizontally - Checks if it is possible for a given player to build a line of 4 tokens horizontally

- hasFourEmptyVertically - Checks if it is possible for a given player to build a line of 4 tokens vertically

- almostFour - brute forces the board and sees if the player is 1 move away from winning.

More products