Skip to content

kcmadd/Connect-Four

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

In my Connect Four project, there are eight methods in total. The method, void initBoard(char board[7][8]), initializes the board by first initializing the coordinate (0, 0) to  ‘ ‘ (a blank space), then initializes the first row at (0, j) to the column names (A-G) by using the equation char c = 'A' + j – 1, lastly this method initialize the row (i, 0) to numbers and the rest of the board to ‘.’. The void printBoard(char board[7][8]) method uses a double for loop with an if-else statement on the inside to check for character ad iterates through out the board to print it. The void gameMechanics(char board[7][8], char playerOne, char playerTwo)  method is the most complex method of the project as it controls movement of the player and alternates between the player and AI. The method uses a while loop with the condition to check if a player has won. If playerOne or playerTwo wins, the loop ends. The method uses an if statement inside the loop to check for a fullboard. If the board isn’t full, it uses a while loop to check for a valid column entry. If the column isn’t entry isn’t valid, then the it will loop until playerOne enters a valid column. After this while loop, a secondary while loop is used with the condition that checks for if the column is full. If it isn’t full, the column entry is converted to an integer using int letterConversion = (columnLetter - 'A') + 1. An if statement is used to check if the coordinate (6, letterCoversion) is equal to ‘.’. if it is, then that coordinate is replaced with playerOne’s token. If it isn’t equal to ‘.’, then a for loop is used to iterate down the column for the next available spot. Whenever a move is made by playerOne, a method call to void aiMechanics(char board[7][8], int row, int col, char playerOne, char playerTwo), to make a move for the AI. This method essentially uses the position of the last token placed by playerOne to place the token of playerTwo on top of it. If playerOne has three tokens in row then the AI will try to block it by placing a token on the right of the third token in a row. The AI maintains continues placing tokens on the right of playerOne for the rest of the game unless playerOne places a token in column G in which the AI will place the token in column F. The checkWin function iterates through the entire board the check for diagonal in a row in both directions, horizontal in a row, and vertical in row.

Video Link: 
NOTE: Please watch both videos
https://drive.google.com/drive/folders/19tpJywW5E_fHnqngJTMgweJbM70JLw80?usp=sharing

About

Implementation of Connect Four in C

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages