Why doesn't this work like I want it to?

It worked fine when I had each spot on the board it's own string, but when I changed it into an array for other stuff in the code it stopped working. I don't get errors or anything, it just doesn't put an X or an O anymore.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  std::string Amove[9] =  {" "," "," "," "," "," "," "," "," "};
    srand(time(0));

    while(WindowOpen)
    {
        if(Menu)
        {
            std::cout << "1-9 to mark position, press enter to begin." << std::endl;
            UpdateMove = false;
            while(Menu2)
            {
                if(GetAsyncKeyState(VK_RETURN))
                {
                    Menu = false;
                    UpdateMove = true;
                    Menu2 = false;
                    Gamestarted = true;
                    if(rand() % 2 == 0)
                    {
                        Player1 = true;
                        secondturn = true;
                        PlayerTurn = true;
                        AiMark =  "X";
                    }
                    if(rand() % 2 == 1)
                    {
                        firstturn = true;
                        Player2 = true;
                        AiTurn = true;
                        AiMark =  "O";
                    }
                }
            }
        }
        while(Gamestarted)
        {
            if(UpdateMove)
            {
                system("cls");
                std::cout << "|" << Amove[0] << "|" << Amove[1] << "|" << Amove[2] << "|\n-------\n|" << Amove[3] << "|" << Amove[4] << "|" << Amove[5] << "|\n-------\n|" << Amove[6] << "|" << Amove[7] << "|" << Amove[8] << "|\n-------" << std::endl;
                std::cout << PlayerTurn;
                UpdateMove = false;
            }

            while(PlayerTurn)
            {
                if(GetAsyncKeyState('1'))
                {
                    if(Player1)
                    {
                        Amove[0] = "O"; //This is the part I'm talking about
                    }
                    if(Player2)
                    {
                        Amove[0] = "X"; // ^^^^^^
                    }
                    UpdateMove = true;
                    PlayerTurn = false;
                }
strings are not arrays, their size changes.

string.resize(string.size() - 1)) resizes the string 1 less. you need to set a conditional to make sure the string is, in fact, holding somthing, before referencing an element.

1
2
3
4
if(temp.size() == 1)
{
    return temp[0];
}


Also, I would highly recommend looking at writing this in a more object-oriented design.
"Also, I would highly recommend looking at writing this in a more object-oriented design. "

What do you mean?
learn about classes.

Classes, in c++, are a way to combine data structures and functions.

What are objects? Well, an object must be defined, therefore we must define it through variables in code. Objects can also have actions performed on them, which is defined in the code as well. In C++, classes are data structures (in a way) that contain functions which allow a programmer to modify the data.

An example:

Instead of writing functions and useing a data structure to load and manipulate a set of data, I can declare a class which stores that data. I can also have this class load all of the data needed upon it's construction (aka, when an instance of the class is declared, it will load the data from the file) and treat it like "data from the file" instead of, say, "a list of strings, integers, and fractions I just loaded".

The reasn you might want to use classes, is that in larger projects, objects are easier to use than functions/variables. It's the difference between calling all of the functions and performing operations on the data, and calling a single function member of a class.

I could load a list with vector<string> mylist(load_list_of_info("myfile.txt"));, or I could simply listfile f("myfile.txt"); and then whenever I want to reference somthing, I could manipulate a public vector (cout<< f.mylist[5] for example).

It would simply make it easier for you to handle the data.
Last edited on
I already knew about classes, but I never saw a purpose In using them so I haven't used them so far. I'll try to use them in this. string.resize(string.size() - 1)) and
1
2
3
4
if(temp.size() == 1)
{
    return temp[0];
}
How do I use them in my code? I've never used something like that before.
Look it up. a string is an instance of the class string. .size() and .operator[](const unsigned int&) are members of the class.
Topic archived. No new replies allowed.