EDIT: TicTacToe with win/lose counter and AI

I have a beginners tic tac toe program. So far I have my program allowing two players to play, each taking turns until one wins, then asking if you want to play again, if yes, restarting the program.

What im trying to do now is add a counter to count the number of wins for X or for O, or the number of ties. I believe the code needed is correct, but the implementation in my code is wrong (i dont exactly know where to put it, so far i have it in my checkForGameEnd function)


Here is what i tried so far, to try and get the counter to count number of wins or ties. It counts the first win, but then stops counting.. (if i could get help on the counter first, then move on to the AI, that would be appreciated:)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

//routine to display the current tictactoe baord
void printBoard (string board);

//routine to let player X or O make a move and return the
//  updated board
string pickMove (string board, char player);

bool checkForGameEnd(string board);\

bool playGameAgain();

void newGameDevider();

const int boardSize = 9;

//count the number of filled positions
    int positionsFilled = 0;
    int xWins = 0;
    int oWins = 0;
    int ties = 0;
int main(){

    do {

        string board = "012345678";
        cout << endl;
        printBoard(board);

        cout << endl;
        cout << "(X) for Player X " << endl;
        cout << "(O) for Player O " << endl;
        cout << "Pick which player goes first: ";

        char choiceOfPlayer;
        cin >> choiceOfPlayer;

        if ((choiceOfPlayer == 'X') || (choiceOfPlayer == 'x'))
        {
            char currentPlayer = 'X';
            do {
                //let the current player make a move
                board = pickMove(board, currentPlayer);
                printBoard(board);

                //switch players
                if (currentPlayer == 'X') {
                    currentPlayer = 'O';
                }
                else {
                    currentPlayer = 'X';
                }
            } while (!checkForGameEnd(board));
        }
        else if ((choiceOfPlayer == 'O') || (choiceOfPlayer == 'o'))
        {
            char currentPlayer = 'O';
            do {
                //let the current player make a move
                board = pickMove(board, currentPlayer);
                printBoard(board);

                //switch players
                if (currentPlayer == 'O') {
                currentPlayer = 'X';
                }
                else {
                    currentPlayer = 'O';
                }
            } while (!checkForGameEnd(board));
        }



        }

    if (board[0] == board[1] && board[1] == board[2]) {
        if (board[0] == 'X') {
            xWins++;

        }
        else if (board[0] == 'O') {
            oWins++;

        }
    }
    if (board[3] == board[4] && board[4] == board[5]) {
        if (board[3] == 'X') {
            xWins++;

        }
        else if (board[3] == 'O') {
            oWins++;
        }

    }
    if (board[6] == board[7] && board[7] == board[8]) {
        if (board[6] == 'X') {
            xWins++;

        }
        else if (board[6] == 'O') {
            oWins++;
        }

    }
    if (board[0] == board[3] && board[3] == board[6]) {
        if (board[0] == 'X') {
            xWins++;

        }
        else if (board[0] == 'O') {
            oWins++;

        }
    }
    if (board[1] == board[4] && board[4] == board[7]) {
        if (board[1] == 'X') {
            xWins++;

        }
        else if (board[1] == 'O') {
            oWins++;
        }

    }
    if (board[2] == board[5] && board[5] == board[8]) {
        if (board[2] == 'X') {
            xWins++;

        }
        else if (board[2] == 'O') {
            oWins++;

        }
    }
    if (board[0] == board[4] && board[4] == board[8]) {
        if (board[0] == 'X') {
            xWins++;

        }
        else if (board[0] == 'O') {
            oWins++;

        }
    }
    if (board[2] == board[4] && board[4] == board[6]) {
        if (board[2] == 'X') {
            xWins++;

        }
        else if (board[2] == 'O') {
            oWins++;

        }
    }
    if (positionsFilled == boardSize) {
        ties++;
        cout << endl << "    X WINS: " << xWins << endl;
        cout << "    O WINS: " << oWins << endl;
        cout << "    TIES " << ties;
        cout << endl;
        cout << "**********************************************" << endl;
        cout << endl;

        } while (playGameAgain());
        return 0;
}

//routine to let player x or o make a move and return the updated board
//parameter 'player' contains 'x' or 'o', indicating whose turn it is

string pickMove(string board, char player) {
    //prompt the current player for their move
    cout << endl;
    cout << "Player " << player << "\'s turn" << endl;
    cout << "Pick an unclaimed board position 0-8: ";

    //read in the players choice
    int moveChosen;
    cin >> moveChosen;

    //if not in the range 0-8
    if (moveChosen > 8 || moveChosen < 0) {
        cout << moveChosen << " is an invalid choice, ";
        cout << "your choice must be in the range 0-8" << endl;
        board = pickMove(board, player);
    }
    //if player chooses a spot already taken
    else if (!isdigit (board[moveChosen])) {
        cout << moveChosen << " is an invalid choice, ";
        cout << "you must choose a spot that isnt already taken";
        cout << endl;
        board = pickMove(board, player);
    }
    //otherwise put the players choice on the chosen spot
    else {
        board [moveChosen] = player;
        cout << endl;

        //return the updated board
        return board;
    }
}

//routine to display the current tic tac toe board
void printBoard(string board) {
    //display the top row of the board
    cout << endl << endl;
    cout << "     " << board[0];
    cout << "    |    " << board[1];
    cout << "    |    " << board[2];
    cout << "     " << endl;

    //display the middle row of the board
    cout << "   -------+---------+-------- " << endl;
    cout << "     " << board[3];
    cout << "    |    " << board[4];
    cout << "    |    " << board[5];
    cout << "     " << endl;

    //display the bottom row of the board
    cout << "   -------+---------+-------- " << endl;
    cout << "     " << board[6];
    cout << "    |    " << board[7];
    cout << "    |    " << board[8];
    cout << "     " << endl;
}

//return true if the game is over, false otherwise




Last edited on
The rest of the code..(to many characters..)
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
bool checkForGameEnd (string board) {
    //check if all board positions are full, in which case game is over

    //count the number of filled positions
    int positionsFilled = 0;

    for (int pos = 0; pos < boardSize; pos++) {
        if ((board[pos] == 'X' || (board[pos] == 'O'))) {
            positionsFilled++;
        }
    }

    if (board[0] == board[1] && board[1] == board[2]) {
        if (board[0] == 'X') {
            cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[0] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[3] == board[4] && board[4] == board[5]) {
        if (board[3] == 'X') {
            cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[3] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[6] == board[7] && board[7] == board[8]) {
        if (board[6] == 'X') {
            cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[6] == 'O') {
            cout << endl <<"GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[0] == board[3] && board[3] == board[6]) {
        if (board[0] == 'X') {
            cout << endl <<"GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[0] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[1] == board[4] && board[4] == board[7]) {
        if (board[1] == 'X') {
            cout << endl <<"GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[1] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[2] == board[5] && board[5] == board[8]) {
        if (board[2] == 'X') {
            cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[2] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[0] == board[4] && board[4] == board[8]) {
        if (board[0] == 'X') {
            cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[0] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (board[2] == board[4] && board[4] == board[6]) {
        if (board[2] == 'X') {
            cout << endl << "GAME OVER, PLAYER X WINS!" << endl;
            return true;
        }
        else if (board[2] == 'O') {
            cout << endl << "GAME OVER, PLAYER O WINS!" << endl;
            return true;
        }
    }
    if (positionsFilled == boardSize) {
        cout << endl << "GAME OVER, TIE GAME" << endl;
        return true;
    }





    //otherwise the game is not over
    return false;
}

bool playGameAgain(){
    char choice;
    cout << "Would you like to play again (Y or N) ?";
    cin >> choice;

    if ((choice == 'Y') || (choice == 'y')) {
        return true;
        cout << endl << endl;
    }
    else if ((choice == 'N') || (choice == 'n')) {
        return false;
    }

}
what compiler do you use? You have an issue with your curly brace.

On line 80 (1.part) there's a '}' which doesn't seem to belong to anything.
On line 162 (1.part) there's a '{', but no '}'.

This is a common issue with this style. I wonder why it's still in use
I use code blocks, not sure if its the best but its free..anyways, I tinkered with my code some more and finally have the program running and counting how many wins X or O has, and will continue to play again if you want it to.

Now I want to try and add a computer to play against, so Ill see what i come up with and post my errors(I just know ill get some...:P)

Thanks!
Topic archived. No new replies allowed.