Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <vector>
- #include <iostream>
- #include <map>
- class TicTacToe {
- struct cSpot {
- int pos;
- bool isPlayer;
- };
- int round, pSum, oSum;
- sf::Texture tBoard, tPlayer, tOpponent;
- sf::Sprite sBoard, sPlayer, sOpponent, sHolder[10];
- sf::RenderWindow window;
- std::vector<sf::IntRect> savedSpot;
- std::vector<sf::IntRect> boardPos;
- sf::IntRect GetClickBox(sf::Vector2i);
- cSpot _checkSpot;
- std::map<int, sf::Texture > checkSpot;
- bool isPlayer(sf::Sprite);
- int CheckWin();
- void SetBoardPositions();
- bool Play(sf::Vector2i);
- int GetBoardPos(sf::IntRect);
- public:
- TicTacToe();
- void Run();
- };
- bool TicTacToe::isPlayer(sf::Sprite t_Player) {
- return t_Player.getTexture() == sPlayer.getTexture();
- }
- //int TicTacToe::CheckWin() {
- // system("CLS");
- // for (int x = 0; x < 4; ++x) {
- // for (int y = 0; y < 4;++y) {
- // checkSpot[x][y]
- // }
- // }
- // return -1;
- //}
- bool TicTacToe::Play(sf::Vector2i mPos) {
- for (auto spotCheck : savedSpot) {
- if (GetClickBox(mPos) == spotCheck) {
- return false;
- }
- }
- char r = round % 2 == 0 ? 'x' : 'o';
- sHolder[round] = round % 2 == 0 ? sPlayer : sOpponent;
- sHolder[round].setPosition(GetClickBox(mPos).left, GetClickBox(mPos).top);
- savedSpot.push_back(GetClickBox(mPos));
- checkSpot[GetBoardPos(GetClickBox(mPos))] = round % 2 == 0 ? tPlayer : tOpponent;
- //CheckWin();
- return true;
- }
- TicTacToe::TicTacToe() : round(0), pSum(0), oSum(0) {
- window.create(sf::VideoMode(273, 273), "TicTacToe", sf::Style::Close);
- if (tBoard.loadFromFile("board.png") && tPlayer.loadFromFile("x.png") && tOpponent.loadFromFile("o.png"))
- {
- sBoard.setTexture(tBoard);
- sPlayer.setTexture(tPlayer);
- sOpponent.setTexture(tOpponent);
- }
- else { return; }
- SetBoardPositions();
- savedSpot.push_back(sf::IntRect(-1, -1, -1, -1));
- }
- void TicTacToe::Run() {
- sf::Event event;
- while (window.isOpen()) {
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) { window.close(); }
- if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
- if ((round < 9) && (Play(sf::Mouse::getPosition(window)))){
- round++;
- }
- }
- }
- window.clear();
- window.draw(sBoard);
- for (auto holder : sHolder) {
- window.draw(holder);
- }
- window.display();
- }
- }
- sf::IntRect TicTacToe::GetClickBox(sf::Vector2i tClick) {
- for (auto box : boardPos) {
- if (tClick.x >= box.left && tClick.x <= box.width && tClick.y >= box.top && tClick.y <= box.height) {
- return box;
- }
- }
- return sf::IntRect(-1, -1, -1, -1);
- }
- int TicTacToe::GetBoardPos(sf::IntRect pos) {
- for (int x = 0; x < boardPos.size(); ++x) {
- if (pos == boardPos[x]) {
- return x;
- }
- }
- return -1;
- }
- void TicTacToe::SetBoardPositions() {
- boardPos.push_back(sf::IntRect(0, 0, 89, 89)); //1
- boardPos.push_back(sf::IntRect(92, 0, 181, 89)); //2
- boardPos.push_back(sf::IntRect(184, 0, 273, 89)); //3
- boardPos.push_back(sf::IntRect(0, 92, 89, 181)); //4
- boardPos.push_back(sf::IntRect(92, 92, 181, 181)); //5
- boardPos.push_back(sf::IntRect(184, 92, 273, 181)); //6
- boardPos.push_back(sf::IntRect(0, 184, 89, 273)); //7
- boardPos.push_back(sf::IntRect(92, 184, 181, 273)); //8
- boardPos.push_back(sf::IntRect(184, 184, 273, 273)); //9
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement