Advertisement
Guest User

Untitled

a guest
Aug 7th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string>
  6. #include <sstream>
  7. #include <time.h>
  8.  
  9. //This function initializes a nine-char grid with underscores
  10. void setGrid ( char grid [] )
  11. {
  12.    
  13.     for ( int i = 0; i < 9; i++ )
  14.     {
  15.         grid[i] = '_';
  16.     }
  17. }
  18.  
  19. //This function prints the grid
  20. void showGrid ( char grid [] )
  21. {
  22.     std::cout << "\n";
  23.     for ( int i = 0; i < 9; i++ )
  24.     {
  25.         if ( i == 3 || i == 6 )
  26.         {
  27.             std::cout << "\n";
  28.         }
  29.         std::cout << grid[i] << " ";
  30.     }
  31.     std::cout << "\n";
  32. }
  33.  
  34. //This function ensures player's input is valid
  35. int checkInput ( char input [] )
  36. {
  37.     int iInput;
  38.     while ( !isdigit(input[0]) )
  39.     {  
  40.         std::cout << "Please enter a valid number: ";
  41.         std::cin >> input;
  42.     }
  43.     iInput = std::atoi ( input );
  44.     return iInput;
  45. }
  46.  
  47. //This function generates a random play for the computer, based on player's moves
  48. int computerCheck ( char grid [] )
  49. {
  50.     std::srand ( time (NULL) );
  51.  
  52.     for ( int i = 0; i < 9; i+=3 )  //Note i+=3 iterator to check row by row
  53.     {
  54.         if ( grid[i] == 'x' )
  55.         {
  56.             if ( grid[i+1] == 'x' && grid[i+2] == '_' )
  57.                 return i + 3;
  58.             else if ( grid[i+1] == '_' && grid[i+2] == 'x' )
  59.                 return i + 2;
  60.         }
  61.         else if ( grid[i] == '_' )
  62.         {
  63.             if ( grid[i+1] == 'x' && grid[i+2] == 'x' )
  64.                 return i + 1;
  65.         }
  66.     }
  67.     for ( int j = 0; j < 3; j++ )   //checks columns
  68.     {
  69.         if ( grid[j] == 'x' )
  70.         {
  71.             if ( grid[j+3] == 'x' && grid[j+6] == '_' )
  72.                 return j + 7;
  73.             else if ( grid[j+3] == '_' && grid[j+6] == 'x' )
  74.                 return j + 4;
  75.         }
  76.         else if ( grid[j] == '_' )
  77.         {
  78.             if ( grid[j+3] == 'x' && grid[j+6] == 'x' )
  79.                 return j + 1;
  80.         }
  81.     }
  82.     return rand() % 9 + 1;
  83. }
  84.  
  85. //This function generates the computer's play
  86. char computerPlay ( char grid [] )
  87. {
  88.     std::string result;
  89.     char toReturn;
  90.     int compGuess = computerCheck ( grid );
  91.    
  92.     std::ostringstream convert;
  93.     convert << compGuess;
  94.     result = convert.str();
  95.     toReturn = result[0];
  96.     return toReturn;
  97. }
  98.  
  99. //This function plays any square, whether user or computer
  100. void playSquare ( char grid [], char player, char input [] )
  101. {
  102.     int iInput = checkInput ( input );
  103.     while ( iInput < 1 || iInput > 9 )
  104.     {
  105.         std::cout << "That number doesn't work. Try again: ";
  106.         if ( player == 'x' )
  107.             std::cin >> input; 
  108.         else
  109.         {  
  110.             input[0] = computerPlay ( grid );
  111.             std::cout << input;
  112.         }
  113.         iInput = checkInput ( input );
  114.     }
  115.     while ( grid[iInput-1] != '_' )
  116.     {  
  117.         std::cout << "That spot's been taken. Try again: ";
  118.         if ( player == 'x' )
  119.             std::cin >> input; 
  120.         else
  121.         {  
  122.             input[0] = computerPlay ( grid );
  123.             std::cout << input;
  124.         }
  125.         iInput = checkInput ( input );
  126.     }
  127.     grid[iInput-1] = player;
  128. }          
  129.  
  130. //This function checks the grid for a winner
  131. bool checkGrid ( char grid [], char player )
  132. {
  133.     if ( grid[0] == player )
  134.     {
  135.         if ( grid[1] == player && grid[2] == player )
  136.             return true;
  137.         else if ( grid[3] == player && grid[6] == player )
  138.             return true;
  139.         else if ( grid[4] == player && grid[8] == player )
  140.             return true;
  141.     }
  142.     if ( grid[1] == player )
  143.     {
  144.          if ( grid[4] == player && grid[7] == player )
  145.             return true;
  146.     }
  147.     if ( grid[2] == player )
  148.     {
  149.                 if ( grid[4] == player && grid[6] == player )
  150.                             return true;
  151.                 else if ( grid[5] == player && grid[8] == player )
  152.                             return true;
  153.     }
  154.     if ( grid[3] == player )
  155.     {
  156.                 if ( grid[4] == player && grid[5] == player )
  157.                             return true;
  158.     }
  159.     if ( grid[6] == player )
  160.     {
  161.                 if ( grid[7] == player && grid[8] == player )
  162.                             return true;
  163.     }
  164.                        
  165.     return false;
  166. }
  167.  
  168. //main function
  169. int main() {
  170.            
  171.     //variable declarations    
  172.     char p1x = 'x';
  173.     char p2o = 'o';
  174.     bool isOver = false;
  175.     int counter = 0;
  176.     char userInput [100];
  177.     char gameGrid [9];
  178.     std::string winner = "It's a draw!";            
  179.    
  180.    
  181.         //initialization   
  182.     std::cout << "Time to play some Tic Tac Toe! Enter the number for where you want to place your mark (1 for upper-left, 5 for middle, 9 for lower-right, etc.)\n";
  183.     setGrid ( gameGrid );
  184.     showGrid ( gameGrid );
  185.  
  186.     //main algorithm
  187.     while ( !isOver || counter < 9 )
  188.     {
  189.         std::cout << "Player 1, enter your number: ";
  190.         std::cin >> userInput;
  191.         playSquare ( gameGrid, p1x, userInput );
  192.         showGrid ( gameGrid );
  193.         isOver = checkGrid ( gameGrid, p1x );
  194.     counter++;
  195.         if ( isOver )
  196.         {
  197.             winner = "Player 1 wins!";
  198.             break;
  199.         }
  200.    
  201.     if ( counter == 9 ) break;              
  202.        
  203.     std::cout << "Computer's pick: ";
  204.     userInput[0] = computerPlay ( gameGrid );
  205.         std::cout << userInput;
  206.     playSquare ( gameGrid, p2o, userInput );
  207.     showGrid ( gameGrid );
  208.         isOver = checkGrid ( gameGrid, p2o );
  209.         if ( isOver )
  210.         {  
  211.             winner = "Player 2 wins!";
  212.             break;
  213.         }
  214.     counter++;
  215.     };
  216.     std::cout << "Game over. " << winner;
  217.     std::cin >> counter;
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement