Advertisement
sashachca

Hursovaya

Nov 6th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.63 KB | None | 0 0
  1. //
  2. //  kursovayaC++.cpp
  3. //  Zadach C++
  4. //
  5. //
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <algorithm>
  9. #include <string>
  10. #include <clocale>
  11.  
  12.  
  13. using namespace std;
  14.  
  15. const char X = 'X', O = '0';
  16. const int fHeight = 15, fWidth = 15;
  17. int lengthForWin = 5;
  18.  
  19. int curLength;
  20. char field[fHeight][fWidth];
  21. int threatMatrix[fHeight][fWidth];
  22. int maxThreat;
  23.  
  24. void cleanField() {
  25.     for (int i = 0; i < fHeight; i++) {
  26.         for (int j = 0; j < fWidth; j++) {
  27.             field[i][j] = ' ';
  28.             threatMatrix[i][j] = 0;
  29.             maxThreat = 0;
  30.         }
  31.     }
  32.    
  33. }
  34.  
  35. void showField() {
  36.     for (int i = 0; i < fHeight; i++) {
  37.         for (int j = 0; j < fWidth; j++)
  38.             cout << field[i][j] << "|";
  39.         cout << endl;
  40.     }
  41.    
  42. }
  43.  
  44. int checkLine(int x, int y, int modX, int modY, char theCharacter) {
  45.     int modedX = x, modedY = y;
  46.     curLength = (field[x][y] == theCharacter) ? 1 : 0;
  47.    
  48.     while (modedX >= 0 && modedX < fHeight && modedY >= 0 && modedY < fWidth) {
  49.         modedX += modX;
  50.         modedY += modY;
  51.         if (theCharacter != field[modedX][modedY])
  52.             break;
  53.         curLength++;
  54.     }
  55.     modedX = x;
  56.     modedY = y;
  57.     while (modedX >= 0 && modedX < fHeight && modedY >= 0 && modedY < fWidth) {
  58.         modedX -= modX;
  59.         modedY -= modY;
  60.         if (theCharacter != field[modedX][modedY])
  61.             break;
  62.         curLength++;
  63.     }
  64.    
  65.     return curLength;
  66. }
  67.  
  68. bool isWinner(int x, int y, char theCharacter) {
  69.    
  70.     curLength = checkLine(x, y, 1, 0, theCharacter);
  71.     if (curLength >= lengthForWin)
  72.         return true;
  73.    
  74.     curLength = checkLine(x, y, 1, 1, theCharacter);
  75.     if (curLength >= lengthForWin)
  76.         return true;
  77.    
  78.     curLength = checkLine(x, y, -1, 1, theCharacter);
  79.     if (curLength >= lengthForWin)
  80.         return true;
  81.    
  82.     curLength = checkLine(x, y, 0, 1, theCharacter);
  83.     if (curLength >= lengthForWin)
  84.         return true;
  85.    
  86.     return false;
  87. }
  88.  
  89. void threatMatrixMod(int x, int y, char theCharacter) {
  90.     threatMatrix[x][y] = -1;
  91.    
  92.     if (theCharacter == X) {
  93.         for (int i = 1; i < lengthForWin; i++) {
  94.             if (threatMatrix[x - i][y - i] != -1)
  95.                 threatMatrix[x - i][y - i] = max(threatMatrix[x - i][y - i], checkLine(x - i, y - i, 1, 1, theCharacter));
  96.            
  97.             if (threatMatrix[x + i][y + i] != -1)
  98.                 threatMatrix[x + i][y + i] = max(threatMatrix[x + i][y + i], checkLine(x + i, y + i, 1, 1, theCharacter));
  99.            
  100.             if (threatMatrix[x][y - i] != -1)
  101.                 threatMatrix[x][y - i] = max(threatMatrix[x][y - i], checkLine(x, y - i, 0, 1, theCharacter));
  102.            
  103.             if (threatMatrix[x][y + i] != -1)
  104.                 threatMatrix[x][y + i] = max(threatMatrix[x][y + i], checkLine(x, y + i, 0, 1, theCharacter));
  105.            
  106.             if (threatMatrix[x + i][y - i] != -1)
  107.                 threatMatrix[x + i][y - i] = max(threatMatrix[x + i][y - i], checkLine(x + i, y - i, 1, -1, theCharacter));
  108.            
  109.             if (threatMatrix[x - i][y + i] != -1)
  110.                 threatMatrix[x - i][y + i] = max(threatMatrix[x - i][y + i], checkLine(x - i, y + i, 1, -1, theCharacter));
  111.            
  112.             if (threatMatrix[x - i][y] != -1)
  113.                 threatMatrix[x - i][y] = max(threatMatrix[x - i][y], checkLine(x - i, y, 1, 0, theCharacter));
  114.            
  115.             if (threatMatrix[x + i][y] != -1)
  116.                 threatMatrix[x + i][y] = max(threatMatrix[x + i][y], checkLine(x + i, y, 1, 0, theCharacter));
  117.         }
  118.     }
  119.    
  120.     maxThreat = 0;
  121.     for (int i = 0; i < fHeight; i++) {
  122.         for (int j = 0; j < fWidth; j++) {
  123.             if (threatMatrix[i][j] > maxThreat)
  124.                 maxThreat = threatMatrix[i][j];
  125.         }
  126.     }
  127.    
  128. }
  129.  
  130. char play(bool ifFirstPlayer) {
  131.     cleanField();
  132.     string inputX, inputY;
  133.     int turn = 1, x = 0, y = 0;
  134.    
  135.     while (turn < fHeight * fWidth) {
  136.         bool found = false;
  137.         if (turn % 2 == ifFirstPlayer) {
  138.             while (true) {
  139.                 cout << "\nEnter the row index: ";
  140.                 cin >> inputX;
  141.                 x = atoi(inputX.c_str());
  142.                 cout << "Enter the column index: ";
  143.                 cin >> inputY; cout << "\n";
  144.                 y = atoi(inputY.c_str());
  145.                 if (field[x][y] == ' ') {
  146.                     field[x][y] = X;
  147.                     break;
  148.                 }
  149.                 else {
  150.                     cout << "Somethig's wrong: choose a free cell from 0 to " << fHeight - 1 <<".";
  151.                 }
  152.             }
  153.             if (isWinner(x, y, X)){
  154.                 showField();
  155.                 return 'P';
  156.             }
  157.             else
  158.                 threatMatrixMod(x, y, X);
  159.         }
  160.         else {
  161.             if (turn == 1) {
  162.                 threatMatrix[fHeight / 2 + fHeight % 2][fWidth / 2 + fWidth % 2] = 100;
  163.                 maxThreat = 100;
  164.             }
  165.             else {
  166.                 for (int i = 0; i < fHeight; i++) {
  167.                     if (found == true)
  168.                         break;
  169.                     for (int j = 0; j < fWidth; j++) {
  170.                         if (isWinner(i, j, O)) {
  171.                             x = i;
  172.                             y = j;
  173.                             found = true;
  174.                             break;
  175.                         }
  176.                     }
  177.                 }
  178.             }
  179.             for (int i = 0; i < fHeight; i++) {
  180.                 if (found == true)
  181.                     break;
  182.                 for (int j = 0; j < fWidth; j++) {
  183.                     if (threatMatrix[i][j] != maxThreat)
  184.                         continue;
  185.                     x = i;
  186.                     y = j;
  187.                     found = true;
  188.                     break;
  189.                 }
  190.             }
  191.            
  192.             field[x][y] = O;
  193.             showField();
  194.            
  195.             if (isWinner(x, y, O))
  196.                 return 'B';
  197.             else
  198.                 threatMatrixMod(x, y, O);
  199.            
  200.         }
  201.         turn++;
  202.     }
  203.    
  204.     return 'D';
  205. }
  206.  
  207. int main() {
  208.     char winner;
  209.     int playerWins, botWins;
  210.     playerWins = botWins = 0;
  211.     string ifFirstPlayer, ifPlaying = "yes";
  212.    
  213.     printf("You play X, b0t plays 0. Your task is to get %d Xes in a row (or in a column, or diagonally), then you win. Let's get started. \n______________________\n", lengthForWin);
  214.    
  215.     while (ifPlaying == "yes") {
  216.         while (true) {
  217.             cout << "\n - Would you like to be the first player? (yes or no)\n - ";
  218.             cin >> ifFirstPlayer;
  219.             if (ifFirstPlayer == "yes" || ifFirstPlayer == "no")
  220.                 break;
  221.             cout << "\nInvalid input. Let's try one more time: \n";
  222.         }
  223.        
  224.         winner = play((ifFirstPlayer == "yes") ? 1 : 0);
  225.         cout << endl;
  226.        
  227.         if (winner == 'P') {
  228.             playerWins++;
  229.             cout << "Player wins!";
  230.         }
  231.         else if (winner == 'B') {
  232.             botWins++;
  233.             cout << "Haha, b0t wins";
  234.         }
  235.         else {
  236.             playerWins++;
  237.             botWins++;
  238.             cout << "It's a draw :)";
  239.         }
  240.         cout << "\nGame statistics: \nPlayer " << playerWins << ":" << botWins << " b0t" << endl;
  241.         while (true) {
  242.             cout << "\n - Would you like to play one more time? (yes or no)\n - ";
  243.             cin >> ifPlaying;
  244.             if (ifPlaying == "yes" || ifPlaying == "no")
  245.                 break;
  246.             cout << "\nInvalid input. Please try again: \n";
  247.         }
  248.     }
  249.    
  250.     return 0;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement