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

Goal: Implement Connect Four using the Model-View-Controller architecture. Conne

ID: 3829388 • Letter: G

Question

Goal: Implement Connect Four using the Model-View-Controller architecture. Connect Four is a game similar to Tic-Tac-Toe. Two players alternate dropping tokens into a grid (7 columns times 6 rows) where tokens fall as far down into the grid as gravity allows. If there is no space available in a particular column, then a player cannot place a token there The game ends (a) when a player places four tokens in a row horizontally, vertically, or diagonally, or (b) when the entire grid is filled so that no player can make a move. Use the Model-View-Controller software architecture to implement a version of the Connect Four game where players switch off using the same keyboard/mouse. The GUI must show the following information at all times: The current state of the game board Whose turn it is In the event that the game ends, it must show which player won, or that the game was a draw. Additionally, the GUI must allow users to perform the following actions: Pick which column to place a token in, while also disallowing placement of tokens in columns that are full Reset the game to an empty game board Quit the game and exit the program. In the event that the game ends, the GUI must notify the players which player won or that the game was a draw.

Explanation / Answer


#include<bits/stdc++.h>
using namespace std;
#define COMPUTER 1
#define HUMAN 2
#define SIDE 3 // Length of the board
#define COMPUTERMOVE 'O'
#define HUMANMOVE 'X'
void showBoard(char board[][SIDE])
{
printf(" ");
printf(" %c | %c | %c ", board[0][0],
board[0][1], board[0][2]);
printf(" -------------- ");
printf(" %c | %c | %c ", board[1][0],
board[1][1], board[1][2]);
printf(" -------------- ");
printf(" %c | %c | %c ", board[2][0],
board[2][1], board[2][2]);
  
return;
}

void showInstructions()
{
printf(" Tic-Tac-Toe ");
printf("Choose a cell numbered from 1 to 9 as below"
" and play ");

printf(" 1 | 2 | 3 ");
printf(" -------------- ");
printf(" 4 | 5 | 6 ");
printf(" -------------- ");
printf(" 7 | 8 | 9 ");

printf("- - - - - - - - - - ");

return;
}

void initialise(char board[][SIDE], int moves[])
{
srand(time(NULL));

// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}

for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;

random_shuffle(moves, moves + SIDE*SIDE);

return;
}

void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf("COMPUTER has won ");
else
printf("HUMAN has won ");
return;
}

bool rowCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != ' ')
return (true);
}
return(false);
}

bool columnCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
return (true);
}
return(false);
}

bool diagonalCrossed(char board[][SIDE])
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != ' ')
return(true);

if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != ' ')
return(true);

return(false);
}

bool gameOver(char board[][SIDE])
{
return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}

void playTicTacToe(int whoseTurn)
{
char board[SIDE][SIDE];

int moves[SIDE*SIDE];
initialise(board, moves);
showInstructions();

int moveIndex = 0, x, y;
while (gameOver(board) == false &&
moveIndex != SIDE*SIDE)
{
if (whoseTurn == COMPUTER)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in cell %d ",
COMPUTERMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = HUMAN;
}

else if (whoseTurn == HUMAN)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = HUMANMOVE;
printf ("HUMAN has put a %c in cell %d ",
HUMANMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER;
}
}
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw ");
else
{
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
declareWinner(whoseTurn);
}
return;
}

int main()
{
playTicTacToe(COMPUTER);

return (0);
}

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