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.
/*
Tic Tac Toe
by namethief
01/29/15
*/
#include <iostream>
usingnamespace 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:
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.