Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem A: Connect Four This part of the homework relates to the Connect-4 code posted on the website. The general purpose of Connect-4 is to

Problem A: Connect Four

This part of the homework relates to the Connect-4 code posted on the website. The general purpose of Connect-4 is to be the first person to get 4-in-a-row. This game has historically been played where you slid chips into columns, so the play is always the lowest available row in the column. See this for more details if you are unfamiliar with the rules:

https://en.wikipedia.org/wiki/Connect_Four

The code is complete, except for the fourInARow() function. This function is supposed to check and see if there are four consecutive tiles/pieces in the direction(s) indicated. Without this code, players cannot win (but a tie is possible if you fill up the board). Complete this function to correctly detect if there is four in a row at the indicated spot in the game. The rest of the code is already completed. All you need to do is add the function where instructed in the comments of the code.

Example 1 (type in 1 2 1 2 1 2 1 ):

-------

-------

X------

XO-----

XO-----

XO-----

1234567

The mighty Xs reign supreme!

Example 2 (type in 2 1 3 2 3 3 4 4 4 4):

-------

-------

---O---

--OX---

-OXO---

OXXX---

1234567

The Os have completed world domination!

*PLEASE SIMPLY ADD TO THE CODE ATTACHED AND ADD THE fourInARow() FUNCTION WHERE INSTRUCTED

*PLEASE CODE IN C++

*THANK YOU

Here is the code:

#include

using namespace std;

const int RSIZE = 6; const int CSIZE = 7;

bool fourInARow(char board[RSIZE][CSIZE], int i, int j, int dr, int dc) { // FILL ME IN FOR PART B! return 0; }

// DO NOT CHANGE ANYTHING BELOW HERE IN THE FINAL SUBMISSION

const char BLANK = '.'; const char P1 = 'X'; const char P2 = 'O';

char winner(char board[RSIZE][CSIZE]); string play(char board[RSIZE][CSIZE], int col, char who); bool fourInARow(char board[RSIZE][CSIZE], int i, int j, int dr, int dc); void print(char board[RSIZE][CSIZE]);

int main() { char board[RSIZE][CSIZE]; for(int i=0; i< RSIZE; i++) { for(int j=0; j < CSIZE; j++) { board[i][j] = BLANK; } } string message = ""; char player = P2; while( !winner(board)) { if(player == P2 && message == "") // rotate player turns { player = P1; } else if(player == P1 && message == "") { player = P2; } print(board); cout << message << endl; message = ""; cout << "Which column do you wish to play in? "; char ans; cin >> ans; string dump; getline(cin, dump); if('1' <= ans && ans <= '7') { message = play(board,ans-'1', player); } else if(ans == 'q') { exit(0); } else { message = "Not a valid move, try again..."; // ima comment! } } print(board); cout << endl; if(winner(board) == P1) { cout << "The mighty " << P1 << "s reign supreme! "; } else if(winner(board) == P2) { cout << "The " << P2 << "s have completed world domination! "; } else if(winner(board) == BLANK) { cout << "Zzzzzzzzzzzzz... draw "; } else { cout << "Derp... something went wrong!"; } }

char winner(char board[RSIZE][CSIZE]) { int blanks = 0; for(int i=0; i< RSIZE; i++) { for(int j=0; j < CSIZE; j++) // loop through all cells on board { if(board[i][j] == BLANK) // count how many blanks { blanks++; } for(int k=2; k < 6; k++) // check all four directions (up/down, left/right, up-right/down-left, up-left/down-right). { int dr=k/3, dc=3+dr-k ; // dr & dc change in row/column. so up/down means dr=1, dc=0 if(board[i][j] != BLANK && fourInARow(board,i,j,dr,dc)) // if 4 in a row (or more) return which player won (char) { return board[i][j]==P1?P1:P2; } } } } if(blanks == 0) // board full and no winner { return BLANK; } else // still going! { return 0; } }

string play(char board[RSIZE][CSIZE], int col, char who) { for(int i=RSIZE-1; i >= 0; i--) { if(board[i][col] == BLANK) { board[i][col] = who; return ""; } } return "That column is full (idiot)."; }

void print(char board[RSIZE][CSIZE]) { cout << " "; for(int i=0; i < RSIZE; i++) { for(int j=0; j < CSIZE; j++) { cout << board[i][j]; } cout << endl; } for(int i=1;i<8;i++) { cout << i; } cout << endl; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

What is oxygens specific heat at constant volume in J/kg K?

Answered: 1 week ago

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago