Question

Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; const int...

Write the following function:

const int MIN_SIZE = 2;
const int MAX_SIZE = 8;

const int UNKNOWN = 0;
const int RED = 1;
const int BLUE = 2;

const char UNKNOWN_LETTER = '-';
const char RED_LETTER = 'X';
const char BLUE_LETTER = 'O';

const string UNKNOWN_STRING = "unknown";
const string RED_STRING = "X";
const string BLUE_STRING = "O";

/**
* Requires: size <= MAX_SIZE and size is a positive even integer,
*           0 <= row && row < size. The board must be valid.
* Modifies: board, cout
* Effects : This function writes the opposite color in UNKNOWN squares in row:
*           * on both ends of two consecutive tiles (example 1) in row, and
*           * in the middle between two tiles of the same color in row
*             (example 2)
*           If announce is true, prints out a message for each square
*           that is modified.
*
*           Example: ----              ----
*                     XX--   becomes    XXO-
*                     -XX-              OXXO
*                     --X-              --X-
*
*           Example: ----              ----
*                     X-X-   becomes    XOX-
*                     ----              ----
*                     --X-              --X-
* Note    : You MUST use mark_square_as() to assign a color to a square.
*           It will take care of printing out the appropriate message.
* Used In : solve()
*/
void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE],
                          int size,
                          int row,
                          bool announce);

You are given:

void mark_square_as(int board[MAX_SIZE][MAX_SIZE],
                    int size,
                    int row,
                    int col,
                    int color,
                    bool announce) {
    if (announce) {
        cout << "marking (" << row + 1 << ", "
             << static_cast<char>(col + 'A') << ") as ";
        if (color == RED) {
            cout << RED_STRING;
        } else if (color == BLUE) {
            cout << BLUE_STRING;
        } else {
            cout << UNKNOWN_STRING;
        }
        cout << endl;
    }
    board[row][col] = color;
}

----------------------------------------------------------

I tried coding this myself and I am not sure why I am getting it wrong with mix match output. Below is my code.

 
void solve_three_in_a_row(int board[MAX_SIZE][MAX_SIZE],

int size,

int row,

bool announce) {

for(int j = 0; j < size; j++) {

if(board[row][j] == 1 && board[row][j+1] == 1) {

if(j - 1 >= 0 && j + 2 <= size) {

mark_square_as(board, size, row, j-1, 2, announce);

mark_square_as(board, size, row, j+2, 2, announce);

}

else if(j + 1 == size && j - 1 >= 0) {

mark_square_as(board, size, row, j-1, 2, announce);

}

else if(j == 0 && j + 2 <= size){

mark_square_as(board, size, row, j+2, 2, announce);

}

}

else if(board[row][j] == 1 && board[row][j+2] == 1) {

mark_square_as(board, size, row, j+1, 2, announce);

}

}

for(int j = 0; j < size; j++) {

if(board[row][j] == 2 && board[row][j+1] == 2) {

if(j - 1 >= 0 && j + 2 <= size) {

mark_square_as(board, size, row, j-1, 1, announce);

mark_square_as(board, size, row, j+2, 1, announce);

}

else if(j + 1 == size && j - 1 >= 0) {

mark_square_as(board, size, row, j-1, 1, announce);

}

else if(j == 0 && j + 2 <= size){

mark_square_as(board, size, row, j+2, 1, announce);

}

}

else if(board[row][j] == 2 && board[row][j+2] == 2) {

mark_square_as(board, size, row, j+1, 1, announce);

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
Write the following function: const int MIN_SIZE = 2; const int MAX_SIZE = 8; const int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[...

    The Code is C++ // tic tac toe game #include <iostream> using namespace std; const int SIZE = 9; int check(char *); void displayBoard(char *); void initBoard(char *); int main() {    char board[SIZE];    int player, choice, win, count;    char mark;    count = 0; // number of boxes marked till now    initBoard(board);       // start the game    player = 1; // default player    mark = 'X'; // default mark    do {        displayBoard(board);        cout << "Player " << player << "(" << mark...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • Similar to the Yahtzee project you completed earlier, in order to really understand the artificial intelligence...

    Similar to the Yahtzee project you completed earlier, in order to really understand the artificial intelligence and how to improve it; you must first have a good grasp on the game, its concepts, and its rules. For your first assignment, download the linked file below. This is a .cpp file of the game Tic-Tac-Toe. Tic-Tac-Toe Unzip the file, and run the game. Play a few games and begin to analyze the artificial intelligence that is currently programmed. Then, review the...

  • How do I do this? -> string print() const; Function to output the data, return the...

    How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){        if (indexStatusList[i] == 1){        cout <<HTable[i]<< " " << endl;        }   }    } **********Rest of the code...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • There is a problem with thecode. I get the error messages " 'board' was not declared...

    There is a problem with thecode. I get the error messages " 'board' was not declared in this scope", \src\TicTacToe.cpp|7|error: expected unqualified-id before ')' token| \src\TicTacToe.cpp|100|error: expected declaration before '}' token| Here is the code #ifndef TICTACTOE_H #define TICTACTOE_H #include class TicTacToe { private: char board [3][3]; public: TicTacToe () {} void printBoard(); void computerTurn(char ch); bool playerTurn (char ch); char winVerify(); }; ************************************ TicTacToe.cpp #endif // TICTACTOE_H #include "TicTacToe.h" #include #include using namespace std; TicTacToe() { for(int i =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT