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

Write a c++ functions that provides some of the functionality needed for the use

ID: 661744 • Letter: W

Question

Write a c++ functions that provides some of the functionality needed for the user to play tic tac toe against the computer.

The board: to represent the board, use a string board consisting of 9 characters, where each character corresponds to a position on the tic tac toe board ( the rows are indexed 0-2 and the columns are indexed 0-2). Index 0 in the string corresponds to row 0, column 0. Index 1 in the string corresponds to row 0, column 1. And so forth, where index 8 in the string corresponds to row 2, column 2.

0

1

2

3

4

5

6

7

8

The string board contains only the characters X, O, - (dash ). There are several winning patterns (applies for both X and O): Three X belonging to the same column, Three X belonging to the same row, Three X from top left to bottom right diagonal (vise versa).

There are four function prototypes:

int getIndex (int row, int column);

The first parameter is an integer row that corresponds to the row index in the tic tac toe board indexed from 0 to 2. The second parameter is an integer col that corresponds to the column index in the tic tac toe board indexed from 0 to 2.

If row and column values are valid values, then the function should return an integer that is he corresponding index of board (the index, not the value of the index). Else, if either the row or column values are integers outside of valid range, then the unction returns -1. You may assume hat only integers are ever input to the function. For example getIndex(1,1) would return an integer value of 4, however getIndex(-1,0) would return -1.

string getMark(int row, int col, string board);

The first parameter is an integer that specifies the row index in a tic tac toe board indexed from 0 to 2. The second parameter is an integer that specifies a column index in a tic tac toe board indexed from 0 to 2. The third parameter is the string that represents the tic tac toe board as specified above.

If there is an "X", "O", or "-" substring in the position specified by the row and column inputs, then the function should return that substring. Else, the function should return the string "Z"; this return value is simply meant to indicate an error (we could have chosen any value for this, except for "X", "O", or "-", of course).

For example, assume board is "--X------". Then, getMark(0, 0, board) returns "-". Similarly, getMark(0, 2, board) returns "X". However, if board is "--Y------", then getMark(0, 2, board) returns "Z".

bool topLeftDiagonalWin(string board);

This function takes a string as its only parameter which represents the board. The function should

Return true if there exists a winning position on the tic tac toe board corresponding to either of the winning positions: Three X from top left to bottom right diagonal, or Three O from top left to bottom right diagonal. Otherwise, the function returns false. You may assume that the string input into the function is valid (has correct length and consists only of X, O, and

0

1

2

3

4

5

6

7

8

Explanation / Answer

// TicTacToe-C++.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <string>

#include <time.h>

using namespace std;

int getIndex (int row, int column);

string getMark(int row, int col, string board);

bool topLeftDiagonalWin(string board);

bool setMark(int row, int col, string & board);

void displayBoard(string board);

bool topBottomColumnWin(string board);

bool leftRightRowWin(string board);

int main()

{

    srand(time(NULL));

    int row, column;

    string board="---------";

    cout<<"Welcome to TIC TAC TOE"<<endl;

    while(true)

    {

         displayBoard(board);

         cout<<endl;

         cout<<"Enter row and column";

         cin>>row>>column;

         if(setMark(row, column, board))

         {

             if(topLeftDiagonalWin(board)|| topBottomColumnWin(board)||leftRightRowWin(board))

             {

                 cout<<"User wins the game"<<endl;

             }           

         }

         else

         {

             int i=rand()%2;

             int j=rand()%2;

             index=getIndex(i, j);

             board[i]='O';

             if(topLeftDiagonalWin(board)|| topBottomColumnWin(board)||leftRightRowWin(board))

             {

                 cout<<"Computer wins the game"<<endl;

             }

         }

    }

    system("pause");

    return 0;

}

int getIndex (int row, int column)

{

    if(row==0 && column==0)

         return 0;

    else if(row==0 && column==1)

         return 1;

    else if(row==0 && column==2)

         return 2;

    else if(row==1 && column==0)

         return 3;

    else if(row==1 && column==1)

         return 4;

    else if(row==1 && column==2)

         return 5;

    else if(row==2 && column==0)

         return 6;

    else if(row==2 && column==1)

         return 7;

    else if(row==2 && column==2)

         return 8;

    else

         return -1;

}

string getMark(int row, int col, string board)

{

    int index=getIndex(row, col);

    if(board[index]=='X' || board[index]=='O' || board[index]=='-')

         return board.substr(row, col);

    else

         return "Z";

}

bool topLeftDiagonalWin(string board)

{

    if(board[0]==board[4]==board[8]=='X')

         return true;

    else if(board[0]==board[4]==board[8]=='O')

         return true;

    else

         return false;

}

bool topBottomColumnWin(string board)

{

    if((board[0]==board[3]==board[6]=='X')||(board[0]==board[3]==board[6]=='O'))

         return true;

    else if((board[1]==board[4]==board[7]=='X')||(board[1]==board[4]==board[7]=='O'))

         return true;

    else if((board[2]==board[5]==board[8]=='X')||(board[2]==board[5]==board[8]=='O'))

         return true;

    else

         return false;

}

bool leftRightRowWin(string board)

{

    if((board[0]==board[1]==board[2]=='X')||(board[0]==board[1]==board[2]=='O'))

         return true;

    else if((board[3]==board[4]==board[5]=='X')||(board[3]==board[4]==board[5]=='O'))

         return true;

    else if((board[6]==board[7]==board[8]=='X')||(board[6]==board[7]==board[8]=='O'))

         return true;

    else

         return false;

}

bool setMark(int row, int col, string& board)

{

    int index=0;

    string value;   

    if((row>=0 && row<=2) && (col>=0 && col<=2)&& (board.length()==9))

    {

         cout<<row<<" "<<col<<endl;

         value=getMark(row, col, board);

         cout<<endl;

         index=getIndex(row, col);

         cout<<index<<endl;

         if(value.compare("-")==0)

         {           

             for(int i=0;i<board.length();i++)

             {               

                 if(i==index)

                 {               

                      board[i]='X';                     

                 }  

                 else

                 {

                      board[i]=board[i];

                 }

             }

             cout<<board<<endl;

         }

         if(board[index]=='X' || board[index]=='O')

             return true;

         else

             return false;

    }

    else

    {

         return false;

    }

}

void displayBoard(string board)

{  

    for(int i=0;i<board.length(); i++)

    {

         cout<<board[i];

    }

    cout<<endl;

}

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