Is there any way shorter to do this?

I'm making a tic-tac-toe game, and I'm trying to develop a function that draws the game to the screen, but the amount of if statements just keeps going up, and I don't know what I'm going to do when I need to draw more than two x's and o's! Please help me.

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
void Draw(int x, int o) {
    // First swipe
    if (x == 11 && o != 12 && o != 13) { cout << "x| | " << endl; }
    if (x == 11 && o == 12) { cout << "x|o| " << endl; }
    if (x == 11 && o == 13) { cout << "x| |o" << endl; }
    if (x == 12 && o != 11 && o != 13) { cout << " |x| " << endl; }
    if (x == 12 && o == 11) { cout << "o|x| " << endl; }
    if (x == 12 && o == 13) { cout << " |x|o" << endl; }
    if (x == 13 && o != 11 && o != 12) { cout << " | |x" << endl; }
    if (x == 13 && o == 11) { cout << "o| |x" << endl; }
    if (x == 13 && o == 12) { cout << " |o|x" << endl; }
    cout << "-----" << endl;
    // Second swipe
    if (x == 21 && o != 22 && o != 23) { cout << "x| | " << endl; }
    if (x == 21 && o == 22) { cout << "x|o| " << endl; }
    if (x == 21 && o == 23) { cout << "x| |o" << endl; }
    if (x == 22 && o != 21 && o != 23) { cout << " |x| " << endl; }
    if (x == 22 && o == 21) { cout << "o|x| " << endl; }
    if (x == 22 && o == 23) { cout << " |x|o" << endl; }
    if (x == 23 && o != 21 && o != 22) { cout << " | |x" << endl; }
    if (x == 23 && o == 21) { cout << "o| |x" << endl; }
    if (x == 23 && o == 22) { cout << " |o|x" << endl; }
    cout << "-----" << endl;
    // Third swipe
    if (x == 31 && o != 32 && o != 33) { cout << "x| | " << endl; }
    if (x == 31 && o == 32) { cout << "x|o| " << endl; }
    if (x == 31 && o == 33) { cout << "x| |o" << endl; }
    if (x == 32 && o != 31 && o != 33) { cout << " |x| " << endl; }
    if (x == 32 && o == 31) { cout << "o|x| " << endl; }
    if (x == 32 && o == 33) { cout << " |x|o" << endl; }
    if (x == 33 && o != 31 && o != 32) { cout << " | |x" << endl; }
    if (x == 33 && o == 31) { cout << "o| |x" << endl; }
    if (x == 33 && o == 32) { cout << " |o|x" << endl; }
    cout << "-----" << endl;
}
Last edited on
this may be an opportunity to learn to write a gui
^ Curses? I don't know how to install that, and can't seem to find a decent tutorial. I'd like to write a 'smart' function, or maybe a class, but it just seems that I can't develop a good algorithm or something.
Last edited on
For posix systems you can use ncurses http://www.faqs.org/docs/Linux-HOWTO/NCURSES-Programming-HOWTO.html

Try using loops instead of all those if statements.
Dear Lord, there is a much more concise way to write it. I think this is stemming from you storing the x's and o's improperly as well. Is a single integer variable x storing all the locations of x's on the board?

Store each board location in array, with 3 different possible values, one for empty, one for X, one for O.

You don't need to draw an entire row at once, you can do it one at a time. First check the first square, and draw what needs to be drawn there. Then draw a '|', then check the next square what needs to be drawn there.

Hint: The final solution is most likely going to involve 2 for loops and 2 if-else statements.
Topic archived. No new replies allowed.