fork download
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. enum class Cell { Empty, X, O };
  6.  
  7. class TicTacToe {
  8. public:
  9. TicTacToe() : window(sf::VideoMode(600, 600), "Tic Tac Toe"), board(9, Cell::Empty), currentPlayer(Cell::X) {}
  10.  
  11. void run() {
  12. while (window.isOpen()) {
  13. handleInput();
  14. update();
  15. render();
  16. }
  17. }
  18.  
  19. private:
  20. void handleInput() {
  21. sf::Event event;
  22. while (window.pollEvent(event)) {
  23. if (event.type == sf::Event::Closed) {
  24. window.close();
  25. }
  26. if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
  27. int cellX = event.mouseButton.x / (600 / 3);
  28. int cellY = event.mouseButton.y / (600 / 3);
  29. int cellIndex = cellY * 3 + cellX;
  30.  
  31. if (cellIndex >= 0 && cellIndex < 9 && board[cellIndex] == Cell::Empty) {
  32. board[cellIndex] = currentPlayer;
  33. if (checkWin(currentPlayer)) {
  34. std::cout << (currentPlayer == Cell::X ? "X wins!" : "O wins!") << std::endl;
  35. resetGame();
  36. } else if (checkDraw()) {
  37. std::cout << "Draw!" << std::endl;
  38. resetGame();
  39. } else {
  40. currentPlayer = (currentPlayer == Cell::X) ? Cell::O : Cell::X;
  41. }
  42. }
  43. }
  44. }
  45. }
  46.  
  47. void update() {}
  48.  
  49. void render() {
  50. window.clear(sf::Color::White);
  51. drawBoard();
  52. drawMarks();
  53. window.display();
  54. }
  55.  
  56. void drawBoard() {
  57. for (int i = 1; i < 3; ++i) {
  58. sf::Vertex lineV[] = {
  59. sf::Vertex(sf::Vector2f(0, i * 600 / 3), sf::Color::Black),
  60. sf Frontier(sf::Vector2f(600, i * 600 / 3), sf::Color::Black)
  61. };
  62. sf::Vertex lineH[] = {
  63. sf::Vertex(sf::Vector2f(i * 600 / 3, 0), sf::Color::Black),
  64. sf::Vertex(sf::Vector2f(i * 600 / 3, 600), sf::Color::Black)
  65. };
  66. window.draw(lineV, 2, sf::Lines);
  67. window.draw(lineH, 2, sf::Lines);
  68. }
  69. }
  70.  
  71. void drawMarks() {
  72. for (int i = 0; i < 9; ++i) {
  73. int cellX = (i % 3) * 600 / 3;
  74. int cellY = (i / 3) * 600 / 3;
  75.  
  76. if (board[i] == Cell::X) {
  77. sf::Font font;
  78. if (!font.loadFromFile("arial.ttf")) {
  79. return;
  80. }
  81. sf::Text text("X", font, 80);
  82. text.setFillColor(sf::Color::Black);
  83. text.setPosition(cellX + 600 / 6 - text.getLocalBounds().width / 2, cellY + 600 / 6 - text.getLocalBounds().height / 2);
  84. window.draw(text);
  85. } else if (board[i] == Cell::O) {
  86. sf::Font font;
  87. if (!font.loadFromFile("arial.ttf")) {
  88. return;
  89. }
  90. sf::Text text("O", font, 80);
  91. text.setFillColor(sf::Color::Black);
  92. text.setPosition(cellX + 600 / 6 - text.getLocalBounds().width / 2, cellY + 600 / 6 - text.getLocalBounds().height / 2);
  93. window.draw(text);
  94. }
  95. }
  96. }
  97.  
  98. bool checkWin(Cell player) {
  99. // Check rows
  100. for (int i = 0; i < 3; ++i) {
  101. if (board[i * 3] == player && board[i * 3 + 1] == player && board[i * 3 + 2] == player) {
  102. return true;
  103. }
  104. }
  105. // Check columns
  106. for (int i = 0; i < 3; ++i) {
  107. if (board[i] == player && board[i + 3] == player && board[i + 6] == player) {
  108. return true;
  109. }
  110. }
  111. // Check diagonals
  112. if (board[0] == player && board[4] == player && board[8] == player) {
  113. return true;
  114. }
  115. if (board[2] == player && board[4] == player && board[6] == player) {
  116. return true;
  117. }
  118. return false;
  119. }
  120.  
  121. bool checkDraw() {
  122. for (int i = 0; i < 9; ++i) {
  123. if (board[i] == Cell::Empty) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129.  
  130. void resetGame() {
  131. for (int i = 0; i < 9; ++i) {
  132. board[i] = Cell::Empty;
  133. }
  134. currentPlayer = Cell::X;
  135. }
  136.  
  137. sf::RenderWindow window;
  138. std::vector<Cell> board;
  139. Cell currentPlayer;
  140. };
  141.  
  142. int main() {
  143. TicTacToe game;
  144. game.run();
  145. return 0;
  146. }
Success #stdin #stdout 0.03s 25940KB
stdin
Standard input is empty
stdout
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

enum class Cell { Empty, X, O };

class TicTacToe {
public:
    TicTacToe() : window(sf::VideoMode(600, 600), "Tic Tac Toe"), board(9, Cell::Empty), currentPlayer(Cell::X) {}

    void run() {
        while (window.isOpen()) {
            handleInput();
            update();
            render();
        }
    }

private:
    void handleInput() {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
                int cellX = event.mouseButton.x / (600 / 3);
                int cellY = event.mouseButton.y / (600 / 3);
                int cellIndex = cellY * 3 + cellX;

                if (cellIndex >= 0 && cellIndex < 9 && board[cellIndex] == Cell::Empty) {
                    board[cellIndex] = currentPlayer;
                    if (checkWin(currentPlayer)) {
                        std::cout << (currentPlayer == Cell::X ? "X wins!" : "O wins!") << std::endl;
                        resetGame();
                    } else if (checkDraw()) {
                        std::cout << "Draw!" << std::endl;
                        resetGame();
                    } else {
                        currentPlayer = (currentPlayer == Cell::X) ? Cell::O : Cell::X;
                    }
                }
            }
        }
    }

    void update() {}

    void render() {
        window.clear(sf::Color::White);
        drawBoard();
        drawMarks();
        window.display();
    }

    void drawBoard() {
        for (int i = 1; i < 3; ++i) {
            sf::Vertex lineV[] = {
                sf::Vertex(sf::Vector2f(0, i * 600 / 3), sf::Color::Black),
                sf Frontier(sf::Vector2f(600, i * 600 / 3), sf::Color::Black)
            };
             sf::Vertex lineH[] = {
                sf::Vertex(sf::Vector2f(i * 600 / 3, 0), sf::Color::Black),
                sf::Vertex(sf::Vector2f(i * 600 / 3, 600), sf::Color::Black)
            };
            window.draw(lineV, 2, sf::Lines);
            window.draw(lineH, 2, sf::Lines);
        }
    }

    void drawMarks() {
        for (int i = 0; i < 9; ++i) {
            int cellX = (i % 3) * 600 / 3;
            int cellY = (i / 3) * 600 / 3;

            if (board[i] == Cell::X) {
                sf::Font font;
                if (!font.loadFromFile("arial.ttf")) {
                    return;
                }
                sf::Text text("X", font, 80);
                text.setFillColor(sf::Color::Black);
                text.setPosition(cellX + 600 / 6 - text.getLocalBounds().width / 2, cellY + 600 / 6 - text.getLocalBounds().height / 2);
                window.draw(text);
            } else if (board[i] == Cell::O) {
                sf::Font font;
                if (!font.loadFromFile("arial.ttf")) {
                    return;
                }
                sf::Text text("O", font, 80);
                 text.setFillColor(sf::Color::Black);
                text.setPosition(cellX + 600 / 6 - text.getLocalBounds().width / 2, cellY + 600 / 6 - text.getLocalBounds().height / 2);
                window.draw(text);
            }
        }
    }

    bool checkWin(Cell player) {
        // Check rows
        for (int i = 0; i < 3; ++i) {
            if (board[i * 3] == player && board[i * 3 + 1] == player && board[i * 3 + 2] == player) {
                return true;
            }
        }
        // Check columns
        for (int i = 0; i < 3; ++i) {
            if (board[i] == player && board[i + 3] == player && board[i + 6] == player) {
                return true;
            }
        }
        // Check diagonals
        if (board[0] == player && board[4] == player && board[8] == player) {
            return true;
        }
        if (board[2] == player && board[4] == player && board[6] == player) {
            return true;
        }
        return false;
    }

    bool checkDraw() {
        for (int i = 0; i < 9; ++i) {
            if (board[i] == Cell::Empty) {
                return false;
            }
        }
        return true;
    }

    void resetGame() {
        for (int i = 0; i < 9; ++i) {
            board[i] = Cell::Empty;
        }
        currentPlayer = Cell::X;
    }

    sf::RenderWindow window;
    std::vector<Cell> board;
    Cell currentPlayer;
};

int main() {
    TicTacToe game;
    game.run();
    return 0;
}