Advertisement
ptrawt

259201 Lab14.2

Nov 21st, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. #include "iostream"
  2. #include "ctime"
  3. #include "cstdlib"
  4. using namespace std;
  5.  
  6. void print_table(int t[3][3]);
  7. int check_winner(int t[3][3]);
  8.  
  9. int main()
  10. {
  11.     int table[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
  12.     int player = 0, check_round = 0;
  13.     //srand(time(NULL));
  14.  
  15.     print_table(table);
  16.  
  17.     int winner=0,L,C;
  18.     while(winner == 0 && check_round < 9)
  19.     {
  20.         if(player == 0)
  21.         {
  22.             do
  23.             {
  24.                 cout<<"Enter coordinate for 'o'(e.g., 1 2): ";
  25.                 cin>>L>>C;
  26.             } while(L<0||L>2||C<0||C>2||table[L][C]!=0);
  27.             table[L][C] = 1;
  28.         }
  29.         else
  30.         {
  31.             do
  32.             {
  33.                 cout<<"Enter coordinate for 'x'(e.g., 1 2): ";
  34.                 cin>>L>>C;
  35.             } while(L<0||L>2||C<0||C>2||table[L][C]!=0);
  36.             table[L][C] = 2;           
  37.         }
  38.         check_round++;
  39.         winner = check_winner(table);
  40.         player = !player;
  41.         cout<<endl;
  42.         print_table(table);
  43.     }
  44.  
  45.     if(check_round == 9 && winner == 0)
  46.     {
  47.         cout<<"Draw\n";
  48.     }
  49.     else if(winner == 1)
  50.     {
  51.         cout<<"Player 'o' win\n";
  52.     }
  53.     else if(winner == 2)
  54.     {
  55.         cout<<"Player 'x' win\n";
  56.     }
  57.     return 0;
  58. }
  59.  
  60. void print_table(int t[3][3])
  61. {
  62.     for (int i = 0; i < 3; ++i)
  63.     {
  64.         for (int j = 0; j < 3; ++j)
  65.         {
  66.             cout<<" ";
  67.             if(t[i][j] == 1)
  68.             {
  69.                 cout <<"o";
  70.             }
  71.             else if (t[i][j] == 2)
  72.             {
  73.                 cout<<"x";
  74.             }
  75.             else
  76.             {
  77.                 cout<<" ";
  78.             }
  79.             cout<<" ";
  80.             if(j!=2)
  81.             {
  82.                 cout<<"|";
  83.             }
  84.         }
  85.         cout<<endl;
  86.         if (i!=2)
  87.         {
  88.             cout<<"---+---+---\n";
  89.         }
  90.     }
  91. }
  92.  
  93. int check_winner(int t[3][3])
  94. {
  95.     if(t[0][0] != 0 && t[0][0] == t[0][1] && t[0][1] == t[0][2])
  96.         return t[0][0];
  97.     else if(t[1][0] != 0 && t[1][0] == t[1][1] && t[1][1] == t[1][2])
  98.         return t[1][0];
  99.     else if(t[2][0] != 0 && t[2][0] == t[2][1] && t[2][1] == t[2][2])
  100.         return t[2][0];
  101.  
  102.     else if(t[0][0] != 0 && t[0][0] == t[1][0] && t[1][0] == t[2][0])
  103.         return t[0][0];
  104.     else if(t[0][1] != 0 && t[0][1] == t[1][1] && t[1][1] == t[2][1])
  105.         return t[0][1];
  106.     else if(t[0][2] != 0 && t[0][2] == t[1][2] && t[1][2] == t[2][2])
  107.         return t[0][2];
  108.  
  109.     else if(t[0][0] != 0 && t[0][0] == t[1][1] && t[1][1] == t[2][2])
  110.         return t[0][0];
  111.     else if(t[2][0] != 0 && t[2][0] == t[1][1] && t[1][1] == t[0][2])
  112.         return t[2][0];
  113.     else
  114.         return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement