Advertisement
Korotkodul

tic_tac_toe/main.cpp_2

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