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

I am writing a game of connect four in c++, I cannot figure out how to write a d

ID: 3817606 • Letter: I

Question

I am writing a game of connect four in c++, I cannot figure out how to write a diagonal win condition or make the pieces fall through to the bottom instead of having to input specific grid locations. Any help would be appreciated. (The board is 6 pieces high and 7 pieces long.) I am writing a game of connect four in c++, I cannot figure out how to write a diagonal win condition or make the pieces fall through to the bottom instead of having to input specific grid locations. Any help would be appreciated. (The board is 6 pieces high and 7 pieces long.)

Explanation / Answer

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits>
using namespace std;
const int ROWS = 6;
const int COLS = 7;

int board [NROWS][NCOLS] = {0, 0};
int playerMove = 1;
int victory;
void displayBoard ();
int inputNewMove ();
int checkForWin ();
void gameOver ();
void pause()
{

cin.seekg( 0, ios::end );
cin.clear();
cout << "Press ENTER to continue..." << flush;
cin.ignore( numeric_limits<streamsize>::max(), ' ' );
}

int main ()
{
displayBoard ();
for (int i = 0 ; i < 15 ; i++)
{
cout << "Player " << playerMove << "'s turn." << endl;

inputNewMove ();
displayBoard ();
victory = checkForWin ();
if (victory > 0)
{
gameOver ();
return 0;
}
}

pause();
return 0;
}

void displayBoard ()
{
// suggestion: use a lookup table
const char LUT[] = " 12";

cout << " 1 2 3 4 5 6 7 8" << endl;
cout << " ---------------";
for (int boardRow = 0 ; boardRow<NROWS ; boardRow++)// cycle through rows
{
cout << endl; // go to the next line
cout << (boardRow + 1) << "|";
for (int boardColumn = 0 ; boardColumn<NCOLS ; boardColumn++) // cycle through columns
{
cout << LUT[ board[boardRow][boardColumn] ] << ' ';

}
}
cout << endl << endl;
return;
}

bool isColumnFull( int col )
{
return board[0][col] != 0;
}

int inputNewMove()
{
int x;
int height;

for (;;)
{
cout << "Column? ";
cin >> x;

if ((x <= 8) && (x >= 1) && !isColumnFull( x-1 ))
{ //(board[8][x-1] != 8))
break;
}

else
{
cout << "Choice must be between 1 and 8, and column cannot be full." << endl;
}
}
for (height=NROWS-1; board[height][x-1] != 0; height--);

board [height][x-1] = playerMove;

if (playerMove == 1)
playerMove++;
else
playerMove--;

return 0;
}
int checkForWin ()
{

for (int i=0; i<NCOLS-3; i++)
{
for (int j=0; j<NROWS; j++)
{

if (board[j][i] == 1 && board[j][i+1] == 1 && board[j][i+2] == 1 && board[j][i+3] == 1)
{
return 1;
}
if (board[j][i] == 2 && board[j][i+1] == 2 && board[j][i+2] == 2 && board[j][i+3] == 2)
{
return 2;
}
}
}

//test HORIZONTAL victory
for (int i=0; i<NCOLS; i++) // columns
{
for (int j=0; j<NROWS-3; j++) // rows
{

if (board[j][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1 && board[j+1][i] == 1)
{
return 1;
}
if (board[j][i] == 2 && board[j+1][i] == 2 && board[j+2][i] == 2 && board[j+3][i] == 2)
{
return 2;
}
}
}


return 0;
}
void gameOver()
{
cout << endl << "Game Over! Player " << victory << "has won! " ;
pause();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote