Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <iostream>
- int w = 400;
- int h = 400;
- int col = 3;
- int row = 3;
- int size = w / col;
- int total_sims = 0;
- char player_turn = 'O';
- char user_type = 'O';
- int inf = std::numeric_limits<int>::max();
- inline int findIndex(int x, int y) {
- return col * y + x;
- }
- struct Grid {
- public:
- int x, y;
- char type = '.'; // free spot jei '.'
- }; std::vector<Grid> grid;
- class Player {
- public:
- char type;
- }players[2];
- class AI {
- public:
- AI(char T) {
- sim_bot_type = T;
- }
- int MinMax(std::vector<Grid> copy_grid, int depth, int alpha, int beta, bool my_turn, char player_type) {
- if (isGameComplete(copy_grid)) {
- total_sims++;
- if (my_turn) return depth - 10;
- else return 10 - depth;
- }if (!free_space(copy_grid)) return 0; // draw
- char future_player_type = ' ';
- if (player_type == players[0].type) future_player_type = players[1].type;
- else future_player_type = players[0].type;
- int minScore = inf;
- int maxScore = -inf;
- Grid best_spot;
- for (auto& spot : copy_grid) {
- if (spot.type == '.') {
- std::vector<Grid> send_grid = copy_grid;
- send_grid[findIndex(spot.x, spot.y)].type = player_type;
- int s = MinMax(send_grid, depth + 1, alpha, beta,!my_turn, future_player_type);
- if (minScore > s) minScore = s;
- if (maxScore < s) {
- maxScore = s;
- best_spot = spot;
- }
- if (my_turn) {
- if (s > alpha) {
- alpha = s;
- }
- }
- else {
- if (s < beta) {
- beta = s;
- }
- }
- if (beta <= alpha)break;
- }if (beta <= alpha)break;
- }
- if (depth == 0) {
- grid[findIndex(best_spot.x, best_spot.y)].type = player_type;
- }
- if (my_turn) {
- return maxScore;
- }
- else {
- return minScore;
- }
- }
- private:
- char sim_bot_type;
- bool isGameComplete(std::vector<Grid>& g) {
- for (int i = 0; i < 2; i++) {
- char type = players[i].type;
- if (g[findIndex(0, 0)].type == type && g[findIndex(1, 0)].type == type && g[findIndex(2, 0)].type == type) return true;
- if (g[findIndex(0, 1)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(2, 1)].type == type) return true;
- if (g[findIndex(0, 2)].type == type && g[findIndex(1, 2)].type == type && g[findIndex(2, 2)].type == type) return true;
- if (g[findIndex(0, 0)].type == type && g[findIndex(0, 1)].type == type && g[findIndex(0, 2)].type == type) return true;
- if (g[findIndex(1, 0)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(1, 2)].type == type) return true;
- if (g[findIndex(2, 0)].type == type && g[findIndex(2, 1)].type == type && g[findIndex(2, 2)].type == type) return true;
- if (g[findIndex(0, 0)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(2, 2)].type == type) return true;
- if (g[findIndex(2, 0)].type == type && g[findIndex(1, 1)].type == type && g[findIndex(0, 2)].type == type) return true;
- }
- return false;
- }
- bool free_space(std::vector<Grid>& g) {
- for (auto& spot : g) {
- if (spot.type == '.') return true;
- }return false;
- }
- };
- void setup() {
- players[0].type = 'X';
- players[1].type = 'O';
- for (int i = 0; i < row; i++) {
- for (int j = 0; j < col; j++) {
- grid.push_back({ j, i, '.' });
- }
- }
- }
- int main() {
- sf::RenderWindow window(sf::VideoMode(w, h), "TIC TAC TOE");
- setup();
- // teksturu stuff
- sf::Texture BG_T, X_T, O_T; // background
- BG_T.loadFromFile("images/background.jpg");
- O_T.loadFromFile("images/O.png");
- X_T.loadFromFile("images/X.png");
- sf::Sprite BG_S, O_S, X_S;
- BG_S.setTexture(BG_T);
- O_S.setTexture(O_T);
- X_S.setTexture(X_T);
- while (window.isOpen()) {
- total_sims = 0;
- sf::Event evnt;
- while (window.pollEvent(evnt)) {
- switch (evnt.type) {
- case sf::Event::Closed:
- window.close();
- case sf::Event::MouseButtonPressed:
- if (evnt.mouseButton.button == sf::Mouse::Left && player_turn == user_type) {
- int x = evnt.mouseButton.x / size;
- int y = evnt.mouseButton.y / size;
- if (grid[findIndex(x, y)].type == '.') {
- char Enemy;
- if (user_type == players[0].type) Enemy = players[1].type;
- else Enemy = players[0].type;
- grid[findIndex(x, y)].type = user_type;
- player_turn = Enemy;
- }
- }
- break;
- }
- }
- if (player_turn != user_type) {
- char Enemy;
- if (user_type == players[0].type) Enemy = players[1].type;
- else Enemy = players[0].type;
- AI ai(Enemy);
- ai.MinMax(grid, 0, -inf, inf,true, Enemy);
- player_turn = user_type;
- }
- window.clear();
- window.draw(BG_S);
- for (auto & spot : grid) {
- if (spot.type == 'O') {
- float x = O_S.getGlobalBounds().width;
- float y = O_S.getGlobalBounds().height;
- O_S.scale(sf::Vector2f((size - 20) / x, (size - 20) / y));
- O_S.setPosition(sf::Vector2f(spot.x * size + 20, spot.y * size + 20));
- window.draw(O_S);
- }
- if (spot.type == 'X') {
- float x = X_S.getGlobalBounds().width;
- float y = X_S.getGlobalBounds().height;
- X_S.scale(sf::Vector2f((size - 20) / x, (size - 20) / y));
- X_S.setPosition(sf::Vector2f(spot.x * size + 20, spot.y * size + 20));
- window.draw(X_S);
- }
- }
- window.display();
- if (total_sims > 0)
- std::cerr << "sim: " << total_sims << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement