Comparing Characters
Sep 25, 2013 at 8:01pm UTC
I'm writing a Tic-Tac-Toe program for a project and I'm running into a small issue with my gameplay function. The spaces of the 3x3 grid are stored as chars - meant to be either # (empty), X or O - and I'm handling occupied spaces by saying that if the space isn't #, reject the player's input and ask them to try again. However, even when the player enters x or o, it returns the error.
Link to full code:
http://pastebin.com/BnM2U42U
Relevant bits of 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
switch (currentSpace){
case 'Q' :
case 'q' :
if ((q == '#' ) && (currentPlayer == 'X' )){
q = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((q == '#' ) && (currentPlayer == 'Y' )){
q = 'Y' ;
currentPlayer = !currentPlayer;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'W' :
case 'w' :
if ((w == '#' ) && (currentPlayer == 'X' )){
w = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((w == '#' ) && (currentPlayer == 'Y' )){
w = 'Y' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'E' :
case 'e' :
if ((e == '#' ) && currentPlayer == 'X' ){
e = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((e == '#' ) && (currentPlayer == 'Y' )){
e = 'Y' ;
currentPlayer = !currentPlayer;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'A' :
case 'a' :
if ((a == '#' ) && (currentPlayer == 'X' )){
a = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((a == '#' ) && (currentPlayer == 'Y' )){
a = 'Y' ;
currentPlayer = !currentPlayer;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'S' :
case 's' :
if ((s == '#' ) && (currentPlayer == 'X' )){
s = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((s == '#' ) && (currentPlayer == 'Y' )){
s = 'Y' ;
currentPlayer = 'X' ;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'D' :
case 'd' :
if ((d == '#' ) && (currentPlayer == 'X' )){
d = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((d == '#' ) && (currentPlayer == 'Y' )){
d = 'Y' ;
currentPlayer = 'X' ;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'Z' :
case 'z' :
if ((z == '#' ) && (currentPlayer == 'X' )){
z = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((z == '#' ) && (currentPlayer == 'Y' )){
z = 'Y' ;
currentPlayer = 'X' ;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'X' :
case 'x' :
if ((x == '#' ) && (currentPlayer == 'X' )){
x = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((x == '#' ) && (currentPlayer == 'Y' )){
x = 'Y' ;
currentPlayer = 'X' ;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
case 'C' :
case 'c' :
if ((c == '#' ) && (currentPlayer == 'X' )){
c = 'X' ;
currentPlayer = 'Y' ;
turnsPassed = turnsPassed++;}
else if ((c == '#' ) && (currentPlayer == 'Y' )){
c = 'Y' ;
currentPlayer = 'X' ;
turnsPassed = turnsPassed++;}
else {
cout << "Space is occupied or invalid. Choose another." << endl;
}
break ;
Last edited on Sep 25, 2013 at 8:02pm UTC
Sep 25, 2013 at 9:59pm UTC
You could make life much simpler by using an array for the board.
char board[3][3];
That way you only need to compare X and 0. Once you cut all the other characters out it will be much easier for you to see what you're doing right and wrong. What you have now is pretty confusing especially since it looks like you are using X for more than one variable name.
Topic archived. No new replies allowed.