SHARE
TWEET

Untitled

a guest Mar 24th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <windows.h>
  3. #include <random>
  4.  
  5. unsigned long long rdtsc() {
  6.     unsigned int lo, hi;
  7.     __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
  8.     return (unsigned long long) hi << 32 | lo;
  9. }
  10.  
  11. std::mt19937 rng(rdtsc()); // NOLINT(cert-err58-cpp)
  12.  
  13. int randInt(int min, int max) {
  14.     std::uniform_int_distribution<int> uni(min, max);
  15.     return uni(rng);
  16. }
  17.  
  18.  
  19. void cls( HANDLE hConsole )
  20. {
  21.     COORD coordScreen = { 0, 0 };    // home for the cursor
  22.     DWORD cCharsWritten;
  23.     CONSOLE_SCREEN_BUFFER_INFO csbi;
  24.     DWORD dwConSize;
  25.     if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
  26.         return;
  27.     dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  28.     if( !FillConsoleOutputCharacter( hConsole, (TCHAR) ' ',
  29.                                      dwConSize, coordScreen, &cCharsWritten ))
  30.         return;
  31.     if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
  32.         return;
  33.     if( !FillConsoleOutputAttribute( hConsole, csbi.wAttributes,
  34.                                      dwConSize, coordScreen, &cCharsWritten ))
  35.         return;
  36.     SetConsoleCursorPosition( hConsole, coordScreen );
  37. }
  38.  
  39. void narysujPlansze(char tablica[9]){
  40.     std::cout << char(201) << char(205) << char(205) << char(205) << char(203) << char(205) << char(205) << char(205) << char(203) << char(205) << char(205) << char(205) << char(187) << "\n";
  41.     std::cout << char(186) << " " << tablica[0] << " " << char(186) << " " << tablica[1] << " " << char(186) << " " << tablica[2] << " " << char(186) << "\n";
  42.     std::cout << char(204) << char(205) << char(205)  << char(205) << char(206) << char(205) << char(205) << char(205) << char(206) << char(205) << char(205) << char(205) << char(185) << "\n";
  43.     std::cout << char(186) << " " << tablica[3] << " " << char(186) << " " << tablica[4] << " " << char(186) << " " << tablica[5] << " " << char(186) << "\n";
  44.     std::cout << char(204) << char(205) << char(205) << char(205) << char(206) << char(205) << char(205) << char(205) << char(206) << char(205) << char(205) << char(205) << char(185) << "\n";
  45.     std::cout << char(186) << " " << tablica[6] << " " << char(186) << " " << tablica[7] << " " << char(186) << " " << tablica[8] << " " << char(186) << "\n";
  46.     std::cout << char(200) << char(205) << char(205) << char(205) << char(202) << char(205) << char(205) << char(205) << char(202) << char(205) << char(205) << char(205) << char(188) << "\n";
  47. }
  48.  
  49. bool checkWinCondition(const char tablica[]){
  50.     for(int i = 0; i<3; i++){
  51.         // X
  52.         if(tablica[i*3] == 'X' && tablica[i*3+1] == 'X' && tablica[i*3+2] == 'X'){
  53.             std::cout << "WYGRANA X";
  54.             return true;
  55.         }
  56.         if(tablica[i] == 'X' && tablica[i+3] == 'X' && tablica[i+6] == 'X'){
  57.             std::cout << "WYGRANA X";
  58.             return true;
  59.         }
  60.         // O
  61.         if(tablica[i*3] == 'O' && tablica[i*3+1] == 'O' && tablica[i*3+2] == 'O'){
  62.             std::cout << "WYGRANA O";
  63.             return true;
  64.         }
  65.         if(tablica[i] == 'O' && tablica[i+3] == 'O' && tablica[i+6] == 'O'){
  66.             std::cout << "WYGRANA O";
  67.             return true;
  68.         }
  69.     }
  70.     // X
  71.     if(tablica[0] == 'X' && tablica[4] == 'X' && tablica[8] == 'X'){
  72.         std::cout << "WYGRANA X";
  73.         return true;
  74.     }
  75.     if(tablica[2] == 'X' && tablica[4] == 'X' && tablica[6] == 'X'){
  76.         std::cout << "WYGRANA X";
  77.         return true;
  78.     }
  79.     // O
  80.     if(tablica[0] == 'O' && tablica[4] == 'O' && tablica[8] == 'O'){
  81.         std::cout << "WYGRANA O";
  82.         return true;
  83.     }
  84.     if(tablica[2] == 'O' && tablica[4] == 'O' && tablica[6] == 'O'){
  85.         std::cout << "WYGRANA O";
  86.         return true;
  87.     }
  88.     return false;
  89. }
  90. void koloKrzyzyk(){
  91.  
  92.     HANDLE hStdout;
  93.     hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  94.  
  95.     std::string in;
  96.     char tablica[9] = {};
  97.     bool gameState = true;
  98.     int runda = 0;
  99.  
  100.     narysujPlansze(tablica);
  101.  
  102.     while(gameState) {
  103.         std::cout << "Podaj litere i cyfre gdzie chcesz postawic X: (np. A3)\n";
  104.         // PLAYER
  105.         while(true) {
  106.             std::cin >> in;
  107.             int index = in[1] - '1' + (in[0] - 'A') * 3;
  108.             if (in.length() != 2 || (in[0] - 'A') > 2 || (in[1] - '1') > 2 || tablica[index]=='X' || tablica[index]=='O') {
  109.                 cls(hStdout);
  110.                 narysujPlansze(tablica);
  111.                 std::cout << "Bledne dane\n";
  112.                 in.clear();
  113.             } else {
  114.                 tablica[index] = 'X';
  115.                 cls(hStdout);
  116.                 narysujPlansze(tablica);
  117.                 runda++;
  118.                 break;
  119.             }
  120.         }
  121.         if(checkWinCondition(tablica)) {
  122.             gameState = false;
  123.             break;
  124.         }
  125.         // AI
  126.         while(runda <= 4) {
  127.             int rand = randInt(0, 8);
  128.             if (tablica[rand] != 'X' && tablica[rand] != 'O') {
  129.                 tablica[rand] = 'O';
  130.                 cls(hStdout);
  131.                 narysujPlansze(tablica);
  132.                 std::cout << rand  << " Runda: " << runda << '\n';
  133.                 break;
  134.             }
  135.  
  136.         }
  137.         if(checkWinCondition(tablica)) {
  138.             gameState = false;
  139.             break;
  140.         }
  141.         //REMIS
  142.         if(runda == 5){
  143.             cls(hStdout);
  144.             narysujPlansze(tablica);
  145.             std::cout << "REMIS" << '\n';
  146.             gameState = false;
  147.         }
  148.     }
  149. }
  150.  
  151. int main(){
  152.     koloKrzyzyk()
  153. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top