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 8866353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:45:53+00:00 2026-06-14T16:45:53+00:00

Please have a look at the following code #include <iostream> #include <stdlib.h> #include <ctime>

  • 0

Please have a look at the following code

#include <iostream>
#include <stdlib.h>
#include <ctime>

using namespace std;

const char PLAYER_1 = 'O';
const char PLAYER_2 = 'X';


int row = 0;
int col = 0;

int winningStates[8][3][2] = {{{0,0},{0,1},{0,2}},{{1,0},{1,1},{1,2}},{{2,0},{2,1},{2,2}},{{3,0},{3,1},{3,2}},
                                {{4,0},{4,1},{4,2}},{{5,0},{5,1},{5,2}},{{6,0},{6,1},{6,2}},{{7,0},{7,1},{7,2}}};

//This sets all the elements within the body to '-' to indicate that the square is free
void intialise(char board[][3])
{
    for(int i=0;i<3;i++)
    {
        for(int a=0;a<=3;a++)
        {
            board[i][a] = '-';

        }
    }


}

//This displays the board on the screen
void display(char board[][3])
{
    for(int i=0;i<3;i++)
    {
        for(int a=0;a<3;a++)
        {
            cout << board[i][a] << "\t ";

        }
        cout << endl;
    }
}

//setValue() sets the cell indicated by the parameter to the value that is contained within the symbol parameter
void setValue(int row, int col, char symbol, char board[][3])
{
    board[row][col] = symbol;
}

//isFree() returns true if the cell identified by the parameter contains '-'
bool isFree(int row, int col, char board[][3])
{
    if(board[row][col]=='-')
    {
        return (true);
    }
    else
    {
        return (false);
    }
}

bool hasWon(char symbol,char board[][3])
{
    bool match = false;

    row = 0;
    col = 0;

    for(int m=0;m<8;m++)
    {
        for(int s=0;s<3;s++)
        {
            row = winningStates[m][s][0];
            col = winningStates[m][s][1];

            if(board[row][col]==symbol)
            {
                match = true;
            }
            else
            {
                match = false;
                break;
            }
        }
    }
    return match;
    //return false;
}

int main()
{
    char board[3][3];

    srand(time(0));

    bool gameOver = false;

    char player = PLAYER_1;

    int count = 0;

    intialise(board);
   // display();

    while(gameOver!=true)
    {     

        do
        {
            row = rand()%3;
            col = rand()%3;
        }
        while(! isFree(row,col,board));


        setValue(row,col,player,board);
        if(player==PLAYER_1)
        {
            player = PLAYER_2;
        }
        else
        {
            player = PLAYER_1;
        }

        gameOver = hasWon(player,board);

        if(gameOver==true)
        {
            cout << "WON" << endl;
        }
        count++;

        if(count==9)
        {
            gameOver = true;
            break;
        }


        display(board);
        cout << endl;


    }
}

In this code, I never get “WON” message. And, one cell is always free, without any symbol.
It doesn’t matter how many times I run this code, No one wins!! Why is this? Please help!!

  • 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-06-14T16:45:54+00:00Added an answer on June 14, 2026 at 4:45 pm

    It looks like a logic error, try this

    bool hasWon(char symbol,char board[][3])
    {
        for(int m=0;m<8;m++)
        {
            bool found_line = true;
            for(int s=0;s<3;s++)
            {
                int row = winningStates[m][s][0];
                int col = winningStates[m][s][1];
    
                if(board[row][col]!=symbol)
                {
                    found_line = false;
                    break;
                }
            }
            if (found_line)
                return true;
        }
        return false;
    }
    

    I think it helps to choose better variable names for this kind of task, match is not very good because it doesn’t say what you are matching. Which seems to have been your error.

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

Sidebar

Related Questions

Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code #include <iostream> using namespace std; int
Please have a look at the following code #include <iostream> #include <stdlib.h> #include <ctime>
Please have a look at the following code. Main.cpp #include <iostream> #include Maximum.h using
i have following code for heapsort #include <iostream> using namespace std; void exch(int a[],int
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code #include <iostream> #include <iomanip> #include <vector>
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
Please have a look at the following code #include <iostream> #include <iomanip> #include <cmath>

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.