Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6784917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:03:33+00:00 2026-05-26T17:03:33+00:00

I have a tic tac toe game I made, I am not done yet,

  • 0

I have a tic tac toe game I made, I am not done yet, but I made it so that if I get the first three rows I win the game for X.

But for some reason it doesn’t work. It prints out that I won, BUT it doesn’t end the while loop and break out and finish the game.

What am I missing?

 #include <iostream>
using namespace std;

// Initialize Array to 9 elements.
// Set array elements 1 to 9 to specify locations for placement of X and O.
char ticTacBoard[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
// gameState will return 1 to 4. #1. PlayerXhaswon, #2. PlayerYhaswon, #3. Draw, #4. GameNotFinished.
int gameState = 0;
int choosePositionX = 0;
int choosePositionO = 0;

// Functions declared to run game.
// Checks for winners, draw and if game should continue.
int checkGameState (int gameState);
// Takes in users moves.
void makeMove ();
// Resets board when game finished.
void reset ();
// Prints the board to the user.
void printBoard ();


int main()
{
    cout << "Welcome to Tic Tac Toe!" << endl << endl
         << "The goal of Tic Tac Toe is to be the first player to get three in a row on the grid." << endl << endl
         << "Please select 1 through 9 to select placement for your turn."<< endl << endl;

int gameState = 4;

        while ( gameState == 4)
        {
            printBoard();
            cout << "Player X please choose a position: ";
            cin >> choosePositionX; makeMove();
            printBoard();
            gameState = checkGameState(gameState);
            cout << "Player O please choose a position: ";
            cin >> choosePositionO;
            makeMove();
            printBoard();
            gameState = checkGameState(gameState);

       }



cout << " END";

return 0;
}

int checkGameState(int gameState)
{
    // Check Rows
    if(ticTacBoard[0] == 'X' && ticTacBoard[1] == 'X' && ticTacBoard[2] == 'X')
    {
        cout << "YOU WIN!" << endl;

        return 1;
    }
    if(ticTacBoard[3] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[5] == 'X')
    {
        cout << "YOU WIN!" << endl;
        return gameState = 1;
    }
    if(ticTacBoard[6] == 'X' && ticTacBoard[7] == 'X' && ticTacBoard[8] == 'X')
    {
        cout << "YOU WIN!" << endl;
        return gameState = 1;
    }
    else
        return gameState = 4;
}

void makeMove()
{
    if ( choosePositionX )
    {
        ticTacBoard[choosePositionX - 1] = 'X';
    }

    if ( choosePositionO )
    {
        ticTacBoard[choosePositionO - 1] = 'O';
    }
}

void printBoard()
{
    for (int y = 0; y < 3; y++)
    {
        for (int x = 0; x < 3; x++)
        {
            cout << ticTacBoard[3 * y + x] << " ";
        }

        cout << endl;
    }
    cout << endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:03:34+00:00Added an answer on May 26, 2026 at 5:03 pm

    You never use the return value of checkGameState. You probably want to either pass gameState by reference and return void, or do gameState = checkGameState(gameState).
    If you do the former, then you change them all to gameState = 1; return; If you do the latter, then you just change them to return 1;

    Version passing by reference:

     #include <iostream>
    using namespace std;
    
    // Initialize Array to 9 elements.
    // Set array elements 1 to 9 to specify locations for placement of X and O.
    char ticTacBoard[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
    // gameState will return 1 to 4. #1. PlayerXhaswon, #2. PlayerYhaswon, #3. Draw, #4. GameNotFinished.
    int gameState = 0;
    int choosePositionX = 0;
    int choosePositionO = 0;
    
    // Functions declared to run game.
    // Checks for winners, draw and if game should continue.
    int checkGameState (int gameState);
    // Takes in users moves.
    void makeMove ();
    // Resets board when game finished.
    void reset ();
    // Prints the board to the user.
    void printBoard ();
    
    
    int main()
    {
        cout << "Welcome to Tic Tac Toe!" << endl << endl
             << "The goal of Tic Tac Toe is to be the first player to get three in a row on the grid." << endl << endl
             << "Please select 1 through 9 to select placement for your turn."<< endl << endl;
    
    int gameState = 4;
    
            while ( gameState == 4)
            {
                printBoard();
                cout << "Player X please choose a position: ";
                cin >> choosePositionX; makeMove();
                printBoard();
                gameState = checkGameState(gameState);
                if (gameState != 4) break;
                cout << "Player O please choose a position: ";
                cin >> choosePositionO;
                makeMove();
                printBoard();
                gameState = checkGameState(gameState);
    
           }
    
    
    
    cout << " END";
    
    return 0;
    }
    
    int checkGameState(int gameState)
    {
        // Check Rows
        if(ticTacBoard[0] == 'X' && ticTacBoard[1] == 'X' && ticTacBoard[2] == 'X')
        {
            cout << "YOU WIN!" << endl;
    
            return 1;
        }
        if(ticTacBoard[3] == 'X' && ticTacBoard[4] == 'X' && ticTacBoard[5] == 'X')
        {
            cout << "YOU WIN!" << endl;
            return 1;
        }
        if(ticTacBoard[6] == 'X' && ticTacBoard[7] == 'X' && ticTacBoard[8] == 'X')
        {
            cout << "YOU WIN!" << endl;
            return 1;
        }
        else
            return 4;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have been working on a 2 player Tic-Tac-Toe game in java that
I have implemented tic tac toe game as it is from api samples.from that
I have made a Tic-Tac-Toe game for 2 players. Now, I want to give
I have this first integration test for a tic tac toe game: [TestClass] public
This is basically a tic tac toe game, and I have another form called
I have read a book and am trying to make a game of tic-tac-toe
I am writing a tic-tac-toe game in Java. I have a 2D array representing
I am making a Tic Tac Toe game and i created a function that
I have 9 buttons set up as a tic tac toe game. It is
I have thought of some heuristics for a big (higher dimensions) tic-tac-toe game. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.