TIC TAC TOE. HELP
Apr 9, 2016 at 7:19pm UTC
When Someone wins the game, it does not print out the winner. It just ends. How do i get it to print out the winner.
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 9, 2016 at 7:56pm UTC
Lines 102-125:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
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 (gameOn) {
cout << " Player " << playerturn << " is the winner" << endl;
}
Look at this
1 2 3
if (gameOn) {
cout << " Player " << playerturn << " is the winner" << endl;
}
You test if wingame is false, but it will never be false as you initialize it and never change the value of the variable of it again. The compiler would have warned you if -Wunused-variable was on. Maybe you meant the following
1 2 3
if (gameOn) {
cout << " Player " << playerturn << " is the winner" << endl;
}
This would fix the issue or you could set winGame to false.
Your variables seem to be describing one thing but doing the opposite. Like look at your gameOn variable, to a reader of the code that would mean the game should continue if it's true but you use it in the opposite manner. Same goes for the wingame varaible. Of course your program is perfectly fine, just a tip I guess.
Last edited on Apr 9, 2016 at 8:02pm UTC
Topic archived. No new replies allowed.