Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- const int ROWS = 3;
- const int COLS = 3;
- const int CELL_SIZE = 300;
- const int SCREEN_WIDTH = COLS * CELL_SIZE;
- const int SCREEN_HEIGHT = ROWS * CELL_SIZE;
- enum class Player { None, Cross, Nought };
- struct GameState {
- Player board[ROWS][COLS];
- Player currentPlayer;
- bool gameOver;
- Player winner;
- };
- void initialize(GameState& state) {
- state.currentPlayer = Player::Cross;
- state.gameOver = false;
- state.winner = Player::None;
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- state.board[i][j] = Player::None;
- }
- }
- }
- bool checkWinner(const GameState& state, Player player) {
- for (int i = 0; i < ROWS; ++i) {
- if (state.board[i][0] == player && state.board[i][1] == player && state.board[i][2] == player) {
- return true;
- }
- }
- for (int j = 0; j < COLS; ++j) {
- if (state.board[0][j] == player && state.board[1][j] == player && state.board[2][j] == player) {
- return true;
- }
- }
- if (state.board[0][0] == player && state.board[1][1] == player && state.board[2][2] == player) {
- return true;
- }
- if (state.board[0][2] == player && state.board[1][1] == player && state.board[2][0] == player) {
- return true;
- }
- return false;
- }
- bool isBoardFull(const GameState& state) {
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- if (state.board[i][j] == Player::None) {
- return false;
- }
- }
- }
- return true;
- }
- bool isMoveValid(const GameState& state, int row, int col) {
- return row >= 0 && row < ROWS && col >= 0 && col < COLS && state.board[row][col] == Player::None;
- }
- void computerMove(GameState& state) {
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- if (state.board[i][j] == Player::None) {
- state.board[i][j] = Player::Nought;
- if (checkWinner(state, Player::Nought)) {
- return;
- }
- state.board[i][j] = Player::None;
- }
- }
- }
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- if (state.board[i][j] == Player::None) {
- state.board[i][j] = Player::Cross;
- if (checkWinner(state, Player::Cross)) {
- state.board[i][j] = Player::Nought;
- return;
- }
- state.board[i][j] = Player::None;
- }
- }
- }
- if (state.board[1][1] == Player::None) {
- state.board[1][1] = Player::Nought;
- return;
- }
- if (state.board[0][0] == Player::None) {
- state.board[0][0] = Player::Nought;
- return;
- }
- if (state.board[0][2] == Player::None) {
- state.board[0][2] = Player::Nought;
- return;
- }
- if (state.board[2][0] == Player::None) {
- state.board[2][0] = Player::Nought;
- return;
- }
- if (state.board[2][2] == Player::None) {
- state.board[2][2] = Player::Nought;
- return;
- }
- int row, col;
- do {
- row = rand() % ROWS;
- col = rand() % COLS;
- } while (state.board[row][col] != Player::None);
- state.board[row][col] = Player::Nought;
- }
- void drawBoard(sf::RenderWindow& window, const GameState& state) {
- window.clear(sf::Color::White);
- for (int i = 0; i < ROWS; ++i) {
- for (int j = 0; j < COLS; ++j) {
- sf::RectangleShape cell(sf::Vector2f(CELL_SIZE, CELL_SIZE));
- cell.setPosition(j * CELL_SIZE, i * CELL_SIZE);
- cell.setFillColor(sf::Color::Transparent);
- cell.setOutlineThickness(2);
- cell.setOutlineColor(sf::Color::Black);
- window.draw(cell);
- if (state.board[i][j] == Player::Cross) {
- sf::Texture crossTexture;
- crossTexture.loadFromFile("cross.png");
- sf::Sprite crossSprite(crossTexture);
- crossSprite.setPosition(j * CELL_SIZE, i * CELL_SIZE);
- window.draw(crossSprite);
- }
- else if (state.board[i][j] == Player::Nought) {
- sf::Texture noughtTexture;
- noughtTexture.loadFromFile("nought.png");
- sf::Sprite noughtSprite(noughtTexture);
- noughtSprite.setPosition(j * CELL_SIZE, i * CELL_SIZE);
- window.draw(noughtSprite);
- }
- }
- }
- if (state.gameOver) {
- sf::Font font;
- font.loadFromFile("arial.ttf");
- sf::Text winnerText;
- winnerText.setFont(font);
- if (state.winner == Player::Nought)
- winnerText.setString("Winner: Algorithm");
- else
- winnerText.setString("Draw!");
- winnerText.setCharacterSize(50);
- winnerText.setFillColor(sf::Color::Red);
- winnerText.setPosition(300, SCREEN_HEIGHT / 2 - 50);
- window.draw(winnerText);
- sf::RectangleShape restartButton(sf::Vector2f(200, 50));
- restartButton.setFillColor(sf::Color::Green);
- restartButton.setPosition(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 + 50);
- window.draw(restartButton);
- sf::Text buttonText;
- buttonText.setFont(font);
- buttonText.setString("Restart");
- buttonText.setCharacterSize(30);
- buttonText.setFillColor(sf::Color::White);
- buttonText.setPosition(SCREEN_WIDTH / 2 - 60, SCREEN_HEIGHT / 2 + 55);
- window.draw(buttonText);
- }
- window.display();
- }
- void handleInput(GameState& state, sf::RenderWindow& window) {
- if (state.gameOver) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed)
- window.close();
- else if (event.type == sf::Event::MouseButtonPressed) {
- sf::Vector2i mousePos = sf::Mouse::getPosition(window);
- if (mousePos.x >= SCREEN_WIDTH / 2 - 100 && mousePos.x <= SCREEN_WIDTH / 2 + 100 &&
- mousePos.y >= SCREEN_HEIGHT / 2 + 50 && mousePos.y <= SCREEN_HEIGHT / 2 + 100) {
- initialize(state);
- return;
- }
- }
- }
- }
- else {
- if (state.currentPlayer == Player::Cross) {
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed)
- window.close();
- if (!state.gameOver && event.type == sf::Event::MouseButtonPressed) {
- sf::Vector2i mousePos = sf::Mouse::getPosition(window);
- int row = mousePos.y / CELL_SIZE;
- int col = mousePos.x / CELL_SIZE;
- if (isMoveValid(state, row, col)) {
- state.board[row][col] = Player::Cross;
- if (checkWinner(state, Player::Cross)) {
- state.gameOver = true;
- state.winner = Player::Cross;
- }
- else if (isBoardFull(state)) {
- state.gameOver = true;
- }
- else {
- state.currentPlayer = Player::Nought;
- }
- }
- }
- }
- }
- else {
- computerMove(state);
- if (checkWinner(state, Player::Nought)) {
- state.gameOver = true;
- state.winner = Player::Nought;
- }
- else if (isBoardFull(state)) {
- state.gameOver = true;
- }
- else {
- state.currentPlayer = Player::Cross;
- }
- }
- }
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Tic Tac Toe");
- srand(static_cast<unsigned>(time(nullptr)));
- GameState gameState;
- initialize(gameState);
- while (window.isOpen()) {
- handleInput(gameState, window);
- drawBoard(window, gameState);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement