tic tac toe

hi guys the code below just gets input from the user and stores and X or an 0 in each position of a 2d array(tic tac toe board) I'm just wondering if you guys could give me some tips on how I would implement a way to end the game if 3 x's or 3 o's appear in a row?

and also I personally think this code while it does what I want it to do looks sloppy and is too long any way I can shorten it and make it more readable?

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>

using namespace std;

void fillBoard(char board[][3]){

     for(int row = 0;row < 3; row++){

        for(int col = 0;col < 3;  col++){

            board[row][col] = 'V';
        }
      }
}

void printBoard(char board[][3]){

      for(int row = 0;row < 3; row++){

        for(int col = 0;col < 3;  col++){

             cout << board[row][col] << " ";
        }
            cout << "" << endl;
      }
}

bool isValidP1(char board[][3],int x,int y){

      if(board[x][y] == 'V'){

           return true;
      }else{

        cout << "invalid number or position already filled" << endl;
        return false;
      }

}

bool isValidP2(char board[][3],int x,int y){

      if(board[x][y] == 'V'){

           return true;
      }else{

        cout << "invalid number or position already filled" << endl;
        return false;
      }

}

int main()
{

    char board[3][3];
    bool gameOver = false;
    fillBoard(board);
    printBoard(board);
    int x,y;
    cout << "enter a board cordinates" << endl;
    cin >> x;
    cin >> y;

    if(isValidP1(board,x,y)){

     board[x][y] = 'X';

    }
    printBoard(board);

    cout << "enter board cordinates " << endl;
    cin >> x;
    cin >> y;
    if(isValidP2(board,x,y) == true){

        board[x][y] = 'O';
    }

    printBoard(board);

    while(gameOver == false){

    cout << "enter a board coardinates" << endl;
    cin >> x;
    cin >> y;

    if(isValidP1(board,x,y)){

     board[x][y] = 'X';

    }
    printBoard(board);

    cout << "enter board codinates " << endl;
    cin >> x;
    cin >> y;
    if(isValidP2(board,x,y) == true){

        board[x][y] = 'O';
    }

    printBoard(board);

    cout << "enter a board coardinates" << endl;
    cin >> x;
    cin >> y;

    }
}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194413/8/#msg935770
Last edited on
Topic archived. No new replies allowed.