Tic Tac Toe game functions

I'm having trouble with the part where I set up the play, switch player, and then outputting results?

I don't know if I got the getPlay function right because I don't see it changing the "value" of board in any way..

Any suggestions? Please and thank you!

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
#include <vector>
#include <iostream>

using namespace std;

const bool CLEAR_SCREEN = true;

/// @brief Utilizes an escape character sequence to clear the screen
void clearScreen()
{
    cout << endl;

    if (CLEAR_SCREEN)
    {
        cout << "\033c";
    }

    cout << endl;
}


/// @brief Draws the provided tic-tac-toe board to the screen
//  @param board is the tic-tac-toe board that should be drawn
void drawBoard(const vector < char >&board)
{
    clearScreen();
    for (int i = 0; i < 9; i += 3)
    {
        cout << "  " << board.at(i) << "  |  " << board.at(i + 1) << "  |  "
            << board.at(i + 2) << "  " << endl;
        if (i < 6)
            cout << "-----|-----|-----" << endl;
    }
    cout << endl;
}



/// @brief Fills vector with characters starting at lower case a.
///
///     If the vector is size 3 then it will have characters a to c.
///     If the vector is size 5 then it will have characters a to e.
///     If the vector is size 26 then it will have characters a to z.
///
/// @param v the vector to initialize
/// @pre-condition the vector size will never be over 26
void initVector(vector<char> &v)
{
    string mapA = "abcdefghijklmnopqrstuvwxyz";
    
    for (int i = 0; i < v.size(); i++)
    {
        v.at(i) = mapA.at(i);
    }
}

/// @brief Converts a character representing a cell to associated vector index
/// @param the position to be converted to a vector index
/// @return the integer index in the vector, should be 0 to (vector size - 1)
int convertPosition(char position)
{
    string mapA = "abcdefghijklmnopqrstuvwxyz";
    
    return mapA.find(position);
}


/// @brief Predicate function to determine if a spot in board is available.
/// @param board the current tic-tac-toe board 
/// @param position is an index into vector to check if available
/// @return true if position's state is available (not marked) AND is in bounds
bool validPlacement(const vector <char> &board, int position)
{
   if (position > 8 || position < 0 || 
        board.at(position) == 'X' || board.at(position) == 'O')
    {
        return false;
    }
    else
    {
        return true;
    }
}

/// @brief Acquires a play from the user as to where to put her mark
///
///     Utilizes convertPosition and validPlacement functions to convert the
///     user input and then determine if the converted input is a valid play.
///
/// @param board the current tic-tac-toe board 
/// @return an integer index in board vector of a chosen available board spot
int getPlay(const vector <char> &board)
{
    char play;
    
    cout << "Please enter a position: ";
    cin >> play;
    cout << endl;
    
    int pos = convertPosition(play);
    
    if (validPlacement(board, pos))
    {
        return pos;
    }
}


/// @brief Predicate function to determine if the game has been won
///
///     Winning conditions in tic-tac-toe require three marks from same 
///     player in a single row, column or diagonal.
///
/// @param board the current tic-tac-toe board 
/// @return true if the game has been won, false otherwise
bool gameWon(const vector <char> &board)
{
    // TODO: implement function
    return false;
}


/// @brief Predicate function to determine if the board is full
/// @param board the current tic-tac-toe board 
/// @return true iff the board is full (no cell is available)
bool boardFull(const vector <char> &board)
{
    // TODO: implement function
    return false;
}


// Global constants for player representation
const int PLAYER1 = 0;
const int PLAYER2 = 1;

int main()
{
    // Variables that you may find useful to utilize
    vector <char> board(9);
    int curPlay;
    int turn = PLAYER1; // Player 1 always goes first and is 'X'

    /// TODO: Initialize board to empty state
    initVector(board);


    /// TODO: Display empty board
    
    drawBoard(board);
    

    /// TODO: Play until game is over

        /// TODO: Get a play
        getPlay(board);

        /// TODO: Set the play on the board
        

        /// TODO: Switch the turn to the other player
        
        /// TODO: Output the updated board


    /// TODO: Determine winner and output appropriate message

    return 0;
}
Last edited on
Help anyone? :(
getPlay() doesn't change anything of the board (it's const, you cannot change it.
you need to return something (-1) if it's not validPlacement()

So you set X/O (according to turn) to the board if getPlay() returns a positve value and then you need to call drawBoard() (Output the updated board)

Update:

Figured out how to do the switching of turns and getPlay

I can't figure out the gameWon function though without literally writing if board.at(0), board.at(1), board.at(2), etc.
Last edited on
For gameWon you can make an array. Since I can not run your program right off the bat, I'll just show you how I made one.

pos[0]" │ " << pos[1] │ pos[2]
──┼───-----------┼──----\n"
pos[3] │ " << pos[4] │ pos[5] // just showing you the design and values of
──┼─----------─-─┼──------ // each spot of my board code for this design
pos[6] │ pos[7] │ pos[8] // below for checking for winner

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
bool TicTacToe::Winner()
{
    int board[8][3] = {{0,1,2},
                                 {3,4,5},
                                 {6,7,8}, // setting up two arrays
                                 {0,3,6},
                                 {1,4,7},
                                 {2,5,8},
                                 {0,4,8},
                                 {2,4,6}}; // winning possibilities

    for (int i = 0; i < 8; i++) // will check if any winning combinations of 1 and two exist after each turn
    {
        if ((pos[board[i][0]] == pos[board[i][1]]) //if value of that spot is == to value of next spot 1 being human 2 being computer
            && (pos[board[i][1]] == pos[board[i][2]]) 
            && pos[board[i][0]] != 0)
        {

            std::cout << "\nPlayer " << pos[board[i][0]]
                      <<  " wins!\n\n";

            return 1;    // return winner true
        }
    }
	
    return 0;   // winner false 


note* the for statement with == is for if the value stored at the positions of the array earlier are all 1 for human player and 2 for computer. So whatever value you pass into the element for that spot on the board must be == to the value passed in the next position for a winning line.

Last edited on
Topic archived. No new replies allowed.