PLEASE HELP ME! TIC TAC TOE

Please, I've been dancing around this code for probably 12 hours and I cant freakin crack it. I NEED this code done TONIGHT.... I'm creating tic tac toe for my class and dont know what I'm doing... this is my code. I have no idea how to while loop this so that the game will play all the way through. SOMEONE ANYONE assist me?!

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

using namespace std;

char board[] = {'.', '.','.', '.','.', '.','.', '.','.'};
int userchoice= 0;
int user2Choice= 0;
void printBoard(){
    cout << "  " << board[0] << " | " << board[1] << " | " << board[2] << endl;
    cout << "-------------" << endl;  
    cout << "  " << board[3] << " | " << board[4] << " | " << board[5] << endl;
    cout << "-------------" << endl;
    cout << "  " << board[6] << " | " << board[7] << " | " << board[8] << endl;
}

bool isValidChoice(int pos){
    if(pos < 0 || pos > 8 || board[pos] =='X' || board[pos] =='O'){
        return false;
    } else {
        return true;
    }
}

bool XsWin()
        {
          if(board[0] == 'X' && board[1] == 'X' && board[2] == 'X') 
		return true;
	else if(board[3] == 'X' && board[4] == 'X' && board[5] == 'X')
		return true;
	else if(board[6] == 'X' && board[7] == 'X' && board[8] == 'X')
		return true;
	else if(board[0] == 'X' && board[3] == 'X' && board[6] == 'X')
		return true;
	else if(board[4] == 'X' && board[1] == 'X' && board[7] == 'X')
		return true;
	else if(board[5] == 'X' && board[8] == 'X' && board[2] == 'X')
		return true;
	else if(board[0] == 'X' && board[4] == 'X' && board[8] == 'X')
		return true;
	else if(board[4] == 'X' && board[6] == 'X' && board[2] == 'X')
		return true;
	else
		return false;
        }

bool OsWin()
        {
          if(board[0] == 'O' && board[1] == 'O' && board[2] == 'O') 
		return true;
	else if(board[3] == 'O' && board[4] == 'O' && board[5] == 'O')
		return true;
	else if(board[6] == 'O' && board[7] == 'O' && board[8] == 'O')
		return true;
	else if(board[0] == 'O' && board[3] == 'O' && board[6] == 'O')
		return true;
	else if(board[4] == 'O' && board[1] == 'O' && board[7] == 'O')
		return true;
	else if(board[5] == 'O' && board[8] == 'O' && board[2] == 'O')
		return true;
	else if(board[0] == 'O' && board[4] == 'O' && board[8] == 'O')
		return true;
	else if(board[4] == 'O' && board[6] == 'O' && board[2] == 'O')
		return true;
	else
		return false;
        }


bool isTie(){
    if (XsWin()) return false;
    else if (OsWin()) return false;
    else if (board[0] =='X' or 'O' && board[1] =='X' or 'O' && board[2] =='X' or 'O' && board[3] =='X' or 'O' && board[4] =='X' or 'O' && board[5] =='X' or 'O' && board[6] =='X' or 'O' && board[7] =='X' or 'O' && board[8] =='X' or 'O') return true;
    else return false;
    }

int main ()
{
while(XsWin() && OsWin())
{
	userchoice;
	printBoard();
	cout << endl;
	
	if(XsWin() && OsWin()!=true);
	else break;
}

cout << "Xs til Death"  << endl;
printBoard();

cout << "Play your Position 1-9: ";
int userChoice;
do {
cin >> userChoice;
userChoice--;
} while (!isValidChoice(userChoice));

board[userChoice] = 'X';
printBoard();

cout << "Play your Position 1-9: ";
int user2Choice;
do {
cin >> user2Choice;
user2Choice--;
} while (!isValidChoice(user2Choice));

board[user2Choice] = 'O';
printBoard();

{

}
}
somehow you need to know if the game is won or drawn. Its won if 3 things connect, and drawn if all 9 squares are filled without a win. If neither of those are true, keep looping.

so you need a function that returns a code for win/draw/ongoing that you can use to control the loop.

Take a deep breath. You're actually very close to having this right.

Line 76 is wrong. Due to order of operations, the compiler interprets this as:
else if ((board[0] =='X') or ('O' && board[1] =='X') or ('O' && board[2] =='X') or ...
Since 'O' is not zero, it's always interpretted as true, so it can be somplified to:
else if ((board[0] =='X') or ( board[1] =='X') or (board[2] =='X') or ...

What you really want to know is if any of the squares are '.':
1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool
isTie()
{
    if (XsWin())
        return false;
    else if (OsWin())
        return false;
    else {
        for (int i=0; i<9; ++i) {
            if (board[i] == '.') return false;
        }
        return true;
    }
}



For the main program, you have to get a move, check for a winner, and check for a tie. Then get the next move and do the same:
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
int
main()
{
    while (true) {
        // Print the board
        cout << "Xs til Death" << endl;
        printBoard();

        // Get X's move
        cout << "Play your Position 1-9: ";
        int userChoice;
        do {
            cin >> userChoice;
            userChoice--;
        } while (!isValidChoice(userChoice));

        board[userChoice] = 'X';

        // Print the board
        printBoard();

        if (XsWin()) {
            cout << "X wins!\n";
            break;
        } else if (isTie()) {
            cout << "Tie game\n";
            break;
        }


        // Get O's move
        cout << "Play your Position 1-9: ";
        int user2Choice;
        do {
            cin >> user2Choice;
            user2Choice--;
        } while (!isValidChoice(user2Choice));

        board[user2Choice] = 'O';

        // Print the board
        printBoard();

        if (OsWin()) {
            cout << "O wins!\n";
            break;
        } else if (isTie()) {
            cout << "Tie game\n";
            break;
        }
    }
}


This code works but it's inefficient. There's lots of duplication. Get the program working, save the file and then consider this:

It would be better to keep a variable that represents the current player and then have each pass through the loop do one player's move:

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
int
main()
{
    char player = 'X';
    while (true) {
        // Print the board
        printBoard();

        // Get move
        cout << player << "'s move Position 1-9: ";
        int userChoice;
        do {
            cin >> userChoice;
            userChoice--;
        } while (!isValidChoice(userChoice));

        board[userChoice] = player;
        if (Wins(player)) {
            cout << player << " wins!\n";
            break;
        } else if (isTie()) {
            cout << "Tie game.\n";
            break;
        }

        // switch players
        if (player == 'X') {
            player = 'O';
        } else {
            player = 'X';
        }
    }
    // Show the final board
    printBoard();
}

Can you change the rest of the code so it works with this main()?
Topic archived. No new replies allowed.