Tic tac toe confusion

Pages: 1... 678910... 13
Make sure you read my post above ^
Here is the function main() :
1
2
3
4
5
6
7
8
9
int main()
{
    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"                          };
    printBoard(ticTac);
    userInput(ticTac);
    return 0;
}
Does that help you? :)
I highly recommend you change the userInput()'s function name ("userInput") to "playGame". Now the role the function becomes much clearer and more consistent.
So, what do you think? :)

==>
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
#include <iostream>
using namespace std;
const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void playGame(string ticTac[][COLS]);

bool inputValidation (string ticTac[][COLS], int positionRow, int positionCol, string player);

int main()
{
    string ticTac[ROWS][COLS];
  
    playGame(ticTac);
    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;
    }
}

bool is_between(int a, int b, int c)
{
     return ((a <= b) && (b < c));
}

bool validateInput(string ticTac[][COLS], int positionRow, int positionCol, string player)
{
     if(!cin)
     {
          cin.clear(); 
          cin.ignore(1000, '\n');
          cout << "Error! Please clarify this error message. (1)" << "\n\n";
          return false;
     }

     if(!is_between(0, positionRow, ROWS) || !is_between(0, positionCol, COLS))
     {
          cout << "Error! Please clarify this error message (2)." << "\n\n";
          return false;
     }

     if(ticTac[positionRow][positionCol] != "*")
     {
          cout << "Error : That spot has already been taken" << "\n\n";
          return false;
     }

     ticTac[positionRow][positionCol] = player;
     displayBoard(ticTac);
     return true;
}

void promptUser(string ticTac[][COLS], string player)
{
     int positionCol, positionRow;
     if(player != "X" && player != "O") player = "X";
     cout << "Player (" << player << ")'s turn : " << endl;
     do
     {
          cout << "enter row : ";
          cin >> positionRow;
           cout << "enter column : ";
          cin >> positionCol;
     } while (validateInput(ticTac, positionRow, positionCol, player) == false);
}

void initializeBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    for (int j = 0; j < COLS; j++)
        ticTac[i][j] = "*";
}

void playGame(string ticTac[][COLS])
{
     bool gameOver = false;
     bool xTurn = true;
     initializeBoard(ticTac);
     while(gameOver == false)
     {
          if(xTurn) promptUser(ticTac, "X");
          else promptUser(ticTac, "O");
          xTurn = !xTurn;
     }
}
Does that help you? :)
Here is a slightly different version of closed account's code. I've changed it around a little so that each function does only one thing. For example, validateInput no longer places the move, it just validates the move.

I've also added a bunch of comments. I think the FBHSIE will find these helpful to understand what each function does.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
using namespace std;
const int ROWS = 3;
const int COLS = 3;

void printBoard(string ticTac[][COLS]);

bool inputValidation(string ticTac[][COLS], int positionRow, int positionCol,
                     string player);
void initializeBoard(string ticTac[][COLS]);

void promptUser(string ticTac[][COLS], string player, int &positionRow, int &po\
sitionCol);

int
main()
{
    string ticTac[ROWS][COLS];
    bool gameOver = false;
    bool xTurn = true;
    string player;
    int positionRow=0, positionCol = 0;

    initializeBoard(ticTac);

    // Print the initial board
    printBoard(ticTac);

    // Loop until the game is over. Each iteration of the loop
    // will prompt for one move and play it.
    while (gameOver == false) {
        // set the current player's character
        if (xTurn) {
            player = "X";
        } else {
            player = "O";
        }

        // Get the player's move.
        promptUser(ticTac, player, positionRow, positionCol);

        // Put the player's mark on the board. Then print the new board.
        ticTac[positionRow][positionCol] = player;
        printBoard(ticTac);

        // switch players
        xTurn = !xTurn;
    }
    return 0;
}


// Print the board
void
printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++) {
        cout << '\t';
        for (int j = 0; j < COLS; j++) {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;
    }
}


// Return true if b is between and c (a <= b < c). Otherwise returns false
bool
is_between(int a, int b, int c)
{
    return ((a <= b) && (b < c));
}

// Is it okay to move to positionRow and positionCol?  This verifies that
// positionRow and positionCol are between 1 and 3. It also verifies that
// that the square on the board is empty.  This returns true if positionRow
// and positionCol are valid, otherwise it returns false
bool
validateInput(string ticTac[][COLS], int positionRow, int positionCol, string p\
layer)
{
    if (!cin) {
        cin.clear();
        cin.ignore(1000, '\n');
        cout << "Error! Please clarify this error message. (1)" << "\n\n";
        return false;
    }

    if (!is_between(0, positionRow, ROWS) || !is_between(0, positionCol, COLS))\
 {
        cout << "Error! Please clarify this error message (2)." << "\n\n";
        return false;
    }

    if (ticTac[positionRow][positionCol] != "*") {
        cout << "Error : That spot has already been taken" << "\n\n";
        return false;
    }

    return true;
}

// Prompt the user for their next move. Keep prompting them until they
// enter a valid move. Store the resulting valid moves in positionRow
// and positionCol, which are passed by reference.
void
promptUser(string ticTac[][COLS], string player, int &positionRow, int &positio\
nCol)
{
    if (player != "X" && player != "O")
        player = "X";
    cout << "Player (" << player << ")'s turn : " << endl;
    do {
        cout << "enter row : ";
        cin >> positionRow;
        cout << "enter column : ";
        cin >> positionCol;
    } while (validateInput(ticTac, positionRow, positionCol, player) == false);
}

// Initialize the board to contain "*" in each square
void
initializeBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
        for (int j = 0; j < COLS; j++)
            ticTac[i][j] = "*";
}
closed account (48T7M4Gy)
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
#include <iostream>

const int ROWS = 3;
const int COLS = 3;

void display_board(char[][COLS]);
void userInput(char [][COLS], char);
int check_status(char [][COLS], char);

int main(){
    char player[] = { 'O', 'X' };
    char ticTac[][COLS] = { '*','*','*','*','*','*','*','*','*'};
    int no_plays = 0;
    int player_no = 0;
    std::string game_status[] = { "NIL", "WINS" };
    int game_score = 0;
    display_board(ticTac);
    
    do {
        player_no = no_plays % 2;
        
        userInput(ticTac, player[player_no]);
        display_board(ticTac);
        
        game_score = check_status(ticTac, player[player_no]);
        std::cout << "Status: " << player[player_no] << ' ' << game_status[game_score] << '\n' << '\n';
        
        no_plays++;
        
    } while (game_score != 1 && no_plays < 9);
    
    std::cout << "GAME OVER\n";
    return 0;
}

int check_status(char ticTac[][COLS], char aPlayer ) {
    int score_1 = 0;
    int score_2 = 0;
    for(int c = 0; c < COLS; c++) {
        score_1 = 0;
        score_2 = 0;
        
        for (int r = 0; r < ROWS; r++) {
            score_2 += ticTac[r][c];
            score_1 += ticTac[c][r];
        }
        if ( score_2 == 3 * aPlayer || score_1 == 3 * aPlayer)
            return 1;
    }
    
    score_1 = 0;
    score_2 = 0;
    for (int r = 0; r < ROWS; r++) {
        score_1 += ticTac[r][r];
        score_2 += ticTac[r][ROWS - r - 1];
    }
    if ( score_1 == 3 * aPlayer || score_2 == 3 * aPlayer )
        return 1;
    
    return 0;
}

void display_board(char ticTac[][COLS]) {
    for (int i = 0; i < ROWS; i++) {
        std::cout << '\t';
        for (int j = 0; j < COLS; j++)
            std::cout << ticTac[i][j] << ' ';
        std::cout << '\n';
    }
}

void userInput(char ticTac[][COLS], char aPlayer) {
    int pRow = 0;
    int pCol = 0;
    
    do {
        std::cout << "Player " << aPlayer << " please enter vacant row and column you'd like: ";
        std::cin >> pRow >> pCol;
    }while(( pRow < 1 || pRow > 3 || pCol < 1 || pCol > 3) || ticTac[pRow - 1][pCol - 1] != '*');
    
    ticTac[pRow - 1][pCol - 1] = aPlayer;
    
    return;
}


	
        * * * 
	* * * 
	* * * 
Player O please enter vacant row and column you'd like: 1
1
	O * * 
	* * * 
	* * * 
Status: O NIL

Player X please enter vacant row and column you'd like: 1
2
	O X * 
	* * * 
	* * * 
Status: X NIL

Player O please enter vacant row and column you'd like: 6 2
Player O please enter vacant row and column you'd like: 1 3
	O X O 
	* * * 
	* * * 
Status: O NIL

Player X please enter vacant row and column you'd like: 1
1
Player X please enter vacant row and column you'd like: 2
1
	O X O 
	X * * 
	* * * 
Status: X NIL

Player O please enter vacant row and column you'd like: 2
2
	O X O 
	X O * 
	* * * 
Status: O NIL

Player X please enter vacant row and column you'd like: 2
3
	O X O 
	X O X 
	* * * 
Status: X NIL

Player O please enter vacant row and column you'd like: 3
2
	O X O 
	X O X 
	* O * 
Status: O NIL

Player X please enter vacant row and column you'd like: 3
3
	O X O 
	X O X 
	* O X 
Status: X NIL

Player O please enter vacant row and column you'd like: 3
2
Player O please enter vacant row and column you'd like: 3
1
	O X O 
	X O X 
	O O X 
Status: O WINS

GAME OVER
Program ended with exit code: 0
Last edited on
Thanks for all the code advice. Using a bool function it helps! I was wondering, however, is there any way it could work with what how I'm doing it now or no?

I got it to compile correctly, but for some reason X and O just will not show up on the board together when printed.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS], int positionRow, int positionCol);
void inputValidation (string ticTac[][COLS], int positionRow, int positionCol);


int main()
{

    int positionRow=0;
    int positionCol=0;


    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);
    userInput(ticTac, positionRow, positionCol);
    inputValidation(ticTac, positionRow, positionCol);



    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;

    }


}

void userInput(string ticTac[][COLS], int positionRow, int positionCol)
{

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;




    ticTac[positionRow][positionCol] = "O";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);


    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "O";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "O";

    inputValidation(ticTac, positionRow, positionCol);

    printBoard(ticTac);
}

void inputValidation (string ticTac[][COLS], int positionRow, int positionCol)
{
    while(ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O")
    {
        cout << "That position has already been taken! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }

}
Last edited on
Line 58: You don't call inputValidation() for the first move.

Line 130: positionRow and positionCol are passed by value. If line 135 is executed to input new values, these new values will be lost and not passed back to the caller. You want to pass positionRow and positionCol by reference so that their values are updated in the caller.

Line 64-127. You have a lot of repeated code here. You should consider using a function call or a loop here.

Lines 73-75: You should call inputValidation BEFORE you place the player's marker on the board.

Lines 86-88, 99-101, 111-113, 123-125: Ditto

Lines 130-138: inputValidation should probably check that the users input for both positionRow and positionCol is valid (0-2).


Line 130: fixed.

64-127: I think I may keep it how it is if that's possible : )

73-75: It may be just how you worded it (spare me), but I'm not sure what you're saying? Are you saying put it before printboard? If so, I think that's what I did?

Ditto parts: ^

Lines 130-138

How would you validate an input 0-2? I've only ever done validations for passwords, slot machines, and names, but never ranges.

I'm not sure what you're saying?

Line 73: You place an "O" on the board in the specified position. Two problems:
1) You have not yet checked that the position is unoccupied.
2) Since you've placed an "O" there, inputValidation is always going to return true since you just put a valid marker in that position.

What I'm saying is that lines 73 (place marker) and 75 (inputValidation) are in the wrong order. You need to check that the input position is unoccupied (and that row and col are valid) BEFORE you put the player marker on the board.

How would you validate an input 0-2?

The easiest way is to just add conditiions to you while statement at line 132:
1
2
3
  while (positionRow >= 0 && positionRow < ROWS 
            && positioonCol >= 0 && positionCol < COLS
           && (ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O"))

Note the parens around the or condition.



Got everything working except the "make sure values 0-2 are inputed" part!

1
2
3
4
5
6
7
8
9
10
11
12
void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol)
{
    while(positionRow >= 0 && positionRow < ROWS
            && positionCol >= 0 && positionCol < COLS
           && (ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O"))
    {
        cout << "That position has already been taken! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }


}


Last edited on
I also decided I'd make a function to find the winner and figure out if it was a tie

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS], int positionRow, int positionCol);
void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol);
void declareWinner (string ticTac[][COLS], int positionRow, int positionCol);


int main()
{

    int positionRow=0;
    int positionCol=0;


    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);
    userInput(ticTac, positionRow, positionCol);
    inputValidation(ticTac, positionRow, positionCol);
    declareWinner (ticTac, positionRow, positionCol);



    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;

    }


}

void userInput(string ticTac[][COLS], int positionRow, int positionCol)
{

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;



    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);


    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";


    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";

    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);
}

void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol)
{
    while(positionRow >= 0 && positionRow < ROWS
            && positionCol >= 0 && positionCol < COLS
           && (ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O"))
    {
        cout << "That position has either already been taken or you entered an invalid row or column! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }


}

void declareWinner (string ticTac[][COLS], int positionRow, int positionCol)
{

}
Line 28: This line is unnecessary. By the time you return from userInput, the game is over.

Line 29: You want to check for a winner after each move. A win can occur as early as the 5th move.

Line 60: You're still not calling inputValidation() for the first move.

Line 144: positionRow and positionCol are not going to be relevant to checking for a winner.
In addition, they're not going to be valid since they're passed to userInput by value.


Last edited on
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>

using namespace std;


const int ROWS=3;
const int COLS=3;

void printBoard(string ticTac[][COLS]);
void userInput(string ticTac[][COLS], int positionRow, int positionCol);
void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol);
void declareWinner (string ticTac[][COLS]);


int main()
{

    int positionRow=0;
    int positionCol=0;


    string ticTac[ROWS][COLS]= {"*","*","*",
                                "*", "*","*",
                                "*","*","*"
                               };
    printBoard(ticTac);
    userInput(ticTac, positionRow, positionCol);
    declareWinner (ticTac);



    return 0;
}

void printBoard(string ticTac[][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        cout << '\t';
        for (int j = 0; j < COLS; j++)
        {
            cout << ticTac[i][j] << ' ';
        }
        cout << endl;

    }


}

void userInput(string ticTac[][COLS], int positionRow, int positionCol)
{

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);
    ticTac[positionRow][positionCol] = "X";
    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;



    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);


    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";


    printBoard(ticTac);


    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);

    cout << "Player X : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "X";

    printBoard(ticTac);

    cout << "Player O : " << endl;
    cout << "Enter row #: ";
    cin >> positionRow;
    cout << "Enter column #: ";
    cin >> positionCol;

    inputValidation(ticTac, positionRow, positionCol);

    ticTac[positionRow][positionCol] = "O";

    printBoard(ticTac);
}

void inputValidation (string ticTac[][COLS], int &positionRow, int &positionCol)
{
    while(positionRow >= 0 && positionRow < ROWS
            && positionCol >= 0 && positionCol < COLS
           && (ticTac[positionRow][positionCol] == "X" || ticTac[positionRow][positionCol] == "O"))
    {
        cout << "That position has either already been taken or you entered an invalid row or column! Please enter another row and column: " << endl;
        cin >> positionRow >> positionCol;
    }


}

void declareWinner (string ticTac[][COLS])
{

}


Line 29: You want to check for a winner after each move. A win can occur as early as the 5th move.

The fifth move? But each user can only move three times? What do you mean?

Also, I assume I use an if then statement.

Last edited on
The fifth move? But each user can only move three times? What do you mean?

1st move: X moves 0,0
2nd move: O moves 1,1
3rd move: X moves 0,2
4th move: O moves 1,2 (dumb, but possible)
5th move: X moves 0,1 - Game over. X wins

I assume I use an if then statement.

Yes, you need a series of if statements.
You need to check each row, each column and both diagonals.

Search the forum. You will find some clever solutions for checking.




Last edited on
Got it.

But, considering the only assignment passed here in the parameter is the array, how am I supposed to compare to figure out who one without the position variables?
Oh my... O.o
Pages: 1... 678910... 13