Question

In: Computer Science

Modify the attached TicTacToeStart.cpp Your job is to complete three functions init, printBoard, and gameOver #include...

Modify the attached TicTacToeStart.cpp

Your job is to complete three functions init, printBoard, and gameOver

#include <iostream>

using namespace std;

char gameBoard[3][3];

void init(){

    /*

    using a double loop to initialize all gameBoard element as 'E'

    */

}

void printBoard()

{

    /*using a double loop to print out gameboard

    */

}

bool gameOver()

{

    /*if any row or column, or diagonal are all X or O, return true*/

    return false;

}

int main()

{

    char again;

    do{

        bool done=false;

        bool xTurn=true;

        int count=0;

        init();

        while(!done){

            printBoard();

            if(xTurn)

                cout<<"It's X's turn"<<endl;

            else

                cout<<"It is O's turn"<<endl;

            int row, col;

            do{

                cout<<"Please enter two integer to specify an empty spot:";

                cin>>row>>col;

            }while(gameBoard[row-1][col-1]!='E');

            if(xTurn)

                gameBoard[row-1][col-1]='X';

            else

                gameBoard[row-1][col-1]='O';

            count++;

            if(gameOver()){

                done=true;

                if(xTurn)

                    cout<<"X wins!"<<endl;

                else

                    cout<<"O wins!"<<endl;

            }else if(count==9){

                done=true;

                cout<<"It's a tie!"<<endl;

            }

            xTurn=!xTurn;

        }

        cout<<"Play again? Enter y to continue:";

        cin>>again;

    }while(again=='y' || again=='Y');

    return 0;

}

Solutions

Expert Solution

In this code, intialized the size parameter method , Also taken a variable gameBoard which can be passed in all those 3 functions to make them work .

Here is the code :->

#include <iostream>

using namespace std;
#define side 3    //Defined the tic toe size as 3

    void init(char gameBoard[][side]){
    /*
    using a double loop to initialize all gameBoard element as 'E'
    */
        for (int i=0; i<side; i++) 
        { 
            for (int j=0; j<side; j++) 
                gameBoard[i][j] = 'E'; 
        }
        return;

    }


    void printBoard(char gameBoard[][side])
    {
        /*using a double loop to print out gameboard
        */
        
        for (int i=0; i<side; i++) 
        { 
            for (int j=0; j<side; j++) {
                printf("%c\t",gameBoard[i][j]);
            }
            printf("\n");
        }
    }


    bool gameOver(char gameBoard[][side])
    {
        /*if any row or column, or diagonal are all X or O, return true*/
        
        
        // Checking Row elements
        for (int i=0; i<side; i++) 
        { 
            if (gameBoard[i][0] == gameBoard[i][1] && 
                gameBoard[i][1] == gameBoard[i][2] &&  
                gameBoard[i][0] != 'E') 
                return true; 
        } 
        
        // Checking column elements
        for (int i=0; i<side; i++) 
        { 
            if (gameBoard[0][i] == gameBoard[1][i] && 
                gameBoard[1][i] == gameBoard[2][i] &&  
                gameBoard[0][i] != 'E') 
                return true; 
        } 
  
        // Checking Diagnol elements
        if (gameBoard[0][0] == gameBoard[1][1] && 
            gameBoard[1][1] == gameBoard[2][2] &&  
            gameBoard[0][0] != 'E') 
            return true; 
          
        // Checking 2nd way Diagnol elements  
        if (gameBoard[0][2] == gameBoard[1][1] && 
            gameBoard[1][1] == gameBoard[2][0] && 
            gameBoard[0][2] != 'E') 
            return true; 
  
        return false;
    }

    int main()
    {
        char again;
    
        do
        {
            bool done=false;
            bool xTurn=true;
            int count=0;
            
            char gameBoard[side][side];    // Assigned the gameBoard array variable
            
            init(gameBoard);             // passing the parameter  for intializating E in every cell
            while(!done){
                printBoard(gameBoard);   // passing the parameter for printing board
                if(xTurn)
                    cout<<"It's X's turn"<<endl;
                else
                    cout<<"It is O's turn"<<endl;
                int row, col;
                do
                {
                    cout<<"Please enter two integer to specify an empty spot:";
                    cin>>row>>col;
                }while(gameBoard[row-1][col-1]!='E');
                if(xTurn)
                    gameBoard[row-1][col-1]='X';
                else
                    gameBoard[row-1][col-1]='O';
                
                count++;
                if(gameOver(gameBoard)){     // passing the parameter to check 
                    done=true;
                    if(xTurn)
                        cout<<"X wins!"<<endl;
                    else
                        cout<<"O wins!"<<endl;
                }
                else if(count==9)
                {
                    done=true;
                    cout<<"It's a tie!"<<endl;
                }
                xTurn=!xTurn;
            }
            cout<<"Play again? Enter y to continue:";
            cin>>again;
            
        }while(again=='y' || again=='Y');
        
        return 0;
    }
    

Hope it helps !!! If any issue , Do Comment.


Related Solutions

C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include...
C++ Download the attached program and complete the functions. (Refer to comments) main.cpp ~ #include #include #define END_OF_LIST -999 using namespace std; /* * */ int exercise_1() { int x = 100; int *ptr; // Assign the pointer variable, ptr, to the address of x. Then print out // the 'value' of x and the 'address' of x. (See Program 10-2) return 0; } int exercise_2() { int x = 100; int *ptr; // Assign ptr to the address of...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
please use linux or unix to complete, and include pictures of the output. Modify the code...
please use linux or unix to complete, and include pictures of the output. Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the...
HW 9: Linear Regression and Correlation Analysis INSTRUCTIONS: Please modify the Excel sheet attached to complete...
HW 9: Linear Regression and Correlation Analysis INSTRUCTIONS: Please modify the Excel sheet attached to complete this assignment. Turn in the following work including: 1) written formulas, 2) values from Excel plugged into formulas, and 3) final answer. Scenario: A graduate student has administered a pro-inflammatory substance, lipopolysaccharide (LPS), to rats in the form of a pill (several doses – 0 mg or placebo, 1.25 mg, 2.5 mg, 5.0 mg, and 10 mg). She then determines the blood concentration of...
Your primary task for this exercise is to complete header file by writing three functions with...
Your primary task for this exercise is to complete header file by writing three functions with its description below: removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list. insertAt function - to insert an item...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
Complete the following functions in the Stack.java/Stack.h files (ATTACHED BELOW): a. void push(int val) b. int...
Complete the following functions in the Stack.java/Stack.h files (ATTACHED BELOW): a. void push(int val) b. int pop() c. int getSize() public class Stack {    private int maxStackSize, topOfStack; private int[] stack;    public Stack(int maxStackSize) { if (maxStackSize <= 0) System.out.println("Stack size should be a positive integer."); else { this.maxStackSize = maxStackSize; topOfStack = -1; stack = new int[maxStackSize]; } }    public void push(int val) { // complete this function }    public int pop() { // complete...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT