Korotkodul

tic_tac_toe/main.cpp_10_заново

Nov 27th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.23 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. //main.cpp
  154. void game_over(int game_over_id) {
  155.   after_game_over_button_pushed = false;
  156.   std::cout << "game over with id " << game_over_id << "\n";
  157.   //std::cout << "now we will show winners\n";
  158.   //show_winner(game_over_id);
  159.   /*while (!after_game_over_button_pushed) {
  160.     //do nothing
  161.   } */
  162.   //want_to_play = (bool)after_game_over_button_pushed_id;
  163.   //want_to_play = false;
  164.   //return;
  165. }
  166.  
  167. //cell.cpp
  168. int cell_occupied_id = 0;
  169. int draw_id = -1;
  170. int put_cross_id = 1;
  171. int put_zero_id = 2;
  172.  
  173. void cell_pushed(Fl_Widget *widget, void *data) {
  174.     std::cout << "func cell_pushed\n";
  175.     std::pair <int, int> *xy = (std::pair <int, int>*)data;
  176.     std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\n";
  177.  
  178.     int check_pos_id = our_board.check_pos((*xy).first, (*xy).second);
  179.     if (check_pos_id == cell_occupied_id) {
  180.       std::cout << "cell occupied\n";
  181.       return;
  182.     }
  183.     if (game_is_over) {
  184.       std::cout << "game_is_over\n";
  185.       return;
  186.     }
  187.     if (check_pos_id == put_cross_id) {
  188.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  189.       std::cout<<"put cross\n";
  190.       return;
  191.     } else if (check_pos_id == put_zero_id) {
  192.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  193.       std::cout<<"put zero\n";
  194.       return;
  195.     }
  196.     //game is over
  197.     if (our_board.moved - 1 == 1) {
  198.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  199.     } else{
  200.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  201.     }
  202.     //cout << "game_o"
  203.     int game_over_id = check_pos_id;
  204.     if (game_over_id == cross_win_id) {
  205.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  206.     } else {
  207.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  208.     }
  209.     game_is_over = true;
  210.     game_over(game_over_id);
  211. }
  212.  
  213. //visualboard.cpp
  214. VisualBoard::VisualBoard() {
  215.   this->playing_field.resize(3);
  216.   for (int i = 0; i < 3; ++i) {
  217.     playing_field[i].resize(3);
  218.   }
  219.   for (int y = 0; y < 3; ++y) {
  220.     for (int x = 0; x < 3; ++ x) {
  221.       this->playing_field[y][x] = new Cell(x, y);
  222.     }
  223.   }
  224. }
  225.  
  226. void VisualBoard::clean_board() {
  227.   for (int y = 0; y < 3; ++y) {
  228.     for (int x = 0; x < 3; ++ x) {
  229.       playing_field[y][x]->button->color(FL_WHITE);
  230.     }
  231.   }
  232. }
  233.  
  234. //main.cpp
  235. void start_game_cycle() {
  236.   //game_is_over = false;
  237.   our_board.clean_board();
  238.   our_visual_board.clean_board();
  239. }
  240.  
  241. void play_again_button_pushed(Fl_Widget *widget, void *data) {
  242.   after_game_over_button_pushed = true;
  243.   want_to_play = true;
  244.   std::cout << "play_again_button_pushed\n";
  245.   start_game_cycle();
  246. }
  247.  
  248. void close_program_button_pushed(Fl_Widget *widget, void *data) {
  249.   after_game_over_button_pushed = true;
  250.   want_to_play = false;
  251.   std::cout << "close_program_button_pushed\n";
  252.   exit(0);
  253. }
  254.  
  255. //InterfaceButtons.h
  256. struct InterfaceButtons {
  257.   Fl_Button *menu_button;
  258.   Fl_Button *play_again_button;
  259.   Fl_Button *close_program_button;
  260.   InterfaceButtons();
  261. };
  262.  
  263. //InterfaceButtons.cpp
  264. InterfaceButtons::InterfaceButtons() {
  265.   this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
  266.   this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
  267.   this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
  268.   //this->button->callback(cell_pushed, (void*)&(this->crd));
  269.   this->play_again_button->callback(play_again_button_pushed);
  270.   this->close_program_button->callback(close_program_button_pushed);
  271. }
  272.  
  273. int main ()
  274. try
  275. {
  276.   std::cout << "EVERYTHINS HAS FALLEN\n";
  277.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  278.   InterfaceButtons our_interface_buttons;
  279.   our_board = *new Board();
  280.   our_visual_board = *new VisualBoard();
  281.   window->end();
  282.   window->show();
  283.   return Fl::run();
  284. }
  285. catch (std::exception& e)
  286. {
  287.   std::cerr << e.what() << std::endl;
  288.   return 1;
  289. }
  290. catch (...)
  291. {
  292.   std::cerr << "Oops, something went wrong..." << std::endl;
  293.   return 2;
  294. }
Add Comment
Please, Sign In to add comment