Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. void WypiszPlansze(char plansza[3][3])
  8. {
  9.     for(int i=0; i<3; i++)
  10.     {
  11.         for(int j=0; j<3; j++)
  12.         {
  13.             cout<<plansza[i][j]<<" | ";
  14.         }
  15.         cout<<endl;
  16.     }
  17. }
  18.  
  19. bool IsWon(char plansza[3][3])
  20. {
  21.     char X = 'X';
  22.     int i =0, j=0;
  23.     bool vartical = false;
  24.     for(i=0; i<3; i++)
  25.     {
  26.         int howManyX =0;
  27.         int howManyO =0;
  28.         for(j=0; j<3; j++)
  29.         {
  30.             if(vartical = false)
  31.             {
  32.             if(plansza[i][j] == 'X')
  33.             {
  34.                 howManyX +=1;
  35.             }
  36.             else if(plansza[i][j] == 'O')
  37.             {
  38.                 howManyO +=1;
  39.             }
  40.             }
  41.             else
  42.             {
  43.                 if(plansza[j][i] == 'X')
  44.                 {
  45.                     howManyX +=1;
  46.                 }
  47.                 else if(plansza[j][i] == 'O')
  48.                 {
  49.                     howManyO +=1;
  50.                 }
  51.             }
  52.         }
  53.         if(howManyX == 3)
  54.         {
  55.             cout<<"Wygral X"<<endl;
  56.             return true;
  57.         }
  58.         else if(howManyO == 3)
  59.         {
  60.             cout<<"Wygral O"<<endl;
  61.             return true;
  62.         }
  63.  
  64.         if(i == 3)
  65.         {
  66.             vartical = true;
  67.             i=0;
  68.         }
  69.     }
  70.  
  71.  
  72.     for(i = 0; i<3; i++)
  73.     {
  74.         if(plansza[i][i]) //TODO: z ukosami
  75.     }
  76.  
  77.     for(int i=0; i<2; i++)
  78.     {
  79.  
  80.         if(plansza[0][0]==X&&plansza[0][1]==X&&plansza[0][2]==X)
  81.         {
  82.             cout<<"wygrana "<<X<< endl;
  83.             return true;
  84.         }
  85.         if(plansza[1][0]==X&&plansza[1][1]==X&&plansza[1][2]==X)
  86.         {
  87.             cout<<"wygrana "<<X<< endl;
  88.             return true;
  89.         }
  90.         if(plansza[2][0]==X&&plansza[2][1]==X&&plansza[2][2]==X)
  91.         {
  92.             cout<<"wygrana "<<X<< endl;
  93.             return true;
  94.         }
  95.         if(plansza[0][0]==X&&plansza[1][0]==X&&plansza[2][0]==X)
  96.         {
  97.             cout<<"wygrana "<<X<< endl;
  98.             return true;
  99.         }
  100.         if(plansza[0][1]==X&&plansza[1][1]==X&&plansza[2][1]==X)
  101.         {
  102.             cout<<"wygrana "<<X<< endl;
  103.             return true;
  104.         }
  105.         if(plansza[0][2]==X&&plansza[1][2]==X&&plansza[2][2]==X)
  106.         {
  107.             cout<<"wygrana "<<X<< endl;
  108.             return true;
  109.         }
  110.         if(plansza[0][0]==X&&plansza[1][1]==X&&plansza[2][2]==X)
  111.         {
  112.             cout<<"wygrana "<<X<< endl;
  113.             return true;
  114.         }
  115.         if(plansza[0][2]==X&&plansza[1][1]==X&&plansza[2][0]==X)
  116.         {
  117.             cout<<"wygrana "<<X<< endl;
  118.             return true;
  119.         }
  120.  
  121.         X = 'O';
  122.     }
  123.  
  124.     return false;
  125. }
  126.  
  127. bool IsGameFinished(char plansza[3][3])
  128. {
  129.  
  130.     for(int i=0; i<3; i++)
  131.     {
  132.         for(int j=0; j<3; j++)
  133.         {
  134.             if(plansza[i][j]==' ')
  135.             {
  136.                 return false;
  137.             }
  138.         }
  139.     }
  140.  
  141.     return true;
  142. }
  143.  
  144. bool IsPlaceTaken(char plansza[3][3], int x, int y)
  145. {
  146.     if(plansza[x][y] != ' ')
  147.     {
  148.         return true;
  149.     }
  150.     else
  151.     {
  152.         return false;
  153.     }
  154. }
  155.  
  156. int main()
  157. {
  158.     char plansza[3][3];
  159.     char X = 'X';
  160.     char O = 'O';
  161.     int wspolrzedne[2];
  162.  
  163.     for(int i=0; i<3; i++)
  164.     {
  165.         for(int j=0; j<3; j++)
  166.         {
  167.             plansza[i][j]=' ';
  168.         }
  169.     }
  170.  
  171. WypiszPlansze(plansza);
  172.     cout<<"Podaj wspolrzedne Twojego ruchu:\n";
  173.     char focusedPlayer = X;
  174.     while(true)
  175.     {
  176.         cout<<"Kolejny ruch:\n";
  177.         cin>>wspolrzedne[0];
  178.         cin>>wspolrzedne[1];
  179.         if(IsPlaceTaken(plansza, wspolrzedne[0], wspolrzedne[1]))
  180.         {
  181.             cout<<"Podane miejsce jest zajete\n";
  182.             continue;
  183.         }
  184.  
  185.         plansza[wspolrzedne[0]] [wspolrzedne[1]] = focusedPlayer;
  186.         if(IsWon(plansza))
  187.         {
  188.             return 0;
  189.         }
  190.         else if(IsGameFinished(plansza))
  191.         {
  192.             cout<<"nikt nie wygral"<< endl;
  193.             return 0;
  194.         }
  195.  
  196.         if(focusedPlayer == X)
  197.         {
  198.             focusedPlayer = O;
  199.         }
  200.         else
  201.         {
  202.             focusedPlayer = X;
  203.         }
  204.         system("cls");
  205.         WypiszPlansze(plansza);
  206.     }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement