I am writing a c program But it break after 1 round Please h

I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!!

Please include sample output.

///Main

#include <iostream>
#include <cstdlib>
#include <time.h>
#include \"tic-ac-toe_3-10.h\"

using namespace Khaliq_10;
using namespace std;

//delcaring functons protypes
int computer_next_Move(TicTacToe board, char opponent_Symoble);
void menu();


int main()
{
   //delcaring variables as requried for this applications
   char user_symbol;
   char computer_symbol;
   char computer_symbol_1 = \'X\';
   char computer_symbol_2 = \'O\';
   bool has_Finished = false;
   bool again = true;
   bool PC_VS_Person = false;
   int input;

   while (true)
   {
       menu();

       //asking user to choose men
       cout << \"\ Choose options from menu: \";
       cin >> user_symbol;

       //uisng if else chain for user input
       if (user_symbol == \'1\')
       {
           //this if use for to watch game computer vs computer
           PC_VS_Person = false;
           break;
       }
       else if (user_symbol == \'2\')
       {
           //this else if use for to play game computer vs person.
           PC_VS_Person = true;
           computer_symbol = \'X\';
           break;
       }
       else
           //will check the user input if that vaild or unvaild
           cout << \"Invaild number that you entered, please choose options from menu.\" << endl;
       break;
   }
   if (PC_VS_Person)
   {
       int ff = 0;
       while (true)
       {
           //giving user options, if user like to play as a X or O.
           cout << \"Press 1 to play as a X.\" << endl;
           cout << \"Press 2 to play as the O.\" << endl;
           cin >> user_symbol;

           //if user like to play as a X postions
           if (user_symbol == \'1\')
           {
               user_symbol = \'X\';
               computer_symbol = \'O\';
               break;
           }

           //this else user if user wants to play as
           else if (user_symbol == \'2\')
           {
               user_symbol = \'O\';
               computer_symbol = \'X\';
               break;
           }
           else
               cout << \"Invaild, number that you enterd.\" << endl;
       }
       TicTacToe playingField(user_symbol);
       while (true)
       {
           int move_for_X;
           int move_for_O;

           if (user_symbol == \'X\')
           {
               while (true)
               {
                   cout << \"enter a number from 0 to 8 for what position you want to take your turn.\" << endl;
                   cin >> input;
                   if (input <= 9)
                   {
                       playingField.playerNextMove(input, user_symbol);
                       break;
                   }
                   else
                       cout << \"enter a valid position\" << endl;
               }

               {
                   //displyaing the number has placeed on which space
                   cout << \"An \" << user_symbol << \" was placed in space number \" << input << \".\" << endl << endl;
                   playingField.displayTicBoard();
                   int res = playingField.isGameOver();
                   if (res != 4)
                   {

                       if (res == 1)
                           cout << \"Player 1 is winner\ \";
                       else if (res == 2)
                           cout << \"Player 2 is winner\ \";
                       else if (res == 3)
                           cout << \"Match draw\ \";
                       break;
                   }// if (playingField.isGameOver() != 0)
                   // break;
               }
               {

                   move_for_O = computer_next_Move(playingField, computer_symbol_2);
                   playingField.playerNextMove(move_for_O, computer_symbol_2);
                   cout << \"An \" << computer_symbol_2 << \" was placed in space number \" << move_for_O << \".\" << endl << endl;
                   playingField.displayTicBoard();
                   // if (playingField.isGameOver() != 0)
                   // break;
                   int res = playingField.isGameOver();
                   if (res != 4)
                   {

                       if (res == 1)
                           cout << \"Player 1 is winner\ \";
                       else if (res == 2)
                           cout << \"Player 2 is winner\ \";
                       else if (res == 3)
                           cout << \"Match draw\ \";
                       break;
                   }
               }
           }
           else if (user_symbol == \'O\')
           {
               move_for_X = computer_next_Move(playingField, computer_symbol_1);
               playingField.playerNextMove(move_for_X, computer_symbol_1);
               cout << \"An \" << computer_symbol_1 << \" was placed in space number \" << move_for_X << \".\" << endl << endl;
               playingField.displayTicBoard();
               // if (playingField.isGameOver() != 0)
               // break;
               int res = playingField.isGameOver();
               if (res != 4)
               {

                   if (res == 1)
                       cout << \"Player 1 is winner\ \";
                   else if (res == 2)
                       cout << \"Player 2 is winner\ \";
                   else if (res == 3)
                       cout << \"Match draw\ \";
                   break;
               }
               while (true)
               {
                   cout << \"enter a number from 1 to 9 for what position you want to take your turn.\" << endl;
                   cin >> input;
                   if (input <= 9)
                   {
                       playingField.playerNextMove(input, user_symbol);
                       break;
                   }
                   else
                       cout << \"enter a valid position\" << endl;
               }
               cout << \"An \" << user_symbol << \" was placed in space number \" << input << \".\" << endl << endl;
               playingField.displayTicBoard();
               // if (playingField.isGameOver() != 0)
               // break;
               res = playingField.isGameOver();
               if (res != 4)
               {

                   if (res == 1)
                       cout << \"Player 1 is winner\ \";
                   else if (res == 2)
                       cout << \"Player 2 is winner\ \";
                   else if (res == 3)
                       cout << \"Match draw\ \";
                   break;
               }
           }
       }
   }

   else
   {
       TicTacToe computerField(computer_symbol_1);
       int gameOutcome;

       while (true)
       {
           int move_for_X;
           int move_for_O;

           move_for_X = computer_next_Move(computerField, computer_symbol_2);
           computerField.playerNextMove(move_for_X, computer_symbol_1);
           cout << \"An \" << computer_symbol_1 << \" was placed in space number \" << move_for_X << \".\" << endl << endl;
           computerField.displayTicBoard();
           gameOutcome = computerField.isGameOver();
           //if (computerField.isGameOver() != 0)
           // break;
           int res = computerField.isGameOver();
           if (res != 4)
           {

               if (res == 1)
                   cout << \"Player 1 is winner\ \";
               else if (res == 2)
                   cout << \"Player 2 is winner\ \";
               else if (res == 3)
                   cout << \"Match draw\ \";
               break;
           }

           move_for_O = computer_next_Move(computerField, computer_symbol_1);
           computerField.playerNextMove(move_for_O, computer_symbol_2);
           cout << \"An \" << computer_symbol_2 << \" was placed in space number \" << move_for_O << \".\" << endl << endl;
           computerField.displayTicBoard();
           gameOutcome = computerField.isGameOver();
           res = computerField.isGameOver();
           if (res != 4)
           {

               if (res == 1)
                   cout << \"Player 1 is winner\ \";
               else if (res == 2)
                   cout << \"Player 2 is winner\ \";
               else if (res == 3)
                   cout << \"Match draw\ \";
               break;
           }
           // if (computerField.isGameOver() != 0)
           // break;
       }

       {
           //gameOutcome = computerField.isGameOver();

           if (gameOutcome == 1)
               cout << \"The computer was controlling for X.\" << endl;
           else if (gameOutcome == 2)
               cout << \"The computer was controlling for O.\" << endl;
           else
               cout << \"Now this point nethire computer vs computer win.\" << endl;
       }

   }

   system(\"pause\");
   return 0;
}
void menu()
{
   cout << \"Press 1 to watch Tic-Toa-Toe played against computer vs computer. \" << endl;
   cout << \"Press 2 to play a game of Tic-Tac-Toe against a computer.\" << endl;
}

int computer_next_Move(TicTacToe board, char opponent_Symoble)
{
   //using if condations
   if (board.isSpaceOccupied(4) == \'E\')
   {
       return 4;
   }
   if (board.isSpaceOccupied(0) == \'E\' && ((board.isSpaceOccupied(1) == board.isSpaceOccupied(2) == opponent_Symoble) ||
       (board.isSpaceOccupied(4) == board.isSpaceOccupied(8) == opponent_Symoble) ||
       (board.isSpaceOccupied(3) == board.isSpaceOccupied(6) == opponent_Symoble)))
   {
       return 0;
   }if (board.isSpaceOccupied(1) == \'E\' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(2) == opponent_Symoble) ||
       (board.isSpaceOccupied(4) == board.isSpaceOccupied(7) == opponent_Symoble)))
   {
       return 1;
   }

   if (board.isSpaceOccupied(2) == \'E\' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(1) == opponent_Symoble) ||
       (board.isSpaceOccupied(4) == board.isSpaceOccupied(6) == opponent_Symoble) ||
       (board.isSpaceOccupied(5) == board.isSpaceOccupied(8) == opponent_Symoble)))
   {
       return 2;
   }

   if (board.isSpaceOccupied(3) == \'E\' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(6) == opponent_Symoble) ||
       (board.isSpaceOccupied(4) == board.isSpaceOccupied(5) == opponent_Symoble)))
   {
       return 3;
   }

   //The middle of the board will never be occupied after the 1st turn
   if (board.isSpaceOccupied(5) == \'E\' && ((board.isSpaceOccupied(2) == board.isSpaceOccupied(8) == opponent_Symoble) ||
       (board.isSpaceOccupied(3) == board.isSpaceOccupied(4) == opponent_Symoble)))
   {
       return 5;
   }

   if (board.isSpaceOccupied(6) == \'E\' && ((board.isSpaceOccupied(0) == board.isSpaceOccupied(3) == opponent_Symoble) ||
       (board.isSpaceOccupied(4) == board.isSpaceOccupied(2) == opponent_Symoble) ||
       (board.isSpaceOccupied(7) == board.isSpaceOccupied(8) == opponent_Symoble)))
   {
       return 6;
   }

   if (board.isSpaceOccupied(7) == \'E\' && ((board.isSpaceOccupied(6) == board.isSpaceOccupied(8) == opponent_Symoble) ||
       (board.isSpaceOccupied(1) == board.isSpaceOccupied(4) == opponent_Symoble)))
   {
       return 7;
   }

   if (board.isSpaceOccupied(8) == \'E\' && ((board.isSpaceOccupied(2) == board.isSpaceOccupied(5) == opponent_Symoble) ||
       (board.isSpaceOccupied(0) == board.isSpaceOccupied(4) == opponent_Symoble) ||
       (board.isSpaceOccupied(6) == board.isSpaceOccupied(7) == opponent_Symoble)))
   {
       return 8;
   }
   else
   {
       //for the else will set the seconds that coomputer will move

       //declaring variables as needed
       int random_number;
       time_t seconds;
       time(&seconds);
       srand((unsigned int)seconds);

       while (true)
       {
           random_number = rand() % (9);
           if (board.isSpaceOccupied(random_number) == \'E\')
               return random_number;

       }

   }
}

//header file

#ifndef TICTACTOE_H
#define TICTACTOE_H

namespace Khaliq_10
{
   class TicTacToe
   {
   private:
       char playerSymbol, playingField[9];
   public:
       // CONSTRUCTOR
       TicTacToe(char Symbol);

       // MODIFICATION MEMBER FUNCTIONS
       void playerNextMove(int const fieldLocation, char playerSymbol);

       // CONSTANT MEMBER FUNCTIONS
       char isSpaceOccupied(int const fieldLocation) const;
       int isGameOver() const;
       void displayTicBoard() const;

   };

}

#endif

//body file


#include \"tic-ac-toe_3-10.h\"
#include <iostream>
#include <cassert>

using namespace std;
using namespace Khaliq_10;

namespace Khaliq_10
{
   //COSTRUCTOR
   TicTacToe::TicTacToe(char Symbol)
   {
       playerSymbol = Symbol;

       //using for loop to count the player filed locations
       for (int count = 0; count < 10; count++)
       {
           playingField[count]=\'E\';
       }
   }
   //MODIFICATION MEMBER FUNCTIONS
   void TicTacToe::playerNextMove(int const fieldLocation, char playerSymbol)
   {
       assert((fieldLocation < 10) && (isSpaceOccupied(fieldLocation) == \'E\'));
       playingField[fieldLocation] = playerSymbol;
   }

   //CONSTANT MEMBER FUNCTIONS
   char TicTacToe::isSpaceOccupied(int const fieldLocation) const
   {
       char currentlyFiledSpace;
       assert(fieldLocation < 10);

       currentlyFiledSpace = playingField[fieldLocation];
       return currentlyFiledSpace;

   }
   void TicTacToe::displayTicBoard() const
   {
       //displying the tictactoe board
       cout << \" \" << playingField[1] << \" | \" << playingField[2] << \" | \" << playingField[3] << endl;
       cout << \"----------\" << endl;
       cout << \" \" << playingField[4] << \" | \" << playingField[5] << \" | \" << playingField[6] << endl;
       cout << \"----------\" << endl;
       cout << \" \" << playingField[7] << \" | \" << playingField[8] << \" | \" << playingField[9] << endl << endl;
   }
   int TicTacToe::isGameOver() const
   {
       //all 3 top are in same row
       if ((isSpaceOccupied(0) == isSpaceOccupied(1)) && (isSpaceOccupied(1) == (isSpaceOccupied(2))) ==\'E\')
       {
           if (isSpaceOccupied(0) == \'X\')
           {
               return 1;
           }
           else
           {
               return 2;
           }

       }

       //all 3 middle row are the same
       if ((isSpaceOccupied(3) == isSpaceOccupied(4)) && (isSpaceOccupied(4) == (isSpaceOccupied(5))) ==\'E\')
       {
           if (isSpaceOccupied(3) == \'X\')
           {
               return 1;
           }
           else
           {
               return 2;
           }
       }

           //all 3 in bottom row are the same
       if ((isSpaceOccupied(6) == isSpaceOccupied(7)) && (isSpaceOccupied(7) == (isSpaceOccupied(8)))!=\'E\')
       {
           if (isSpaceOccupied(6) == \'X\')
           {
               return 1;
           }
           else
           {
               return 2;
           }
       }

               //all in 3 in the left colunmn are the same
               if ((isSpaceOccupied(0) == isSpaceOccupied(3)) && (isSpaceOccupied(3) == (isSpaceOccupied(6))) ==\'E\')
               {
                   if (isSpaceOccupied(0) == \'X\')
                   {
                       return 1;
                   }
                   else
                   {
                       return 2;
                   }
               }


               //all the 3 middle column are the same
               if ((isSpaceOccupied(0) == isSpaceOccupied(1)) && (isSpaceOccupied(1) == (isSpaceOccupied(2))) ==\'E\')
               {
                   if (isSpaceOccupied(1) == \'X\')
                   {
                       return 1;
                   }
                   else
                   {
                       return 2;
                   }
               }


               //all the three right column are the same
               if ((isSpaceOccupied(1) == isSpaceOccupied(4) == isSpaceOccupied(4)) && (isSpaceOccupied(7)) ==\'E\')
               {
                   if (isSpaceOccupied(1) == \'X\')
                   {
                       return 1;
                   }
                   else
                   {
                       return 2;
                   }
               }

               //all the 3 digonally from top left to bottom right are the same
               if ((isSpaceOccupied(2) == isSpaceOccupied(5) == isSpaceOccupied(5)) && (isSpaceOccupied(8)) ==\'E\')
               {
                   if (isSpaceOccupied(2) == \'X\')
                   {
                       return 1;
                   }
                   else
                   {
                       return 2;
                   }
               }

               //all 3 diangonally from bottom left to top right are the same
               if ((isSpaceOccupied(0) == isSpaceOccupied(4) == isSpaceOccupied(4)) && (isSpaceOccupied(8)) ==\'E\')
               {
                   if (isSpaceOccupied(0) == \'X\')
                   {
                       return 1;
                   }
                   else
                   {
                       return 2;
                   }
               }
               if ((isSpaceOccupied(2) == isSpaceOccupied(4) == isSpaceOccupied(4)) && (isSpaceOccupied(6)) ==\'E\')
               {
                   if (isSpaceOccupied(0) == \'X\')
                   {
                       return 1;
                   }
                   else
                   {
                       return 2;
                   }
               }
               //return 0;


               //int flag = 3;
               //for (int i = 1;i <= 9;i++)
               //{
               //   if (isSpaceOccupied(i) == \'E\')
               //       flag = 4;
               //}
               //this for loop will check is there any empty space left
               for (int i = 1; i <= 9; i++)
               {
               if (isSpaceOccupied(i) == \'E\')
               return 0;
               else
               return 3;
               }
              
               //return flag; //for continueing the game
           }
}

Solution

The program seems to have problem with isGameOver funtion where it returns 1 2 or 3 even when any of the player or computer wins. Hence, your program iterates for only 1 time.

For an Example, In First Iteration if you choose to place player\'s X mark at 4th location, then your first condition i.e.

if ((isSpaceOccupied(0) == isSpaceOccupied(1)) && (isSpaceOccupied(1) == (isSpaceOccupied(2))) ==\'E\') will be true as all the elements in first row are \'E\'

Inside this if condition, it is being checked that whether isSpaceOccupied(0) is \'X\' or not. In this case it will not be \'X\' as you have placed \'X\' at location 4. Hence, the else part would be executed and the value returned from isGameOver function will be 2.

And the story ends there itself. Player 2 wins because of 3 \'E\' values in first row and 1 \'X\' value at 4th location

I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc
I am writing a c++ program. But it break after 1 round, Please help me to figue this out!!! Please include sample output. ///Main #include <iostream> #inc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site