Vladislav_Bezruk

Some task

Jan 23rd, 2021 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <math.h>
  6. #include <string>
  7. #include <windows.h>
  8.  
  9. #define nLines 7
  10.  
  11. #define defColor 15
  12. #define Black 0
  13. #define DarkBlue 1
  14. #define DarkGreen 2
  15. #define LightBlue 3
  16. #define DarkRed 4
  17. #define Magenta 5
  18. #define Orange 6
  19. #define LightGray 7
  20. #define Gray 8
  21. #define Blue 9
  22. #define Green 10
  23. #define Cyan 11
  24. #define Red 12
  25. #define Pink 13
  26. #define Yellow 14
  27. #define White 15
  28.  
  29. #define uc unsigned char
  30.  
  31. using namespace std;
  32.  
  33. const int maxsizeM = 10;
  34. const int nSym = 4;
  35. const int s = 2 * maxsizeM * (maxsizeM + 1);
  36. const char sym[nSym] = { ' ' , 'X' , 'O' , '|' };
  37.  
  38. class coord {
  39. public:
  40.     int x;
  41.     int y;
  42. };
  43.  
  44. class coordMass {
  45. public:
  46.     int x[s];
  47.     int y[s];
  48.     int val[s];
  49.     int vsVal[s];
  50.     int rept[s];
  51. };
  52.  
  53. struct ASTR {
  54.     string* str;
  55.     uc color;
  56.     int sizeM;
  57.  
  58.     ASTR(int a, string* b, int c) {
  59.         sizeM = a;
  60.         str = b;
  61.         color = c;
  62.     }
  63. };
  64.  
  65. struct SYMBOL {
  66.     uc color;
  67.     char text;
  68.  
  69.     SYMBOL() {
  70.         color = defColor;
  71.     }
  72.  
  73.     SYMBOL(uc c, uc t) {
  74.         color = c;
  75.         text = t;
  76.     }
  77. };
  78.  
  79. struct PICTURE {
  80.     int width;
  81.     int height;
  82.  
  83.     SYMBOL** symbols;
  84.  
  85.     PICTURE(int h, int w) {
  86.         width = w;
  87.         height = h;
  88.  
  89.         symbols = new SYMBOL * [h];
  90.  
  91.         for (int i = 0; i < h; i++) {
  92.             symbols[i] = new SYMBOL[w];
  93.         }
  94.     }
  95. };
  96.  
  97. struct COOR {
  98.     int x;
  99.     int y;
  100.  
  101.     COOR() {
  102.         x = 0;
  103.         y = 0;
  104.     }
  105. };
  106.  
  107. struct ACOOR {
  108.     COOR* data;
  109.     int sizeM;
  110.     int pos;
  111.  
  112.     ACOOR(int a) {
  113.         sizeM = a;
  114.         data = new COOR[sizeM];
  115.         pos = -1;
  116.     }
  117.  
  118.     void add(int x, int y) {
  119.         pos++;
  120.  
  121.         data[pos].x = x;
  122.         data[pos].y = y;
  123.     }
  124. };
  125.  
  126. struct CELL {
  127.     int import;
  128.     int type;
  129.     int rept;
  130.  
  131.     CELL() {
  132.         import = -2;
  133.         type = 2;
  134.         rept = -1;
  135.     }
  136. };
  137.  
  138. struct DATAU {
  139.     int prob[4][2];
  140.     int count;
  141.     int** value;
  142.     CELL cell;
  143.  
  144.     void create(int cPlayer) {
  145.  
  146.         value = new int* [4];
  147.  
  148.         for (int i = 0; i < 4; i++) {
  149.             value[i] = new int[cPlayer + 1];
  150.             prob[i][0] = prob[i][1] = -2;
  151.  
  152.             for (int j = 0; j <= cPlayer; j++) {
  153.                 value[i][j] = 0;
  154.             }
  155.         }
  156.  
  157.         count = 0;
  158.     }
  159. };
  160.  
  161. struct DATA {
  162.     DATAU** data;
  163.     int cPlayer;
  164.     int msizeM;
  165.  
  166.     DATA(int a, int b) {
  167.         cPlayer = a;
  168.         msizeM = b;
  169.  
  170.         data = new DATAU * [msizeM];
  171.         for (int i = 0; i < msizeM; i++) {
  172.             data[i] = new DATAU[msizeM];
  173.  
  174.             for (int j = 0; j < msizeM; j++) {
  175.                 data[i][j].create(cPlayer);
  176.             }
  177.         }
  178.     }
  179.  
  180.     void add(int x, int y, int count, int player) {
  181.         if (count > data[x][y].count) {
  182.             data[x][y].count = count;
  183.         }
  184.         data[x][y].value[count][player]++;
  185.     }
  186. };
  187.  
  188. struct MAP {
  189.     int** value;
  190.     int msizeM;
  191.  
  192.     MAP(int sizeM) {
  193.         msizeM = sizeM;
  194.         value = new int* [msizeM];
  195.         for (int i = 0; i < msizeM; i++) {
  196.             value[i] = new int[msizeM];
  197.         }
  198.         CLEAR();
  199.     }
  200.  
  201.     void CLEAR() {
  202.         for (int i = 0; i < msizeM; i++) {
  203.             for (int j = 0; j < msizeM; j++) {
  204.                 value[i][j] = 0;
  205.             }
  206.         }
  207.     }
  208. };
  209.  
  210. void printPicture(PICTURE picture, bool newLine) {
  211.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  212.  
  213.     for (int x = 0; x < picture.height; x++) {
  214.         for (int y = 0; y < picture.width; y++) {
  215.             SetConsoleTextAttribute(hConsole, picture.symbols[x][y].color);
  216.             cout << picture.symbols[x][y].text;
  217.         }
  218.         if (newLine == true) {
  219.             cout << endl;
  220.         }
  221.     }
  222.  
  223.     SetConsoleTextAttribute(hConsole, defColor);
  224. }
  225.  
  226. int isMax(int a, int b) {
  227.     return a == max(a, b);
  228. }
  229.  
  230. bool isMaxCell(CELL a, CELL b) {
  231.     if (a.import > b.import || a.import == b.import && a.type < b.type || a.import == b.import && a.type == b.type && a.rept > b.rept) {
  232.         return true;
  233.     }
  234.     return false;
  235. }
  236.  
  237. CELL getMaxCell(DATA data) {
  238.     CELL cell;
  239.  
  240.     for (int x = 0; x < data.msizeM; x++) {
  241.         for (int y = 0; y < data.msizeM; y++) {
  242.             if (isMaxCell(data.data[x][y].cell, cell)) {
  243.                 cell = data.data[x][y].cell;
  244.             }
  245.         }
  246.     }
  247.  
  248.     return cell;
  249. }
  250.  
  251. bool isEqualCells(CELL a, CELL b) {
  252.     if (a.import == b.import && a.rept == b.rept && a.type == b.type) {
  253.         return true;
  254.     }
  255.     return false;
  256. }
  257.  
  258. int countInData(DATA data, CELL maxCell) {
  259.     int count = 0;
  260.  
  261.     for (int x = 0; x < data.msizeM; x++) {
  262.         for (int y = 0; y < data.msizeM; y++) {
  263.             if (isEqualCells(data.data[x][y].cell, maxCell)) {
  264.                 count++;
  265.             }
  266.         }
  267.     }
  268.  
  269.     return count;
  270. }
  271.  
  272. ACOOR getMaxData(DATA data) {
  273.     CELL maxCell = getMaxCell(data);
  274.     ACOOR coords(countInData(data, maxCell));
  275.  
  276.     for (int x = 0; x < data.msizeM; x++) { //add all coord
  277.         for (int y = 0; y < data.msizeM; y++) {
  278.             if (isEqualCells(data.data[x][y].cell, maxCell)) {
  279.                 coords.add(x, y);
  280.             }
  281.         }
  282.     }
  283.  
  284.     return coords;
  285. }
  286.  
  287. int random(int a) {
  288.     return round((a - 1) * (double)(rand() % 100) / 100);
  289. }
  290.  
  291. COOR getRandomCoord(ACOOR coords) {
  292.     srand(time(NULL));
  293.     return coords.data[random(coords.sizeM)];
  294. }
  295.  
  296. DATA calc(MAP map, int player, int cPlayer) {
  297.     DATA data(cPlayer, map.msizeM);
  298.  
  299.     for (int x = 0; x < map.msizeM; x++) {
  300.         for (int y = 0; y < map.msizeM; y++) {
  301.             if (map.value[x][y] == 0) {
  302.  
  303.                 for (int a = 0; a < map.msizeM; a++) { //calc value
  304.                     data.add(x, y, 0, map.value[a][y]);
  305.                     data.add(x, y, 1, map.value[x][a]);
  306.                     if (x == y) {
  307.                         data.add(x, y, 2, map.value[a][a]);
  308.                     }
  309.                     if (x == map.msizeM - (y + 1)) {
  310.                         data.add(x, y, 2 + (x == y), map.value[a][map.msizeM - (a + 1)]);
  311.                     }
  312.                 }
  313.  
  314.                 for (int i = 0; i <= data.data[x][y].count; i++) { //calc prob
  315.                     data.data[x][y].prob[i][0] = data.data[x][y].prob[i][1] = -1;
  316.  
  317.                     if (data.data[x][y].value[i][player] == map.msizeM - data.data[x][y].value[i][0]) {
  318.                         data.data[x][y].prob[i][0] = data.data[x][y].value[i][player];
  319.                     }
  320.  
  321.                     for (int j = 1; j <= cPlayer; j++) {
  322.                         if (j != player && data.data[x][y].value[i][j] == map.msizeM - data.data[x][y].value[i][0]) {
  323.                             data.data[x][y].prob[i][1] = data.data[x][y].value[i][j];
  324.                             break;
  325.                         }
  326.                     }
  327.  
  328.                     for (int j = 0; j < 2; j++) { //calc rept
  329.                         data.data[x][y].cell.rept += (data.data[x][y].prob[i][j] > -1);
  330.                     }
  331.  
  332.                     //calc import and type
  333.                     if (max(data.data[x][y].prob[i][0], data.data[x][y].prob[i][1]) > data.data[x][y].cell.import || (max(data.data[x][y].prob[i][0], data.data[x][y].prob[i][1]) == data.data[x][y].cell.import && data.data[x][y].prob[i][0] >= data.data[x][y].prob[i][1])) {
  334.                         data.data[x][y].cell.import = max(data.data[x][y].prob[i][0], data.data[x][y].prob[i][1]);
  335.                         data.data[x][y].cell.type = !isMax(data.data[x][y].prob[i][0], data.data[x][y].prob[i][1]);
  336.                     }
  337.                 }
  338.             }
  339.         }
  340.     }
  341.  
  342.     return data;
  343. }
  344.  
  345. PICTURE convert(ASTR s) {
  346.     PICTURE picture(s.sizeM, s.str[0].length());
  347.  
  348.     for (int i = 0; i < s.sizeM; i++) {
  349.         for (int j = 0; j < s.str[0].length(); j++) {
  350.             picture.symbols[i][j].text = s.str[i][j];
  351.             if (s.str[i][j] != ' ') {
  352.                 picture.symbols[i][j].color = s.color;
  353.             }
  354.         }
  355.     }
  356.  
  357.     return picture;
  358. }
  359.  
  360. ASTR convertS(int color, initializer_list<string> sl) {
  361.     string* s = new string[sl.size()];
  362.     int i = 0;
  363.  
  364.     for (string si : sl) {
  365.         s[i] = si;
  366.         i++;
  367.     }
  368.     ASTR as(sl.size(), s, color);
  369.     return as;
  370. }
  371.  
  372. PICTURE null[nLines] = { convert(convertS(defColor, {
  373.     { "        "}
  374.     })), convert(convertS(defColor, {
  375.     { " ~~~~~~ "}
  376.     })), convert(convertS(defColor, {
  377.     { " ~~~~~~ "}
  378.     })), convert(convertS(defColor, {
  379.     { " ~~~~~~ "}
  380.     })), convert(convertS(defColor, {
  381.     { " ~~~~~~ "}
  382.     })), convert(convertS(defColor, {
  383.     { " ~~~~~~ "}
  384.     })), convert(convertS(defColor, {
  385.     { "        "}
  386.     })) };
  387.  
  388. PICTURE cross[nLines] = { convert(convertS(Green, {
  389.     { "        "}
  390.     })), convert(convertS(Green, {
  391.     { " $$  $$ "}
  392.     })), convert(convertS(Green, {
  393.     { "  $$$$  "}
  394.     })), convert(convertS(Green, {
  395.     { "   $$   " }
  396.     })), convert(convertS(Green, {
  397.     { "  $$$$  " }
  398.     })), convert(convertS(Green, {
  399.     { " $$  $$ " }
  400.     })), convert(convertS(Green, {
  401.     { "        " }
  402.     })) };
  403.  
  404. PICTURE naught[nLines] = { convert(convertS(Red, {
  405.     { "        "}
  406.     })), convert(convertS(Red, {
  407.     { "  $$$$  "}
  408.     })), convert(convertS(Red, {
  409.     { " $$  $$ "}
  410.     })), convert(convertS(Red, {
  411.     { " $$  $$ " }
  412.     })), convert(convertS(Red, {
  413.     { " $$  $$ " }
  414.     })), convert(convertS(Red, {
  415.     { "  $$$$  " }
  416.     })), convert(convertS(Red, {
  417.     { "        " }
  418.     })) };
  419.  
  420. void printMap(MAP map) {
  421.     for (int x = 0; x < map.msizeM; x++) {
  422.  
  423.         for (int i = 0; i < nLines; i++) {
  424.             for (int y = 0; y < map.msizeM; y++) {
  425.                 if (map.value[x][y] == 1) {
  426.                     printPicture(cross[i], false);
  427.                 }
  428.                 if (map.value[x][y] == 2) {
  429.                     printPicture(naught[i], false);
  430.                 }
  431.                 if (map.value[x][y] == 0) {
  432.                     printPicture(null[i], false);
  433.                 }
  434.             }
  435.             cout << endl;
  436.         }
  437.     }
  438.  
  439. }
  440.  
  441. PICTURE welcome = convert(convertS(Blue, {
  442.     { "                     $$   $ $$$$$  $$      $$$$   $$$$  $$   $$ $$$$$                      "},
  443.     { "                     $$   $ $$     $$     $$  $$ $$  $$ $$$ $$$ $$                         "},
  444.     { "                     $$ $ $ $$$$   $$     $$     $$  $$ $$ $ $$ $$$$                       "},
  445.     { "                     $$$$$$ $$     $$     $$  $$ $$  $$ $$   $$ $$                         "},
  446.     { "                      $$ $$ $$$$$  $$$$$$  $$$$   $$$$  $$   $$ $$$$$                      "},
  447.     { "                                                                                           "},
  448.     { "              $$$$$$ $$$$$$  $$$$  $$$$$$  $$$$   $$$$  $$$$$$  $$$$  $$$$$                "},
  449.     { "                $$     $$   $$  $$   $$   $$  $$ $$  $$   $$   $$  $$ $$                   "},
  450.     { "                $$     $$   $$       $$   $$$$$$ $$       $$   $$  $$ $$$$                 "},
  451.     { "                $$     $$   $$  $$   $$   $$  $$ $$  $$   $$   $$  $$ $$                   "},
  452.     { "                $$   $$$$$$  $$$$    $$   $$  $$  $$$$    $$    $$$$  $$$$$                "},
  453.     { "                                                                                           "},
  454.     { "$$$$$  $$  $$    $$  $$ $$      $$$$  $$$$$  $$$$$$  $$$$  $$      $$$$  $$  $$   $$$$$    "},
  455.     { "$$  $$  $$$$     $$  $$ $$     $$  $$ $$  $$   $$   $$     $$     $$  $$ $$  $$   $$  $$   "},
  456.     { "$$$$$    $$      $$  $$ $$     $$$$$$ $$  $$   $$    $$$$  $$     $$$$$$ $$  $$   $$$$$    "},
  457.     { "$$  $$   $$       $$$$  $$     $$  $$ $$  $$   $$       $$ $$     $$  $$  $$$$    $$  $$   "},
  458.     { "$$$$$    $$        $$   $$$$$$ $$  $$ $$$$$  $$$$$$  $$$$  $$$$$$ $$  $$   $$     $$$$$  $$"}
  459.     }));
  460.  
  461. int sizeM = 3;
  462. int map[maxsizeM][maxsizeM];
  463. int x, y;
  464. int player, type;
  465. coord move;
  466. char a;
  467. char b[5];
  468.  
  469. void ClearMap() {
  470.     int i, j;
  471.     for (i = 0; i < sizeM; i++)
  472.         for (j = 0; j < sizeM; j++)
  473.             map[i][j] = 0;
  474. }
  475.  
  476. int vs(int player) {
  477.     if (player == 1)
  478.         return 2;
  479.     if (player == 2)
  480.         return 1;
  481. }
  482.  
  483. coord CalcMove(int player) {
  484.     coord PlayerMove;
  485.     int nMove = 0, nMoveRept = 0;
  486.     coordMass mMove, mMoveRept;
  487.     int i, j, n, vsN, x, y;
  488.     int valMax = 0, vsValMax = 0;
  489.     int reptMax = 0, nMax = 0;
  490.     int rpos;
  491.  
  492.     srand(time(NULL));
  493.     for (i = 0; i < sizeM; i++) {
  494.         n = 0;
  495.         vsN = 0;
  496.  
  497.         for (j = 0; j < sizeM; j++) {
  498.             if (map[i][j] == player)
  499.                 n += 1;
  500.             if (map[i][j] == vs(player))
  501.                 vsN += 1;
  502.             mMove.x[sizeM * nMove + j] = i;
  503.             mMove.y[sizeM * nMove + j] = j;
  504.         }
  505.         for (j = 0; j < sizeM; j++)
  506.             if (map[i][j] == 0) {
  507.                 mMove.val[sizeM * nMove + j] = n;
  508.                 mMove.vsVal[sizeM * nMove + j] = vsN;
  509.             }
  510.             else {
  511.                 mMove.val[sizeM * nMove + j] = -1;
  512.                 mMove.vsVal[sizeM * nMove + j] = -1;
  513.             }
  514.         nMove += 1;
  515.     }
  516.  
  517.     for (j = 0; j < sizeM; j++) {
  518.         n = 0;
  519.         vsN = 0;
  520.  
  521.         for (i = 0; i < sizeM; i++) {
  522.             if (map[i][j] == player)
  523.                 n += 1;
  524.             if (map[i][j] == vs(player))
  525.                 vsN += 1;
  526.             mMove.x[sizeM * nMove + i] = i;
  527.             mMove.y[sizeM * nMove + i] = j;
  528.         }
  529.         for (i = 0; i < sizeM; i++)
  530.             if (map[i][j] == 0) {
  531.                 mMove.val[sizeM * nMove + i] = n;
  532.                 mMove.vsVal[sizeM * nMove + i] = vsN;
  533.             }
  534.             else {
  535.                 mMove.val[sizeM * nMove + i] = -1;
  536.                 mMove.vsVal[sizeM * nMove + i] = -1;
  537.             }
  538.         nMove += 1;
  539.     }
  540.  
  541.     for (j = 0; j <= 1; j++) {
  542.         n = 0;
  543.         vsN = 0;
  544.         for (i = 0; i < sizeM; i++) {
  545.             x = i;
  546.             y = (j * 2 - 1) * (j * (sizeM - 1) - i);
  547.             if (map[x][y] == player)
  548.                 n += 1;
  549.             if (map[x][y] == vs(player))
  550.                 vsN += 1;
  551.             mMove.x[sizeM * nMove + i] = x;
  552.             mMove.y[sizeM * nMove + i] = y;
  553.         }
  554.  
  555.         for (i = 0; i < sizeM; i++) {
  556.             x = i;
  557.             y = (j * 2 - 1) * (j * (sizeM - 1) - i);
  558.             if (map[x][y] == 0) {
  559.                 mMove.val[sizeM * nMove + i] = n;
  560.                 mMove.vsVal[sizeM * nMove + i] = vsN;
  561.             }
  562.             else {
  563.                 mMove.val[sizeM * nMove + i] = -1;
  564.                 mMove.vsVal[sizeM * nMove + i] = -1;
  565.             }
  566.         }
  567.         nMove += 1;
  568.     }
  569.     nMove *= sizeM;
  570.  
  571.     for (i = 0; i < s; i++)
  572.         mMoveRept.rept[i] = 0;
  573.  
  574.     for (i = 0; i < nMove; i++)
  575.         if (mMove.val[i] != 0 and mMove.vsVal[i] != 0)
  576.             mMove.val[i] = -1;
  577.  
  578.     for (i = 0; i < nMove; i++) {
  579.         if (mMove.val[i] != -1)
  580.             for (j = 0; j <= nMoveRept; j++) {
  581.                 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]) {
  582.                     mMoveRept.rept[j] += 1;
  583.                     mMove.x[i] = -1;
  584.                     break;
  585.                 }
  586.             }
  587.         if (mMove.val[i] != -1 and mMove.x[i] != -1) {
  588.             mMoveRept.x[nMoveRept] = mMove.x[i];
  589.             mMoveRept.y[nMoveRept] = mMove.y[i];
  590.             mMoveRept.val[nMoveRept] = mMove.val[i];
  591.             mMoveRept.vsVal[nMoveRept] = mMove.vsVal[i];
  592.             mMoveRept.rept[nMoveRept] += 1;
  593.             nMoveRept += 1;
  594.             mMove.x[i] = -1;
  595.         }
  596.  
  597.     }
  598.  
  599.     for (i = 0; i < nMoveRept; i++) {
  600.         if (mMoveRept.val[i] > valMax)
  601.             valMax = mMoveRept.val[i];
  602.         if (mMoveRept.vsVal[i] > vsValMax)
  603.             vsValMax = mMoveRept.vsVal[i];
  604.     }
  605.  
  606.     if (valMax >= vsValMax) {
  607.         for (i = 0; i < nMoveRept; i++)
  608.             if (mMoveRept.val[i] == valMax and mMoveRept.rept[i] > reptMax)
  609.                 reptMax = mMoveRept.rept[i];
  610.         vsValMax = 0;
  611.     }
  612.     else {
  613.         for (i = 0; i < nMoveRept; i++)
  614.             if (mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] > reptMax)
  615.                 reptMax = mMoveRept.rept[i];
  616.         valMax = 0;
  617.     }
  618.  
  619.     for (i = 0; i < nMoveRept; i++)
  620.         if (mMoveRept.val[i] == valMax and mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] == reptMax)
  621.             nMax += 1;
  622.  
  623.     rpos = random(nMax);
  624.  
  625.     i = 0;
  626.     while (rpos > 0) {
  627.         if (mMoveRept.val[i] == valMax and mMoveRept.vsVal[i] == vsValMax and mMoveRept.rept[i] == reptMax)
  628.             rpos -= 1;
  629.         i += 1;
  630.     }
  631.     rpos = i - 1;
  632.  
  633.     if (nMoveRept > 0) {
  634.         PlayerMove.x = mMoveRept.x[rpos];
  635.         PlayerMove.y = mMoveRept.y[rpos];
  636.     }
  637.     else {
  638.  
  639.         n = 0;
  640.         for (i = 0; i < sizeM; i++)
  641.             for (j = 0; j < sizeM; j++)
  642.                 if (map[i][j] == 0)
  643.                     n += 1;
  644.  
  645.         rpos = random(n);
  646.  
  647.         for (i = 0; i < sizeM; i++)
  648.             for (j = 0; j < sizeM; j++)
  649.                 if (map[i][j] == 0) {
  650.                     rpos -= 1;
  651.                     if (rpos == 0) {
  652.                         PlayerMove.x = i;
  653.                         PlayerMove.y = j;
  654.                         break;
  655.                     }
  656.  
  657.                 }
  658.  
  659.     }
  660.     return PlayerMove;
  661. }
  662.  
  663. int CheckGame() {
  664.     int i, j, nPlayer, nVsPlayer, stat = 0, x, y, player = 1;
  665.  
  666.     for (i = 0; i < sizeM; i++) {
  667.         nPlayer = 0;
  668.         nVsPlayer = 0;
  669.         for (j = 0; j < sizeM; j++)
  670.             if (map[i][j] == player)
  671.                 nPlayer += 1;
  672.             else
  673.                 if (map[i][j] == vs(player))
  674.                     nVsPlayer += 1;
  675.         if (nPlayer == sizeM) {
  676.             stat = player;
  677.             break;
  678.         }
  679.         if (nVsPlayer == sizeM) {
  680.             stat = vs(player);
  681.             break;
  682.         }
  683.     }
  684.  
  685.     if (stat == 0)
  686.         for (j = 0; j < sizeM; j++) {
  687.             nPlayer = 0;
  688.             nVsPlayer = 0;
  689.             for (i = 0; i < sizeM; i++)
  690.                 if (map[i][j] == player)
  691.                     nPlayer += 1;
  692.                 else
  693.                     if (map[i][j] == vs(player))
  694.                         nVsPlayer += 1;
  695.             if (nPlayer == sizeM) {
  696.                 stat = player;
  697.                 break;
  698.             }
  699.             if (nVsPlayer == sizeM) {
  700.                 stat = vs(player);
  701.                 break;
  702.             }
  703.         }
  704.  
  705.     if (stat == 0)
  706.         for (j = 0; j <= 1; j++) {
  707.             nPlayer = 0;
  708.             nVsPlayer = 0;
  709.             for (i = 0; i < sizeM; i++) {
  710.                 x = i;
  711.                 y = (j * 2 - 1) * (j * (sizeM - 1) - i);
  712.                 if (map[x][y] == player)
  713.                     nPlayer += 1;
  714.                 if (map[x][y] == vs(player))
  715.                     nVsPlayer += 1;
  716.             }
  717.             if (nPlayer == sizeM) {
  718.                 stat = player;
  719.                 break;
  720.             }
  721.             if (nVsPlayer == sizeM) {
  722.                 stat = vs(player);
  723.                 break;
  724.             }
  725.         }
  726.  
  727.     if (stat == 0) {
  728.         x = 0;
  729.         for (i = 0; i < sizeM; i++)
  730.             for (j = 0; j < sizeM; j++)
  731.                 if (map[i][j] != 0)
  732.                     x += 1;
  733.         if (x == sizeM * sizeM)
  734.             stat = 3;
  735.     }
  736.     return stat;
  737. }
  738.  
  739. MAP cToMap(int m[maxsizeM][maxsizeM], int sizeM) {
  740.     MAP map(sizeM);
  741.  
  742.     for (int x = 0; x < map.msizeM; x++) {
  743.         for (int y = 0; y < map.msizeM; y++) {
  744.             map.value[x][y] = m[x][y];
  745.  
  746.         }
  747.     }
  748.  
  749.     return map;
  750. }
  751.  
  752. int main() {
  753.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  754.  
  755.     printPicture(welcome, true);
  756.  
  757.     while (1) {
  758.         printf("\nSelect a size of game (min 3 , max 10) :\n");
  759.         do {
  760.             scanf_s("%i", &sizeM);
  761.             if (not (sizeM >= 3 and sizeM <= 10))
  762.                 printf("Eror!\n");
  763.         } while (not (sizeM >= 3 and sizeM <= 10));
  764.  
  765.         system("cls");
  766.         printf("Select a type of game :\n");
  767.         printf("1 - game with computer\n");
  768.         printf("2 - game with friend\n");
  769.         do {
  770.             scanf_s("%i", &type);
  771.             if (not (type >= 1 and type <= 2))
  772.                 printf("Eror!\n");
  773.         } while (not (type >= 1 and type <= 2));
  774.  
  775.         system("cls");
  776.         if (type == 1) {
  777.             printf("Select a cross or toe :\n");
  778.             printf("1 - cross\n");
  779.             printf("2 - toe\n");
  780.             do {
  781.                 scanf_s("%i", &player);
  782.                 if (not (player >= 1 and player <= 2))
  783.                     printf("Eror!\n");
  784.             } while (not (player >= 1 and player <= 2));
  785.             system("cls");
  786.         }
  787.  
  788.         ClearMap();
  789.         while (CheckGame() == 0) {
  790.  
  791.             if (type == 2) {
  792.                 printMap(cToMap(map, sizeM));
  793.                 printf("Cross move:\n");
  794.                 do {
  795.                     scanf_s("%i %i", &x, &y);
  796.                     if (not (x >= 1 and x <= sizeM and y >= 1 and y <= sizeM and map[x - 1][y - 1] == 0))
  797.                         printf("Eror!\n");
  798.                 } while (not (x >= 1 and x <= sizeM and y >= 1 and y <= sizeM and map[x - 1][y - 1] == 0));
  799.  
  800.                 map[x - 1][y - 1] = 1;
  801.                 if (CheckGame() != 0) {
  802.                     break;
  803.                 }
  804.                 else
  805.                     system("cls");
  806.  
  807.                 printMap(cToMap(map, sizeM));
  808.                 printf("Toe move:\n");
  809.                 do {
  810.                     scanf_s("%i %i", &x, &y);
  811.                     if (not (x >= 1 and x <= sizeM and y >= 1 and y <= sizeM and map[x - 1][y - 1] == 0))
  812.                         printf("Eror!\n");
  813.                 } while (not (x >= 1 and x <= sizeM and y >= 1 and y <= sizeM and map[x - 1][y - 1] == 0));
  814.                 map[x - 1][y - 1] = 2;
  815.                 if (CheckGame() != 0) {
  816.                     break;
  817.                 }
  818.                 else
  819.                     system("cls");
  820.             }
  821.  
  822.             if (type == 1 and player == 2) {
  823.                 COOR move = getRandomCoord(getMaxData(calc(cToMap(map, sizeM), vs(player), 2)));
  824.                 //move = CalcMove(vs(player));
  825.                 map[move.x][move.y] = vs(player);
  826.                 if (CheckGame() != 0) {
  827.                     break;
  828.                 }
  829.             }
  830.  
  831.             if (type == 1) {
  832.                 printMap(cToMap(map, sizeM));
  833.                 printf("Your move :\n");
  834.                 do {
  835.                     scanf_s("%i %i", &x, &y);
  836.                     if (not (x >= 1 and x <= sizeM and y >= 1 and y <= sizeM and map[x - 1][y - 1] == 0))
  837.                         printf("Eror!\n");
  838.                 } while (not (x >= 1 and x <= sizeM and y >= 1 and y <= sizeM and map[x - 1][y - 1] == 0));
  839.                 map[x - 1][y - 1] = player;
  840.                 if (CheckGame() != 0) {
  841.                     break;
  842.                 }
  843.                 else
  844.                     system("cls");
  845.             }
  846.  
  847.             if (type == 1 and player == 1) {
  848.                 COOR move = getRandomCoord(getMaxData(calc(cToMap(map, sizeM), vs(player), 2)));
  849.                 //move = CalcMove(vs(player));
  850.                 map[move.x][move.y] = vs(player);
  851.                 if (CheckGame() != 0) {
  852.                     break;
  853.                 }
  854.             }
  855.  
  856.         }
  857.         system("cls");
  858.         printMap(cToMap(map, sizeM));
  859.         if (type == 1 and CheckGame() == player)
  860.             printf("You win!\n");
  861.         else
  862.             if (type == 1 and CheckGame() == vs(player))
  863.                 printf("You lose!\n");
  864.  
  865.         if (type == 2) {
  866.             if (CheckGame() == 1)
  867.                 printf("Cross win!\n");
  868.             if (CheckGame() == 2)
  869.                 printf("Toe win!\n");
  870.         }
  871.         if (CheckGame() == 3)
  872.             printf("Draw!\n");
  873.  
  874.         printf("Do you want to try again ?\n");
  875.         printf("Print Yes to continue\n");
  876.         scanf_s("%s", &b);
  877.         if (not (b[0] == 'Y' and b[1] == 'e' and b[2] == 's'))
  878.             break;
  879.     }
  880.  
  881.     return 0;
  882. }
Add Comment
Please, Sign In to add comment