A Tic Tac Toe Game

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

A Tic Tac Toe Game

 
0
  #1
Oct 12th, 2009
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.


  1.  
  2. #include <iostream>
  3. #include <cstdlib>
  4. using namespace std;
  5. class ticTac
  6. {
  7. private:
  8. int counter;
  9. char posn[9];
  10. char player;
  11. public:
  12. void nextPlayer();
  13. char getPlayer();
  14. void newBoard();
  15. void startGame();
  16. char checkWinner();
  17. bool checkPosn(int spot, char player);
  18. void updateBoard();
  19.  
  20. };
  21.  
  22. int main()
  23. {
  24. char ch;
  25. ticTac Toe;
  26. do{
  27. Toe.newBoard(); //Brings up the new board
  28. Toe.startGame(); //Starts the new game
  29. cout << "Would you like to play again (Enter 'y')? ";
  30. cin >> ch;
  31. }while (ch == 'y');
  32.  
  33. return 0;
  34. }
  35.  
  36. //The new board before the game starts
  37. void ticTac::newBoard()
  38. {
  39.  
  40. char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
  41. int i;
  42. counter = 0;
  43. player = 'x';
  44. for (i=0; i<9; i++) posn[i]=posndef[i];
  45. return;
  46.  
  47. }
  48.  
  49. //This will update the board with the 'X' or 'O'
  50. void ticTac::updateBoard()
  51. {
  52. cout << "\n\n" <<endl;
  53. cout << "\n\n\t\t" <<posn [0]<< " | " <<posn [1]<< " | " <<posn [2]<<endl;
  54. cout << " \t\t | | " <<endl;
  55. cout << " \t\t ___|____|___ " <<endl;
  56. cout << "\n\n\t\t" <<posn [3]<< " | " <<posn [4]<< " | " <<posn [5]<<endl;
  57. cout << " \t\t | | " <<endl;
  58. cout << " \t\t ___|____|___ " <<endl;
  59. cout << "\n\n\t\t" <<posn [6]<< " | " <<posn [7]<< " | " <<posn [8]<<endl;
  60. cout << " \t\t | | " <<endl;
  61. cout << " \t\t | | " <<endl;
  62. cout << "\n" <<endl;
  63.  
  64. }
  65.  
  66. //Starts the game
  67. void ticTac::startGame()
  68. {
  69. int spot;
  70. char blank = ' ';
  71. bool result = false;
  72. char posndef[9] = {'0', '1', '2', '3', '4', '5', '6', '7', '8'};
  73.  
  74. cout << "\n\nPlayer " << getPlayer() <<" will be the first to play." <<endl;
  75.  
  76. do {
  77. updateBoard(); // display update board
  78.  
  79. cout << "Player " << getPlayer() <<" choose a position between (0 - 8)." <<endl;
  80. cin >> spot;
  81.  
  82. if (spot < 0 || spot > 8)
  83. {
  84. cout << "Invalid position! Please input a number of 0 thru 8." <<endl;
  85. continue;}
  86.  
  87.  
  88.  
  89. result = checkPosn(spot, getPlayer()); // to check if the position is taken
  90.  
  91. system("CLS");
  92.  
  93. if (result == false){
  94. nextPlayer(); }
  95.  
  96. if(checkWinner() == 'x' || checkWinner() == 'o' || checkWinner () == 't')
  97. blank = 'v';
  98. }
  99. while ( blank != 'v' );
  100.  
  101.  
  102. return;
  103.  
  104. }
  105.  
  106. //Checks to see who the winner will be
  107. char ticTac::checkWinner()
  108. {
  109. char game = ' ';
  110. int check = 1;
  111.  
  112. // Check if X wins
  113. if (posn[0] == 'x' && posn[1] == 'x' && posn[2] == 'x') game = 'x';
  114. if (posn[3] == 'x' && posn[4] == 'x' && posn[5] == 'x') game = 'x';
  115. if (posn[6] == 'x' && posn[7] == 'x' && posn[8] == 'x') game = 'x';
  116. if (posn[0] == 'x' && posn[3] == 'x' && posn[6] == 'x') game = 'x';
  117. if (posn[1] == 'x' && posn[4] == 'x' && posn[7] == 'x') game = 'x';
  118. if (posn[2] == 'x' && posn[5] == 'x' && posn[8] == 'x') game = 'x';
  119. if (posn[0] == 'x' && posn[4] == 'x' && posn[8] == 'x') game = 'x';
  120. if (posn[2] == 'x' && posn[4] == 'x' && posn[6] == 'x') game = 'x';
  121. if (game == 'x' )
  122. {cout << "Player1 wins the game." <<endl; return game; };
  123.  
  124. // Check if O wins
  125. if (posn[0] == 'o' && posn[1] == 'o' && posn[2] == 'o') game = 'o';
  126. if (posn[3] == 'o' && posn[4] == 'o' && posn[5] == 'o') game = 'o';
  127. if (posn[6] == 'o' && posn[7] == 'o' && posn[8] == 'o') game = 'o';
  128. if (posn[0] == 'o' && posn[3] == 'o' && posn[6] == 'o') game = 'o';
  129. if (posn[1] == 'o' && posn[4] == 'o' && posn[7] == 'o') game = 'o';
  130. if (posn[2] == 'o' && posn[5] == 'o' && posn[8] == 'o') game = 'o';
  131. if (posn[0] == 'o' && posn[4] == 'o' && posn[8] == 'o') game = 'o';
  132. if (posn[2] == 'o' && posn[4] == 'o' && posn[6] == 'o') game = 'o';
  133. if (game == 'o' )
  134. {cout << "Player2 wins the game." <<endl; return game; };
  135.  
  136. for(int i = 0; i < 8; i++ ){
  137. if(posn[i] >= '1' && posn[i] <= '8' ){
  138. check = 0;
  139. break;
  140. }
  141. }
  142.  
  143. // Check if tie
  144. if (game != 'x' && game != 'o' && check == 1) game = 't';
  145. if (game == 't')
  146. {cout << "Tie Game." <<endl; return game; };
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. return game;
  154. }
  155.  
  156. //Checks to see if the position is taken
  157. bool ticTac::checkPosn(int spot, char player)
  158. {
  159.  
  160. if (posn[spot] == 'x' || posn[spot] == 'o'){
  161. cout << "Position already taken, please choose another position." <<endl;
  162. system ("pause");
  163. return true;
  164. }
  165. else {
  166. posn[spot] = player;
  167. cout << "Nice move." <<endl;
  168. system ("pause");
  169. counter++;
  170. return false;
  171.  
  172. }
  173.  
  174.  
  175.  
  176. return false;
  177.  
  178. }
  179.  
  180. //Tells the next player that it's thier turn
  181. void ticTac::nextPlayer()
  182. {
  183. if (player == 'x')
  184. player = 'o';
  185. else player = 'x';
  186. return;
  187. }
  188.  
  189. //This switches between players
  190. char ticTac::getPlayer()
  191. {
  192. return player;
  193. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,676
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 262
Lerner Lerner is offline Offline
Posting Virtuoso
 
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster
 
0
  #3
Oct 12th, 2009
Thank you for the info. I will try that. Again thank you.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 23
Reputation: countrygirl1970 is an unknown quantity at this point 
Solved Threads: 0
countrygirl1970 countrygirl1970 is offline Offline
Newbie Poster

Almost There

 
0
  #4
Oct 14th, 2009
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]
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC