Tic Tac Toe

I am working on a console Tic Tac Toe game and I found this weird bug I can't seem to find a solution to. When you enter numbers in any order the board cahnges as it should do, but when you enter the combination 1,2,4. 1 = 'X' which it should but the next two are 'O' and I can't find out why, hope one of you sees the error :D.

Comment:
-I know I should't use system("cls");
-winPos isn't used yet.

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
60
61
62
#include <iostream>
#include <cstdlib>
using namespace std;

char board[3][3] = {'1','2','3','4','5','6','7','8','9'};
bool gameFinished = false;

void draw();
int turnDec(int t);
void input();

int main(){
    int winPos = 0;

    while(gameFinished != true){
        winPos ++;
        draw();
        input();
        system("cls");
        }

}

void draw(){
    cout << "Tic Tac Toe v1" << endl;
    for(int x = 0; x < 3; x++){
        for(int y = 0; y < 3; y++){
            cout << board[x][y] << " ";
        }
        cout << endl;
    }
}

void input(){
    int turn = 1;
    char temp;
        cout << "Input: "; cin >> temp;
        for(int x = 0; x < 3; x++){
            for(int y = 0; y < 3; y++){
                if(board[x][y] == temp){
                    if(turn == 1){
                        board[x][y] = 'X';
                        turn --;
                    }
                    else{
                        board[x][y] = 'O';
                        turn ++;
                    }

                }
                else{
                    if(turn == 1){
                        turn ++;
                    }
                    else{
                        turn --;
                    }
                }
            }
        }
}
The problem is that turn is being reset to 1 every time input is called,

You could get around this by declaring int turn = 1 in the main function, then passing a reference to turn into the input function

1
2
3
void input(int & turn){
   char temp;
   ...
Thanks! What does the & do? Has it to do with pointers?
It specifies that turn is a reference to an int - the effect is similar to passing a pointer to an int, but the mechanism is not quite the same. This describes it better than I can:

http://www.cplusplus.com/doc/tutorial/functions/#reference
Thanks :D
Topic archived. No new replies allowed.