Jan 29, 2015 at 10:54am Jan 29, 2015 at 10:54am UTC
I am new to C++ and I am trying to put together a little tic tac toe game. I have it figured out except for validating whether there is a winner. I have 9 slots in an array, they start as numbers but get changed to X or O as char type.
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
/*
Tic Tac Toe
by namethief
01/29/15
*/
#include <iostream>
using namespace std;
int main(){
bool gameOver = false ;
char whosTurn = 'X' ;
int sel = 0;
char spot[9]; //declaring values of spots in array
spot[0] = '1' ;
spot[1] = '2' ;
spot[2] = '3' ;
spot[3] = '4' ;
spot[4] = '5' ;
spot[5] = '6' ;
spot[6] = '7' ;
spot[7] = '8' ;
spot[8] = '9' ;
do {
cout << "\n\n\n\n " << spot[0] << " | " << spot[1] << " | " << spot[2] << "\n" <<
"-----------\n" << // This will print
" " << spot[3] << " | " << spot[4] << " | " << spot[5] << "\n" << // the game board
"-----------\n" <<
" " << spot[6] << " | " << spot[7] << " | " << spot[8] << "\n" << endl;
cout << "\n" << whosTurn << "'s turn. Please enter the number of the" <<
"spot you would like to mark." << endl; // User input spot they want to play
cin >> sel;
if (spot[sel - 1] == 'X' || spot[sel - 1] == 'O' ){ // check to see if space is available
cout << "\n\n\n\nSeat's taken! Try again." << endl;
}else {
spot[sel - 1] = whosTurn;} // place a marker on spot selected
if (whosTurn == 'X' ){ // change player for next turn
whosTurn = 'O' ;
}else {
whosTurn = 'X' ;
}
}while (gameOver == false );
return 0;
}
I want to use a switch statement between "Place a marker" and "change player" to check all possible winning scenarios. (I know I need to make sure the change player portion only occurs if there is not a winner). The only example of switch I can find only uses one variable, but I am imagining something like:
1 2 3 4 5 6 7 8 9 10 11 12
switch (spot[0-8]){
case spot[0] == 'X' && spot[1] == 'X' && spot[2] == 'X' :
gameOver = True;
cout << whosTurn << " wins!" << endl;
break ;
Case ..etc....
default :
// change player
}
But that is not working. Any thoughts? Thanks in advance! I'm having a lot of fun playing with C++!
-namethief
Last edited on Jan 29, 2015 at 11:58am Jan 29, 2015 at 11:58am UTC
Jan 29, 2015 at 11:10am Jan 29, 2015 at 11:10am UTC
You can't. A switch only works for a single variable; if you need to check multiple variables, use an if statement.
Jan 29, 2015 at 11:16am Jan 29, 2015 at 11:16am UTC
Thank you for the info. Will keep that in mind.
Jan 29, 2015 at 11:57am Jan 29, 2015 at 11:57am UTC
For any other beginners who may be interested in seeing what my "final" version looks like, I'll paste it below. I use quotation marks around final because it is not programmed to detect a full board lacking a 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
/*
Tic Tac Toe
by namethief
01/29/15
*/
#include <iostream>
using namespace std;
int main(){
bool gameOver = false ;
char whosTurn = 'X' ;
int sel = 0;
char spot[9]; //declaring values of spots in array
spot[0] = '1' ; //to be used as the 9 squares in game
spot[1] = '2' ;
spot[2] = '3' ;
spot[3] = '4' ;
spot[4] = '5' ;
spot[5] = '6' ;
spot[6] = '7' ;
spot[7] = '8' ;
spot[8] = '9' ;
do {
cout << "\n\n\n\n " << spot[0] << " | " << spot[1] << " | " << spot[2] << "\n" <<
"-----------\n" << // This will print
" " << spot[3] << " | " << spot[4] << " | " << spot[5] << "\n" << // the game board
"-----------\n" << //to console
" " << spot[6] << " | " << spot[7] << " | " << spot[8] << "\n" << endl;
cout << "\n" << whosTurn << "'s turn. Please enter the number of the " <<
"spot you would like to mark." << endl; // User input spot they want to play
cin >> sel;
// check to see if space is available
if (spot[sel - 1] == 'X' || spot[sel - 1] == 'O' || sel > 9 || sel < 1){
cout << "\n\n\n\nSeat's taken! Try again." << endl;
}else {
spot[sel - 1] = whosTurn; // place a marker on spot selected
if (spot[0] == 'X' && spot[1] == 'X' && spot[2] == 'X' ){ //check
gameOver = true ; //all
}else if (spot[3] == 'X' && spot[4] == 'X' && spot[5] == 'X' ){ //possible
gameOver = true ; //winning
}else if (spot[6] == 'X' && spot[7] == 'X' && spot[8] == 'X' ){ //scenarios
gameOver = true ;
}else if (spot[0] == 'X' && spot[3] == 'X' && spot[6] == 'X' ){
gameOver = true ;
}else if (spot[1] == 'X' && spot[4] == 'X' && spot[7] == 'X' ){
gameOver = true ;
}else if (spot[2] == 'X' && spot[5] == 'X' && spot[8] == 'X' ){
gameOver = true ;
}else if (spot[0] == 'X' && spot[4] == 'X' && spot[8] == 'X' ){
gameOver = true ;
}else if (spot[6] == 'X' && spot[4] == 'X' && spot[2] == 'X' ){
gameOver = true ;
}else if (spot[0] == 'O' && spot[1] == 'O' && spot[2] == 'O' ){
gameOver = true ;
}else if (spot[3] == 'O' && spot[4] == 'O' && spot[5] == 'O' ){
gameOver = true ;
}else if (spot[6] == 'O' && spot[7] == 'O' && spot[8] == 'O' ){
gameOver = true ;
}else if (spot[0] == 'O' && spot[3] == 'O' && spot[6] == 'O' ){
gameOver = true ;
}else if (spot[1] == 'O' && spot[4] == 'O' && spot[7] == 'O' ){
gameOver = true ;
}else if (spot[2] == 'O' && spot[5] == 'O' && spot[8] == 'O' ){
gameOver = true ;
}else if (spot[0] == 'O' && spot[4] == 'O' && spot[8] == 'O' ){
gameOver = true ;
}else if (spot[6] == 'O' && spot[4] == 'O' && spot[2] == 'O' ){
gameOver = true ;
}else { // if no winner:
if (whosTurn == 'X' ){ // change player for next turn
whosTurn = 'O' ;
}else {
whosTurn = 'X' ;
}}}
}while (gameOver == false );
cout << "\n\n\n\n " << spot[0] << " | " << spot[1] << " | " << spot[2] << "\n" <<
"-----------\n" << // This will print
" " << spot[3] << " | " << spot[4] << " | " << spot[5] << "\n" << // the game board
"-----------\n" <<
" " << spot[6] << " | " << spot[7] << " | " << spot[8] << "\n\n\n" << endl;
cout << whosTurn << " wins!!!" << endl; // Declare winner
return 0; // end program
}
This is what the game board ends up looking like:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
with numbers changing to X's and O's as they are entered.
Last edited on Jan 29, 2015 at 12:09pm Jan 29, 2015 at 12:09pm UTC