C++ project connect 4 game || source code

Play the classic Connect 4 game in C++. It's a two-player game of strategy where you try to connect four pieces in a row. The game features a graphical console interface. You can win by connecting four pieces horizontally, vertically, or diagonally. The game also checks for ties. You'll use arrays, loops, and player interaction to build the game



 Introduction:

This C++ code creates a virtual representation of the Connect 4 game. It simulates a grid where two players drop colored disks. The goal is to connect four disks of the same color in a row, column, or diagonal before the other player does.


#include<iostream>
#include<windows.h>
using namespace std;
char conn_4[7][6];
int counter[7] = { 0 };
void print() {
 system("color 06");
 cout << "\t\t\t************************* CONNECT 4 GAME **************************\n\n\n";
 for (int i = 5; i >= 0; i--) {
  cout << endl << "\t\t\t\t\t |___|___|___|___|___|___|___|\n";
  cout << "\t\t\t\t\t" << " |";
  for (int j = 0; j < 7; j++) {
   char x = ' ';
   if (conn_4[j][i] == 'X' or conn_4[j][i] == 'O') {
    x = conn_4[j][i];
   }
   cout << " " << x << " |";
  }
 }
 cout << endl << "\t\t\t\t\t ___________________________";
 cout << endl << "\t\t\t\t\t |_1_|_2_|_3_|_4_|_5_|_6_|_7_|";
}
bool is_win(int x) {
 int win = 0;
 bool iswin = 0;
 for (int i = 0; i < counter[x] - 1; i++) {
  if (conn_4[x][i] == conn_4[x][i + 1] && (conn_4[x][i] == 'X' or conn_4[x][i] == 'O')) {
   ++win;
  }
  else {
   win = 0;
  }
  if (win == 3) {
   iswin = 1;
   break;
  }
 }
 win = 0;
 if (!iswin) {
  for (int i = 0; i < 6; i++) {
   if (conn_4[i][counter[x] - 1] == conn_4[i + 1][counter[x] - 1] && (conn_4[i][counter[x] - 1] == 'X' or conn_4[i][counter[x] - 1] == 'O')) {
    win++;
   }
   else {
    win = 0;
   }
   if (win == 3) {
    iswin = 1;
    break;
   }
  }
  if (!iswin) {
   win = 0;
   int x_a = 0, y_a = 0;
   if (x > counter[x] - 1) {
    x_a = x - (counter[x] - 1);
   }
   else
    y_a = (counter[x] - 1) - x;
   while (x_a < 6 && y_a < 5) {
    if (conn_4[x_a][y_a] == conn_4[x_a + 1][y_a + 1] && (conn_4[x_a][y_a] == 'X' or conn_4[x_a][y_a] == 'O')) {
     win++;
    }
    else {
     win = 0;
    }
    if (win == 3) {
     iswin = 1;
     break;
    }
    x_a++; y_a++;
   }
  }
 }
 return iswin;
}
bool is_end() 
{
 int co = 0;
 for (int i = 0; i < 7; i++) {
  for (int j = 0; j < 6; j++) {
   if (!(conn_4[i][j] == 'X' or conn_4[i][j] == 'O')) {
    return 0;
    break;
   }
   else {
    co++;
   }
  }
 }
 if (co == (6 * 7)) {
  return 1;
 }
 else {
  return 0;
 }
}
int main() {
 int x = 1, y = 1;
 char c = 'X';
 string player1, player2;
 cout << " --------------------------------------------------" << endl;
 cout << " WELCOME TO CONNECT 4" << endl;
 cout << " --------------------------------------------------" << endl;
 cout << "Enter Player 1 name: ";
 cin >> player1;
 cout << "Enter Player 2 name: ";
 cin >> player2;
 int tossResult = rand() % 2;
 if (tossResult == 0) {
  cout << player1 << " won the toss! They will start with 'X'\n";
 }
 else 
 {
  cout << player2 << " won the toss! They will start with 'O'\n";
  c = 'O';
 }
 print();
 while (true) {
  if (is_end()) {
   system("color 04");
   cout << "\n\n\t\t\tNO ONE WON. IT'S A DRAW. \n";
   break;
  }
  cout << "\n\n\t\t\t\t\t" << (c == 'X' ? player1 : player2) << "'s turn - select the column (1 to 7): ";
  cin >> x;
  if (0 < x && x <= 7) {
   x--;
   if (counter[x] < 6) {
    conn_4[x][counter[x]] = c;
    if (c == 'X') {
     c = 'O';
    }
    else {
     c = 'X';
    }
    counter[x]++;
   }
   else {
    cout << "this column is full. choose a different column.\n";
    system("color 04");
    if (is_end()) {
     break;
    }
    continue;
   }
  }
  else {
   system("color 04");
   cout << "\n\n\t\t\t\t\tinsert a valid column please :/\n";
   continue;
  }
  system("CLS");
  print();
  if (is_win(x)) {
   system("color 0A");
   cout << "\n\n\n\t\tWINNER IS :: " << (c == 'X' ? player1 : player2) << endl;
   break;
  }
 }
 system("pause");
 system("cls");
 return 0;
}


Main Features:

1. Print Function (`print()`): Displays the Connect 4 game board with the current state of discs.

2. Winning Condition (`is_win(int x)`): Checks if the current move results in a win for the player.

3. End of Game Condition (`is_end()`): Checks if the game has ended in a draw.

4. Main function (`main()`): Implements the game loop and player interactions.




Explanation:

- The `conn_4` array represents the Connect 4 grid, initialized with empty spaces.

- The `counter` array keeps track of the number of discs in each column.

- The `print()` function displays the Connect 4 game board with colored discs.

- The `is_win(int x)` function checks if the current move results in a win for the player by examining horizontal, vertical, and diagonal lines.

- The `is_end()` function checks if the game has ended in a draw by verifying if the grid is fully filled.

- In the `main()` function:

  - Player names are inputted.

  - A coin toss determines which player starts the game with either 'X' or 'O'.

  - Players take turns selecting a column to drop their disc.

  - The game board is updated, and winning/draw conditions are checked after each move.

  - The game continues until a player wins or the game ends in a draw.


Conclusion:

This C++ program lets you play Connect 4 against another person on a grid displayed on your computer screen. It uses arrays, loops, conditions, and functions to create a basic but fun game.



Comments

Popular posts from this blog

How to hack Facebook account using termux (password guessing method)

From Start To Success| A Step By Step Guide To Becoming A Profitable Amazon Seller

Bitcoin Price Prediction In 2024: Boom Or Bust?