Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help creating a c++ program. Please go to this link for directions::: http://algebra.sci.csueastbay.edu/~grewe/CS1160/Projects/Project7.html Ok, Here is the code that I created:: //Project 8 //Sreelekha

Need help creating a c++ program. Please go to this link for directions::: http://algebra.sci.csueastbay.edu/~grewe/CS1160/Projects/Project7.html

Ok, Here is the code that I created::

//Project 8

//Sreelekha Bollu

//This program allows two players to play a game of Tic-Tac-Toe

#include

using namespace std;

void initializeBoard(char board[][3]); /*initializes the array*/

void displayBoard(char board[][3]);/*gets input from user returns character number of location chosen by user*/

void getInput(char board[][3], char marker,int& x,int& y);/* mark character marker of player on chosen character pos of board*/

void markBoard(char board[][3],int x,int y, char marker);/*checks to see if someone has won returns true or false*/

bool gameOver(char board[][3]); /*checks to see if someone won on rows of board*/

bool checkHorizontal(char board[][3], char marker);/*checks to see if someone won on columns of board*/

bool checkVertical(char board[][3], char marker);/*checks to see if someone won on diagonal of board*/

bool checkDiagonal(char board[][3], char marker);/*checks to see if players have tied*/

bool checkTie(char board[][3]);/*prints winner as marker or ties*/

void printWinner(char marker);/*checks to see if selected location is available returns false when location has already been taken or is an invalid number*/

bool validMove(char board[][3], int x,int y);

int main()

{ srand(time(0));

char board[3][3];

char f,s;

int x,y;

f='X';

s='O';

initializeBoard(board);

displayBoard(board);

while(true)

{getInput(board,f,x,y);

markBoard(board, x,y, f);

displayBoard(board);

if (gameOver(board))

break;

getInput(board,s,x,y);

markBoard(board, x,y, s);

displayBoard(board);

if (gameOver(board))

break;

}

return 0;

}

void initializeBoard(char board[][3])

{

for (int i = 0; i<3; i++)

for (int j = 0; j<3; j++)

board[i][j]= '*';

}

bool validMove(char board[][3], int x, int y)

{if(x<0||x>2||y<0||y>2)

return false;

if(board[x][y]=='X'||board[x][y]=='O')

return false;

else

return true;

}

void displayBoard(char board[][3])

{ int t;

for(t=0; t<3; t++) {

cout<<" "<

if(t!=2) cout<<" ---|---|--- ";

}

cout<<" ";

}

void getInput(char board[][3], char marker,int& x,int& y)

{for(;;)

{cout << "Player "<< marker << " Enter a Row and Column (between 1-3): ";

cin >> x>>y;

x--;

y--;

if (validMove(board,x,y))

return;

cout << "Invalid Move: Please Try Again ";

}

}

void markBoard(char board[][3], int x,int y, char marker)

{ board[x][y]=marker;

}

bool gameOver(char board[][3])

{if (checkHorizontal(board,'X'))

{printWinner('X');

return true;

}

if (checkVertical(board, 'X'))

{printWinner('X');

return true;

}

if (checkDiagonal(board, 'X'))

{ printWinner('X');

return true;

}

if (checkHorizontal(board,'O'))

{printWinner('O');

return true;

}

if (checkVertical(board, 'O'))

{printWinner('O');

return true;

}

if (checkDiagonal(board, 'O'))

{printWinner('O');

return true;

}

if (checkTie(board))

{printWinner('T');

return true;

}

return false;

}

bool checkHorizontal(char board[][3], char marker)

{int i,j,count;

for(i=0; i<3; i++)

{count=0;

for(j=0;j<3;j++)

if(board[i][j]==marker)

count++;

if(count==3)

return true;

}

return false;

}

bool checkVertical(char board[][3], char marker)

{int i,j,count;

for(i=0; i<3; i++)

{count=0;

for(j=0;j<3;j++)

if(board[j][i]==marker)

count++;

if(count==3)

return true;

}

return false;

}

bool checkDiagonal(char board[][3], char marker)

{if(board[0][0]==board[1][1] && board[1][1]==board[2][2]&& board[0][0]==marker)

return true;

if(board[0][2]==board[1][1] && board[1][1]==board[2][0]&& board[0][2]==marker)

return true;

return false;

}

bool checkTie(char board[][3])

{int i,j;

for(i=0;i<3;i++)

for(j=0;j<3;j++)

if(board[i][j]=='*')

return false;

return true;

}

void printWinner(char marker)

{ if(marker=='T')

cout<<"TIE GAME! ";

else

cout<<"The winner is "<

}

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

This code works, however, I need you to change this code so that it follows all the directions in the link EXACTLY!!!! You can change the names of the functions, etc, if you need to. But it must exactly correspond to the directions!!!!

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 Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

Students also viewed these Databases questions

Question

How to find if any no. is divisble by 4 or not ?

Answered: 1 week ago

Question

Explain the Pascals Law ?

Answered: 1 week ago

Question

What are the objectives of performance appraisal ?

Answered: 1 week ago

Question

Do you suggest Lisa use a PEO? Why?

Answered: 1 week ago