Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello, this is the assignment. The project emphasizes the use and coding of arra

ID: 3758662 • Letter: H

Question

Hello, this is the assignment.

The project emphasizes the use and coding of arrays, subroutines (and passing data), algorithms and a basic user interface.

You are create a simulation of this game using c (no other language is acceptable).

A graphic interface (shown here) is not required, text/characters are sufficient.
X's and O's may used, like in Tic-Tac-Toe with a dash or blank as the background. Illegal moves are not allowed (and thus must be checked).
You'll need to check for a winner, announce such and be able to play the simulation again .... until the user indicates to end the program.

I had a code however that is not the right one for the connect.4

#include<stdio.h>
#include<conio.h>

void playing_area();
void Player1();
void Player2();
void winner();
void status();
int win=0,wrong_X=0,wrong_O=0,chk=0;

char name_X[30];
char name_O[30];
int pos_for_X[3][3];
int pos_for_O[3][3];
int pos_marked[3][3];

void main()
{
   int i,ch,j;
   char ans;
   do
   {
       printf(" TIC TAC TOE");
       printf(" ");
       for(i=1;i<=11;i++)
       {
           printf("*");
       }
       printf(" 1.Start The Game");
       printf(" 2.Quit The Game");
       printf(" Enter your choice(1-2) : ");
       scanf("%d",&ch);
       switch(ch)
       {
           case 1:
               chk=0;
               win=0;
               for(i=1;i<=3;i++)
               {
                   for(j=1;j<=3;j++)
                   {
                       pos_for_X[i][j]=0;
                       pos_for_O[i][j]=0;
                       pos_marked[i][j]=0;
                   }
               }
               printf(" ");
               printf(" Enter the first player name: ");
               fflush(stdin);
               gets(name_X);
               printf(" Enter the second player name: ");
               fflush(stdin);
               gets(name_O);
               playing_area();
               for(;;)
               {
                   if(win==1)
                       break;
                   status();
                   if(chk==9)
                   {
                       printf(" MATCH DRAWS!!");
                       printf(" Press any key....");
                       break;
                   }
                   else
                       chk=0;
                   printf(" TURN FOR %s:",name_X);
                   Player1();
                   do
                   {
                       if(wrong_X!=1)
                           break;
                       wrong_X=0;
                       printf(" TURN FOR %s:",name_X);
                       Player1();
                   }while(wrong_X==1);
                   status();
                   if(chk==9)
                   {
                       printf(" MATCH DRAWS");
                       printf(" Press any key....");
                       break;
                   }
                   else
                       chk=0;
                   printf(" TURN FOR %s:",name_O);
                   Player2();
                   do
                   {
                       if(wrong_O!=1)
                           break;
                       wrong_O=0;
                       printf(" TURN FOR %s:",name_O);
                       Player2();
                   }while(wrong_O==1);

                   }
               playing_area();
               if(win!=1)
               {
                   printf(" MATCH DRAWS!!");
                   printf(" Press any key.......");
               }
               getch();
               break;
           case 2:
               printf(" Thank You For Playing The Game.");
               printf(" ###############################");
               getch();
               exit(1);
               break;
       }
       printf(" Want To Play(Y/N) ? ");
       fflush(stdin);
       scanf("%c",&ans);
   }while(ans=='y' || ans=='Y');
}


void playing_area()
{
   int i,j;
   printf(" TIC TAC TOE playing_area");
   printf(" *****************");
   printf(" ");
   printf(" 1 2 3");
   for(i=1;i<=3;i++)
   {
       printf(" _____________________________");
       printf(" ¦ ¦ ¦ ¦");
       printf(" %d ",i);
       for(j=1;j<=3;j++)
       {

           if(pos_for_X[i][j]==1)
           {
               printf(" X");
               printf(" ");
           }
           else if(pos_for_O[i][j]==1)
           {
               printf(" O");
               printf(" ");
           }
           else
           {
               printf(" ");
               continue;
           }
       }
       printf(" ¦ ¦ ¦ ¦");
   }
   printf(" ------------------------------");
   winner();
}


void Player1()
{
   int row,col;
   if(win==1)
       return;
   printf(" Enter the row no. : ");
   fflush(stdin);
   scanf("%d",&row);
   printf("Enter the column no. : ");
   fflush(stdin);
   scanf("%d",&col);
   if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
   {
       printf(" WRONG POSITION!! Press any key.....");
       wrong_X=1;
       getch();
       playing_area();
   }
   else
   {
       pos_for_X[row][col]=1;
       pos_marked[row][col]=1;
       playing_area();
   }
}
void Player2()
{
   int row,col;
   if(win==1)
       return;
   printf(" Enter the row no. : ");
   scanf("%d",&row);
   printf("Enter the column no. : ");
   scanf("%d",&col);
   if(pos_marked[row][col]==1 || row<1 || row>3 || col<1 || col>3)
   {
       printf(" WRONG POSITION!! Press any key....");
       wrong_O=1;
       getch();
       playing_area();
   }
   else
   {
       pos_for_O[row][col]=1;
       pos_marked[row][col]=1;
       playing_area();
   }
}
void winner()
{
   int i;
   for(i=1;i<=3;i++)
   {
       if(pos_for_X[i][1]==1 && pos_for_X[i][2]==1 && pos_for_X[i][3]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_X);
           printf(" Press any key............");
           return;
       }
   }
   for(i=1;i<=3;i++)
   {
       if(pos_for_X[1][i]==1 && pos_for_X[2][i]==1 && pos_for_X[3][i]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_X);
           printf(" Press any key............");
           return;
       }
   }
   if(pos_for_X[1][1]==1 && pos_for_X[2][2]==1 && pos_for_X[3][3]==1)
   {
       win=1;
       printf(" RESULTL: %s wins!!",name_X);
       printf(" Press any key......");
       return;
   }
   else if(pos_for_X[1][3]==1 && pos_for_X[2][2]==1 &&
pos_for_X[3][1]==1)
   {
   win=1;
       printf(" RESULT: %s wins!!",name_X);
printf(" Press any key.....");
       return;
   }

for(i=1;i<=3;i++)
   {
       if(pos_for_O[i][1]==1 && pos_for_O[i][2]==1 && pos_for_O[i][3]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_O);
printf(" Press any key.....");
           return;
       }
   }
   for(i=1;i<=3;i++)
   {
       if(pos_for_O[1][i]==1 && pos_for_O[2][i]==1 && pos_for_O[3][i]==1)
       {
           win=1;
           printf(" RESULT: %s wins!!",name_O);
printf(" Press any key.....");
           return;
       }
   }
   if(pos_for_O[1][1]==1 && pos_for_O[2][2]==1 && pos_for_O[3][3]==1)
   {
       win=1;
       printf(" RESULT: %s wins!!",name_O);
       printf(" Press any key.....");
       return;
   }
   else if(pos_for_O[1][3]==1 && pos_for_O[2][2]==1 &&
pos_for_O[3][1]==1)
   {
   win=1;
       printf(" RESULT: %s wins!!",name_O);
printf(" Press any key.....");
       return;
   }
}
void status()
{
   int i,j;
   for(i=1;i<=3;i++)
   {
       for(j=1;j<=3;j++)
       {
           if(pos_marked[i][j]==1)
               chk++;
           else
               continue;
       }
   }
}

I need a code for the connect.4

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BOARD_ROWS 6
#define BOARD_COLS 7
void printBoard(char *board);
int takeTurn(char *board, int player, const char*);
int checkWin(char *board);
int checkFour(char *board, int, int, int, int);
int horizontalCheck(char *board);
int verticalCheck(char *board);
int diagonalCheck(char *board);
int main(int argc, char *argv[])
{
const char *PIECES = "XO";
char board[BOARD_ROWS * BOARD_COLS];
memset(board, ' ', BOARD_ROWS * BOARD_COLS);
int turn, done = 0;
for(turn = 0; turn < BOARD_ROWS * BOARD_COLS && !done; turn++)
{
printBoard(board);   
while(!takeTurn(board, turn % 2, PIECES))
{
printBoard(board);   
puts("**Column full!** ");
}
done = checkWin(board);
}
printBoard(board);
if(turn == BOARD_ROWS * BOARD_COLS && !done)
{
puts("It's a tie!");
}
else
{
turn--;
printf("Player %d (%c) wins! ", turn % 2 + 1, PIECES[turn % 2]);
}
return 0;
}
void printBoard(char *board)
{
int row, col;
puts(" ****Connect Four**** ");
for(row = 0; row < BOARD_ROWS; row++)
{
for(col = 0; col < BOARD_COLS; col++)
{
printf("| %c ", board[BOARD_COLS * row + col]);
}
puts("|");
puts("-----------------------------");
}
puts(" 1 2 3 4 5 6 7 ");
}
int takeTurn(char *board, int player, const char *PIECES)
{
int row, col = 0;
printf("Player %d (%c): Enter number coordinate: ", player + 1, PIECES[player]);
while(1)
{
if(1 != scanf("%d", &col) || col < 1 || col > 7 )
{
while(getchar() != ' ');
puts("Number out of bounds! Try again.");
}
else
{
break;
}
}
col--;
for(row = BOARD_ROWS - 1; row >= 0; row--)
{
if(board[BOARD_COLS * row + col] == ' ')
{
board[BOARD_COLS * row + col] = PIECES[player];
return 1;
}
}
return 0;
}
int checkWin(char *board)
{
return (horizontalCheck(board) || verticalCheck(board) || diagonalCheck(board));
}
int checkFour(char *board, int a, int b, int c, int d){
return (board[a] == board[b] && board[b] == board[c] && board[c] == board[d] && board[a] != ' ');
}
int horizontalCheck(char *board)
{
int row, col, idx;
const int WIDTH = 1;
for(row = 0; row < BOARD_ROWS; row++)
{
for(col = 0; col < BOARD_COLS - 3; col++)
{
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + WIDTH, idx + WIDTH * 2, idx + WIDTH * 3)){
return 1;
}
}
}
return 0;
}
int verticalCheck(char *board)
{
int row, col, idx;
const int HEIGHT = 7;
for(row = 0; row < BOARD_ROWS - 3; row++)
{
for(col = 0; col < BOARD_COLS; col++)
{
idx = BOARD_COLS * row + col;
if(checkFour(board, idx, idx + HEIGHT, idx + HEIGHT * 2, idx + HEIGHT * 3)){
return 1;
}
}
}
return 0;
}
int diagonalCheck(char *board)
{
int row, col, idx, count = 0;
const int DIAG_RGT = 6, DIAG_LFT = 8;
for(row = 0; row < BOARD_ROWS - 3; row++)
{
for(col = 0; col < BOARD_COLS; col++)
{
idx = BOARD_COLS * row + col;
if(count <= 3 && checkFour(board, idx, idx + DIAG_LFT, idx + DIAG_LFT * 2, idx + DIAG_LFT * 3) || count >= 3 && checkFour(board, idx, idx + DIAG_RGT, idx + DIAG_RGT * 2, idx + DIAG_RGT * 3)){
return 1;
}
count++;
}
count = 0;
}
return 0;
}