Tic-tac-toe game runtime error
Apr 25, 2013 at 6:47pm UTC
I have already asked for help once before about this game. The feedback i got was great, and i fixed a lot of errors. But now i'm stuck with a runtime error i have no idea how to fix. I would appriciate if someone could point out how i can fix the error. Thank :)
This is my error:
Run-Time Check Failure #3 - The variable 'iPlayAgain (This also happens to iValidMove and iEndGame)' is being used without being initialized.
And here is my code:
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 171 172 173 174 175
#include <iostream>
using namespace std;
int main() {
// Define
int cSquare1 = 1;
int cSquare2 = 2;
int cSquare3 = 3;
int cSquare4 = 4;
int cSquare5 = 5;
int cSquare6 = 6;
int cSquare7 = 7;
int cSquare8 = 8;
int cSquare9 = 9;
char iPlayerMark;
bool iEndGame;
int iPlayersTurn = 1;
bool iDraw;
bool iGameOver;
// Display board
do {
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
// Tell which player's turn it is
cout << "It is player" << iPlayersTurn << "'s turn" << endl;
if (iPlayersTurn == 1) {
iPlayerMark = 'X' ;
}
else
iPlayerMark = 'O' ;
// Ask player what to do
cout << "Enter a number from 1-9 representing the square you want to place your mark" << endl;
// Does what player decided
bool iValidMove = true ;
do {
char iPlayersChoice;
cin >> iPlayersChoice;
bool iValidMove = true ;
if (iPlayersChoice == 1 && cSquare1 == 1) {
cSquare1 = iPlayerMark;
}
else if (iPlayersChoice == 2 && cSquare2 == 2) {
cSquare2 = iPlayerMark;
}
else if (iPlayersChoice == 3 && cSquare3 == 3) {
cSquare3 = iPlayerMark;
}
else if (iPlayersChoice == 4 && cSquare4 == 4) {
cSquare4 = iPlayerMark;
}
else if (iPlayersChoice == 5 && cSquare5 == 5) {
cSquare5 = iPlayerMark;
}
else if (iPlayersChoice == 6 && cSquare6 == 6) {
cSquare6 = iPlayerMark;
}
else if (iPlayersChoice == 7 && cSquare7 == 7) {
cSquare7 = iPlayerMark;
}
else if (iPlayersChoice == 8 && cSquare8 == 8) {
cSquare8 = iPlayerMark;
}
else if (iPlayersChoice == 9 && cSquare9 == 9) {
cSquare9 = iPlayerMark;
}
else {
cout << "Invalid move. Please try again." << endl;
iValidMove = false ;
}
}
while (iValidMove == false );
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
// Check if game is over
if (cSquare1 != '1' && cSquare2 == cSquare1 && cSquare3 == cSquare1)
iGameOver = true ;
else if (cSquare1 != '1' && cSquare4 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare4 == cSquare1 && cSquare6 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare3 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare2 == cSquare1 && cSquare8 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare1 == cSquare1 && cSquare9 == cSquare1)
iGameOver = true ;
else if (cSquare9 != '9' && cSquare8 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare9 != '9' && cSquare6 == cSquare9 && cSquare3 == cSquare1)
iGameOver = true ;
else if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3'
&& cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6'
&& cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' ) {
iDraw = true ;
}
// if Game is over, a winner might be declared
if (iGameOver == true )
cout << "Player" << iPlayersTurn << " has won" << endl;
else if (iDraw == true )
cout << "The game ended in a draw" << endl;
// check if player wants to play agian
char iPlayAgain = 'y' ;
if ((iGameOver = true ) || (iDraw == true )) {
cout << "Play agian? y/n" << endl;
char iPlayAgain;
cin >> iPlayAgain;
}
if (iPlayAgain == 'y' ) {
cSquare1 = 1;
cSquare2 = 2;
cSquare3 = 3;
cSquare4 = 4;
cSquare5 = 5;
cSquare6 = 6;
cSquare7 = 7;
cSquare8 = 8;
cSquare9 = 9;
iPlayersTurn = 1;
iGameOver = false ;
}
else if (iPlayAgain == 'n' )
iEndGame = true ;
if (iPlayersTurn == 1) {
iPlayersTurn = 2;
}
else iPlayersTurn = 1;
// No winner is currently decided
}
while (iEndGame == false );
char response;
cin >> response;
return 0;
}
Apr 25, 2013 at 6:56pm UTC
You declared iValidMove and iPlayAgain twice, the first of each initialized while the other is not. The compiler may have mistaken these as different variables, so your methods attempted to use the second, uninitialized declarations. I don't know about iEndGame though.
Edit:
I think the reason why the compiler thinks you have different variables is because of how you declared them.
1 2 3 4
char iPlayAgain = 'y' ; //First declaration
if (){
char iPlayAgain; //technically a different variable
}
Correct me if I'm wrong veteran programmers.
Last edited on Apr 25, 2013 at 6:59pm UTC
Apr 25, 2013 at 7:10pm UTC
Yet you still did not listen to us. you are still have
if (!csquare == '1' )
....that would ALWAYS come true because it is an int and it would be
if (!csquare == 1)
and you could make your code neater and more readable by doing
1 2
int csquare[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
//then call csquare[0] -> csquare[8] instead of csquare 1, csqaure 2, csquare 3
THen at the end when you are setting back to default values you could do
for (unsigned int i = 0; i<9; i++) csquare[i] = i+1;
Edit:
you need to make the csquares into chars..you are trying to do int number = 1;
number = 'x';
which clearly does not work when you try and set to x.
Last edited on Apr 25, 2013 at 7:15pm UTC
Apr 26, 2013 at 1:45pm UTC
Thanks for the help guys. I fixed what you said. I feel kinda stupid for trying to give an integer 'X'. I managed to run the program and placed the first mark. But right after that I still get the runtime error that iGameOver, iDraw, and iEndGame are being used without initialized.
Seems like what Daleth told me fixed it with iPlayAgain, but what about the others? I declared those three at the top, so they should be available from anywhere in my code, shouldn't they?
Now my code looks like this:
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 171 172 173 174 175 176 177
#include <iostream>
using namespace std;
int main() {
// Define
char cSquare1 = '1' ;
char cSquare2 = '2' ;
char cSquare3 = '3' ;
char cSquare4 = '4' ;
char cSquare5 = '5' ;
char cSquare6 = '6' ;
char cSquare7 = '7' ;
char cSquare8 = '8' ;
char cSquare9 = '9' ;
char iPlayerMark;
bool iEndGame;
int iPlayersTurn = 1;
bool iDraw;
bool iGameOver;
// Display board
do {
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
// Tell which player's turn it is
cout << "It is player" << iPlayersTurn << "'s turn" << endl;
if (iPlayersTurn == 1) {
iPlayerMark = 'X' ;
}
else
iPlayerMark = 'O' ;
// Ask player what to do
cout << "Enter a number from 1-9 representing the square you want to place your mark" << endl;
// Does what player decided
bool iValidMove;
do {
char iPlayersChoice;
cin >> iPlayersChoice;
iValidMove = true ;
if (iPlayersChoice == '1' && cSquare1 == '1' ) {
cSquare1 = iPlayerMark;
}
else if (iPlayersChoice == '2' && cSquare2 == '2' ) {
cSquare2 = iPlayerMark;
}
else if (iPlayersChoice == '3' && cSquare3 == '3' ) {
cSquare3 = iPlayerMark;
}
else if (iPlayersChoice == '4' && cSquare4 == '4' ) {
cSquare4 = iPlayerMark;
}
else if (iPlayersChoice == '5' && cSquare5 == '5' ) {
cSquare5 = iPlayerMark;
}
else if (iPlayersChoice == '6' && cSquare6 == '6' ) {
cSquare6 = iPlayerMark;
}
else if (iPlayersChoice == '7' && cSquare7 == '7' ) {
cSquare7 = iPlayerMark;
}
else if (iPlayersChoice == '8' && cSquare8 == '8' ) {
cSquare8 = iPlayerMark;
}
else if (iPlayersChoice == '9' && cSquare9 == '9' ) {
cSquare9 = iPlayerMark;
}
else {
cout << "Invalid move. Please try again." << endl;
iValidMove = false ;
}
}
while (iValidMove == false );
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
// Check if game is over
if (cSquare1 != '1' && cSquare2 == cSquare1 && cSquare3 == cSquare1)
iGameOver = true ;
else if (cSquare1 != '1' && cSquare4 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare4 == cSquare1 && cSquare6 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare3 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare2 == cSquare1 && cSquare8 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare1 == cSquare1 && cSquare9 == cSquare1)
iGameOver = true ;
else if (cSquare9 != '9' && cSquare8 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare9 != '9' && cSquare6 == cSquare9 && cSquare3 == cSquare1)
iGameOver = true ;
else if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3'
&& cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6'
&& cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' ) {
iDraw = true ;
}
// if Game is over, a winner might be declared
if (iGameOver == true )
cout << "Player" << iPlayersTurn << " has won" << endl;
else if (iDraw == true )
cout << "The game ended in a draw" << endl;
// check if player wants to play agian
char iPlayAgain;
if ((iGameOver = true ) || (iDraw == true )) {
cout << "Play agian? y/n" << endl;
cin >> iPlayAgain;
if (iPlayAgain == 'y' ) {
cSquare1 = '1' ;
cSquare2 = '2' ;
cSquare3 = '3' ;
cSquare4 = '4' ;
cSquare5 = '5' ;
cSquare6 = '6' ;
cSquare7 = '7' ;
cSquare8 = '8' ;
cSquare9 = '9' ;
iPlayersTurn = 1;
iGameOver = false ;
}
else if (iPlayAgain == 'n' )
iEndGame = true ;
}
if (iPlayersTurn == 1) {
iPlayersTurn = 2;
}
else iPlayersTurn = 1;
// No winner is currently decided
}
while (iEndGame == false );
char response;
cin >> response;
return 0;
}
Apr 26, 2013 at 3:56pm UTC
I declared those three at the top, so they should be available from anywhere in my code, shouldn't they?
Well, yes. Look at what the error message is actually telling you. It's not telling you that you can't use them; it's telling you that you
are using them,
but you haven't initialised them before you use them .
Look at iGameOver. You declare it at line 24. You try and use its value at line 130. But what value will it have if none of the conditions in your "if... else" statement are met? What value will it have if only the final condition at 122 - 124 are met?
Apr 27, 2013 at 4:55pm UTC
Yay! :D Finally, it works the way it should. Thanks for the help everyone, i'm very grateful for it :D
Here is my finished product:
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 171 172 173 174 175 176 177 178 179
#include <iostream>
using namespace std;
int main() {
// Define
char cSquare1 = '1' ;
char cSquare2 = '2' ;
char cSquare3 = '3' ;
char cSquare4 = '4' ;
char cSquare5 = '5' ;
char cSquare6 = '6' ;
char cSquare7 = '7' ;
char cSquare8 = '8' ;
char cSquare9 = '9' ;
char iPlayerMark = 'X' ;
bool iEndGame = false ;
int iPlayersTurn = 1;
bool iDraw = false ;
bool iGameOver = false ;
// Display board
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
do {
// Tell which player's turn it is
cout << "It is player" << iPlayersTurn << "'s turn" << endl;
if (iPlayersTurn == 1) {
iPlayerMark = 'X' ;
}
else
iPlayerMark = 'O' ;
// Ask player what to do
cout << "Enter a number from 1-9 representing the square you want to place your mark" << endl;
// Does what player decided
bool iValidMove;
do {
char iPlayersChoice;
cin >> iPlayersChoice;
iValidMove = true ;
if (iPlayersChoice == '1' && cSquare1 == '1' ) {
cSquare1 = iPlayerMark;
}
else if (iPlayersChoice == '2' && cSquare2 == '2' ) {
cSquare2 = iPlayerMark;
}
else if (iPlayersChoice == '3' && cSquare3 == '3' ) {
cSquare3 = iPlayerMark;
}
else if (iPlayersChoice == '4' && cSquare4 == '4' ) {
cSquare4 = iPlayerMark;
}
else if (iPlayersChoice == '5' && cSquare5 == '5' ) {
cSquare5 = iPlayerMark;
}
else if (iPlayersChoice == '6' && cSquare6 == '6' ) {
cSquare6 = iPlayerMark;
}
else if (iPlayersChoice == '7' && cSquare7 == '7' ) {
cSquare7 = iPlayerMark;
}
else if (iPlayersChoice == '8' && cSquare8 == '8' ) {
cSquare8 = iPlayerMark;
}
else if (iPlayersChoice == '9' && cSquare9 == '9' ) {
cSquare9 = iPlayerMark;
}
else {
cout << "Invalid move. Please try again." << endl;
iValidMove = false ;
}
}
while (iValidMove == false );
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
// Check if game is over
if (cSquare1 != '1' && cSquare2 == cSquare1 && cSquare3 == cSquare1)
iGameOver = true ;
else if (cSquare1 != '1' && cSquare4 == cSquare1 && cSquare7 == cSquare1)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare4 == cSquare5 && cSquare6 == cSquare5)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare3 == cSquare5 && cSquare7 == cSquare5)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare2 == cSquare5 && cSquare8 == cSquare5)
iGameOver = true ;
else if (cSquare5 != '5' && cSquare1 == cSquare5 && cSquare9 == cSquare5)
iGameOver = true ;
else if (cSquare9 != '9' && cSquare8 == cSquare9 && cSquare7 == cSquare9)
iGameOver = true ;
else if (cSquare9 != '9' && cSquare6 == cSquare9 && cSquare3 == cSquare9)
iGameOver = true ;
else if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3'
&& cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6'
&& cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' ) {
iDraw = true ;
}
// if Game is over, a winner might be declared
if (iGameOver == true )
cout << "Player" << iPlayersTurn << " has won" << endl;
else if (iDraw == true )
cout << "The game ended in a draw" << endl;
// check if player wants to play agian
char iPlayAgain;
if ((iGameOver == true ) || (iDraw == true )) {
cout << "Play agian? y/n" << endl;
cin >> iPlayAgain;
if (iPlayAgain == 'y' ) {
cSquare1 = '1' ;
cSquare2 = '2' ;
cSquare3 = '3' ;
cSquare4 = '4' ;
cSquare5 = '5' ;
cSquare6 = '6' ;
cSquare7 = '7' ;
cSquare8 = '8' ;
cSquare9 = '9' ;
iGameOver = false ;
iDraw = false ;
// Display board for the start of a new game
cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << endl;
cout << "-+-+-" << endl;
cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << endl;
cout << "-+-+-" << endl;
cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << endl <<endl;
}
else if (iPlayAgain == 'n' )
iEndGame = true ;
}
if (iPlayersTurn == 1) {
iPlayersTurn = 2;
}
else iPlayersTurn = 1;
// No winner is currently decided
}
while (iEndGame == false );
return 0;
}
Topic archived. No new replies allowed.