Tic Tac Toe?

Figured it out.
Last edited on
Anyone? :(
closed account (EwCjE3v7)
On line 110, did you mean if (!validPlacement(board, position))
instead of
if !(validPlacement(board, position))

I will try to see if I can fix it

EDIT: Can`t really fix it but I`ll give you my code if that helps

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
#include <iostream>
#include <string>
using std::cout; using std::endl; using std::cin;
using std::string;
int main();
// function to call when there is a winner
void winner(char places2[9], string players2[2]) // get the player name and the current game board
{
    string playagn = "choice"; // holding the string, if user wants to player again
    cout << "_                  _  _    _      _   _      _   _______   _ _____\n" // Print giant WINNER
            << "\\\\       __       // | |  |  \\   | | |  \\   | | | ______| | |  _  \\\n"
            << " \\\\     //\\\\     //  | |  | |\\\\  | | | |\\\\  | | ||______  | |_____/\n"
            << "  \\\\   //  \\\\   //   | |  | | \\\\ | | | | \\\\ | | ||______| | |\\ \\\n"
            << "   \\\\_//    \\\\_//    | |  | |  \\\\| | | |  \\\\| | ||______  | | \\ \\\n"
            << "    \\_/      \\_/     |_|  |_|   \\ _| |_|   \\ _| |_______| |_|  \\_\\_"
            << "\n\nThe winner is " << players2[0] << ".\n" // announce winner
            << "\n\t" << places2[0] << " " << "|" << " " << places2[1] << " " << "|" << " "  << places2[2] << "\n\t---------\n" // print the board
            << "\t" << places2[3] << " " << "|" << " " << places2[4] << " " << "|" << " "  <<  places2[5] << "\n\t---------\n"
            << "\t" << places2[6] << " " << "|" << " " << places2[7] << " " << "|" << " "  <<  places2[8]
            << "\n\nWould you like to play again? (yes or no)" << string( 8, '\n' ) << endl; // Tell user if he/she wants to play again
    cin >> playagn; // get input
    if (playagn == "yes") { // if playagn is yes
        std::swap(players2[0], players2[1]);
        main(); // call the main functions, restart the game
    } else {
        cout << "Okay bye." << endl; // else say bye and finish the program
    }
}
// function to call if its a draw
void draw(char places3[9]) { // get the game board
    string playagn2 = "choice"; // holding the string, if user wants to player again
    cout << " ________   _ _____     ___   _                  _ \n|   ___  \\ | |  _  \\   // \\\\  \\\\       __       //\n" // Print DRAW
            << "|  |   \\  || |_____/  //   \\\\  \\\\     //\\\\     // \n|  |    | || |\\ \\    ||_____||  \\\\   //  \\\\   // \n"
            << "|  |___/  || | \\ \\   ||_____||   \\\\_//    \\\\_// \n|________/ |_|  \\_\\_ ||     ||    \\_/      \\_/ \n\n"
            << "It was a draw.\n\t"
            << places3[0] << " " << "|" << " " << places3[1] << " " << "|" << " "  << places3[2] << "\n\t---------\n" // Print the board
            << "\t" << places3[3] << " " << "|" << " " << places3[4] << " " << "|" << " "  <<  places3[5] << "\n\t---------\n"
            << "\t" << places3[6] << " " << "|" << " " << places3[7] << " " << "|" << " "  <<  places3[8]
            << "\n\nWould you like to play again? (yes or no)" << string( 8, '\n' ) << endl; // Ask user if he/she wants to play again
    cin >> playagn2; // get input
    if (playagn2 == "yes") { // if playagn is yes
        main(); // restart the game
    } else {
        cout << "Okay bye." << endl; // else say bye and end program
    }
}

int main()
{
    string players[2] = {"Player1", "Player2"}; // For holding user names
    cout << "How to play: \n1. Enter players names\n2. Enter choice 1-9 \n\nEnter first player`s name: ";
    cin >> players[0]; // get first user`s name
    cout << "\nEnter second player`s name: ";
    cin >> players[1]; // get second user`s name

    char places[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; // Representing the board
    unsigned choice = 0;
    unsigned counter = 0; // counter for incase it is a draw

    while (true) {
        cout << string( 9, '\n' ) << "\t" << places[0] << " " << "|" << " " << places[1] << " " << "|" << " "  << places[2] << "\n\t---------\n" // Print board
                << "\t" << places[3] << " " << "|" << " " << places[4] << " " << "|" << " "  <<  places[5] << "\n\t---------\n"
                << "\t" << places[6] << " " << "|" << " " << places[7] << " " << "|" << " "  <<  places[8] << "\n"
                << "\nIt is " << players[0] << "`s turn: ";
        cin >> choice; // get first players choice
        // Check if choice is valid
        if (places[choice -1] != 'o') {
            places[choice -1] = 'x';
            ++counter;
        } else {
            cout << "Don`t try to cheat, start again.\n" << endl;
            main();
        }
        // check if there is a winner or its a draw
        if (counter == 9) {
            draw(places);
            return 0;
        }
        else if (places[0] == 'x' && places[4] == 'x' && places[8] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[2] == 'x' && places[4] == 'x' && places[6] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[1] == 'x' && places[4] == 'x' && places[7] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[3] == 'x' && places[4] == 'x' && places[5] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[0] == 'x' && places[1] == 'x' && places[2] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[6] == 'x' && places[7] == 'x' && places[8] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[2] == 'x' && places[5] == 'x' && places[8] == 'x') {
            winner(places, players);
            return 0;
        }
        else if (places[0] == 'x' && places[3] == 'x' && places[6] == 'x') {
            winner(places, players);
            return 0;
        }


        cout << string( 9, '\n' ) << "\t" << places[0] << " " << "|" << " " << places[1] << " " << "|" << " "  << places[2] << "\n\t---------\n" // print board
                << "\t" << places[3] << " " << "|" << " " << places[4] << " " << "|" << " "  <<  places[5] << "\n\t---------\n"
                << "\t" << places[6] << " " << "|" << " " << places[7] << " " << "|" << " "  <<  places[8] << "\n"
                << "\nIt is " << players[1] << "`s turn: ";
        cin >> choice; // get second players choice
        // check if input is valid
        if (places[choice -1] != 'x') {
            places[choice -1] = 'o';
            ++counter;
        } else {
            cout << "Don`t try to cheat, start again.\n" << endl;
            main();
        }
        // check if there are any winners or if its a draw
        if (counter == 9) {
            draw(places);
            return 0;
        }
        else if (places[0] == 'o' && places[4] == 'o' && places[8] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[2] == 'o' && places[4] == 'o' && places[6] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[1] == 'o' && places[4] == 'o' && places[7] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[3] == 'o' && places[4] == 'o' && places[5] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[0] == 'o' && places[1] == 'o' && places[2] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[6] == 'o' && places[7] == 'o' && places[8] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[2] == 'o' && places[5] == 'o' && places[8] == 'o') {
            winner(places, players);
            return 0;
        }
        else if (places[0] == 'o' && places[3] == 'o' && places[6] == 'o') {
            winner(places, players);
            return 0;
        }
    }

    return 0;
}
Last edited on
Unfortunately I cannot use your code because my tic tac toe program has to be specifically like how I presented it. It's due at 9PM today. Ughhhh.

And yes, I did mean (!validPlacement(board, position)).
Last edited on
closed account (EwCjE3v7)
Sorry, I can`t help. You just have to wait till the pros are on :D. Your 9 PM is probably different to mine :D
Topic archived. No new replies allowed.