Korotkodul

tic_tac_toe/main.cpp_11_заново

Nov 27th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.19 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 our_board;
  29.  
  30. //Board.cpp
  31. Board::Board() {
  32.   this->board.assign(3, std::vector <int> (3, 0));
  33.   this->moved = 1;
  34.   this->moves = 0;
  35. }
  36.  
  37. int Board::check_win()
  38. {  
  39.     int winner=0;
  40.     for (int i=0;i<3;i++)
  41.     {
  42.         if (board[0][i]==board[1][i] and board[1][i]==board[2][i] and board[0][i]!=0)
  43.             winner = board[0][i];
  44.         if (board[i][0]==board[i][1] and board[i][1]==board[i][2] and board[i][0]!=0)
  45.             winner = board[i][0];
  46.     }
  47.     if (board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[0][0]!=0)
  48.         winner = board[0][0];
  49.     if (board[2][0]==board[1][1] and board[1][1]==board[0][2] and board[2][0]!=0)
  50.         winner = board[2][0];
  51.     return winner;
  52. }
  53.  
  54. void Board::check_board()
  55. {
  56.     for (int i=0;i<3;i++)
  57.     {
  58.         for (int i1:board[i])
  59.         {
  60.             std::cout<<i1<<" ";
  61.         }
  62.         std::cout<<"\n";
  63.     }
  64.     std::cout<<"\n";
  65. }
  66.  
  67. int Board::check_pos(int x, int y)
  68. {  
  69.     if (board[y][x]==0)
  70.     {
  71.         moves++;
  72.         board[y][x]=moved;
  73.         moved=1+moved%2;
  74.         int win = check_win();
  75.         //checkboard();
  76.         if (win!=0)
  77.             return win*10;
  78.         if (win==0 and moves==9)
  79.             return -1;
  80.         return board[y][x];
  81.     }
  82.     else
  83.         return 0;
  84. }
  85.  
  86. void Board::clean_board() {
  87.   this->board.assign(3, std::vector <int> (3, 0));
  88. }
  89. //still works
  90.  
  91. //cell.h
  92. struct Cell{
  93.   std::pair <int, int> crd;
  94.   Fl_Button *button;
  95.   Cell();
  96.   Cell(int X, int Y);
  97.   Cell(const Cell & ) = delete;
  98.   ~Cell();
  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. void cell_pushed(Fl_Widget *widget, void *data);
  110.  
  111. /*void cell_pushed(Fl_Widget *widget, void *data) {
  112.   std::cout << "HEY NIGGA!\n";
  113. }*/
  114.  
  115.  
  116. int window_x_len = 600;
  117. int window_y_len = 400;
  118. int step_from_edge = 50;
  119. int button_size = 100;
  120.  
  121.  
  122. Cell::Cell (int X, int Y) {
  123.   this->crd.first = X;
  124.   this->crd.second = Y;
  125.   this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
  126.   this->button->color(FL_WHITE);
  127.   this->button->callback(cell_pushed, (void*)&(this->crd));
  128. }
  129.  
  130. Cell::~Cell() {
  131.   delete this->button;
  132. }
  133.  
  134. //visualboard.h
  135. struct VisualBoard{
  136.   std::vector <std::vector <Cell*>> playing_field;
  137.   VisualBoard();
  138.   void clean_board();
  139. };
  140.  
  141. VisualBoard our_visual_board;
  142.  
  143. int cross_win_id = 10;//and put
  144. int zero_win_id = 20;//and put
  145.  
  146. bool want_to_play = true;
  147.  
  148. bool after_game_over_button_pushed;
  149. int after_game_over_button_pushed_id;
  150.  
  151. bool game_is_over;
  152.  
  153. void start_game_cycle();
  154.  
  155. //main.cpp
  156. void game_over(int game_over_id) {
  157.   after_game_over_button_pushed = false;
  158.   std::cout << "game over with id " << game_over_id << "\n";
  159.   //start_game_cycle();
  160. }
  161.  
  162. //cell.cpp
  163. int cell_occupied_id = 0;
  164. int draw_id = -1;
  165. int put_cross_id = 1;
  166. int put_zero_id = 2;
  167.  
  168. void cell_pushed(Fl_Widget *widget, void *data) {
  169.     std::pair <int, int> *xy = (std::pair <int, int>*)data;
  170.     std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\n";
  171.     int check_pos_id = our_board.check_pos((*xy).first, (*xy).second);
  172.     if (check_pos_id == cell_occupied_id) {
  173.       std::cout << "cell occupied\n";
  174.       return;
  175.     }
  176.     if (game_is_over) {
  177.       std::cout << "game_is_over\n";
  178.       return;
  179.     }
  180.     if (check_pos_id == put_cross_id) {
  181.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  182.       std::cout<<"put cross\n";
  183.       return;
  184.     } else if (check_pos_id == put_zero_id) {
  185.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  186.       std::cout<<"put zero\n";
  187.       return;
  188.     }
  189.     //game is over
  190.     int game_over_id = check_pos_id;
  191.     std::cout << "CASE GAME IS OVER\n";
  192.     std::cout << "game_over_id: " << game_over_id << "\n";
  193.    
  194.     if (game_over_id == draw_id) {
  195.       if (our_board.moved - 1 == 1) {
  196.         our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  197.       } else{
  198.         our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  199.       }
  200.     }
  201.     //cout << "game_o"
  202.     if (game_over_id == cross_win_id) {
  203.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  204.     } else if (game_over_id == zero_win_id) {
  205.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  206.     }
  207.     game_is_over = true;
  208.     game_over(game_over_id);
  209. }
  210.  
  211. //visualboard.cpp
  212. VisualBoard::VisualBoard() {
  213.   this->playing_field.resize(3);
  214.   for (int i = 0; i < 3; ++i) {
  215.     playing_field[i].resize(3);
  216.   }
  217.   for (int y = 0; y < 3; ++y) {
  218.     for (int x = 0; x < 3; ++ x) {
  219.       this->playing_field[y][x] = new Cell(x, y);
  220.     }
  221.   }
  222. }
  223.  
  224. void VisualBoard::clean_board() {
  225.   for (int y = 0; y < 3; ++y) {
  226.     for (int x = 0; x < 3; ++ x) {
  227.       playing_field[y][x]->button->color(FL_WHITE);
  228.     }
  229.   }
  230. }
  231.  
  232. //main.cpp
  233. void start_game_cycle() {
  234.   //game_is_over = false;
  235.   our_board.clean_board();
  236.   our_visual_board.clean_board();
  237. }
  238.  
  239. /*void play_again_button_pushed(Fl_Widget *widget, void *data) {
  240.   after_game_over_button_pushed = true;
  241.   want_to_play = true;
  242.   std::cout << "play_again_button_pushed\n";
  243.   start_game_cycle();
  244. }*/
  245.  
  246. void close_program_button_pushed(Fl_Widget *widget, void *data) {
  247.   after_game_over_button_pushed = true;
  248.   want_to_play = false;
  249.   std::cout << "close_program_button_pushed\n";
  250.   exit(0);
  251. }
  252.  
  253. //InterfaceButtons.h
  254. struct InterfaceButtons {
  255.   Fl_Button *menu_button;
  256.   //Fl_Button *play_again_button;
  257.   Fl_Button *close_program_button;
  258.   InterfaceButtons();
  259. };
  260.  
  261. //InterfaceButtons.cpp
  262. InterfaceButtons::InterfaceButtons() {
  263.   this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
  264.   //this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
  265.   this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
  266.   //this->button->callback(cell_pushed, (void*)&(this->crd));
  267.   //this->play_again_button->callback(play_again_button_pushed);
  268.   this->close_program_button->callback(close_program_button_pushed);
  269. }
  270.  
  271. int main ()
  272. try
  273. {
  274.   std::cout << "EVERYTHINS HAS FALLEN\n";
  275.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  276.   InterfaceButtons our_interface_buttons;
  277.   our_board = *new Board();
  278.   our_visual_board = *new VisualBoard();
  279.   window->end();
  280.   window->show();
  281.   return Fl::run();
  282. }
  283. catch (std::exception& e)
  284. {
  285.   std::cerr << e.what() << std::endl;
  286.   return 1;
  287. }
  288. catch (...)
  289. {
  290.   std::cerr << "Oops, something went wrong..." << std::endl;
  291.   return 2;
  292. }
Add Comment
Please, Sign In to add comment