3

I'm a very new C++ user (and a new StackOverflow user at that), and I'm trying to code a very basic Tic-Tac-Toe (Naughts and Crosses) game. I'm not sure how to render the board as it is updated.

My main question is if it is possible to call a string using concatenation. I have an array set up that indexes the states of the 9 spaces of the board using a 0 for empty, a 1 for an X, and a 2 for an O. If I set up 9 variables in a user-defined renderBoard() function named bit1, bit2, etc; Can I call them this way:

void renderBoard()
{
    int i = 1;
    string bit1;
    string bit2;
    string bit3;
    string bit4;
    string bit5;
    string bit6;
    string bit7;
    string bit8;
    string bit9;

    while (i < 10)
    {
        if (Spaces[i] = 0)
        {
            (bit + i) = * //This is the main bit I'm wondering about
        }
        else
        {
            //Check for 1, 2, and edit the string bits accordingly
        }
        ++i;
    }
    //Put all of the strings together, as well as some more strings for adding the grid
    //Output the whole concatenated string to the command line
}

If anyone knows of a better way to do this, please let me know. I've tried Googling and rifling through various C++ help websites, but I find it difficult to express my particular case through anything other than a long-winded and specific explanation.

Thanks for you help!!

  • @TedLyngmo Yeah, I just don't know how to edit those in a loop instead of just going through and hardcoding them all. Again, I'm very new to this whole thing :P – PhysicsLover999 Aug 05 '19 at 03:53
  • 1
    `string bit1; string bit2; string bit3; string bit4; string bit5; string bit6; string bit7; string bit8; string bit9;` You have to be doing something wrong if you are doing this. – L. F. Aug 05 '19 at 04:00
  • @L.F. Yes, I'm aware it's a very inefficient approach. I'm trying to get answers so that I can improve my approach and overall coding skill. :) – PhysicsLover999 Aug 05 '19 at 04:02
  • 1
    Please read a [good C++ book](https://stackoverflow.com/q/388242) instead of "Googling and rifling through various C++ help websites". – L. F. Aug 05 '19 at 04:02
  • @L.F. I'm not exactly trying to make this into a career - I'm just trying to get into this as a hobby, and therefore I'm just going to be Googling where I encounter small syntax problems and asking for help on community websites where I encounter slightly larger ones. While I appreciate your book suggestions, I'm not ready to commit that much time or money to this yet. :) – PhysicsLover999 Aug 05 '19 at 04:07
  • @PhysicsLover999 Not really what one would hope for. Every question that helpful people spend time on answering because you've not committed is on their tab. You can just buy a book and/or read up on the basics. – Ted Lyngmo Aug 05 '19 at 04:14
  • @PhysicsLover999 I appreciate your do-it-yourself attitude, but I have to agree with LF. You'll save yourself a lot of pain if you get yourself a C++ book. It's not as much about making it a career as much as it is about trying to save you some trouble by getting you a good base on the language itself, as well as a wonderful reference tool if you forget something (it happens even to the best of us). –  Aug 05 '19 at 05:16

2 Answers2

2

If I correctly understood your problem, your problem is that you want to access the strings named bit1, bit2, etc using a variable i like bit + i.

And no, you cannot do that! It will throw a compile time error.

Please correct me if I didn't get what you are looking for.

But one question is still in my mind that why are you using string variables bit1, bit2 etc? I think you just want to store single digit value in those strings. If this is the case, you can just use a single string of length 9.

You can do this as follows:

int i = 0; //because string indices start from 0 and also array indices.
string bit(9, ' '); //declare a string of length 9 with default value of a space (you can modify it with your default value)
while (i < 9) { // i < 9 because highest index will be 8 
     if (Spaces[i] == 0) { 
         bit[i] = '*';
     } else { 

     } 
    ++i;
 }
sanketd617
  • 809
  • 5
  • 16
  • Thank you so much! This worked out great, with the exception of line 2 - You have `string bit(9)`, while I believe it should be `string bit[9]`. Thanks again! – PhysicsLover999 Aug 05 '19 at 04:22
  • Oops! Sorry for that. But it shouldn't be bit[9]. I've corrected the code in my answer and it should work now.also there should be `==` instead of just `=` in you `if` statement – sanketd617 Aug 05 '19 at 04:33
0

Declaring 9 variables like this is apparently wrong. What you are looking for is an array.

std::array<std::string, 9> bits;

(You need #include <array> and #include <string>.)

Then, you can traverse the string using a for-loop: (in C++, arrays are indexed starting from zero, not one)

for (std::size_t i = 0; i < 9; ++i) {
    // operate on bits[i]
}

In the for-loop, you can use the subscript operator to access the element: bits[i].

Finally, to put all the strings together, use std::accumulate:

std::accumulate(bits.begin(), bits.end(), std::string{})

(You need #include <numeric>.)

L. F.
  • 19,445
  • 8
  • 48
  • 82