Korotkodul

Game_new/main.cpp_3

Dec 12th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.01 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. //#include "board.h"
  15.  
  16. using namespace Graph_lib;
  17.  
  18. //Board.h
  19. struct Board{
  20.     std::vector <std::vector <int>> board;
  21.     int moved;
  22.     int moves;
  23.     Board();
  24.     int check_win();
  25.     int check_pos(int x, int y);
  26.     void check_board();
  27.     void clean_board();
  28. };
  29.  
  30. Board our_board;
  31.  
  32. //Board.cpp
  33. Board::Board() {
  34.   this->board.assign(3, std::vector <int> (3, 0));
  35.   this->moved = 1;
  36.   this->moves = 0;
  37. }
  38.  
  39. int Board::check_win()
  40. {  
  41.     int winner=0;
  42.     for (int i=0;i<3;i++)
  43.     {
  44.         if (board[0][i]==board[1][i] and board[1][i]==board[2][i] and board[0][i]!=0)
  45.             winner = board[0][i];
  46.         if (board[i][0]==board[i][1] and board[i][1]==board[i][2] and board[i][0]!=0)
  47.             winner = board[i][0];
  48.     }
  49.     if (board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[0][0]!=0)
  50.         winner = board[0][0];
  51.     if (board[2][0]==board[1][1] and board[1][1]==board[0][2] and board[2][0]!=0)
  52.         winner = board[2][0];
  53.     return winner;
  54. }
  55.  
  56. void Board::check_board()
  57. {
  58.     for (int i=0;i<3;i++)
  59.     {
  60.         for (int i1:board[i])
  61.         {
  62.             std::cout<<i1<<" ";
  63.         }
  64.         std::cout<<"\n";
  65.     }
  66.     std::cout<<"\n";
  67. }
  68.  
  69. int Board::check_pos(int x, int y)
  70. {  
  71.     if (board[y][x]==0)
  72.     {
  73.         moves++;
  74.         board[y][x]=moved;
  75.         moved=1+moved%2;
  76.         int win = check_win();
  77.         //checkboard();
  78.         if (win!=0)
  79.             return win*10;
  80.         if (win==0 and moves==9)
  81.             return -1;
  82.         return board[y][x];
  83.     }
  84.     else
  85.         return 0;
  86. }
  87.  
  88. void Board::clean_board() {
  89.   this->board.assign(3, std::vector <int> (3, 0));
  90.   this->moved = 1;
  91.   this->moves = 0;
  92. }
  93. //still works
  94.  
  95. //delete
  96. //перед тем, как удалить значение - надо удалить указатели на этот объект
  97.  
  98. //cell.h
  99. struct Cell{
  100.   std::pair <int, int> crd;
  101.   Fl_Button *button;
  102.   static void cell_pushed(Fl_Widget *widget, void *data);
  103.   Game *pointer_to_our_game;
  104.   Cell(int X, int Y, Game *pointer_to_our_game);
  105.   Cell(const Cell & ) = delete;
  106.   ~Cell();
  107. };
  108.  
  109.  
  110. //void Cell::cell_pushed(Fl_Widget *widget, void *data);
  111.  
  112. /*void cell_pushed(Fl_Widget *widget, void *data) {
  113.   std::cout << "HEY NIGGA!\n";
  114. }*/
  115.  
  116.  
  117. int window_x_len = 600;
  118. int window_y_len = 400;
  119. int step_from_edge = 50;
  120. int button_size = 100;
  121.  
  122.  
  123. Cell::Cell (int X, int Y, Game *pointer_to_our_game) {
  124.   this->crd.first = X;
  125.   this->crd.second = Y;
  126.   this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
  127.   this->button->color(FL_WHITE);
  128.   this->button->callback(this->cell_pushed, this);//(void*)&(this->crd)
  129.   this->pointer_to_our_game = pointer_to_our_game;
  130. }
  131.  
  132. Cell::~Cell() {
  133.   delete this->button;
  134. }
  135.  
  136. //visualboard.h
  137. struct VisualBoard{
  138.   std::vector <std::vector <Cell*>> playing_field;
  139.   VisualBoard();
  140.   void clean_board();
  141. };
  142.  
  143. VisualBoard our_visual_board;
  144.  
  145. //bool after_game_over_button_pushed;
  146. //int after_game_over_button_pushed_id;
  147.  
  148. bool game_is_over;
  149.  
  150. void start_game_cycle();
  151.  
  152. int cross_win_id = 10;//and put
  153. int zero_win_id = 20;//and put
  154. int draw_id = -1;
  155.  
  156. //main.cpp
  157. std::string message;
  158.  
  159. void game_over(int game_over_id) {
  160.   //after_game_over_button_pushed = false;
  161.   std::cout << "game over with id " << game_over_id << "\n";
  162.   /*
  163.   show_winner_button->redraw();
  164.   show_winner_button->show();
  165.   */
  166.   Fl_Window *window = new Fl_Window(200, 500, 300, 300 , "Show winner");
  167.   if (game_over_id == cross_win_id) {
  168.     message = "RED WINS\n";
  169.   } else if (game_over_id == zero_win_id) {
  170.     message = "BLUE WINS\n";
  171.   } else {
  172.     message = "DRAW\n";
  173.   }
  174.   Fl_Button *show_winner_button;
  175.   show_winner_button = new Fl_Button(10, 10, 50, 50, message.c_str());
  176.   window->show();
  177. }
  178.  
  179. //cell.cpp
  180. int cell_occupied_id = 0;
  181. int put_cross_id = 1;
  182. int put_zero_id = 2;
  183.  
  184. void Cell::cell_pushed(Fl_Widget *widget, void *data) {
  185.   //data = this
  186.     //std::pair <int, int> *xy = (std::pair <int, int>*)data;
  187.     auto pointer_to_cell = static_cast<Cell*>(data);
  188.     auto xy = pointer_to_cell->crd;
  189.     std::cout << "Pushed: " << xy.first << ' ' << xy.second << "\n";
  190.     int check_pos_id = our_board.check_pos(xy.first, xy.second);
  191.     if (check_pos_id == cell_occupied_id) {
  192.       std::cout << "cell occupied\n";
  193.       return;
  194.     }
  195.     if (game_is_over) {
  196.       std::cout << "game_is_over\n";
  197.       return;
  198.     }
  199.     if (check_pos_id == put_cross_id) {
  200.       our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_RED);
  201.       std::cout<<"put cross\n";
  202.       return;
  203.     } else if (check_pos_id == put_zero_id) {
  204.       our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_BLUE);
  205.       std::cout<<"put zero\n";
  206.       return;
  207.     }
  208.     //game is over
  209.     int game_over_id = check_pos_id;
  210.     std::cout << "CASE GAME IS OVER\n";
  211.     std::cout << "game_over_id: " << game_over_id << "\n";
  212.    
  213.     if (game_over_id == draw_id) {
  214.       if (our_board.moved - 1 == 1) {
  215.         our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_RED);
  216.       } else{
  217.         our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_BLUE);
  218.       }
  219.     }
  220.     //cout << "game_o"
  221.     if (game_over_id == cross_win_id) {
  222.       our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_RED);
  223.     } else if (game_over_id == zero_win_id) {
  224.       our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_BLUE);
  225.     }
  226.     game_is_over = true;
  227.     game_over(game_over_id);
  228. }
  229.  
  230. //visualboard.cpp
  231. VisualBoard::VisualBoard() {
  232.   this->playing_field.resize(3);
  233.   for (int i = 0; i < 3; ++i) {
  234.     playing_field[i].resize(3);
  235.   }
  236.   for (int y = 0; y < 3; ++y) {
  237.     for (int x = 0; x < 3; ++ x) {
  238.       this->playing_field[y][x] = new Cell(x, y);
  239.     }
  240.   }
  241. }
  242.  
  243. void VisualBoard::clean_board() {
  244.   for (int i = 0; i < 3; ++i) {
  245.     for (int j = 0; j < 3; ++j) {
  246.       our_visual_board.playing_field[i][j]->button->color(FL_WHITE);
  247.       our_visual_board.playing_field[i][j]->button->redraw();
  248.     }
  249.   }
  250. }
  251.  
  252. //InterfaceButtons.h
  253. struct InterfaceButtons {
  254.   Fl_Button *menu_button;
  255.   Fl_Button *play_again_button;
  256.   Fl_Button *close_program_button;
  257.   InterfaceButtons();
  258. };
  259.  
  260. Fl_Window *window;
  261.  
  262. //main.cpp
  263. void start_game_cycle() {
  264.   std::cout << "NEW GAME CYCLE STARTED\n";
  265.   game_is_over = false;
  266.   our_board.clean_board();
  267.   std::cout << "check board\n";
  268.   our_board.check_board();
  269.   our_visual_board.clean_board();
  270. }
  271.  
  272. void play_again_button_pushed(Fl_Widget *widget, void *data) {
  273.   //after_game_over_button_pushed = true;
  274.   //want_to_play = true;
  275.   std::cout << "play_again_button_pushed\n";
  276.   start_game_cycle();
  277. }
  278.  
  279. void close_program_button_pushed(Fl_Widget *widget, void *data) {
  280.   //after_game_over_button_pushed = true;
  281.   //want_to_play = false;
  282.   std::cout << "close_program_button_pushed\n";
  283.   exit(0);
  284. }
  285.  
  286.  
  287.  
  288. //InterfaceButtons.cpp
  289. InterfaceButtons::InterfaceButtons() {
  290.   this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
  291.   this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
  292.   this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
  293.   //this->button->callback(cell_pushed, (void*)&(this->crd));
  294.   this->play_again_button->callback(play_again_button_pushed);
  295.   this->close_program_button->callback(close_program_button_pushed);
  296. }
  297.  
  298. struct Game{
  299.   bool game_is_over = false;
  300.     Board our_board;
  301.     int cross_win_id = 10;//and put
  302.     int zero_win_id = 20;//and put
  303.     int draw_id = -1;
  304.     Game();
  305.     void game_over(int game_over_id);
  306.     void start_game_cycle();
  307.     Game(const Game & ) = delete;
  308.     ~Game();
  309. };
  310.  
  311.  
  312.  
  313. int main ()
  314. try
  315. {
  316.   //std::cout << "EVERYTHINS HAS FALLEN\n";
  317.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  318.   InterfaceButtons our_interface_buttons;
  319.   our_board = *new Board();
  320.   our_visual_board = *new VisualBoard();
  321.   start_game_cycle();
  322.   // window->end();
  323.   window->show();
  324.   /*Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  325.   InterfaceButtons our_interface_buttons;
  326.   our_board = *new Board();
  327.   our_visual_board = *new VisualBoard();
  328.   window->end();
  329.   window->show();*/
  330.   return Fl::run();
  331.   /*
  332.   This is how it will look at the end:
  333.   int window_x_len = 600;
  334.   int window_y_len = 400;
  335.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  336.   InterfaceButtons our_interface_buttons;
  337.   Game our_game;
  338.   our_game.start_game_cycle();
  339.   window->show();
  340.   //return Fl::run();
  341.   */
  342. }
  343. catch (std::exception& e)
  344. {
  345.   std::cerr << e.what() << std::endl;
  346.   return 1;
  347. }
  348. catch (...)
  349. {
  350.   std::cerr << "Oops, something went wrong..." << std::endl;
  351.   return 2;
  352. }
Add Comment
Please, Sign In to add comment