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

  • Home
  • SEARCH
  • 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 429311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:45:46+00:00 2026-05-12T19:45:46+00:00

So I am trying to program a way to replay a tic tac toe

  • 0

So I am trying to program a way to replay a tic tac toe game after someone wins, loses, or ties.
So basically my attempt to get replay to work, doesnt work. If player 1 won and I type 1 to replay, it would ask player 2 for their input.

Pseudocode outline:

do {
  set entire 2d array to '*'
  do {
    player 1 input
    does game tie?
    does player 1 win
    player 2 input
    does game tie?
    does player 2 win
  } while no one wins
} while replay = 1

My actual code:

//tie check, replay, use pointer notation
#include <iostream>
using namespace std;

void initialize(char [][3]);
void player1(char [][3]);
void player2(char [][3]);
void display(char [][3]);
char check(char [3][3]);
int checkWin(int);
int tie(int);
int askReplay();
int main()
{
    char board[3][3];
    char end = '*';
    int row1, column1, row2,column2;
    int replay = 0;
    int turns = 0;
    //replay loop
    do {
        //set board to *
        initialize(board);
        display(board);
        do {
            //player 1 turn
            player1(board);
            turns++;
            display(board);

            //if turns = 9 then tie
            replay = tie(turns);
            //check if player 1 won
            end = check(board);
            replay = checkWin(end);

            //player 2 turn
            player2(board);
            turns++;
            display(board);

            //if turns = 9 then tie
            replay = tie(turns);
            //check if player 2 won
            end = check(board);
            replay = checkWin(end);

        } while (end == '*');
    } while (replay == 1);
    return 0;
}

void initialize(char (*array)[3])
{
    for (int i = 0;i < 3;i++)
        for (int j = 0;j < 3;j++)
            array[i][j] = '*';
    cout << "New Game\n";
}

void player1(char (*array)[3])
{
    int row1, column1;
    cout << "Player 1\nRow: ";
    cin >> row1;
    while (row1 < 0 || row1 > 2) {
        cout << "Enter a number between 0 and 2 for Row:: ";
        cin >> row1;
    }

    cout << "Column: ";
    cin >> column1;
    while (column1 < 0 || column1 > 2) {
        cout << "Enter a number between 0 and 2 for Column: ";
        cin >> column1;
    }

    if (array[row1][column1] == '*')
        array[row1][column1] = 'X';
    else {
        cout << "Space Occupied\n";
        player1(array);
    }
}

void player2(char (*array)[3])
{
    int row2,column2;
    cout << "Player 2\nRow: ";
    cin >> row2;
    while (row2 < 0 || row2 > 2) {
        cout << "Enter a number between 0 and 2 for Row: ";
        cin >> row2;
    }

    cout << "Column: ";
    cin >> column2;
    while (column2 < 0 || column2 > 2) {
        cout << "Enter a number between 0 and 2 for Column: ";
        cin >> column2;
    }

    if (array[row2][column2] == '*')
        array[row2][column2] = 'O';
    else {
        cout << "Space Occupied\n";
        player2(array);
    }
}

void display(char (*array)[3])
{
    for (int x = 0;x < 3;x++) {
        for (int y = 0;y < 3;y++)
            cout << *(*(array + x) + y) << " ";
        cout << endl;
    }
}

char check(char (*array)[3])
{
    int i;

    /* check rows */
    for(i = 0; i < 3; i++)
    if(array[i][0] == array[i][1] && array[i][0] == array[i][2])
        return array[i][0];

    /* check columns */
    for(i = 0; i < 3; i++)
        if(array[0][i] == array[1][i] && array[0][i] == array[2][i])
            return array[0][i];

    /* test diagonals */
    if(array[0][0] == array[1][1] && array[1][1] == array[2][2])
        return array[0][0];

    if(array[0][2] == array[1][1] && array[1][1] == array[2][0])
        return array[0][2];
    return '*';
}

int checkWin(int over)
{
    if (over == '*')
        return 0;
    if (over == 'X')
        cout << "Player 1 Won!\n";
    else if (over == 'O')
        cout << "Player 2 Won!\n";

    //ask if they want to play again
    int answer;
    answer = askReplay();

    switch (answer) {
    case 1:
        return 1;
    case 2:
        cout << "Thank you for playing.\n";
        exit(0);
    }
}

int tie(int count)
{
    if (count == 9) {
        int answer;
        cout << "Tie game";

        answer = askReplay();

        switch (answer) {
        case 1:
            return 1;
        case 2:
            cout << "Thank you for playing.\n";
            exit(0);
        }
    }
}

int askReplay()
{
    int input;
    do {
        cout << "Play Again?\n1.Yes\n2.No\nEnter 1 or 2: ";
        cin >> input;
        if (input > 2 || input < 1)
            cout << "Invalid Option\n";
    } while(input > 2 || input < 1);

    return input;
}
  • 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-12T19:45:46+00:00Added an answer on May 12, 2026 at 7:45 pm

    It sounds like you’re having troubles with your main loop.

    I’d suggest making a variable that controls which player is running and just toggle that.

    do  
    {    
      set entire 2d array to '*'  
      current player = 0
      do  
      {  
        (current player + 1) input  
        does game tie?  
        does (current player + 1) win
    
        current player = (current player + 1) % 2  
      }while no one wins     
    }while replay = 1
    

    See if that gets you further along.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to program using pure python in Xcode? I want something
This relates to a project to convert a 2-way ANOVA program in SAS to
Ok I have been trying every which way to figure this out. I need
I am trying to print a string in a way that's OS-neutral. For example,
I'm trying to write a graphical program in C++ with QT where users can
Possible Duplicate: Reliable way of generating unique hardware ID Am trying to generate an
I am trying to write a simple program that lets me overlay a dot
I've been trying to figure out if this is possible the way I've done
I'm trying to write a program to automate a ticket draft. We have a
I wrote an application in C# for win 7 which used .NET 4.0 and

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.