Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.19 KB | None | 0 0
  1.  
  2. //tic tac toe game
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. int const SIZE = 4;
  7.  
  8. int main() {
  9.    
  10.     char matrix[SIZE][SIZE]{ { ' ' ,'A','B','C' },{ '1',' ',' ',' '},{ '2',' ',' ',' '},{ '3',' ',' ',' '} };
  11.     int i, j, k;
  12.  
  13.     char ch;
  14.     int n;
  15.     bool win = false;
  16.     bool isX = true;
  17.  
  18.    
  19.  
  20.     for (k = 1; k <= 9 && !win; k++) {
  21.  
  22.         //displaying game status for x
  23.  
  24.  
  25.         if (matrix[1][3] == 'x' && matrix[2][2] == 'x' && matrix[3][1] == 'x') {
  26.             cout << " X wins!" << endl;
  27.             win = true;
  28.             break;
  29.         }
  30.  
  31.         if (matrix[1][1] == 'x' && matrix[2][2] == 'x' && matrix[3][3] == 'x') {
  32.             cout << " X wins!" << endl;
  33.             win = true;
  34.             break;
  35.         }
  36.  
  37.         if (matrix[1][1] == 'x' && matrix[1][2] == 'x' && matrix[1][3] == 'x') {
  38.             cout << " X wins!" << endl;
  39.             win = true;
  40.             break;
  41.         }
  42.  
  43.         if (matrix[2][1] == 'x' && matrix[2][2] == 'x' && matrix[2][3] == 'x') {
  44.             cout << " X wins!" << endl;
  45.             win = true;
  46.             break;
  47.         }
  48.  
  49.         if (matrix[3][1] == 'x' && matrix[3][2] == 'x' && matrix[3][3] == 'x') {
  50.             cout << " X wins!" << endl;
  51.             win = true;
  52.         }
  53.  
  54.         if (matrix[1][1] == 'x' && matrix[2][1] == 'x' && matrix[3][1] == 'x') {
  55.             cout << " X wins!" << endl;
  56.             win = true;
  57.             break;
  58.         }
  59.  
  60.         if (matrix[1][2] == 'x' && matrix[2][2] == 'x' && matrix[3][2] == 'x') {
  61.             cout << " X wins!" << endl;
  62.             win = true;
  63.             break;
  64.         }
  65.  
  66.         if (matrix[1][3] == 'x' && matrix[2][3] == 'x' && matrix[3][3] == 'x') {
  67.             cout << " X wins!" << endl;
  68.             win = true;
  69.             break;
  70.         }
  71.  
  72.         //displaying game status for O
  73.  
  74.         if (matrix[1][3] == 'o' && matrix[2][3] == 'o' && matrix[3][3] == 'o') {
  75.             cout << " O wins!" << endl;
  76.             win = true;
  77.             break;
  78.         }
  79.  
  80.         if (matrix[1][3] == 'o' && matrix[2][2] == 'o' && matrix[3][1] == 'o') {
  81.             cout << " O wins!" << endl;
  82.             win = true;
  83.             break;
  84.         }
  85.         if (matrix[1][1] == 'o' && matrix[2][2] == 'o' && matrix[3][3] == 'o') {
  86.             cout << " O wins!" << endl;
  87.             win = true;
  88.             break;
  89.         }
  90.         if (matrix[1][1] == 'o' && matrix[1][2] == 'o' && matrix[1][3] == 'o') {
  91.             cout << " O wins!" << endl;
  92.             win = true;
  93.             break;
  94.         }
  95.         if (matrix[2][1] == 'o' && matrix[2][2] == 'o' && matrix[2][3] == 'o') {
  96.             cout << " O wins!" << endl;
  97.             win = true;
  98.             break;
  99.         }
  100.         if (matrix[3][1] == 'o' && matrix[3][2] == 'o' && matrix[3][3] == 'o') {
  101.             cout << " O wins!" << endl;
  102.             win = true;
  103.             break;
  104.         }
  105.         if (matrix[1][1] == 'o' && matrix[2][1] == 'o' && matrix[3][1] == 'o') {
  106.             cout << " O wins!" << endl;
  107.             win = true;
  108.             break;
  109.         }
  110.         if (matrix[1][2] == 'o' && matrix[2][2] == 'o' && matrix[3][2] == 'o') {
  111.             cout << " O wins!" << endl;
  112.             win = true;
  113.             break;
  114.         }
  115.  
  116.  
  117.         if (isX == true) {
  118.            
  119.  
  120.             cout << "* Player number 1 (X) * " << endl;
  121.             cout << "Please enter cell input: ";
  122.             cin >> ch >> n;
  123.             cout << endl;
  124.  
  125.            
  126.             if (ch == 'A' && n == 1) {
  127.                 matrix[1][1] = 'x';
  128.             }
  129.            
  130.             if (ch == 'A' && n == 2) {
  131.                 matrix[2][1] = 'x';
  132.             }
  133.            
  134.             if (ch == 'A' && n == 3) {
  135.                 matrix[3][1] = 'x';
  136.             }
  137.            
  138.             if (ch == 'B' && n == 1) {
  139.                 matrix[1][2] = 'x';
  140.             }
  141.            
  142.             if (ch == 'B' && n == 2) {
  143.                 matrix[2][2] = 'x';
  144.             }
  145.            
  146.             if (ch == 'B' && n == 3) {
  147.                 matrix[3][2] = 'x';
  148.             }
  149.            
  150.             if (ch == 'C' && n == 1) {
  151.                 matrix[1][3] = 'x';
  152.             }
  153.            
  154.             if (ch == 'C' && n == 2) {
  155.                 matrix[2][3] = 'x';
  156.             }
  157.            
  158.             if (ch == 'C' && n == 3) {
  159.                 matrix[3][3] = 'x';
  160.             }
  161.  
  162.  
  163.             //printing the matrix
  164.  
  165.             for (i = 0; i < SIZE; i++) {
  166.  
  167.                 for (j = 0; j < SIZE; j++) {
  168.  
  169.                     cout << matrix[i][j] << "\t";
  170.                 }
  171.                 cout << endl << endl;
  172.             }
  173.         }
  174.  
  175.  
  176.         if (isX == false) {
  177.            
  178.  
  179.             cout << "* Player number 2 (O) * " << endl;
  180.             cout << "Please enter cell input: ";
  181.             cin >> ch >> n;
  182.             cout << endl;
  183.  
  184.            
  185.             if (ch == 'A' && n == 1) {
  186.                 matrix[1][1] = 'o';
  187.             }
  188.        
  189.            
  190.             if (ch == 'A' && n == 2) {
  191.                 matrix[2][1] = 'o';
  192.             }
  193.            
  194.             if (ch == 'A' && n == 3) {
  195.                 matrix[3][1] = 'o';
  196.             }
  197.            
  198.             if (ch == 'B' && n == 1) {
  199.                 matrix[1][2] = 'o';
  200.             }
  201.            
  202.             if (ch == 'B' && n == 2) {
  203.                 matrix[2][2] = 'o';
  204.             }
  205.            
  206.             if (ch == 'B' && n == 3) {
  207.                 matrix[3][2] = 'o';
  208.             }
  209.            
  210.             if (ch == 'C' && n == 1) {
  211.                 matrix[1][3] = 'o';
  212.             }
  213.            
  214.             if (ch == 'C' && n == 2) {
  215.                 matrix[2][3] = 'o';
  216.             }
  217.            
  218.             if (ch == 'C' && n == 3) {
  219.                 matrix[3][3] = 'o';
  220.             }
  221.        
  222.  
  223.             //printing the matrix
  224.  
  225.             for (i = 0; i < SIZE; i++) {
  226.  
  227.                 for (j = 0; j < SIZE; j++) {
  228.  
  229.                     cout << matrix[i][j] << "\t";
  230.                 }
  231.                 cout << endl << endl;
  232.  
  233.             }
  234.         }
  235.         isX = !isX;
  236.     }
  237.  
  238.     if (win == false) {
  239.         cout << "it's a tie" << endl;
  240.     }
  241.  
  242.     system("pause");
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement