Advertisement
Guest User

tictactoe

a guest
Aug 19th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.69 KB | None | 0 0
  1. #include <iostream>;
  2. using namespace std;
  3.  
  4.     void drawBoard();
  5.  
  6.     char box1 = '7';
  7.     char box2 = '8';
  8.     char box3 = '9';
  9.     char box4 = '4';
  10.     char box5 = '5';
  11.     char box6 = '6';
  12.     char box7 = '1';
  13.     char box8 = '2';
  14.     char box9 = '3';
  15.     char player1Move = '0';
  16.     int playerNum = 0;
  17.     bool turnOver = false;
  18.  
  19.     void interpretInput(int player);
  20.     void playerTurn1();
  21.     bool gameOver = false;
  22.     void gameOverCheck();
  23.     void drawFinalBoard();
  24.     void playerTurn2();
  25.     void ticTacToe();
  26.     void main(){
  27.    
  28.     ticTacToe();
  29.  
  30.     system("PAUSE");
  31.    
  32.     }
  33.  
  34. void ticTacToe(){
  35.  
  36.     cout << "LET'S PLAY TICTACTOE MOTHERFUCKER!!    " << endl << endl;
  37.     cout << endl << box1 << " | " << box2 << " | " << box3 << endl;
  38.             cout << "---"<<         "---"<<          "---" << endl;
  39.             cout << box4 << " | " << box5 << " | " << box6 << endl;
  40.             cout << "---"<<         "---"<<          "---" << endl;
  41.             cout << box7 << " | " << box8 << " | " << box9 << endl << endl;
  42.     cout << "Press 'enter' to begin the game.";
  43.    
  44.     //will just pause the program for waiting for the player to begin
  45.     cin.get();
  46.    
  47.     //starts the game loop until a winner is found
  48.     playerTurn1();
  49.  
  50. }
  51.  
  52. //draws the current board on screen
  53. void drawBoard(){
  54.    
  55.     system("CLS");
  56.     cout << "It is player " << playerNum << "'s turn now: "<< endl << endl;
  57.  
  58.     cout << endl << box1 << " | " << box2 << " | " << box3 << endl;
  59.             cout << "---"<<         "---"<<          "---" << endl;
  60.             cout << box4 << " | " << box5 << " | " << box6 << endl;
  61.             cout << "---"<<         "---"<<          "---" << endl;
  62.             cout << box7 << " | " << box8 << " | " << box9 << endl << endl;
  63. }
  64.  
  65. void drawFinalBoard(){
  66.  
  67.     cout << endl << box1 << " | " << box2 << " | " << box3 << endl;
  68.             cout << "---"<<         "---"<<          "---" << endl;
  69.             cout << box4 << " | " << box5 << " | " << box6 << endl;
  70.             cout << "---"<<         "---"<<          "---" << endl;
  71.             cout << box7 << " | " << box8 << " | " << box9 << endl << endl;
  72.  
  73. }
  74.  
  75. //reads the input and changes the box if it hasn't been selected before
  76. void interpretInput(int player){
  77.  
  78.     if(player == 1){cout << endl << "Player is " << player << endl;
  79.        
  80. //player 1 input check and select
  81.         switch(player1Move){
  82.    
  83.             case '0':
  84.                 cout << "Please make a move!";
  85.                 interpretInput(player);
  86.                 break;
  87.  
  88.             case '1':
  89.                 box7 = (box7 == '1') ? 'X': box7;
  90.                 break;
  91.  
  92.             case '2':
  93.                 box8 = (box8 == '2') ? 'X': box8;
  94.                 drawBoard();
  95.                 break;
  96.  
  97.             case '3':
  98.                 box9 = (box9 == '3') ? 'X': box9;
  99.                 drawBoard();
  100.                 break;
  101.  
  102.             case '4':
  103.                 box4 = (box4 == '4') ? 'X': box4;
  104.                 drawBoard();
  105.                 break;
  106.  
  107.             case '5':
  108.                 box5 = (box5 == '5') ? 'X': box5;
  109.                 drawBoard();
  110.                 break;
  111.  
  112.             case '6':
  113.                 box6 = (box6 == '6') ? 'X': box6;
  114.                 drawBoard();
  115.                 break;
  116.  
  117.             case '7':
  118.                 box1 = (box1 == '7') ? 'X': box1;
  119.                 drawBoard();
  120.                 break;
  121.  
  122.             case '8':
  123.                 box2 = (box2 == '8') ? 'X': box2;
  124.                 drawBoard();
  125.                 break;
  126.  
  127.             case '9':
  128.                 box3 = (box3 == '9') ? 'X': box3;
  129.                 drawBoard();
  130.                 break;
  131.  
  132.             default:
  133.                 cout << endl << "Try again!!" << endl;
  134.                 system("PAUSE");
  135.                 playerTurn1();      }}      else{cout << endl << "Player is " << player << endl;
  136.        
  137. //player 2 input check and select
  138.         switch(player1Move){
  139.    
  140.             case '0':
  141.                 cout << "Please make a move!";
  142.                 interpretInput(player);
  143.                 break;
  144.  
  145.             case '1':
  146.                 box7 = (box7 == '1') ? 'O': box7;
  147.                 drawBoard();
  148.                 break;
  149.  
  150.             case '2':
  151.                 box8 = (box8 == '2') ? 'O': box8;
  152.                 drawBoard();
  153.                 break;
  154.  
  155.             case '3':
  156.                 box9 = (box9 == '3') ? 'O': box9;
  157.                 drawBoard();
  158.                 break;
  159.  
  160.             case '4':
  161.                 box4 = (box4 == '4') ? 'O': box4;
  162.                 drawBoard();
  163.                 break;
  164.  
  165.             case '5':
  166.                 box5 = (box5 == '5') ? 'O': box5;
  167.                 drawBoard();
  168.                 break;
  169.  
  170.             case '6':
  171.                 box6 = (box6 == '6') ? 'O': box6;
  172.                 drawBoard();
  173.                 break;
  174.  
  175.             case '7':
  176.                 box1 = (box1 == '7') ? 'O': box1;
  177.                 drawBoard();
  178.                 break;
  179.  
  180.             case '8':
  181.                 box2 = (box2 == '8') ? 'O': box2;
  182.                 drawBoard();
  183.                 break;
  184.  
  185.             case '9':
  186.                 box3 = (box3 == '9') ? 'O': box3;
  187.                 drawBoard();
  188.                 break;
  189.  
  190.             default:
  191.                 cout << endl << "Try again!!" << endl;
  192.                 system("PAUSE");
  193.                 playerTurn2();      }}
  194.        
  195.        
  196. }
  197.  
  198. //cycles between player turns
  199. void playerTurn1(){
  200.  
  201.         do{
  202.            
  203.             playerNum = 1;
  204.             drawBoard();
  205.             cin >> player1Move;
  206.             interpretInput(playerNum);
  207.             gameOverCheck();
  208.             turnOver = true;
  209.            
  210.         }while(gameOver != true && turnOver != true);
  211.     turnOver = false;
  212.     playerTurn2();
  213.     }
  214.  
  215. void playerTurn2(){
  216.  
  217.     do{
  218.            
  219.             playerNum = 2;
  220.             drawBoard();
  221.             cin >> player1Move;
  222.             interpretInput(playerNum);
  223.             gameOverCheck();
  224.             turnOver = true;
  225.            
  226.         }while(gameOver != true && turnOver != true);
  227.     turnOver = false;
  228.     playerTurn1();
  229.     }
  230.  
  231. //checks if the game is over and quits the game if it is...and if not...will not do anything
  232. void gameOverCheck(){
  233.  
  234.     if((box1==box2) && (box1==box3) || (box3==box6) && (box3==box9) || (box7==box8) && (box7==box9) || (box1==box4) && (box1==box7) || (box2==box5) && (box2==box8) || (box4==box5) && (box4==box6) || (box1==box5) && (box1==box9) || (box3==box5) && (box3==box7)){
  235.        
  236.         system("CLS");
  237.         cout << endl << "!!!!!!!!!!!!!!!!!  GAMEOVER  !!!!!!!!!!!!!!!!!!!";
  238.         gameOver = true;
  239.         cout << endl << "---------------Player " << playerNum << " won!!!!---------------"<< endl << endl;
  240.         drawFinalBoard();
  241.  
  242.         system("PAUSE");
  243.         exit(EXIT_FAILURE);
  244.    
  245.     }
  246.     else if(box1 != '7' && box2 != '8' && box3 != '9' && box4 != '4' && box5 != '5' && box6 != '6' && box7 != '1' && box8 != '2' && box9 != '3'){
  247.        
  248.         cout << "!!!!!!!!!!!!!!!! THE GAME WAS A DRAW !!!!!!!!!!!!!!!!" << endl;
  249.         system("PAUSE");
  250.         exit(EXIT_FAILURE);
  251.    
  252.    
  253.     }
  254.  
  255.     else {gameOver = false;}
  256.  
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement