Advertisement
Korotkodul

tic_tac_toe/main.cpp

Nov 27th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <stdexcept>
  4. #include <string>
  5. #include <vector>
  6.  
  7. #include <FL/Fl.H>
  8. #include <FL/Fl_Window.H>
  9. #include <FL/Fl_Button.H>
  10.  
  11. #include <Graph_lib/Graph.h>
  12. #include <Graph_lib/Simple_window.h>
  13.  
  14. using namespace Graph_lib;
  15.  
  16. //Board.h
  17. struct Board{
  18.     std::vector <std::vector <int>> board;
  19.     int moved;
  20.     int moves;
  21.     Board();
  22.     int checkwin();
  23.     int checkpos(int x, int y);
  24.     void checkboard();
  25.     void clean_board();
  26. };
  27.  
  28. //Board.cpp
  29. Board::Board() {
  30.   this->board.assign(3, std::vector <int> (3, 0));
  31.   this->moved = 1;
  32.   this->moves = 0;
  33. }
  34.  
  35. int Board::checkwin()
  36. {  
  37.     int winner=0;
  38.     for (int i=0;i<3;i++)
  39.     {
  40.         if (board[0][i]==board[1][i] and board[1][i]==board[2][i] and board[0][i]!=0)
  41.             winner = board[0][i];
  42.         if (board[i][0]==board[i][1] and board[i][1]==board[i][2] and board[i][0]!=0)
  43.             winner = board[i][0];
  44.     }
  45.     if (board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[0][0]!=0)
  46.         winner = board[0][0];
  47.     if (board[2][0]==board[1][1] and board[1][1]==board[0][2] and board[2][0]!=0)
  48.         winner = board[2][0];
  49.     return winner;
  50. }
  51.  
  52. void Board::checkboard()
  53. {
  54.     for (int i=0;i<3;i++)
  55.     {
  56.         for (int i1:board[i])
  57.         {
  58.             std::cout<<i1<<" ";
  59.         }
  60.         std::cout<<"\n";
  61.     }
  62.     std::cout<<"\n";
  63. }
  64.  
  65. int Board::checkpos(int x, int y)
  66. {  
  67.     if (board[y][x]==0)
  68.     {
  69.         moves++;
  70.         board[y][x]=moved;
  71.         moved=1+moved%2;
  72.         int win = checkwin();
  73.         //checkboard();
  74.         if (win!=0)
  75.             return win*10;
  76.         if (win==0 and moves==9)
  77.             return -1;
  78.         return board[y][x];
  79.     }
  80.     else
  81.         return 0;
  82. }
  83.  
  84.  
  85.  
  86.  
  87. int window_x_len = 600;
  88. int window_y_len = 400;
  89. int step_from_edge = 50;
  90. int button_size = 100;
  91.  
  92. void button_callback(Fl_Widget *widget, void *data) {
  93.     //std::string *label = (std::string*)data;
  94.     //std::cout << "Pushed: " << *label << std::endl;
  95.     //std::cout << "CB " << data << std::endl;
  96.     std::pair <int, int> *xy = (std::pair <int, int>*)data;
  97.     std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\n";
  98. }
  99. //free = 1, used = -1
  100.  
  101.  
  102.  
  103.  
  104. struct Cell {
  105.   //int x;
  106.   //int y;
  107.   std::pair <int, int> crd;
  108.   //std::string state;
  109.   Fl_Button *button;
  110.   Cell(const Cell & ) = delete;
  111.   // Cell( Cell & ) = delete;
  112.   // operator= ()
  113.  
  114.   Cell () {
  115.     //this->x = 0;
  116.     //this->y = 0;
  117.     this->crd.first = 0;
  118.     this->crd.second = 0;
  119.     //this->state = "free";
  120.     this->button = new Fl_Button(0, 0, 0, 0, "");
  121.     this->button->color(FL_WHITE);
  122.     //std::cout << "Cell "  << std::endl;
  123.   }
  124.   Cell (int X, int Y) {
  125.     //this->x = X;
  126.     //this->y = Y;
  127.     //this->state = "free";
  128.     this->crd.first = X;
  129.     this->crd.second = Y;
  130.     this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
  131.     this->button->color(FL_WHITE);
  132.     this->button->callback(button_callback, (void*)&(this->crd));
  133.     //std::cout << "Cell " << X << Y << std::endl;
  134.  
  135.   }
  136.   ~Cell() {
  137.     delete this->button;
  138.   }
  139. };
  140. //0-ничего, 1-крестик,2 - нолик
  141.  
  142. std::vector <std::vector <Cell*>> playing_field;
  143.  
  144.  
  145. int main ()
  146. try
  147. {
  148.   /*Fl_Window *window = new Fl_Window(100,400,window_x_len, window_y_len , "Window");
  149.  
  150.   playing_field.resize(3);
  151.   for (int i = 0; i < 3; ++i) {
  152.     playing_field[i].resize(3);
  153.   }
  154.   for (int y = 0; y < 3; ++y) {
  155.     for (int x = 0; x < 3; ++x) {
  156.       // Cell new_Cell(x, y);
  157.       // Cell new_Cell = new Cell(x, y);
  158.       playing_field[y][x] = new Cell(x, y);
  159.     }
  160.   }*/
  161.   /*Board board;
  162.   board.checkpos(0, 2);
  163.   board.checkpos(2, 0);
  164.   board.checkboard();*/
  165.   //window->end();
  166.   //window->show();
  167.   //return Fl::run();
  168. }
  169. catch (std::exception& e)
  170. {
  171.   std::cerr << e.what() << std::endl;
  172.   return 1;
  173. }
  174. catch (...)
  175. {
  176.   std::cerr << "Oops, something went wrong..." << std::endl;
  177.   return 2;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement