Advertisement
KDOXG

Minimax Algorithm

Sep 3rd, 2019
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #define cross 1
  5. #define circle -1
  6. #define byte unsigned char
  7.  
  8. byte invertMinmax(byte m);
  9. byte JogoDaVelha(std::vector<byte> map);
  10.  
  11. struct node
  12. {
  13.     std::vector<byte> map;
  14.     byte minmax;
  15.     std::vector<node*> nodes;
  16. };
  17.  
  18. class Minimax
  19. {
  20.     private:
  21.         void alocarNodos(node *no, byte altura);
  22.         void criarArvore(node *no, std::vector<byte> estadoAtual, byte n, byte minmax);
  23.         void valorMinMax(node *no, byte altura, byte minmax);
  24.         node head;
  25.  
  26.     public:
  27.         Minimax()
  28.         {
  29.             head.map = std::vector<byte>(9);
  30.             head.minmax = 2;
  31.             head.nodes = std::vector<node*>(9);
  32.         }
  33.         byte getMinmax()
  34.         {
  35.             return head.minmax;
  36.         }
  37.         void criarNodos(byte altura);
  38.         void gerarArvore(byte inicio);
  39.         void fatorMinMax(byte m);
  40. };
  41.  
  42. byte invertMinmax(byte m)
  43. {
  44.     if (m == cross)
  45.         return circle;
  46.     else
  47.         return cross;
  48. }
  49.  
  50. byte JogoDaVelha(std::vector<byte> map)
  51. {
  52.     if (map[0] == map[1] && map[0] == map[2] && map[0] != 0)
  53.         return map[0];
  54.     if (map[3] == map[4] && map[3] == map[5] && map[3] != 0)
  55.         return map[3];
  56.     if (map[6] == map[7] && map[6] == map[8] && map[6] != 0)
  57.         return map[6];
  58.     if (map[0] == map[3] && map[0] == map[6] && map[0] != 0)
  59.         return map[0];
  60.     if (map[1] == map[4] && map[1] == map[7] && map[1] != 0)
  61.         return map[1];
  62.     if (map[2] == map[5] && map[2] == map[8] && map[2] != 0)
  63.         return map[2];
  64.     if (map[0] == map[4] && map[0] == map[8] && map[0] != 0)
  65.         return map[0];
  66.     if (map[3] == map[5] && map[3] == map[7] && map[3] != 0)
  67.         return map[3];
  68.     return 0;
  69. }
  70.  
  71. void Minimax::alocarNodos(node *no, byte altura)
  72. {
  73.     no = new node;
  74.     for (byte i = 0; i < altura; i++)
  75.         alocarNodos(no->nodes[i], altura-1);
  76. }
  77.  
  78. void Minimax::criarNodos(byte n)
  79. {
  80.     for (byte i = 0; i < n && n < 9; i++)
  81.         alocarNodos(head.nodes[i],n-1);
  82. }
  83.  
  84. void Minimax::criarArvore(node *no, std::vector<byte> estadoAtual, byte n, byte minmax)
  85. {
  86.     if (n != 0)
  87.     {
  88.         byte j;
  89.         std::vector<byte> estadoProx(estadoAtual);
  90.         std::vector<byte> estadoAux(estadoAtual);
  91.         for (byte i = 0; i < n; i++)
  92.         {
  93.             for (j = 0; j < estadoAux.size(); j++)
  94.                 if (estadoAux[j] == 0)
  95.                     break;
  96.             if (j == estadoAux.size())
  97.                 break;
  98.             estadoAux[j] = minmax;
  99.             estadoProx[j] = minmax;
  100.             criarArvore(no->nodes[i],estadoProx,n-1,invertMinmax(minmax));
  101.             estadoProx = estadoAtual;
  102.         }
  103.     }
  104.     no->map = estadoAtual;
  105.     no->minmax = 2;
  106. }
  107.  
  108. void Minimax::gerarArvore(byte inicio)
  109. {
  110.     for (byte i = 0; i < head.nodes.size(); i++)
  111.         criarArvore(head.nodes[i],head.map,head.nodes.size()-1,invertMinmax(inicio));
  112. }
  113.  
  114. void Minimax::valorMinMax(node *no, byte altura, byte minmax)
  115. {
  116.     if (altura == 0)
  117.     {
  118.         no->minmax = JogoDaVelha(no->map);
  119.         return;
  120.     }
  121.     else
  122.     {
  123.         no->minmax = JogoDaVelha(no->map);
  124.         if (no->minmax != 0)
  125.             return;
  126.         for (byte i = 0; i < altura; i++)
  127.         {
  128.             valorMinMax(no->nodes[i],altura-1,invertMinmax(minmax));
  129.             if (no->nodes[i]->minmax == minmax || no->nodes[i]->minmax == 0)
  130.             {
  131.                 no->minmax = no->nodes[i]->minmax;
  132.                 return;
  133.             }
  134.         }
  135.     }
  136.    
  137. }
  138.  
  139. void Minimax::fatorMinMax(byte m)
  140. {
  141.     for (byte i = 0; i < head.nodes.size(); i++)
  142.     {
  143.         valorMinMax(head.nodes[i],9-1,invertMinmax(m));
  144.         if (head.nodes[i]->minmax == m)
  145.         {
  146.             head.minmax = m;
  147.             return;
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement