| | |
A Tic Tac Toe Game
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2007
Posts: 23
Reputation:
Solved Threads: 0
Hello everyone,
I am having a slight problem in my function "startGame". My code works on Bloodshed and the code does compile. I am trying to refine the part on making sure that only a number gets input and not a letter or any other key gets input. I have try a couple different ways and it is not coming out the way I think it should. I have tried a do/while loop and a if/else statement. Does anyone have a sugestion.
I am having a slight problem in my function "startGame". My code works on Bloodshed and the code does compile. I am trying to refine the part on making sure that only a number gets input and not a letter or any other key gets input. I have try a couple different ways and it is not coming out the way I think it should. I have tried a do/while loop and a if/else statement. Does anyone have a sugestion.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> using namespace std; class ticTac { private: int counter; char posn[9]; char player; public: void nextPlayer(); char getPlayer(); void newBoard(); void startGame(); char checkWinner(); bool checkPosn(int spot, char player); void updateBoard(); }; int main() { char ch; ticTac Toe; do{ Toe.newBoard(); //Brings up the new board Toe.startGame(); //Starts the new game cout << "Would you like to play again (Enter 'y')? "; cin >> ch; }while (ch == 'y'); return 0; } //The new board before the game starts void ticTac::newBoard() { char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'}; int i; counter = 0; player = 'x'; for (i=0; i<9; i++) posn[i]=posndef[i]; return; } //This will update the board with the 'X' or 'O' void ticTac::updateBoard() { cout << "\n\n" <<endl; cout << "\n\n\t\t" <<posn [0]<< " | " <<posn [1]<< " | " <<posn [2]<<endl; cout << " \t\t | | " <<endl; cout << " \t\t ___|____|___ " <<endl; cout << "\n\n\t\t" <<posn [3]<< " | " <<posn [4]<< " | " <<posn [5]<<endl; cout << " \t\t | | " <<endl; cout << " \t\t ___|____|___ " <<endl; cout << "\n\n\t\t" <<posn [6]<< " | " <<posn [7]<< " | " <<posn [8]<<endl; cout << " \t\t | | " <<endl; cout << " \t\t | | " <<endl; cout << "\n" <<endl; } //Starts the game void ticTac::startGame() { int spot; char blank = ' '; bool result = false; char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'}; cout << "\n\nPlayer " << getPlayer() <<" will be the first to play." <<endl; do { updateBoard(); // display update board cout << "Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl; cin >> spot; if (spot < 0 || spot > 8) { cout << "Invalid position! Please input a number of 0 thru 8." <<endl; continue;} result = checkPosn(spot, getPlayer()); // to check if the position is taken system("CLS"); if (result == false){ nextPlayer(); } if(checkWinner() == 'x' || checkWinner() == 'o' || checkWinner () == 't') blank = 'v'; } while ( blank != 'v' ); return; } //Checks to see who the winner will be char ticTac::checkWinner() { char game = ' '; int check = 1; // Check if X wins if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') game = 'x'; if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') game = 'x'; if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') game = 'x'; if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') game = 'x'; if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') game = 'x'; if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') game = 'x'; if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') game = 'x'; if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') game = 'x'; if (game == 'x' ) {cout << "Player1 wins the game." <<endl; return game; }; // Check if O wins if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') game = 'o'; if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') game = 'o'; if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') game = 'o'; if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') game = 'o'; if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') game = 'o'; if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') game = 'o'; if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') game = 'o'; if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') game = 'o'; if (game == 'o' ) {cout << "Player2 wins the game." <<endl; return game; }; for(int i = 0; i < 8; i++ ){ if(posn[i] >= '1' && posn[i] <= '8' ){ check = 0; break; } } // Check if tie if (game != 'x' && game != 'o' && check == 1) game = 't'; if (game == 't') {cout << "Tie Game." <<endl; return game; }; return game; } //Checks to see if the position is taken bool ticTac::checkPosn(int spot, char player) { if (posn[spot] == 'x' || posn[spot] == 'o'){ cout << "Position already taken, please choose another position." <<endl; system ("pause"); return true; } else { posn[spot] = player; cout << "Nice move." <<endl; system ("pause"); counter++; return false; } return false; } //Tells the next player that it's thier turn void ticTac::nextPlayer() { if (player == 'x') player = 'o'; else player = 'x'; return; } //This switches between players char ticTac::getPlayer() { return player; }
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
0
#2 Oct 12th, 2009
Focus your code posting to just the pertinent part. sometimes it's hard to know where that is, but in your case it shouldn't be.
Sounds like a do/while loop using something like isdigit() might work if the numerical input is from 0-9.
Otherwise you could check the state of the stream after attempted input. If it is still a valid stream then the input was valid. If not, it wasn't and you will need to ignore the invalid input part and reset the stream.
As a further alternative you could accept input only as a string and then scan the string char by char for validity.
Sounds like a do/while loop using something like isdigit() might work if the numerical input is from 0-9.
Otherwise you could check the state of the stream after attempted input. If it is still a valid stream then the input was valid. If not, it wasn't and you will need to ignore the invalid input part and reset the stream.
As a further alternative you could accept input only as a string and then scan the string char by char for validity.
Klatu Barada Nikto
•
•
Join Date: Feb 2007
Posts: 23
Reputation:
Solved Threads: 0
I am almost finished with this code. I am able to compile and when I input a number like 7 the 'x'' is placed in the zero position. What did I miss.
#include <iostream>
#include <cstdlib>
#include <cctype>
using namespace std;
class ticTac
{
private:
int counter;
char posn[9];
char player;
public:
void nextPlayer();
char getPlayer();
void newBoard();
void startGame();
char checkWinner();
bool checkPosn(int spot, char player);
void updateBoard();
};// end class ticTac
int main()
{
char ch;
ticTac Toe;
do{
Toe.newBoard(); //Brings up the new board
Toe.startGame(); //Starts the new game
cout << "Would you like to play again (Enter 'y')? ";
cin >> ch; // places the user input into response variable
}while (ch == 'y');
return 0;
}// end main
//The new board before the game starts
void ticTac::newBoard()
{
char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
int i;
counter = 0;
player = 'x';
for (i=0; i<9; i++) posn[i]=posndef[i];
return;
}// end function newBoard
//This will update the board with the 'X' or 'O'
void ticTac::updateBoard()
{
cout << "\n\n" <<endl;
cout << "\n\n\t\t" <<posn [0]<< " | " <<posn [1]<< " | " <<posn [2]<<endl;
cout << " \t\t | | " <<endl;
cout << " \t\t ___|____|___ " <<endl;
cout << "\n\n\t\t" <<posn [3]<< " | " <<posn [4]<< " | " <<posn [5]<<endl;
cout << " \t\t | | " <<endl;
cout << " \t\t ___|____|___ " <<endl;
cout << "\n\n\t\t" <<posn [6]<< " | " <<posn [7]<< " | " <<posn [8]<<endl;
cout << " \t\t | | " <<endl;
cout << " \t\t | | " <<endl;
cout << "\n" <<endl;
}// end funtion updateBoard
//Starts the game
void ticTac::startGame()
{
char input;
int spot;
char blank = ' ';
bool result = false;
char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
cout << "\n\nPlayer " << getPlayer() <<" will be the first to play." <<endl;
do {
updateBoard(); // display update board
cout << "Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
cin >> input; // places the user input into response variable
if (isdigit(input))cout << " " << endl;
else {cout << "Invalid input, Please try again" << endl;
continue;
}
int spot = atoi ( input + " " );
result = checkPosn(spot, getPlayer()); // to check if the position is taken
system("CLS");
if (result == false){
nextPlayer(); }
if(checkWinner() == 'x' || checkWinner() == 'o' || checkWinner () == 't')
blank = 'v';
}
while ( blank != 'v' );
return;
}// end funtion startGame
//Checks to see who the winner will be
char ticTac::checkWinner()
{
char game = ' ';
int check = 1;
// Check if X wins
if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') game = 'x';
if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') game = 'x';
if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') game = 'x';
if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') game = 'x';
if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') game = 'x';
if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') game = 'x';
if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') game = 'x';
if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') game = 'x';
if (game == 'x' )
{cout << "Player1 wins the game." <<endl; return game; };
// Check if O wins
if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') game = 'o';
if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') game = 'o';
if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') game = 'o';
if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') game = 'o';
if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') game = 'o';
if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') game = 'o';
if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') game = 'o';
if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') game = 'o';
if (game == 'o' )
{cout << "Player2 wins the game." <<endl; return game; };
for(int i = 0; i < 8; i++ ){
if(posn[i] >= '1' && posn[i] <= '8' ){
check = 0;
break;
}
}
// Check if tie
if (game != 'x' && game != 'o' && check == 1) game = 't';
if (game == 't')
{cout << "Tie Game." <<endl; return game; };
return game;
}// end funtion checkWinner
//Checks to see if the position is taken
bool ticTac::checkPosn(int spot, char player)
{
if (posn[spot] == 'x' || posn[spot] == 'o'){
cout << "Position already taken, please choose another position." <<endl;
system ("pause");
return true;
}
else {
posn[spot] = player;
cout << "Nice move." <<endl;
system ("pause");
counter++;
return false;
}
return false;
}// end funtion checkPosn
//Tells the next player that it's thier turn
void ticTac::nextPlayer()
{
if (player == 'x')
player = 'o';
else player = 'x';
return;
}// end funtion nextPlayer
//This switches between players
char ticTac::getPlayer()
{
return player;
}// end funtion getPlayer Last edited by peter_budo; 3 Days Ago at 4:40 pm. Reason: Correcting code tags, please use it as [code]YOUR CODE HERE[/code]
![]() |
Similar Threads
- help~Create a tic-tac-toe game.for C (C)
- Help Needed With Tic Tac Toe Game Using Graphics (Python)
- tic-tac-toe game (Game Development)
- tic-tac-toe game? (C++)
- Tic-Tac-Toe Game (Visual Basic 4 / 5 / 6)
- Tic-Tac-Toe Game - Need some information or help (C++)
- HELP!!! Tic-tac-toe game! (C++)
Other Threads in the C++ Forum
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






