TIC TAC TOE. HELP!!
Apr 5, 2016 at 2:08am UTC
When you choose a number to mark, it always prints out that someone is the winner. Even if it is the first move of the game. However, the game does not end until someone actually wins. It is obviously not finished, but i just want to know why it is doing that.
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
#include <iostream>
#include <string>
using namespace std;
int main() {
char menuChoice;
char square1 = '1' ;
char square2 = '2' ;
char square3 = '3' ;
char square4 = '4' ;
char square5 = '5' ;
char square6 = '6' ;
char square7 = '7' ;
char square8 = '8' ;
char square9 = '9' ;
cout << " WOULD YOU LIKE TO PLAY TIC TAC TOE?" << endl;
cout << " Play(y)" << endl;
cout << " Quit(n)" << endl;
cin >> menuChoice;
bool gameOn = false ;
bool validinput;
bool playerturn = false ;
if (menuChoice == 'y' ) {
do {
cout << " " << square1 << " | " << square2 << " | " << square3 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << square4 << " | " << square5 << " | " << square6 << endl;
cout << " -----+-----+-----" << endl;
cout << " " << square7 << " | " << square8 << " | " << square9 << endl;
cout << " -----+-----+-----" << endl;
char playermarker;
if (playerturn == false ) {
playerturn = true ;
playermarker = 'O' ;
}
else {
playermarker = 'X' ;
playerturn = false ;
}
do {
char CurrentMove;
cout <<"Player " << playerturn<< ", Enter the number of the square you want to take" << endl;
cin >> CurrentMove;
validinput = true ;
if (CurrentMove == '1' && square1 == '1' ) {
square1 = playermarker;
}
else if (CurrentMove == '2' && square2 == '2' ) {
square2 = playermarker;
}
else if (CurrentMove == '3' && square3 == '3' ) {
square3 = playermarker;
}
else if (CurrentMove == '4' && square4 == '4' ) {
square4 = playermarker;
}
else if (CurrentMove == '5' && square5 == '5' ) {
square5 = playermarker;
}
else if (CurrentMove == '6' && square6 == '6' ) {
square6 = playermarker;
}
else if (CurrentMove == '7' && square7 == '7' ) {
square7 = playermarker;
}
else if (CurrentMove == '8' && square8 == '8' ) {
square8 = playermarker;
}
else if (CurrentMove == '9' && square9 == '9' ) {
square9 = playermarker;
}
else {
cout << " Invalid Input, Try Again" << endl;
validinput = false ;
}
} while (!validinput);
gameOn = false ;
bool wingame = true ;
if (square1 == square5 && square5 == square9) {
gameOn = true ;
}if (square1 == square4 && square4 == square7) {
gameOn = true ;
}if (square1 == square2 && square2 == square3) {
gameOn = true ;
}if (square7 == square8 && square8 == square9) {
gameOn = true ;
}if (square3 == square6 && square6 == square9) {
gameOn = true ;
}if (square3 == square5 && square5 == square7) {
gameOn = true ;
}if (square2 == square5 && square5 == square8) {
gameOn = true ;
}if (square4 == square5 && square5 == square6) {
gameOn = true ;
}
if (wingame) {
cout << " Player " << playerturn << " is the winner" << endl;
}
} while (!gameOn);
}
system("pause" );
return 0;
}
Apr 5, 2016 at 3:06am UTC
what is the value of gameOn when the program quits ?
Apr 5, 2016 at 11:36am UTC
False
Apr 5, 2016 at 11:46am UTC
When you choose a number to mark, it always prints out that someone is the winner.
The winner will be printed when
wingame is true.
wingame is
always true, so the winner will always be printed.
Topic archived. No new replies allowed.