Advertisement
Korotkodul

tic_tac_toe/main.cpp_1

Nov 27th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.34 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 check_win();
  23.     int check_pos(int x, int y);
  24.     void check_board();
  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::check_win()
  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::check_board()
  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::check_pos(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 = check_win();
  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. void Board::clean_board() {
  85.   this->board.assign(3, std::vector <int> (3, 0));
  86. }
  87.  
  88.  
  89. //cell.h
  90. struct Cell{
  91.   std::pair <int, int> crd;
  92.   Fl_Button *button;
  93.   Cell();
  94.   Cell(int X, int Y);
  95.   //Cell(const Cell & ) = delete;
  96.   Cell(const Cell & );
  97.   ~Cell();
  98. };
  99.  
  100.  
  101. //cell.cpp
  102. Cell::Cell() {
  103.   this->crd.first = 0;
  104.   this->crd.second = 0;
  105.   this->button = new Fl_Button(0, 0, 0, 0, "");
  106.   this->button->color(FL_WHITE);
  107. }
  108.  
  109. Cell::Cell(const Cell & ) = delete;
  110.  
  111. void button_callback(Fl_Widget *widget, void *data) {
  112.     //std::string *label = (std::string*)data;
  113.     //std::cout << "Pushed: " << *label << std::endl;
  114.     //std::cout << "CB " << data << std::endl;
  115.     std::pair <int, int> *xy = (std::pair <int, int>*)data;
  116.     std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\n";
  117. }
  118.  
  119. int window_x_len = 600;
  120. int window_y_len = 400;
  121. int step_from_edge = 50;
  122. int button_size = 100;
  123.  
  124.  
  125. Cell::Cell (int X, int Y) {
  126.   this->crd.first = X;
  127.   this->crd.second = Y;
  128.   this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
  129.   this->button->color(FL_WHITE);
  130.   this->button->callback(button_callback, (void*)&(this->crd));
  131. }
  132.  
  133. /*struct Cell {
  134.  
  135.   Cell (int X, int Y) {
  136.     //this->x = X;
  137.     //this->y = Y;
  138.     //this->state = "free";
  139.     this->crd.first = X;
  140.     this->crd.second = Y;
  141.     this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
  142.     this->button->color(FL_WHITE);
  143.     this->button->callback(button_callback, (void*)&(this->crd));
  144.     //std::cout << "Cell " << X << Y << std::endl;
  145.  
  146.   }
  147.   ~Cell() {
  148.     delete this->button;
  149.   }
  150. };*/
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. std::vector <std::vector <Cell*>> playing_field;
  158.  
  159.  
  160. int main ()
  161. try
  162. {
  163.   /*Fl_Window *window = new Fl_Window(100,400,window_x_len, window_y_len , "Window");
  164.  
  165.   playing_field.resize(3);
  166.   for (int i = 0; i < 3; ++i) {
  167.     playing_field[i].resize(3);
  168.   }
  169.   for (int y = 0; y < 3; ++y) {
  170.     for (int x = 0; x < 3; ++x) {
  171.       // Cell new_Cell(x, y);
  172.       // Cell new_Cell = new Cell(x, y);
  173.       playing_field[y][x] = new Cell(x, y);
  174.     }
  175.   }*/
  176.   /*Board board;
  177.   board.check_pos(0, 2);
  178.   board.check_pos(2, 0);
  179.   board.check_board();
  180.   board.clean_board();
  181.   board.check_board();*/
  182.   //window->end();
  183.   //window->show();
  184.   //return Fl::run();
  185. }
  186. catch (std::exception& e)
  187. {
  188.   std::cerr << e.what() << std::endl;
  189.   return 1;
  190. }
  191. catch (...)
  192. {
  193.   std::cerr << "Oops, something went wrong..." << std::endl;
  194.   return 2;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement