Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.24 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3.  
  4. int w = 400;
  5. int h = 400;
  6. int col = 3;
  7. int row = 3;
  8. int size = w / col;
  9. int total_sims = 0;
  10.  
  11. char player_turn = 'O';
  12. char user_type = 'O';
  13. int inf = std::numeric_limits<int>::max();
  14. inline int findIndex(int x, int y) {
  15.     return col * y + x;
  16. }
  17. struct Grid {
  18. public:
  19.     int x, y;
  20.     char type = '.'; // free spot jei '.'
  21. }; std::vector<Grid> grid;
  22.  
  23.  
  24. class Player {
  25. public:
  26.     char type;
  27. }players[2];
  28.  
  29. class AI {
  30. public:
  31.     AI(char T) {
  32.         sim_bot_type = T;
  33.     }
  34.     int MinMax(std::vector<Grid> copy_grid, int depth, int alpha, int beta, bool my_turn, char player_type) {
  35.         if (isGameComplete(copy_grid)) {
  36.             total_sims++;
  37.             if (my_turn)  return depth - 10;
  38.             else          return 10 - depth;
  39.         }if (!free_space(copy_grid)) return 0;  // draw
  40.  
  41.         char future_player_type = ' ';
  42.         if (player_type == players[0].type) future_player_type = players[1].type;
  43.         else future_player_type = players[0].type;
  44.  
  45.         int minScore = inf;
  46.         int maxScore = -inf;
  47.         Grid best_spot;
  48.         for (auto& spot : copy_grid) {
  49.            
  50.             if (spot.type == '.') {
  51.                 std::vector<Grid> send_grid = copy_grid;
  52.  
  53.                 send_grid[findIndex(spot.x, spot.y)].type = player_type;
  54.                 int s = MinMax(send_grid, depth + 1, alpha, beta,!my_turn,  future_player_type);
  55.                 if (minScore > s) minScore = s;
  56.                 if (maxScore < s) {
  57.                     maxScore = s;
  58.                     best_spot = spot;
  59.                 }
  60.             if (my_turn) {
  61.                     if (s > alpha) {
  62.                         alpha = s;
  63.                     }
  64.  
  65.                 }  
  66.                 else {
  67.                     if (s < beta) {
  68.                         beta = s;
  69.                     }
  70.                    
  71.                 }
  72.  
  73.                     if (beta <= alpha)break;
  74.  
  75.             }if (beta <= alpha)break;
  76.         }
  77.         if (depth == 0) {
  78.             grid[findIndex(best_spot.x, best_spot.y)].type = player_type;
  79.         }
  80.         if (my_turn) {
  81.             return maxScore;
  82.         }
  83.         else {
  84.             return minScore;
  85.  
  86.         }
  87.  
  88.     }
  89. private:
  90.     char sim_bot_type;
  91.     bool isGameComplete(std::vector<Grid>& g) {
  92.         for (int i = 0; i < 2; i++) {
  93.             char type = players[i].type;
  94.             if (g[findIndex(0, 0)].type == type && g[findIndex(1, 0)].type == type && g[findIndex(2, 0)].type == type) return true;
  95.             if (g[findIndex(0, 1)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(2, 1)].type == type) return true;
  96.             if (g[findIndex(0, 2)].type == type && g[findIndex(1, 2)].type == type && g[findIndex(2, 2)].type == type) return true;
  97.  
  98.             if (g[findIndex(0, 0)].type == type && g[findIndex(0, 1)].type == type && g[findIndex(0, 2)].type == type) return true;
  99.             if (g[findIndex(1, 0)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(1, 2)].type == type) return true;
  100.             if (g[findIndex(2, 0)].type == type && g[findIndex(2, 1)].type == type && g[findIndex(2, 2)].type == type) return true;
  101.  
  102.             if (g[findIndex(0, 0)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(2, 2)].type == type) return true;
  103.  
  104.             if (g[findIndex(2, 0)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(0, 2)].type == type) return true;
  105.         }
  106.         return false;
  107.     }
  108.     bool free_space(std::vector<Grid>& g) {
  109.         for (auto& spot : g) {
  110.             if (spot.type == '.') return true;
  111.         }return false;
  112.     }
  113.  
  114. };
  115.  
  116. void setup() {
  117.     players[0].type = 'X';
  118.     players[1].type = 'O';
  119.     for (int i = 0; i < row; i++) {
  120.         for (int j = 0; j < col; j++) {
  121.             grid.push_back({ j, i, '.' });
  122.         }
  123.     }
  124.  
  125. }
  126. int main() {
  127.     sf::RenderWindow window(sf::VideoMode(w, h), "TIC TAC TOE");
  128.     setup();
  129.  
  130.  
  131.     // teksturu stuff
  132.     sf::Texture BG_T, X_T, O_T; // background
  133.     BG_T.loadFromFile("images/background.jpg");
  134.     O_T.loadFromFile("images/O.png");
  135.     X_T.loadFromFile("images/X.png");
  136.     sf::Sprite BG_S, O_S, X_S;
  137.     BG_S.setTexture(BG_T);
  138.     O_S.setTexture(O_T);
  139.     X_S.setTexture(X_T);
  140.     while (window.isOpen()) {
  141.         total_sims = 0;
  142.         sf::Event evnt;
  143.         while (window.pollEvent(evnt)) {
  144.             switch (evnt.type) {
  145.             case sf::Event::Closed:
  146.                 window.close();
  147.             case sf::Event::MouseButtonPressed:
  148.                 if (evnt.mouseButton.button == sf::Mouse::Left && player_turn == user_type) {
  149.                     int x = evnt.mouseButton.x / size;
  150.                     int y = evnt.mouseButton.y / size;
  151.  
  152.                     if (grid[findIndex(x, y)].type == '.') {
  153.                         char Enemy;
  154.                         if (user_type == players[0].type) Enemy = players[1].type;
  155.                         else Enemy = players[0].type;
  156.                         grid[findIndex(x, y)].type = user_type;
  157.                         player_turn = Enemy;
  158.                     }
  159.                 }
  160.                 break;
  161.  
  162.             }
  163.         }
  164.         if (player_turn != user_type) {
  165.             char Enemy;
  166.             if (user_type == players[0].type) Enemy = players[1].type;
  167.             else Enemy = players[0].type;
  168.             AI ai(Enemy);
  169.             ai.MinMax(grid, 0, -inf, inf,true,  Enemy);
  170.             player_turn = user_type;
  171.         }
  172.  
  173.         window.clear();
  174.         window.draw(BG_S);
  175.         for (auto & spot : grid) {
  176.             if (spot.type == 'O') {
  177.                 float x = O_S.getGlobalBounds().width;
  178.                 float y = O_S.getGlobalBounds().height;
  179.                 O_S.scale(sf::Vector2f((size - 20) / x, (size - 20) / y));
  180.                 O_S.setPosition(sf::Vector2f(spot.x * size + 20, spot.y * size + 20));
  181.                 window.draw(O_S);
  182.             }
  183.             if (spot.type == 'X') {
  184.                 float x = X_S.getGlobalBounds().width;
  185.                 float y = X_S.getGlobalBounds().height;
  186.                 X_S.scale(sf::Vector2f((size - 20) / x, (size - 20) / y));
  187.                 X_S.setPosition(sf::Vector2f(spot.x * size + 20, spot.y * size + 20));
  188.                 window.draw(X_S);
  189.             }
  190.  
  191.         }
  192.  
  193.         window.display();
  194.         if (total_sims > 0)
  195.             std::cerr << "sim: " << total_sims << std::endl;
  196.  
  197.  
  198.     }
  199.  
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement