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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:25:47+00:00 2026-05-30T04:25:47+00:00

Im trying to build a tic tac toe game, and for some reason seem

  • 0

Im trying to build a tic tac toe game, and for some reason seem to be getting hit by a bug when I try and put a value into any values, it seems to give the selected character(say its player 1, and thus an ‘X’), into the values selected by the user(where he wants it to be) but also annoyingly into [1][2] and [2][0] of the array. The code is attached.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace System;
using namespace std;

string game_board[2][2];
bool exit_condition = false;
int x_coords;
int y_coords;
int current_player;
string cp_char;

int returnxwin();
int returnowin();

int main()
{
    cout << "Welcome to Tic-Tac-Toe Console!\n" << endl;

    //Start of game
    while(exit_condition != true){
        cout << "This is the game board: " << endl;
        cout << " - | 1 | 2 | 3 | " << endl;
        cout << " 1 | " << game_board[0][0] <<" | " <<game_board[1][0] << " | " << game_board[2][0] << " | " <<endl;
        cout << " 2 | " << game_board[0][1] <<" | " <<game_board[1][1] << " | " << game_board[2][1] << " | "<<endl;
        cout << " 3 | " << game_board[0][2] <<" | " <<game_board[1][2] << " | " << game_board[2][2] << " | \n"<<endl;

        //TESTING PURPOSES BE SURE TO REMOVE!
        current_player = 1;

        //Says which player is first
        cout << "Player " << current_player << " is first!" << endl;

        //Determines which player is first, and if so, which character to give to them.
        if(current_player == 1)
        {
            cp_char = "X";
        } else if(current_player == 2) 
        {
            cp_char = "Y";
        }

        bool valid_input = false;

        while(valid_input != true){
            cout << "Where would you like to place your " << cp_char << " ? (X co-ordinates(top row))\n" ;
            cin >> x_coords;
            cout << "Where would you like to place your " << cp_char << " ? (Y co-ordinates(side row))\n" ;
            cin >> y_coords;

            if((x_coords <= 3) && (y_coords <=3))
            {
                if(game_board[x_coords-1][y_coords-1] == "")
                {
                    game_board[1][2] = "";
                    game_board[2][0] = "";
                    //Problem Here!
                    game_board[x_coords-1][y_coords-1] = cp_char;
                    break;
                    valid_input = true;
                }
                else
                {
                    cout << "Invalid Input! Somebody has already taken the slot!" << endl;
                }
            }
            else
            {
                cout << "Invalid input! Please enter a number from 1 to 3" << endl;
            }
        }
        //End of Valid Input Loop

        //make sure to remove break; 
        // break;
    }

    system("pause");
    return 0;
}

I haven’t finished the game yet, but was at a loss as to why this is happening :s

On another side note, Im also looking for a way to randomly decide whether player 1 or 2 goes first.

Edit: I tried doing [3][3], as suggested but it still gives me an error. Ideas?

  • 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-30T04:25:48+00:00Added an answer on May 30, 2026 at 4:25 am

    game_board is defined as an array of 2×2 strings, should it be 3×3?

    string game_board[3][3]
    

    I guess you are going over the array so you get undefined results..

    Regarding having a way to randomly choose which player will start just use the rand() function:

    srand(time(NULL)); // to initialize the random sequence
    
    current_player = rand()%2 + 1; //player will be either 1 or 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a tic-tac-toe game for my first foray into jQuery and
I'm trying to build a tic tac toe game to demonstrate and experiment with
I am trying to create a tic - tac - toe type of game
I'm trying build an App Engine connected Android application and am having some problems
Trying to build a plugin effect that will some what look better than this
am trying to build buttons list slides from bottom of screen when some button
I am trying build an automated testing framework for some of our internal web
I'm trying build my application using REST and Spring MVC. For some entities I
I'm trying build a method which returns the shortest path from one node to
I'm trying build a MVC framework, but I'm confused about manage themes. Well... I

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.