Advertisement
Guest User

ticytacy

a guest
Dec 19th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <vector>
  3. #include <iostream>
  4. #include <map>
  5.  
  6.  
  7. class TicTacToe {
  8.     struct cSpot {
  9.         int pos;
  10.         bool isPlayer;
  11.     };
  12.     int round, pSum, oSum;
  13.     sf::Texture tBoard, tPlayer, tOpponent;
  14.     sf::Sprite sBoard, sPlayer, sOpponent, sHolder[10];
  15.     sf::RenderWindow window;
  16.     std::vector<sf::IntRect> savedSpot;
  17.     std::vector<sf::IntRect> boardPos;
  18.     sf::IntRect GetClickBox(sf::Vector2i);
  19.     cSpot _checkSpot;
  20.     std::map<int, sf::Texture > checkSpot;
  21.     bool isPlayer(sf::Sprite);
  22.     int CheckWin();
  23.     void SetBoardPositions();
  24.     bool Play(sf::Vector2i);
  25.     int GetBoardPos(sf::IntRect);
  26. public:
  27.     TicTacToe();
  28.     void Run();
  29. };
  30.  
  31. bool TicTacToe::isPlayer(sf::Sprite t_Player) {
  32.     return t_Player.getTexture() == sPlayer.getTexture();
  33. }
  34.  
  35. //int TicTacToe::CheckWin() {
  36. //  system("CLS");
  37. //  for (int x = 0; x < 4; ++x) {
  38. //      for (int y = 0; y < 4;++y) {
  39. //          checkSpot[x][y]
  40. //      }
  41. //  }
  42. //  return -1;
  43. //}
  44.  
  45. bool TicTacToe::Play(sf::Vector2i mPos) {
  46.     for (auto spotCheck : savedSpot) {
  47.         if (GetClickBox(mPos) == spotCheck) {
  48.             return false;
  49.         }
  50.     }
  51.     char r = round % 2 == 0 ? 'x' : 'o';
  52.     sHolder[round] = round % 2 == 0 ? sPlayer : sOpponent;
  53.     sHolder[round].setPosition(GetClickBox(mPos).left, GetClickBox(mPos).top);
  54.     savedSpot.push_back(GetClickBox(mPos));
  55.     checkSpot[GetBoardPos(GetClickBox(mPos))] = round % 2 == 0 ? tPlayer : tOpponent;
  56.     //CheckWin();
  57.     return true;
  58. }
  59.  
  60. TicTacToe::TicTacToe() : round(0), pSum(0), oSum(0) {
  61.     window.create(sf::VideoMode(273, 273), "TicTacToe", sf::Style::Close);
  62.     if (tBoard.loadFromFile("board.png") && tPlayer.loadFromFile("x.png") && tOpponent.loadFromFile("o.png"))
  63.     {
  64.         sBoard.setTexture(tBoard);
  65.         sPlayer.setTexture(tPlayer);
  66.         sOpponent.setTexture(tOpponent);
  67.     }
  68.     else { return; }
  69.     SetBoardPositions();
  70.     savedSpot.push_back(sf::IntRect(-1, -1, -1, -1));
  71. }
  72.  
  73. void TicTacToe::Run() {
  74.     sf::Event event;
  75.     while (window.isOpen()) {
  76.         while (window.pollEvent(event)) {
  77.             if (event.type == sf::Event::Closed) { window.close(); }
  78.             if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
  79.                 if ((round < 9) && (Play(sf::Mouse::getPosition(window)))){
  80.                     round++;
  81.                 }
  82.             }
  83.         }
  84.         window.clear();
  85.         window.draw(sBoard);
  86.         for (auto holder : sHolder) {
  87.             window.draw(holder);
  88.         }
  89.         window.display();
  90.     }
  91. }
  92.  
  93. sf::IntRect TicTacToe::GetClickBox(sf::Vector2i tClick) {
  94.     for (auto box : boardPos) {
  95.         if (tClick.x >= box.left && tClick.x <= box.width && tClick.y >= box.top && tClick.y <= box.height) {
  96.             return box;
  97.         }
  98.     }
  99.     return sf::IntRect(-1, -1, -1, -1);
  100. }
  101.  
  102. int TicTacToe::GetBoardPos(sf::IntRect pos) {
  103.     for (int x = 0; x < boardPos.size(); ++x) {
  104.         if (pos == boardPos[x]) {
  105.             return x;
  106.         }
  107.     }
  108.     return -1;
  109. }
  110. void TicTacToe::SetBoardPositions() {
  111.     boardPos.push_back(sf::IntRect(0, 0, 89, 89)); //1
  112.     boardPos.push_back(sf::IntRect(92, 0, 181, 89)); //2
  113.     boardPos.push_back(sf::IntRect(184, 0, 273, 89)); //3
  114.     boardPos.push_back(sf::IntRect(0, 92, 89, 181)); //4
  115.     boardPos.push_back(sf::IntRect(92, 92, 181, 181)); //5
  116.     boardPos.push_back(sf::IntRect(184, 92, 273, 181)); //6
  117.     boardPos.push_back(sf::IntRect(0, 184, 89, 273)); //7
  118.     boardPos.push_back(sf::IntRect(92, 184, 181, 273)); //8
  119.     boardPos.push_back(sf::IntRect(184, 184, 273, 273)); //9
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement