Advertisement
qekaqeka

tiktoktoe

Jan 14th, 2020
2,631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "tiktoktoe.h"
  2. #include "ui_tiktoktoe.h"
  3. #define  ERROR QPair<char,int>(-1,-1)
  4. tiktoktoe::tiktoktoe(QWidget *parent)
  5.     : QWidget(parent)
  6.     , ui(new Ui::tiktoktoe)
  7.  
  8. {
  9.     ui->setupUi(this);
  10.     ui->pushButton->hide();
  11.     ui->checkBox->hide();
  12.     setFixedSize(210,350);
  13.     ui->radioButton->click();
  14. }
  15. /*
  16.         masiv[0][0] = pushButton_4;
  17.         masiv[0][1] = pushButton_8;
  18.         masiv[0][2] = pushButton_9;
  19.         masiv[1][0] = pushButton_2;
  20.         masiv[1][1] = pushButton_7;
  21.         masiv[1][2] = pushButton_10;
  22.         masiv[2][0] = pushButton_5;
  23.         masiv[2][1] = pushButton_6;
  24.         masiv[2][2] = pushButton_11
  25. */
  26. /*
  27.     matrix это
  28.     _\_\_
  29.     _\_\_
  30.      \ \
  31.      сетка(поле) для крестиков ноликов ,изначально заполнена числами от 2 до 10 включительно
  32.      поэтому matrix[...][...] > 1 означает что клетка пустая
  33. */
  34. tiktoktoe::~tiktoktoe()
  35. {
  36.     delete ui;
  37. }
  38. void tiktoktoe::randomMove()
  39. {
  40.     for(;;)
  41.     {
  42.         int oneRandIndex = rand()%3;
  43.         int twoRandIndex = rand()%3;
  44.         if(matrix[oneRandIndex][twoRandIndex] > 1)
  45.         {
  46.             ui->masiv[oneRandIndex][twoRandIndex]->click();
  47.             return;
  48.         }
  49.     }
  50. }
  51. QPair<char , int> tiktoktoe::check2(char *_matrix)
  52. {
  53.     char simbol = -1; //символы из которых состоит 2 одинаковые клетки линии если такая имеется
  54.     int location = -1;//расположение пустой клетки если условие ниже выполняется
  55.     if((_matrix[0] == _matrix[1]) || (_matrix[1] == _matrix[2]) || (_matrix[0] == _matrix[2]))
  56.     {
  57.  
  58.         for(int q = 0;q<3;q++)
  59.         {
  60.             if(_matrix[q] > 1)
  61.             {
  62.                 location = q;
  63.             }
  64.         }
  65.     }
  66.     for(int q=0;q<3 && location != -1;q++)
  67.     {
  68.         if(q != location)
  69.         {
  70.             simbol = _matrix[q];
  71.             break;
  72.         }
  73.     }
  74.     return QPair<char , int>(simbol,location); // если возвращает QPair<char,int>(-1,-1) значит предвыйгрышной ситуации в
  75.     //данных 3 клетках нет
  76. }
  77. void tiktoktoe::computerEnemy()
  78. {
  79.     if(matrix[1][1] > 1)//скрипт занимания центра если он свободен
  80.     {
  81.         ui->masiv[1][1]->click();
  82.         return;
  83.     }
  84.     char _matrix[3];
  85.     for(int i = 0;i < 3;i++)
  86.     {
  87.         for(int a=0;a < 3;a++)
  88.         {
  89.             _matrix[a] = matrix[i][a];
  90.         }
  91.         QPair<char,int> resul(check2(_matrix));
  92.         if(resul != ERROR)
  93.         {
  94.             variants.push_back(QPair<char,QPushButton*>(resul.first,ui->masiv[i][resul.second]));
  95.         }
  96.     }
  97.     for(int i = 0;i < 3;i++)
  98.     {
  99.         for(int a=0;a < 3;a++)
  100.         {
  101.             _matrix[a] = matrix[a][i];
  102.         }
  103.         QPair<char,int> resul(check2(_matrix));
  104.         if(resul != ERROR)
  105.         {
  106.             variants.push_back(QPair<char,QPushButton*>(resul.first,ui->masiv[resul.second][i]));
  107.         }
  108.     }
  109.     {//начало искусственного блока
  110.     for (int a = 0;a < 3;a++)
  111.     {
  112.             _matrix[a] = matrix[a][a];
  113.     }
  114.     QPair<char,int> resul(check2(_matrix));
  115.     if(resul != ERROR)
  116.     {
  117.         variants.push_back(QPair<char,QPushButton*>(resul.first,ui->masiv[resul.second][resul.second]));
  118.     }
  119.     _matrix[0] = matrix[0][2];
  120.     _matrix[1] = matrix[1][1];
  121.     _matrix[2] = matrix[2][0];
  122.     }//конец искусственного блока
  123.     QPair<char,int> resul(check2(_matrix));
  124.     if(resul != QPair<char,int>(0,0))
  125.     {
  126.         switch (resul.second)
  127.         {
  128.             case (0):
  129.                 variants.push_back(QPair<char,QPushButton*>(resul.first,ui->masiv[resul.second][resul.second+2]));
  130.             break;
  131.             case (1):
  132.                 variants.push_back(QPair<char,QPushButton*>(resul.first,ui->masiv[resul.second][resul.second]));
  133.             break;
  134.             case (2):
  135.                 variants.push_back(QPair<char,QPushButton*>(resul.first,ui->masiv[resul.second][resul.second-2]));
  136.             break;
  137.         }
  138.     }
  139.     if(variants.size() != 0)
  140.     {
  141.         for(int i=0;i<variants.size();i++)
  142.         {
  143.             if(variants[i].first == move)//если превыйгрышная ситуация получается у стороны за которую играет робот
  144.             { //то он её завершает в первую очередь
  145.                 variants[i].second->click();
  146.                 variants.clear();
  147.                 return;
  148.             }
  149.         }
  150.         variants[rand()%variants.size()].second->click();
  151.         variants.clear();
  152.         return;
  153.  
  154.     }
  155.     if((moveCounter == 1  || moveCounter  == 3) && (matrix[1][1] == ((move+1)%2)))//скрипт предотвращения
  156.     {//100% выйгрыша игрока если тот занял центр и хочет использовать 100% выйгрышную тактику у который только 1 способ предотвращения
  157.         for(;;)//но тактика не сработает если бот будет ходит по диагоналям в начале
  158.         {
  159.             int oneRandIndex = (rand()%2)*2;
  160.             int twoRandIndex = (rand()%2)*2;
  161.             if(matrix[oneRandIndex][twoRandIndex] > 2)
  162.             {
  163.                 ui->masiv[oneRandIndex][twoRandIndex]->click();
  164.                 return;
  165.             }
  166.         }
  167.     }
  168.     randomMove();
  169. }
  170.  
  171.  
  172. void tiktoktoe::_end()
  173. {
  174.     ui->pushButton->show();
  175.     ui->label->setText(strMove + " победили!");
  176.     for(int i=0;i<3;i++)
  177.     {
  178.         for (int a=0;a<3;a++)
  179.         {
  180.             ui->masiv[i][a]->setEnabled(false);
  181.         }
  182.     }
  183. }
  184. void tiktoktoe::checkWin()
  185. {
  186.     moveCounter++;
  187.     for(int i=0;i<3;i++)
  188.     {
  189.         if((matrix[i][0] == matrix[i][1]) && (matrix[i][0] == matrix[i][2]) && (matrix[i][1] == matrix[i][2]))
  190.         {
  191.                 _end();
  192.                 return;
  193.         }
  194.     }
  195.     for(int i=0;i<3;i++)
  196.     {
  197.         if((matrix[0][i] == matrix[1][i]) && (matrix[0][i] == matrix[2][i]) && (matrix[1][i] == matrix[2][i]))
  198.         {
  199.                 _end();
  200.                 return;
  201.         }
  202.     }
  203.     if(((matrix[0][0] == matrix[1][1]) && (matrix[0][0] == matrix[2][2]) && (matrix[1][1] == matrix[2][2])) || ((matrix[2][0] == matrix[0][2]) && (matrix[1][1] == matrix[0][2]) && (matrix[1][1] == matrix[2][0])))
  204.     {
  205.             _end();
  206.             return;
  207.     }
  208.     if(moveCounter==9)
  209.     {
  210.         ui->pushButton->show();
  211.         ui->label->setText("Ничья");
  212.         return;
  213.     }
  214.     move = (1+move)%2;
  215.     move == 0 ? strMove = "X" : strMove = "O";
  216.     //2 cтроки ниже определяют ходит сейчас бот или нет
  217.     bool moveComputerEnemy = playerIsFirst ? move == 1 ? true : false : move == 0 ? true : false;
  218.     if(gameFromComputer && moveComputerEnemy)
  219.         computerEnemy();
  220. }
  221.  
  222. void tiktoktoe::on_pushButton_clicked() //функция очищения поля
  223. {
  224.     char z = 2;
  225.     for(int i = 0;i < 3;i++)
  226.     {
  227.         for(int a=0;a<3;a++)
  228.         {
  229.             matrix[i][a] = z++;
  230.         }
  231.     }
  232.     for (int i =0;i < 3;i++)
  233.     {
  234.         for(int a=0;a<3;a++)
  235.         {
  236.             ui->masiv[i][a]->setText("");
  237.             ui->masiv[i][a]->setEnabled(true);
  238.         }
  239.     }
  240.     strMove = "X";
  241.     moveCounter=0;
  242.     move = 0;
  243.     ui->label->clear();
  244.     ui->pushButton->hide();
  245. }
  246.  
  247. void tiktoktoe::on_radioButton_clicked()
  248. {
  249.     gameFromComputer = false;
  250.     ui->checkBox->hide();
  251.     on_pushButton_clicked();
  252. }
  253. void tiktoktoe::on_radioButton_2_clicked()
  254. {
  255.     gameFromComputer = true;
  256.     ui->checkBox->show();
  257.     ui->checkBox->setCheckState(Qt::Checked);
  258.     on_pushButton_clicked();
  259. }
  260.  
  261.  
  262.  
  263. void tiktoktoe::on_pushButton_4_clicked()
  264. {
  265.     ui->pushButton_4->setEnabled(false);
  266.     ui->pushButton_4->setText(strMove);
  267.     matrix[0][0] = move;
  268.     checkWin();
  269. }
  270.  
  271. void tiktoktoe::on_pushButton_8_clicked()
  272. {
  273.     ui->pushButton_8->setEnabled(false);
  274.     ui->pushButton_8->setText(strMove);
  275.     matrix[0][1] = move;
  276.     checkWin();
  277. }
  278.  
  279. void tiktoktoe::on_pushButton_9_clicked()
  280. {
  281.     ui->pushButton_9->setEnabled(false);
  282.     ui->pushButton_9->setText(strMove);
  283.     matrix[0][2] = move;
  284.     checkWin();
  285. }
  286.  
  287. void tiktoktoe::on_pushButton_2_clicked()
  288. {
  289.     ui->pushButton_2->setEnabled(false);
  290.     ui->pushButton_2->setText(strMove);
  291.     matrix[1][0] = move;
  292.     checkWin();
  293. }
  294.  
  295. void tiktoktoe::on_pushButton_7_clicked()
  296. {
  297.     ui->pushButton_7->setEnabled(false);
  298.     ui->pushButton_7->setText(strMove);
  299.     matrix[1][1] = move;
  300.     checkWin();
  301. }
  302.  
  303. void tiktoktoe::on_pushButton_10_clicked()
  304. {
  305.     ui->pushButton_10->setEnabled(false);
  306.     ui->pushButton_10->setText(strMove);
  307.     matrix[1][2] = move;
  308.     checkWin();
  309. }
  310.  
  311. void tiktoktoe::on_pushButton_5_clicked()
  312. {
  313.     ui->pushButton_5->setEnabled(false);
  314.     ui->pushButton_5->setText(strMove);
  315.     matrix[2][0] = move;
  316.     checkWin();
  317. }
  318.  
  319. void tiktoktoe::on_pushButton_6_clicked()
  320. {
  321.     ui->pushButton_6->setEnabled(false);
  322.     ui->pushButton_6->setText(strMove);
  323.     matrix[2][1] = move;
  324.     checkWin();
  325. }
  326.  
  327. void tiktoktoe::on_pushButton_11_clicked()
  328. {
  329.     ui->pushButton_11->setEnabled(false);
  330.     ui->pushButton_11->setText(strMove);
  331.     matrix[2][2] = move;
  332.     checkWin();
  333. }
  334.  
  335. void tiktoktoe::on_checkBox_stateChanged(int) //если Qt::checked то игрок ходит первым иначе первым ходит бот
  336. {
  337.     on_pushButton_clicked();
  338.     if(ui->checkBox->checkState() == Qt::Checked)
  339.         playerIsFirst = true;
  340.     else
  341.     {
  342.         playerIsFirst = false;
  343.         computerEnemy();
  344.     }
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement