Advertisement
ptrawt

259201 Lab14.3

Nov 21st, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 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.     bool mode = true;
  14.     int p;
  15.     cout<<"Do you want to play 1P or 2P?(1 or 2): ";
  16.     cin>>p;
  17.     if (p == 1)
  18.     {
  19.         mode = false;
  20.     }
  21.    
  22.     srand(time(NULL));
  23.  
  24.     print_table(table);
  25.  
  26.     int winner=0,L,C;
  27.     while(winner == 0 && check_round < 9)
  28.     {
  29.         if(player == 0)
  30.         {
  31.             if(mode)
  32.             {
  33.                 do
  34.                 {
  35.                     cout<<"Enter coordinate for 'o'(e.g., 1 2): ";
  36.                     cin>>L>>C;
  37.                 } while(L<0||L>2||C<0||C>2||table[L][C]!=0);
  38.                 table[L][C] = 1;
  39.             }
  40.             else
  41.             {
  42.                 do
  43.                 {
  44.                     L = rand() % 3;
  45.                     C = rand() % 3;
  46.                 } while(table[L][C]!=0);
  47.                 table[L][C] = 1;
  48.             }
  49.         }
  50.         else
  51.         {
  52.             do
  53.             {
  54.                 cout<<"Enter coordinate for 'x'(e.g., 1 2): ";
  55.                 cin>>L>>C;
  56.             } while(L<0||L>2||C<0||C>2||table[L][C]!=0);
  57.             table[L][C] = 2;           
  58.         }
  59.         check_round++;
  60.         winner = check_winner(table);
  61.         player = !player;
  62.         cout<<endl;
  63.         print_table(table);
  64.     }
  65.  
  66.     if(check_round == 9 && winner == 0)
  67.     {
  68.         cout<<"Draw\n";
  69.     }
  70.     else if(winner == 1)
  71.     {
  72.         cout<<"Player 'o' win\n";
  73.     }
  74.     else if(winner == 2)
  75.     {
  76.         cout<<"Player 'x' win\n";
  77.     }
  78.     return 0;
  79. }
  80.  
  81. void print_table(int t[3][3])
  82. {
  83.     for (int i = 0; i < 3; ++i)
  84.     {
  85.         for (int j = 0; j < 3; ++j)
  86.         {
  87.             cout<<" ";
  88.             if(t[i][j] == 1)
  89.             {
  90.                 cout <<"o";
  91.             }
  92.             else if (t[i][j] == 2)
  93.             {
  94.                 cout<<"x";
  95.             }
  96.             else
  97.             {
  98.                 cout<<" ";
  99.             }
  100.             cout<<" ";
  101.             if(j!=2)
  102.             {
  103.                 cout<<"|";
  104.             }
  105.         }
  106.         cout<<endl;
  107.         if (i!=2)
  108.         {
  109.             cout<<"---+---+---\n";
  110.         }
  111.     }
  112. }
  113.  
  114. int check_winner(int t[3][3])
  115. {
  116.     if(t[0][0] != 0 && t[0][0] == t[0][1] && t[0][1] == t[0][2])
  117.         return t[0][0];
  118.     else if(t[1][0] != 0 && t[1][0] == t[1][1] && t[1][1] == t[1][2])
  119.         return t[1][0];
  120.     else if(t[2][0] != 0 && t[2][0] == t[2][1] && t[2][1] == t[2][2])
  121.         return t[2][0];
  122.  
  123.     else if(t[0][0] != 0 && t[0][0] == t[1][0] && t[1][0] == t[2][0])
  124.         return t[0][0];
  125.     else if(t[0][1] != 0 && t[0][1] == t[1][1] && t[1][1] == t[2][1])
  126.         return t[0][1];
  127.     else if(t[0][2] != 0 && t[0][2] == t[1][2] && t[1][2] == t[2][2])
  128.         return t[0][2];
  129.  
  130.     else if(t[0][0] != 0 && t[0][0] == t[1][1] && t[1][1] == t[2][2])
  131.         return t[0][0];
  132.     else if(t[2][0] != 0 && t[2][0] == t[1][1] && t[1][1] == t[0][2])
  133.         return t[2][0];
  134.     else
  135.         return 0;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement