TicTacToe

I'm getting this weird glitch and I have no idea how to fix it. Here's the code:
note:It seems like P2 is losing its string.
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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void new_game(int i, char S[]){
    for (i=1;i<=9;i++){
    S[i] = (char)(((int)'0')+i);
    }
}
void board(char S[]){
    cout << S[1] << "|" << S[2] << "|" << S[3] << endl;
    cout << "-+-+-"<< endl;
    cout << S[4] << "|" << S[5] << "|" << S[6] << endl;
    cout << "-+-+-"<< endl;
    cout << S[7] << "|" << S[8] << "|" << S[9] << endl;
}
int main()
{
    srand(time(NULL));
    string P1;
    string P2;
    int i;
    char S[9];
    bool compON; //false = not on
    char compC;
    cout << "Do you want to use a computer(1 Player)?(y, n)";
    cin >> compC;
    if(compC == 'y'){
        compON = true; // turns computer AI on EASY mode
        cout << "Player 1's name(X):";
        cin >> P1;
        cout << "Computer is O." << endl;
    } else {
        cout << "Player 1's name(X):";
        cin >> P1;
        cout << "Player 2's name(O):";
        cin >> P2;
    }
    new_game(i, S);
    int pTurn = 1; //if pTurn = 1 = player one and if pTurn = 2 = player two
    bool gameOff = true; // sets gameOff to not play again
    char pMark; // player mark / computer mark
    string pTurnS;
    compON = false;
    do{
    board(S);
    if(pTurn == 1){
        pTurnS = P1;
    }else if(!compON && pTurn == 2){
        pTurnS = P2;
    } else {
        pTurnS = "Computer";
    }
    //checks mark
    if(pTurn == 1){
        pMark = 'X';
    } else {
        pMark = 'O';
    }
    cout << pTurnS << "'s turn." << endl; // tells which player turn it is
    bool validMove; // checks if it is a valid move or not
    do {
			int nMove;
			if(pTurn == 1){
                cin >> nMove;
			} else if(pTurn == 2 && !compON){
			    cin >> nMove;
			} else if(compON){
			    nMove = rand() % 9 + 1;
			    cout << "Random computer number is:" << nMove << endl;
			}
			validMove = true;
			// Check if the move is valid
			if (S[nMove] != 'X' && S[nMove] != 'O') {
				S[nMove] = pMark;
			} else {
			    if(compON && pTurn == 1){
				//cout << "Invalid Move. Try again." << endl;
			    }
				validMove = false;
			}
		} while (!validMove); // does while it is false

		gameOff = false;
		bool winGame = false;
		// checks game conditions
        if (S[1] != '1') {
			if (S[2] == S[1] && S[3] == S[1]) {
				gameOff = true;
			}
			if (S[4] == S[1] && S[7] == S[1]) {
				gameOff = true;
			}
		}
		if (S[5] != '5') {
			if (S[1] == S[5] && S[9] == S[5]) {
				gameOff = true;
			}
			if (S[2] == S[5] && S[8] == S[5]) {
				gameOff = true;
			}
			if (S[4] == S[5] && S[6] == S[5]) {
				gameOff = true;
			}
			if (S[3] == S[5] && S[7] == S[5]) {
				gameOff = true;
			}
		}
		if (S[9] != '9') {
			if (S[3] == S[9] && S[6] == S[9]) {
				gameOff = true;
			}
			if (S[7] == S[9] && S[8] == S[9]) {
				gameOff = true;
			}
		}
		// checks board
		if(S[1] != '1' && S[2] != '2' && S[3] != '3' && S[4] != '4' && S[5] != '5' && S[6] != '6' && S[7] != '7' && S[8] != '8' && S[9] != '9' && !gameOff){ // if nobody won
            cout << "Tie!" << endl;
            gameOff = true;
			winGame = false;
           }else if (gameOff) {
			// Print ending board
                board(S);
                cout << "Game Over!" << endl;
                cout << endl;
                cout << pTurnS << " wins!" << endl;
                cout << endl;
			cout << "Play again?(y,n)" << endl;
			char pAgain;
			cin >> pAgain;
			if (tolower(pAgain) == 'y'){
				gameOff = false;
				// clear the board so you can play again
				new_game(i, S);
			} else {
			    gameOff = true; // close game
			}
		} else {
			// alternate player turns
			if (pTurn == 1) {
				pTurn = 2;
			} else {
				pTurn = 1;
            }
		}
    } while(!gameOff); //restart if gameOff is false
}

Thanks if you help.
Last edited on
closed account (Dy7SLyTq)
no one is going to scrounge 150 lines for a glitch. please tell us what is and where you are hitting it
On line 38 it inputs P2 but on line 51 it seems like P2 doesn't exist anymore. It also doesn't output it ec. BLANK.
Last edited on
closed account (Dy7SLyTq)
does your input have spaces?
nope. Also it says it has been terminated with 8958 minutes, 59 seconds
Last edited on
closed account (Dy7SLyTq)
well where are you making pturn = 2?
bottom line 143.
First thing I notice is that the parameter i for new_game shouldn't be a parameter.

Second thing: indexing arrays in new_game and board should begin at 0 and end at 8, not begin at 1 and end at 9.

Third thing: Same thing as the second thing only this time in main.

Weird things happen when you access memory you shouldn't.

Thanks for helping.
Line 24 - char S[9]; should have been S[10].
Thanks though.
Topic archived. No new replies allowed.