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

I need this code following the restrictions that i have provided.Program must us

ID: 3537203 • Letter: I

Question

I need this code following the restrictions that i have provided.Program must use functions and CANNOT be written as one main program.Use a function to display a set of instructions describing how the user must enter a "move" (this has to called only once at the beginning of the program)Use a function to display the contents of the board.The program must clear the display screen after the user enters a new move, then display the contents of the board with the new move shown (see sample output below).To clear the screen you can use: system("cls");Use a function for the player's turn.Validate each users move (i.e. if the user enters a location that already contains an X or an O or the numbers entered are not a valid position on the board an error will be displayed). The program must then ask the user to re-input their move.Use a function to determine whether the player has won. The function should receive as arguments, the board and the player ( 'X' or 'O'). You also check for a Tie or End of Game in this function or create a separate function to do this.The program should ask the player to enter the row and column number.The board and the elements contained within the board must line up correctly.The program MUST use a 2D Array.Determine whether a player has won or there is a tie. If there is a winner, the program should display that a player has won the game and which player has won. If there is a tie the program should display that a tie has occured an exit.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

void print_grid();
char player(int number);
int search4win(int *moves, int length);
void tictactoe();
void reset_board();

int wins[8][3]={{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
char board[9]={'1','2','3','4','5','6','7','8','9'};

/* X will be represented by 1
O will be represented by -1
*/

int main()
{
int play=1;
char again;

while (play >= 1)
{
tictactoe();
printf("would you like another go Y or N ");
scanf("%c", &again);
getchar();
if ((again='y') || (again='Y'))
{
play++;
reset_board();
}
else if ((again='n') || (again='N'))
{
printf("Thank you for playing goodbye ");
play=0;
}
else
printf("Invalid entry ");
}

return 0;
}

void tictactoe()
{
int Xmoves[5], Omoves[4];
int go, goes=0, turn=1, row=0, Xgoes=0, Ogoes=0;
print_grid();

while ((goes < 9) && (row != 3))
{
printf("Would player %c please enter there move ", (player(turn)));
scanf("%d", &go);
getchar();
printf("your go was %d ", go);

if((go < 10) && (go > 0) && (board[go-1] != 'X') && (board[go-1] != 'O'))
{
board[go-1]=(player(turn));
print_grid();
if (turn > 0)
{
Xmoves[Xgoes]=go;
//printf("Xgoes is %d ", Xgoes);
//printf("Xmoves[Xgoes] is %d ", Xmoves[Xgoes]);
Xgoes++;
if (Xgoes >2)
{
row = search4win(Xmoves, Xgoes);
}
//printf("row is %d ", row);
}
else
{
Omoves[Ogoes]=go;
Ogoes++;
if (Ogoes >2)
{
row=search4win(Omoves, Ogoes);
}
//printf("row is %d ", row);
}

if(row==3) // if there is a line of 3 the winner is announced
{
printf("The winner is %c ", (player(turn)));
}
goes++; //increments the number of goes had by both O and X

turn=turn*(-1); //this line changes whose turn it is

}
else
printf("invalid entry ");
}

}

void reset_board()
{ int i=0, n=1;
for(i=0; i<=8; i++)
{
board[i]=n;
n++;
}
}

char player(int number)
{

return (number > 0) ? 'X' : 'O';
/*char temp;
if ( number > 0)
temp = 'X';
else
temp = 'O';
return temp;*/

}

void print_grid()
{
printf(" %c|%c|%c -+-+- ", board[6], board[7], board[8]);
printf(" %c|%c|%c -+-+- ", board[3], board[4], board[5]);
printf(" %c|%c|%c ", board[0], board[1], board[2]);
}

int search4win(int *moves, int length)
{
int i=0, j=0, k=0, n, line=0, ret;
while(line < 3 && j < 8)
{
for(i=0; i<3; i++)
{
n=wins[j][i];
//printf("n is %d ", n);

for(k=0; k<length; k++)
{
if (n == moves[k])
line++;
//printf("line is %d ", line);
}
}
if (line == 3)
return 3;
else
line=0;
j++;
}

return 0;
}






#include <iostream>
using namespace std;



int main()
{

        //Menu
        //Want to play Tic Tac Toe
        //Player Chooses Yes or No
        //Game Starts and Displays Board
        //Menu Choice

        int MenuChoice;

        //Board Variables
        char Square1('1');
        char Square2('2');
        char Square3('3');
        char Square4('4');
        char Square5('5');
        char Square6('6');
        char Square7('7');
        char Square8('8');
        char Square9('9');

        //Player Variable
        int PlayerTurn(1);

        //Whether the game is over or not in the while loop
        bool GameOverDecider(true);

        //Main Menu

        cout << " Would you like to play some TIC-TAC-TOE today?" << endl;
        cout << endl << endl;
        cout << " (1)Game Start(2 players required)" << endl;
        cout << " (2)Quit Game" << endl;
        cout << endl;
        cout << "Choice( 1 or 2):";
        cin >> MenuChoice;
        if (MenuChoice == 1){
                do {
                        //Player Variable
                        int PlayerTurn(1);

                        //Whether the game is over or not in the while loop
                        bool GameOverDecider(true);
                       
                        //Main Game Board
                        cout << "       " << Square1 << " | " << Square2 << " | " << Square3 << endl;
                        cout << "     -----+-----+-----" << endl;
                        cout << "       " << Square4 << " | " << Square5 << " | " << Square6 << endl;
                        cout << "     -----+-----+-----" << endl;
                        cout << "       " << Square7 << " | " << Square8 << " | " << Square9 << endl;
                        cout << "     -----+-----+-----" << endl;

                        //Set Player Marker, Player 1 uses X and Player 2 uses O
                        char PlayerMarker;
                        if (PlayerTurn = 1){
                                PlayerMarker = 'X';
                        }else{
                                PlayerMarker = 'O';
                        }

                        //Start the game, asking for each Player for a move

                        //Bool Variable in order to check if the move is valid
                        bool ValidTurn;
                        //Run a do while loop to check if their hasn't been a square filled in or
                        do{
                                //Char Variable to get next move
                                char CurrentMove;

                                cout << "Player" << PlayerTurn << "'s turn, set move on what square: " << endl;
                                cin >> CurrentMove;
                                ValidTurn = true;

                                //If Statment Checking if the move is invalid
                                //If the move equals 1-9 and a number, and if no one has marked the space yet
                                //Checks Each Square
                                if (CurrentMove == '1' && Square1 == '1') {
                                        Square1 = PlayerMarker;
                                } else if (CurrentMove == '2' && Square1 == '2') {
                                        Square2 = PlayerMarker;
                                } else if (CurrentMove == '3' && Square1 == '3') {
                                        Square3 = PlayerMarker;
                                } else if (CurrentMove == '4' && Square1 == '4') {
                                        Square4 = PlayerMarker;
                                } else if (CurrentMove == '5' && Square1 == '5') {
                                        Square5 = PlayerMarker;
                                } else if (CurrentMove == '6' && Square1 == '6') {
                                        Square6 = PlayerMarker;
                                } else if (CurrentMove == '7' && Square1 == '7') {
                                        Square7 = PlayerMarker;
                                } else if (CurrentMove == '8' && Square1 == '8') {
                                        Square8 = PlayerMarker;
                                } else if (CurrentMove == '9' && Square1 == '9') {
                                        Square9 = PlayerMarker;
                                } else {
                                        cout << "Invalid Move, make another one:" << endl;
                                        ValidTurn = false;
                                }
                        } while(!ValidTurn);


                        // GameOverDecider Bool Variable now set to false, showing the game is over

                        GameOverDecider = false;
                       
                        // New Bool variable
                        bool WinGame = true;

                        //Win Condition that's through either square 1, 4, and 7 or 1, 2, and 3

                        if(Square1 != '1'){
                                //1, 2, and 3

                                if (Square2 == Square1 && Square3 == Square1) {
                                        GameOverDecider = true;
                                }
                                //1, 4, and 7

                                if (Square4 == Square1 && Square7 == Square1) {
                                        GameOverDecider = true;
                                }
                        }
       
                        //Win Condition that's through either square 3, 6, and 9 or 7, 8, and 9

                        if(Square1 != '9'){
                                //3, 6, and 9

                                if (Square3 == Square9 && Square6 == Square9) {
                                        GameOverDecider = true;
                                }
                                //7, 8, and 9

                                if (Square7 == Square9 && Square8 == Square9) {
                                        GameOverDecider = true;
                                }
                        }

                        //Win Conditions through the middle square, 4 conditions
                       
                        if(Square1 != '5'){
                                //1, 5, and 9
                                if (Square1 == Square5 && Square9 == Square5) {
                                        GameOverDecider = true;
                                }
                                //2, 5, and 8
                                if (Square2 == Square5 && Square8 == Square5) {
                                        GameOverDecider = true;
                                }
                                //4, 5, and 6
                                if (Square4 == Square5 && Square6 == Square5) {
                                        GameOverDecider = true;
                                }
                                //3, 5, and 7
                                if (Square3 == Square5 && Square7 == Square5) {
                                        GameOverDecider = true;
                                }
                        }
                               
                        //Check if their are draws
                        if (Square1 != '1' && Square2 != '2' && Square3 != '3' &&
                                Square4 != '4' && Square5 != '5' && Square6 != '6' &&
                                Square7 != '7' && Square8 != '8' && Square9 != '9' && !GameOverDecider){
                                        GameOverDecider = true;
                                        WinGame = false;

                        if(GameOverDecider){
                                if(WinGame){
                                        cout << "Player " << PlayerTurn << " totally wins!" << endl;
                                }
                                //Print the result board
                                cout << "       " << Square1 << " | " << Square2 << " | " << Square3 << endl;
                                cout << "     -----+-----+-----" << endl;
                                cout << "       " << Square4 << " | " << Square5 << " | " << Square6 << endl;
                                cout << "     -----+-----+-----" << endl;
                                cout << "       " << Square7 << " | " << Square8 << " | " << Square9 << endl;
                                cout << "     -----+-----+-----" << endl;

                                //Choice to whether to play the game again

                                cout << " Game Over!" << endl;
                                cout << " Want to play again?(Y/N)?: ";
                                char PlayAgain;
                                cin >> PlayAgain;

                                //Clears Board Back to the original numbers
                                if(PlayAgain = 'y'){
                                        GameOverDecider = false;
                                        Square1 = '1';
                                        Square2 = '2';
                                        Square3 = '3';
                                        Square4 = '4';
                                        Square5 = '5';
                                        Square6 = '6';
                                        Square7 = '7';
                                        Square8 = '8';
                                        Square9 = '9';
                                }
                                PlayerTurn = 1;
                        } else {
                                // If the game is still going on, alternate the players turns
                                if (PlayerTurn == 1){
                                        PlayerTurn = 2;
                                } else {
                                        PlayerTurn = 1;
                                }
                        }
               
                // End of Main Do While Loop
                } while (!GameOverDecider);

               
        //End of Menu Choice If Statement
        } else(MenuChoice == 2) {
                cout << "Ok....." << endl;
                }

        system ("pause");
        return 0;
       
}


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