Vladislav_Bezruk

Some task

Jan 5th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.59 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <math.h>
  5.  
  6. const int maxSize = 10;
  7. const int nSym = 4;
  8. const int s = 2 * maxSize * (maxSize + 1);
  9. const char sym[nSym] = {' ' , 'X' , 'O' , '|'};
  10.  
  11. class coord {      
  12.   public:            
  13.     int x;      
  14.     int y;
  15. };
  16.  
  17. class coordMass {      
  18.   public:            
  19.     int x[s];      
  20.     int y[s];
  21.     int val[s];
  22.     int vsVal[s];
  23.     int rept[s];
  24. };
  25.  
  26. int size = 3;
  27. int map[maxSize][maxSize];
  28. int x , y;
  29. int player , type;
  30. coord move;
  31. char a;
  32. char b[5];
  33.  
  34. int random (int a) {
  35.     a = round(a * (double)(rand()%100) / 100); 
  36.     if (a == 0)
  37.     a = 1;
  38.     return a;  
  39. }
  40.  
  41. void PrintMap() {
  42.     int i , j;
  43.     for (i = 0 ; i < size ; i++)  {
  44.         printf("%c",sym[nSym - 1]);
  45.         for (j = 0; j < size ; j++)
  46.         printf("%c%c",sym[map[i][j]],sym[nSym - 1]);
  47.         printf("\n");      
  48.     }  
  49. }
  50.  
  51. void ClearMap() {
  52.     int i , j;
  53.     for (i = 0 ; i < size ; i++)
  54.         for (j = 0 ; j < size ; j++)
  55.         map[i][j] = 0;
  56. }
  57.  
  58. int vs(int player) {   
  59.     if (player == 1)
  60.     return 2;
  61.     if (player == 2)
  62.     return 1;
  63. }
  64.    
  65. coord CalcMove(int player) {
  66.     coord PlayerMove;
  67.     int nMove = 0 , nMoveRept = 0;
  68.     coordMass mMove , mMoveRept;
  69.     int i , j , n , vsN , x , y;
  70.     int valMax = 0 , vsValMax = 0;
  71.     int reptMax = 0 , nMax = 0 ;
  72.     int rpos;
  73.    
  74.     srand(time(NULL));
  75.     for (i = 0 ; i < size ; i++) {
  76.         n = 0;
  77.         vsN = 0;
  78.        
  79.         for (j = 0; j < size ; j++) {
  80.             if (map[i][j] == player)
  81.             n += 1;
  82.             if (map[i][j] == vs(player))
  83.             vsN += 1;
  84.             mMove.x[size * nMove + j] = i;
  85.             mMove.y[size * nMove + j] = j;             
  86.         }
  87.         for (j = 0; j < size ; j++)
  88.         if (map[i][j] == 0) {
  89.             mMove.val[size * nMove + j] = n;
  90.             mMove.vsVal[size * nMove + j] = vsN;   
  91.         }
  92.         else {
  93.             mMove.val[size * nMove + j] = -1;
  94.             mMove.vsVal[size * nMove + j] = -1;    
  95.         }
  96.         nMove += 1;
  97.     }
  98.    
  99.     for (j = 0 ; j < size ; j++) {
  100.         n = 0;
  101.         vsN = 0;
  102.        
  103.         for (i = 0; i < size ; i++) {
  104.             if (map[i][j] == player)
  105.             n += 1;
  106.             if (map[i][j] == vs(player))
  107.             vsN += 1;
  108.             mMove.x[size * nMove + i] = i;
  109.             mMove.y[size * nMove + i] = j;             
  110.         }
  111.         for (i = 0; i < size ; i++)
  112.         if (map[i][j] == 0) {
  113.            mMove.val[size * nMove + i] = n;
  114.            mMove.vsVal[size * nMove + i] = vsN;
  115.         }          
  116.         else {
  117.            mMove.val[size * nMove + i] = -1;
  118.            mMove.vsVal[size * nMove + i] = -1;     
  119.         }
  120.         nMove += 1;
  121.     }
  122.    
  123.     for (j = 0 ; j <= 1 ; j++) {
  124.         n = 0;
  125.         vsN = 0;
  126.         for (i = 0 ; i < size ; i++) {
  127.             x = i;
  128.             y = (j * 2 - 1) * (j *  (size - 1) - i);
  129.             if (map[x][y] == player)
  130.             n += 1;
  131.             if (map[x][y] == vs(player))
  132.             vsN += 1;
  133.             mMove.x[size * nMove + i] = x;
  134.             mMove.y[size * nMove + i] = y;
  135.         }
  136.        
  137.         for (i = 0 ; i < size ; i++) {
  138.             x = i;
  139.             y = (j * 2 - 1) * (j *  (size - 1) - i);
  140.             if (map[x][y] == 0) {
  141.                 mMove.val[size * nMove + i] = n;
  142.                 mMove.vsVal[size * nMove + i] = vsN;   
  143.                 }          
  144.                 else {
  145.                 mMove.val[size * nMove + i] = -1;
  146.                 mMove.vsVal[size * nMove + i] = -1;    
  147.                 }      
  148.         }
  149.         nMove += 1;
  150.     }
  151.     nMove *= size;
  152.    
  153.     for (i = 0 ; i < s ; i++)
  154.     mMoveRept.rept[i] = 0;
  155.    
  156.     for (i = 0 ; i < nMove ; i++)
  157.     if (mMove.val[i] != 0 and mMove.vsVal[i] != 0)
  158.     mMove.val[i] = -1;
  159.    
  160.     for (i = 0 ; i < nMove ; i++) {
  161.         if (mMove.val[i] != -1)
  162.         for (j = 0 ; j <= nMoveRept ; j++) {
  163.         if (mMove.x[i] != -1 and mMove.x[i] == mMoveRept.x[j] and mMove.y[i] == mMoveRept.y[j] and mMove.val[i] == mMoveRept.val[j] and mMove.vsVal[i] == mMoveRept.vsVal[j]) {
  164.             mMoveRept.rept[j] += 1;
  165.             mMove.x[i] = -1;
  166.             break; 
  167.         }
  168.     }
  169.         if (mMove.val[i] != -1 and mMove.x[i] != -1) {
  170.             mMoveRept.x[nMoveRept] = mMove.x[i];
  171.             mMoveRept.y[nMoveRept] = mMove.y[i];
  172.             mMoveRept.val[nMoveRept] = mMove.val[i];
  173.             mMoveRept.vsVal[nMoveRept] = mMove.vsVal[i];
  174.             mMoveRept.rept[nMoveRept] += 1;
  175.             nMoveRept += 1;
  176.             mMove.x[i] = -1;
  177.         }
  178.        
  179.     }
  180.    
  181.     for (i = 0; i < nMoveRept ; i++) {
  182.         if (mMoveRept.val[i] > valMax)
  183.         valMax = mMoveRept.val[i];
  184.         if (mMoveRept.vsVal[i] > vsValMax)
  185.         vsValMax = mMoveRept.vsVal[i];         
  186.     }
  187.    
  188.     if (valMax >= vsValMax) {  
  189.         for (i = 0 ; i < nMoveRept ; i++)
  190.         if (mMoveRept.val[i] == valMax and mMoveRept.rept[i] > reptMax)
  191.         reptMax = mMoveRept.rept[i];
  192.         vsValMax = 0;              
  193.     }
  194.     else {
  195.         for (i = 0 ; i < nMoveRept ; i++)
  196.         if (mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] > reptMax)
  197.         reptMax = mMoveRept.rept[i];       
  198.         valMax = 0;
  199.     }
  200.    
  201.     for (i = 0 ; i < nMoveRept ; i++)
  202.     if (mMoveRept.val[i] == valMax and mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] == reptMax)
  203.     nMax += 1;
  204.      
  205.     rpos = random(nMax);   
  206.    
  207.     i = 0;
  208.     while (rpos > 0) { 
  209.         if (mMoveRept.val[i] == valMax and mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] == reptMax)
  210.         rpos -= 1;
  211.         i += 1;
  212.     }
  213.     rpos = i - 1;
  214.    
  215.     if (nMoveRept > 0) {   
  216.         PlayerMove.x = mMoveRept.x[rpos];  
  217.         PlayerMove.y = mMoveRept.y[rpos];      
  218.     }
  219.     else {
  220.        
  221.         n = 0;
  222.         for (i = 0 ; i < size ; i++)
  223.         for (j = 0 ; j < size ; j++)
  224.         if (map[i][j] == 0)
  225.         n += 1;
  226.        
  227.         rpos = random(n);  
  228.    
  229.         for (i = 0 ; i < size ; i++)
  230.         for (j = 0 ; j < size ; j++)
  231.         if (map[i][j] == 0) {  
  232.             rpos -= 1;
  233.             if (rpos == 0) {   
  234.                 PlayerMove.x = i;  
  235.                 PlayerMove.y = j;
  236.                 break; 
  237.             }  
  238.  
  239.         }
  240.                
  241.     }  
  242.     return PlayerMove;
  243. }
  244.  
  245. int CheckGame() {
  246.     int i , j , nPlayer , nVsPlayer , stat = 0 , x , y , player = 1;
  247.    
  248.     for (i = 0 ; i < size ; i++) {
  249.         nPlayer = 0;
  250.         nVsPlayer = 0;
  251.         for (j = 0 ; j < size ; j++)
  252.             if (map[i][j] == player)
  253.             nPlayer += 1;
  254.             else
  255.             if (map[i][j] == vs(player))
  256.             nVsPlayer += 1;
  257.         if (nPlayer == size){
  258.             stat = player; 
  259.             break; 
  260.         }
  261.         if (nVsPlayer == size) {
  262.             stat = vs(player);
  263.             break; 
  264.         }          
  265.     }
  266.    
  267.     if (stat == 0)
  268.     for (j = 0 ; j < size ; j++) {
  269.         nPlayer = 0;
  270.         nVsPlayer = 0;
  271.         for (i = 0 ; i < size ; i++)
  272.             if (map[i][j] == player)
  273.             nPlayer += 1;
  274.             else
  275.             if (map[i][j] == vs(player))
  276.             nVsPlayer += 1;
  277.         if (nPlayer == size){
  278.             stat = player; 
  279.             break; 
  280.         }
  281.         if (nVsPlayer == size) {
  282.             stat = vs(player);
  283.             break; 
  284.         }          
  285.     }
  286.    
  287.     if (stat == 0)
  288.     for (j = 0 ; j <= 1 ; j++) {
  289.         nPlayer = 0;
  290.         nVsPlayer = 0;
  291.         for (i = 0 ; i < size ; i++) {
  292.             x = i;
  293.             y = (j * 2 - 1) * (j *  (size - 1) - i);
  294.             if (map[x][y] == player)
  295.             nPlayer += 1;
  296.             if (map[x][y] == vs(player))
  297.             nVsPlayer += 1;
  298.         }
  299.         if (nPlayer == size) {
  300.         stat = player;
  301.         break; 
  302.         }
  303.         if (nVsPlayer == size) {
  304.         stat = vs(player); 
  305.         break;
  306.         }
  307.     }
  308.    
  309.     if (stat == 0) {
  310.     x = 0;
  311.     for (i = 0 ; i < size ; i++)
  312.     for (j = 0 ; j < size ; j++)
  313.     if (map[i][j] != 0)
  314.     x += 1;
  315.     if (x == size * size)
  316.     stat = 3;      
  317.     }  
  318.     return stat;
  319. }
  320.  
  321. int main() {
  322.    
  323.     while (1) {
  324.     system("cls");
  325.     printf("Select a size of game (min 3 , max 10) :\n");
  326.     do {
  327.     scanf("%i",&size);
  328.     if (not (size >= 3 and size <= 10))
  329.     printf("Eror!\n");
  330.     } while (not (size >= 3 and size <= 10));
  331.  
  332.     system("cls");
  333.     printf("Select a type of game :\n");
  334.     printf("1 - game with computer\n");
  335.     printf("2 - game with friend\n");
  336.     do {
  337.     scanf("%i",&type);
  338.     if (not (type >= 1 and type <= 2))
  339.     printf("Eror!\n");
  340.     } while (not (type >= 1 and type <= 2));
  341.    
  342.     system("cls");
  343.     if (type == 1) {
  344.     printf("Select a cross or toe :\n");
  345.     printf("1 - cross\n");
  346.     printf("2 - toe\n");
  347.     do {
  348.     scanf("%i",&player);
  349.     if (not (player >= 1 and player <= 2))
  350.     printf("Eror!\n");
  351.     } while (not (player >= 1 and player <= 2));   
  352.     system("cls");
  353.     }
  354.    
  355.     ClearMap();
  356.     while (CheckGame() == 0) {
  357.    
  358.     if (type == 2) {
  359.     PrintMap();
  360.     printf("Cross move:\n");
  361.     do {
  362.     scanf("%i %i",&x,&y);
  363.     if (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0))
  364.     printf("Eror!\n");
  365.     } while (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0));  
  366.    
  367.     map[x-1][y-1] = 1;
  368.     if (CheckGame() != 0) {
  369.     break;
  370.     }
  371.     else
  372.     system("cls");
  373.    
  374.     PrintMap();
  375.     printf("Toe move:\n");
  376.     do {
  377.     scanf("%i %i",&x,&y);
  378.     if (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0))
  379.     printf("Eror!\n");
  380.     } while (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0));
  381.     map[x-1][y-1] = 2;
  382.     if (CheckGame() != 0) {
  383.     break;
  384.     }
  385.     else
  386.     system("cls"); 
  387.     }
  388.    
  389.     if (type == 1 and player == 2) {
  390.     move = CalcMove(vs(player));
  391.     map[move.x][move.y] = vs(player);
  392.     if (CheckGame() != 0) {
  393.     break;
  394.     }
  395.     }
  396.    
  397.     if (type == 1) {
  398.     PrintMap();
  399.     printf("Your move :\n");
  400.     do {
  401.     scanf("%i %i",&x,&y);
  402.     if (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0))
  403.     printf("Eror!\n");
  404.     } while (not (x >= 1 and x <= size and y >= 1 and y <= size and map[x-1][y-1] == 0));
  405.     map[x-1][y-1] = player;
  406.     if (CheckGame() != 0) {
  407.     break;
  408.     }
  409.     else
  410.     system("cls"); 
  411.     }
  412.  
  413.     if (type == 1 and player == 1) {
  414.     move = CalcMove(vs(player));
  415.     map[move.x][move.y] = vs(player);
  416.     if (CheckGame() != 0) {
  417.     break;
  418.     }
  419.     }
  420.    
  421.     }
  422.     system("cls");
  423.     PrintMap();
  424.     if (type == 1 and CheckGame() == player)
  425.     printf("You win!\n");
  426.     else
  427.     if (type == 1 and CheckGame() == vs(player))
  428.     printf("You lose!\n");
  429.    
  430.     if (type == 2) {
  431.         if (CheckGame() == 1)
  432.         printf("Cross win!\n");
  433.         if (CheckGame() == 2)
  434.         printf("Toe win!\n");
  435.     }
  436.     if (CheckGame() == 3)
  437.     printf("Draw!\n");
  438.  
  439.     printf("Do you want to try again ?\n");
  440.     printf("Print Yes to continue\n");
  441.     scanf("%s",&b);
  442.     if (not (b[0] == 'Y' and b[1] == 'e' and b[2] == 's'))
  443.     break;
  444.     }
  445.    
  446.     return 0;
  447. }
Add Comment
Please, Sign In to add comment