Advertisement
Guest User

Untitled

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