Question

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 << ")" << " Enter number: ";
       do {
           cin >> choice;
           if (choice < 1 || choice > 9 || board[choice - 1] != (choice + '0'))
               cout << "Number invalid. Enter again: ";
           else {
               board[choice - 1] = mark;
               count++;
               win = check(board);
               if (win == 1) {
                   displayBoard(board);
                   cout << "\n\n PLAYER " << player << " IS THE WINNER!\n\n";
                   return 0;
               }
               break;
           }
       } while (1);

       if (count == 9) {
           displayBoard(board);
           cout << "\n\n GAME ENDED IN DRAW\n\n";
           return 0;
       }

       else if (player == 1) {
           player = 2;
           mark = 'O';
       }

       else {
           player = 1;
           mark = 'X';
       }
   } while (1);
   return 0;
}

// function initializes the board
void initBoard(char *board) {
   int i;
   for (i = 0; i < SIZE; i++) {
       board[i] = '0' + i + 1;
   }
}

// function checks if game has been won
int check (char *board) {
   if (board[0] == board[1] && board[1] == board[2])
       return 1;

   else if (board[3] == board[4] && board[4] == board[5])
       return 1;

   else if (board[6] == board[7] && board[7] == board[8])
       return 1;

   else if (board[0] == board[3] && board[3] == board[6])
       return 1;

   else if (board[1] == board[4] && board[4] == board[7])
       return 1;

   else if (board[2] == board[5] && board[5] == board[8])
       return 1;

   else if (board[0] == board[4] && board[4] == board[8])
       return 1;

   else if (board[2] == board[4] && board[4] == board[6])
       return 1;
  
   else
       return 0;
}

// displays the board
void displayBoard(char *board) {
   int i;

   cout << endl;
   for (i = 0; i < SIZE; i++) {
       cout << board[i] << "\t";
       if ((i+1) % 3 == 0) {
           cout << endl;
       }
   }
   cout << endl;
}

so how can this code be modified to not include pointers? Pointers aren't allowed. And to include arrays with loops rather than using a bunch of if-else statements? and instead of the value count that the user must enter, give the user the option to enter the array location, do this so that both players can enter using array location.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

c++ program :- Note :- pointers are removed and user need to given array location now row(0-2) and column (0-2)

// tic tac toe game
#include <iostream>

using namespace std;

const int SIZE = 9;

char board[SIZE];

int check();
void displayBoard();
void initBoard();

int main() {
int player, choicex,choicey,choice, win, count;
char mark;

count = 0; // number of boxes marked till now

initBoard();
  
// start the game
player = 1; // default player
mark = 'X'; // default mark
do {
displayBoard();
cout << "Player " << player << "(" << mark << ")" << "Enter array location x(0-2) y(0-2) ";
do {
cin >> choicex>>choicey;
choice=(choicex*3)+choicey;
if ((choicex <0 && choicex >2) || (choicey <0 && choicey >2) || board[choice - 1] != (choice + '0'))
cout << "Location invalid. Enter again: ";
else {
board[choice - 1] = mark;
count++;
win = check();
if (win == 1) {
displayBoard();
cout << "\n\n PLAYER " << player << " IS THE WINNER!\n\n";
return 0;
}
break;
}
} while (1);

if (count == 9) {
displayBoard();
cout << "\n\n GAME ENDED IN DRAW\n\n";
return 0;
}

else if (player == 1) {
player = 2;
mark = 'O';
}

else {
player = 1;
mark = 'X';
}
} while (1);
return 0;
}

// function initializes the board
void initBoard() {
int i;
for (i = 0; i < SIZE; i++) {
board[i] = '0' + i + 1;
}
}

// function checks if game has been won
int check () {
if (board[0] == board[1] && board[1] == board[2])
return 1;

else if (board[3] == board[4] && board[4] == board[5])
return 1;

else if (board[6] == board[7] && board[7] == board[8])
return 1;

else if (board[0] == board[3] && board[3] == board[6])
return 1;

else if (board[1] == board[4] && board[4] == board[7])
return 1;

else if (board[2] == board[5] && board[5] == board[8])
return 1;

else if (board[0] == board[4] && board[4] == board[8])
return 1;

else if (board[2] == board[4] && board[4] == board[6])
return 1;
  
else
return 0;
}

// displays the board
void displayBoard() {
int i;

cout << endl;
for (i = 0; i < SIZE; i++) {
cout << board[i] << "\t";
if ((i+1) % 3 == 0) {
cout << endl;
}
}
cout << endl;
}

Add a comment
Know the answer?
Add Answer to:
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[...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 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...

  • 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;...

  • can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() {...

    can someone help me fix my jeopardy game #include<iostream> #include<stdlib.h> using namespace std; int rollDie() { return (rand() % 6+1); } void askYoNs(){ cout<<"Do you want to roll a dice (Y/N)?:"<<endl; } void printScores(int turnTotal,int humanTotal,int compTotal){ int player; int human; if(player==human){ cout<<"Your turn total is "<<turnTotal<<endl; } else{ cout<<"computer turn total is "<<turnTotal<<endl; } cout<<"computer: "<<compTotal<<endl; cout<<"human: "<<humanTotal<<endl; cout<<endl; } int human; int changePlayer(int player){ if(player==human) return 1; return human; } int process(int& turnTotal,int roll,int curr_player,int& humanTotal,int& computerTotal){ if(roll==2...

  • #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void...

    #include <iostream> using namespace std; const int SIZE = 10; void displayGreaterThan(int[], int); void displaySmallerThan(int[],int); void displayArrayContent(int[]); void displayLargestValue(int[]); void displaySmallestValue(int[]); int main(){    int number;    int numbers[SIZE] = {9,1,90,98,53,22,76,29,37,65}; cout <<"Enter a number: "; cin >> number; cout << endl;    displayGreaterThan(numbers,number); cout << endl; displaySmallerThan(numbers,number); cout << endl; displayArrayContent(numbers); cout << endl; displayLargestValue(numbers); cout << endl; displaySmallestValue(numbers); cout << endl;    return 0;       } void displayGreaterThan(int value[],int num){ cout << " All larger value(s)than" <<...

  • In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and...

    In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...

  • #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the...

    #include <iostream> using namespace std; int main(void) {    int SIZE;    cout<<"Enter the size of the array"<<endl;    cin>>SIZE;     int *numlist = new int[SIZE];     // Read SIZE integers from the keyboard     for (int i = 0; i<SIZE; i++ )    {        cout << "Enter value #" << i+1 << ": ";        cin >> numlist[i];     }     // Display the numbers in a reverse order     for (int i = SIZE; i > 0; i--...

  • Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer...

    Game of Craps C++ #include <iostream> using namespace std; void roll (int *); // using pointer to catch the random variable value of two dicerolls at one time . //these global variable with static variable win use to operate the working of code . //static win, account because two times input given by user to add the diceroll with win number and //account (means balance of user) . static int account = 100; static int win = 0; int bet...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

  • Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void...

    Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...

  • #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str);...

    #include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() {    char string[100];    char inputChoice, choice[2];    int vowelTotal, consonantTotal;    //Input a string    cout << "Enter a string: " << endl;    cin.getline(string, 100);       do    {        //Displays the Menu        cout << "   (A) Count the number of vowels in the string"<<endl;        cout << "   (B) Count...

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