Need help with player turns

I'm having trouble changing players from X to O. I've tried many combinations but I'm at a loss. ROWS and COLS are constants equal to 3. Basically my problem is that I want to start with Player X and then switch to player O. If I can figure that out, my project is practically done. Any help would be appreciated. Here is the code containing the function I'm working with:

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
void    UpdateBoard(char Board[][COLS])
{

    auto    char    player('X');
    auto    char    playerMark;
    auto    int     row;
    auto    int     col;
    auto    char    response;

    if (player == 'X')
        {
        playerMark = 'X';
        }
    cout << "Player " << player << ": ";
    cin >> response;

    cout << endl;

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            if (Board[row][col] == response)
                {
                Board[row][col] = playerMark;
                }
            }
        }

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            cout << Board[row][col] << setw(2);
            }
        cout << setw(1) << endl;
        }
    cout << endl;
}


EDIT: For clarity, this function will display the 2-D array with an X or O marked on the matrix
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int turn = ...; //0 for x, 1 for o
turn = -turn + 1; //change turn (toggles between, 0 and 1)

//or

int turn = ...; //1 for x, 2 for o
turn = -turn + 3; //change turn (toggles between, 1 and 2)

//or

char turn = ...; //'x' for x, 'o' for o
turn = turn == 'x' ? 'o' : 'x'; //change turn (toggles between, 'x' and 'o') 


1
2
3
4
5
6
7
8
//for games with more then two players

//start
const int PLAYERS = ...;
int turn = 0;

//next turn
turn = (turn + 1) % PLAYERS;
Last edited on
I don't quite understand how that code works. What are the periods for?
Last edited on
I changed my code, but it still won't switch over to 'O'. It just stays on 'X'

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
void    UpdateBoard(char Board[][COLS])
{

    auto    int     player = 0;
    auto    char    playerMark;
    auto    int     row;
    auto    int     col;
    auto    char    response;

    cout << "Player ";

    if (player == 1)
        {
        cout << "O";
        playerMark = 'O';
        --player;
        }
    else if (player == 0);
        {
        cout << "X";
        playerMark = 'X';
        ++player;
        }

    cout << ": ";
    cin >> response;

    cout << endl;

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            if (Board[row][col] == response)
                {
                Board[row][col] = playerMark;
                }
            }
        }

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            cout << Board[row][col] << setw(2);
            }
        cout << setw(1) << endl;
        }
    cout << endl;
}

Last edited on
basically i think u have to put it in a loop and alternate player X or O around...you can use the if else statement to do it and end the loop when it detect all the boxes are being filled....
Brianzor
What are the periods for?
Lol, fill in the blank. It just means you initialized the variables to something, but what value it gets is up to you.

Toshitaka
basically i think u have to put it in a loop...
Yea. You are making the player 'X', but then you dont ever run that code again. you want line 12-23 in the loop that runs your game, so every turn, the player changes.
Last edited on
Ahhh, let me post all the code I have so far, the do/while in main is infinite for testing purposes. The turns should be looping.

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

void    DispBoard(char myGrid[][COLS]);
void    UpdateBoard(char Board[][COLS]);

int main(void)
{
    auto    char    myBoard[ROWS][COLS];
    auto    bool    bGameOver;

    cout << "****************************************" << endl;
    cout << "*** Welcome to the TIC-TAC-TOE game! ***" << endl;
    cout << "****************************************" << endl;
    cout << "Enter a target digit to select a cell, " << endl;
    cout << "or 'Q' to quit..." << endl << endl;

    DispBoard(myBoard);

    cout << endl;

    do
    {

    UpdateBoard(myBoard);

    }while(true);

    return 0;
}

void    DispBoard(char myGrid[][COLS])
{
    auto    int     row;
    auto    int     col;
    auto    char    value = 48;

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            myGrid[row][col] = ++value;
            cout << myGrid[row][col] << setw(2);
            }
        cout << setw(1) << endl;
        }
}

void    UpdateBoard(char Board[][COLS])
{

    auto    int     player = 0;
    auto    char    playerMark;
    auto    int     row;
    auto    int     col;
    auto    char    response;

    cout << "Player: ";

    if (player == 1)
        {
        cout << "O";
        playerMark = 'O';
        --player;
        }
    else if (player == 0);
        {
        cout << "X";
        playerMark = 'X';
        ++player;
        }

    cout << ": ";
    cin >> response;

    cout << endl;

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            if (Board[row][col] == response)
                {
                Board[row][col] = playerMark;
                }
            }
        }

    for (row = 0; row < ROWS; ++row)
        {
        for (col = 0; col < COLS; ++col)
            {
            cout << Board[row][col] << setw(2);
            }
        cout << setw(1) << endl;
        }
    cout << endl;
}
Topic archived. No new replies allowed.