Not saving input and updating.

Pages: 123
All right I sent my code to your inbox.
board.cpp
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
237
238
239
240
241
242
243
244
245
246
247
#include <iostream>
#include <limits>
#include "Board.h"

using namespace std;

Board::Board()
{
    for(unsigned int i = 0; i < 3; i++)
        for(unsigned int j = 0; j < 3; j++)
            board[i][j] = ' ';
}

void Board::draw()
{
    for(unsigned int i = 0; i < 3; i++)
    {
        cout << "\t\t";
        for(unsigned int j = 0;  j < 3;  j++)
        {
            // Display the board.
            cout << "|" << board[i][j] << "|";
        }
        cout << endl; // Create a space between each row and column.
    }

}


void Board::choosingLetter()
{
    // Prompt the user to choose the letter X or O
    cout << "\nPlayer 1, which letter would you like to start as? X or O? ";
    cout << endl;

    cout << "\n*Note X or O are the only acceptable inputs. ";
    cin >> PlayerLetter1;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    // Check for valid input.
    while(PlayerLetter1 != 'X' && PlayerLetter1 != 'O')
    {
        cerr << "\nError, invalid input. ";
        cin >> PlayerLetter1;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    // If player 1 chooses X, then player2 will be O and vice versa.
    if(PlayerLetter1 == 'X')
        PlayerLetter2 = 'O';
    else if(PlayerLetter1 == 'O')
        PlayerLetter2 = 'X';
}

void Board::playerMove1()
{
    cout << endl;

    // Commands that dictates where the letter will be placed.
    cout << "\t\t" << "Legend Key" << endl << endl;

    cout << "UL=UpperLeft  UC=UpperCenter   UR=UpperRight " << endl;
    cout << "LC=LeftCenter MC=MiddleCenter  RC=RightCenter " << endl;
    cout << "BL=BottomLeft BM=BottomMiddle  BR=BottomRight " << endl;

    cout << endl;

    // Prompting Player1.
    cout << "\nPlayer 1, pick a field. ";

    // Checks for valid letter placement and non duplicate locations.
    playerLocationCheck1();
}

void Board::playerMove2()
{
    cout << endl;

    // Commands that dictates where the letter will be placed.
    cout << "\t\t" << "Legend Key" << endl << endl;

    cout << "UL=UpperLeft  UC=UpperCenter   UR=UpperRight " << endl;
    cout << "LC=LeftCenter MC=MiddleCenter  RC=RightCenter " << endl;
    cout << "BL=BottomLeft BM=BottomMiddle  BR=BottomRight " << endl;

    cout << endl;

    // Prompting Player2.
    cout << "\nPlayer 2, pick a field. ";

    // Checks for valid letter placement and non duplicate locations.
    playerLocationCheck2();
}

void Board::playerInputCheck1()
{
    cin >> playerInput1;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    while(playerInput1 != "UL" && playerInput1 != "UC" && playerInput1 != "UR"
       && playerInput1 != "LC" && playerInput1 != "MC" && playerInput1 != "RC"
       && playerInput1 != "BL" && playerInput1 != "BM" && playerInput1 != "BR")
    {
        cerr << "\nError, unknown command entered. Read the legend key. ";
        cin >> playerInput1;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

void Board::playerInputCheck2()
{
    cin >> playerInput2;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    while(playerInput2 != "UL" && playerInput2 != "UC" && playerInput2 != "UR"
       && playerInput2 != "LC" && playerInput2 != "MC" && playerInput2 != "RC"
       && playerInput2 != "BL" && playerInput2 != "BM" && playerInput2 != "BR")
    {
        cerr << "\nError, unknown command entered. Read the legend key. ";
        cin >> playerInput2;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

void Board::playerLocationCheck1()
{
    bool isEmpty;

    while(!isEmpty)
    {
        // Checks for valid commands.
        playerInputCheck1();

        if(playerInput1=="UL" && (board[0][0] != 'X' && board[0][0] != 'O'))
        {
            board[0][0] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="UC" && (board[0][1] != 'X' && board[0][1] != 'O'))
        {
            board[0][1] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="UR" && (board[0][2] != 'X' && board[0][2] != 'O'))
        {
            board[0][2] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="LC" && (board[1][0] != 'X' && board[1][0] != 'O'))
        {
            board[1][0] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="MC" && (board[1][1] != 'X' && board[1][1] != 'O'))
        {
            board[1][1] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="RC" && (board[1][2] != 'X' && board[1][2] != 'O'))
        {
            board[1][2] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="BL" && (board[2][0] != 'X' && board[2][0] != 'O'))
        {
            board[2][0] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="BM" && (board[2][1] != 'X' && board[2][1] != 'O'))
        {
            board[2][1] = PlayerLetter1;
            isEmpty = true;
        }
        else if(playerInput1=="BR" && (board[2][2] != 'X' && board[2][2] != 'O'))
        {
            board[2][2] = PlayerLetter1;
            isEmpty = true;
        }
        else
        {
            cerr << "\nError, this position has already been taken Try again. ";
            isEmpty = false;
        }
    }
}

void Board::playerLocationCheck2()
{
    bool isEmpty;

    while(!isEmpty)
    {
        // Checks for valid commands.
        playerInputCheck2();

        if(playerInput2=="UL" && (board[0][0] != 'X' && board[0][0] != 'O'))
        {
            board[0][0] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="UC" && (board[0][1] != 'X' && board[0][1] != 'O'))
        {
            board[0][1] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="UR" && (board[0][2] != 'X' && board[0][2] != 'O'))
        {
            board[0][2] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="LC" && (board[1][0] != 'X' && board[1][0] != 'O'))
        {
            board[1][0] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="MC" && (board[1][1] != 'X' && board[1][1] != 'O'))
        {
            board[1][1] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="RC" && (board[1][2] != 'X' && board[1][2] != 'O'))
        {
            board[1][2] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="BL" && (board[2][0] != 'X' && board[2][0] != 'O'))
        {
            board[2][0] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="BM" && (board[2][1] != 'X' && board[2][1] != 'O'))
        {
            board[2][1] = PlayerLetter2;
            isEmpty = true;
        }
        else if(playerInput2=="BR" && (board[2][2] != 'X' && board[2][2] != 'O'))
        {
            board[2][2] = PlayerLetter2;
            isEmpty = true;
        }
        else
        {
            cerr << "\nError, this position has already been taken Try again. ";
            isEmpty = false;
        }
    }
}
Last edited on
continue board.cpp

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
bool Board::isGameOver(int &tileCounter, int &playerWins1, int &PlayerLose1, int &playerWins2, int &playerLose2, int &tie)
{
    bool gameOver1 = false; // If there are 3 X's.
    bool gameOver2 = false; // If there are 3 O's.
    bool tieGame = false;
    bool noGameOver; // If there are no 3 X's or 3 O's.


    if(board[0][0] == 'X' && board[0][1] == 'X' && board [0][2] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[1][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[2][0] == 'X' && board[2][1] == 'X' && board [2][2] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[0][0] == 'X' && board[1][0] == 'X' && board [2][0] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[0][1] == 'X' && board[1][1] == 'X' && board [2][1] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[0][2] == 'X' && board[1][2] == 'X' && board [2][2] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[0][0] == 'X' && board[1][1] == 'X' && board [1][2] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[0][2] == 'X' && board[1][1] == 'X' && board [2][0] == 'X')
    {
        gameOver1 = true;
    }
    else if(board[0][0] == 'O' && board[0][1] == 'O' && board [0][2] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[1][0] == 'O' && board[1][1] == 'O' && board [1][2] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[2][0] == 'O' && board[2][1] == 'O' && board [2][2] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[0][0] == 'O' && board[1][0] == 'O' && board [2][0] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[0][1] == 'O' && board[1][1] == 'O' && board [2][1] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[0][2] == 'O' && board[1][2] == 'O' && board [2][2] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[0][0] == 'O' && board[1][1] == 'O' && board [1][2] == 'O')
    {
        gameOver2 = true;
    }
    else if(board[0][2] == 'O' && board[1][1] == 'O' && board [2][0] == 'O')
    {
        gameOver2 = true;
    }

    if(gameOver1 == true)
    {
        if(PlayerLetter1 == 'X')
        {
            playerWins1++;
            playerLose2++;
            cout << "Game over! Player 1 is the victor! " << endl;
            return gameOver1;
        }
        else if(PlayerLetter2 == 'X')
        {
            playerWins2++;
            PlayerLose1++;
            cout << "Game over! Player 2 is the victor! " << endl;
            return gameOver1;
        }
    }
    else if(gameOver2 == true)
    {
        if(PlayerLetter1 == 'O')
        {
            playerWins1++;
            playerLose2++;
            cout << "Game over! Player 1 is the victor! " << endl;
            return gameOver2;
        }
        else if(PlayerLetter2 == 'O')
        {
            playerWins2++;
            PlayerLose1++;
            cout << "Game over! Player 2 is the victor! " << endl;
            return gameOver2;
        }
    }
    else if(tileCounter == 9)
    {
        tieGame = true;
        tie++;
        cout << "Tie game! No one wins! " << endl;
        return tieGame;
    }
    else if(gameOver1 && gameOver2 == false)
    {
        noGameOver = false;
    }

    return noGameOver;
}

void Board::refresh()
{
    char newTile = ' '; // gets rid of all the X's and O's

    for(unsigned int i = 0; i < 3; i++)
        for(unsigned int j = 0; j < 3; j++)
            board[i][j] = newTile;
}

void Board::playAgain(char &playerResponse1, char &playerResponse2)
{
        cout << "\n\nWould you like to play again player1? [y/n] ";
        cin >> playerResponse1;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        while(toupper(playerResponse1) != 'Y' && toupper(playerResponse1) != 'N')
        {
            cerr << "\nError, Invalid response. ";
            cin >> playerResponse1;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        cout << "\n\nWould you like to play again player2? [y/n] ";
        cin >> playerResponse2;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        while(toupper(playerResponse2) != 'Y' && toupper(playerResponse2) != 'N')
        {
            cerr << "\nError, Invalid response. ";
            cin >> playerResponse2;
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        if(toupper(playerResponse1) == 'Y' && toupper(playerResponse2) == 'Y')
        {
            cout << "\n\nCreating a new board to play on. " << endl;
            refresh(); // refreshes the board.
        }
        else
            cout << "\n\nThanks for playing! " << endl;
}

void Board::scoreBoard(int &playerWins1, int &PlayerLose1, int &playerWins2, int &playerLose2, int &tie)
{
    cout << "\t\t\tScoreBoard " << endl << endl;
    cout << "--------------------------------------------------------------" << endl;
    cout << "                \t\tWins\t\tLosses " << endl << endl;

    cout << "player 1: " << "\t\t\t" << playerWins1 << "\t\t" << PlayerLose1 << endl;

    cout << "player 2: " << "\t\t\t" << playerWins2 << "\t\t" << playerLose2 << endl << endl;

    cout << "Number of tie games: " << tie << endl;
    cout << "--------------------------------------------------------------" << endl;
}
Last edited on
1
2
3
4
5
6
    void playerMove1();
    void playerMove2();
    void playerInputCheck1();
    void playerInputCheck2();
    void playerLocationCheck1();
    void playerLocationCheck2();

you can make this into just 3 func.
just for advice
1
2
3
void playerMove();
void playerInputCheck();
void playerLocationCheck();
Yes, I could do that. Thanks for the advice.
For the most part I'm done. All I want to do is refresh the board once the game is done. Any ideas? Also, don't forget to offer any improvements. Check my main.cpp and board.h in the main page and the boar.cpp at the top of this page.
Last edited on
Anyone know how to refresh the tic tac toe board after you finish a match?
1
2
3
4
5
6
7
8
9
void refresh() {
int tiles = '1';
for ( int a = 0 a < 3; a++ ) {
   for ( int b = 0; b < 3; b++ ) {
   board[a][b] = tiles;
++tiles;
}
}
}


Last edited on
Alright, fixed it. Thanks lorence30, it was so simple. Now, my final addition to this project is to add a computer AI.
Last edited on
Sorry typo. It should be char tiles
Don't worry.
Try search about "game state" you'll found about on how to code an AI. But since its just a 3x3 board you can just generate a random move but you should limit the range of move. Instance, on the first move you can generate possible move from 1-9 (assume computer TURN first)
1
2
3
12X //compter pic 3 from rand move
456
789

And then
1
2
3
12X
4O6
789

Computer's turn again.
At this rate you have to limit the range of the random moves.
The computer should only have pick tile 1 or 2 or 6 or 9...
and so on.



Last edited on
Thanks, you made an excellent point. Also, the way I have it setup is that player 1 is always a human player and player 2 is either a human player or a computer player. I could add in an event where player 1 and player2(or the computer) flip a coin and decide who will go first. The general structure that I go with is that player 1 goes first, then player 2(or computer) goes next.
Though the problem is that I'm not using numbers; I'm using commands.

Couldn't I use the same function that I have player 2 for the computer AI. If the computer has to return a command, and the command that the computer returns is invalid, it would know that position has already been taken and would try another command.
You could also random it.
Example:
flip coin
Flip coin has only 2 possible generation.
1 for heads and 1 for tails
then you decide if who's going to bet for heads or tails.
then flip the coin.
Though the problem is that I'm not using numbers; I'm using commands

What do you mean?

Couldn't I use this. Same function that I have player 2 for the computer AI

isn't player 2 should also be controlled by a player?
But if you're saying about detecting the valid moves. Yes you could.
If you go back to my board.cpp(the first post in this page), you would see that I'm using commands like UL, UC, UR, that represents the location of the tile.

What I meant about having the same function is, couldn't I use the same general structure that I did for player2 for the computer AI.

Anyways. I need to add in a way to tie the game and a scoreboard.

Update: Ok I've added a way to tie the game.
Last edited on
Oh sorry I didn't see your code
well, I see, in that case you can store the commands in an array.
by the way you can also use it(numbers) even you're using commands
Example:
commands[]={//commands}
Again the random move for computer move ,let say, 3.
Loop the board until it goes to [0][2]
then the computer move ,which is UR, will fill the board
Last edited on
You could.
Good job then
Pages: 123