Korotkodul

Game_new/main.cpp_2

Dec 12th, 2024
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 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.   void cell_pushed();
  103.   Cell();
  104.   Cell(int X, int Y);
  105.   Cell(const Cell & ) = delete;
  106.   ~Cell();
  107. };
  108.  
  109. //cell.cpp
  110. Cell::Cell() {
  111.   this->crd.first = 0;
  112.   this->crd.second = 0;
  113.   this->button = new Fl_Button(0, 0, 0, 0, "");
  114.   this->button->color(FL_WHITE);
  115. }
  116.  
  117. void Cell::cell_pushed(Fl_Widget *widget, void *data);
  118.  
  119. /*void cell_pushed(Fl_Widget *widget, void *data) {
  120.   std::cout << "HEY NIGGA!\n";
  121. }*/
  122.  
  123.  
  124. int window_x_len = 600;
  125. int window_y_len = 400;
  126. int step_from_edge = 50;
  127. int button_size = 100;
  128.  
  129.  
  130. Cell::Cell (int X, int Y) {
  131.   this->crd.first = X;
  132.   this->crd.second = Y;
  133.   this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
  134.   this->button->color(FL_WHITE);
  135.   this->button->callback(cell_pushed, (void*)&(this->crd));
  136. }
  137.  
  138. Cell::~Cell() {
  139.   delete this->button;
  140. }
  141.  
  142. //visualboard.h
  143. struct VisualBoard{
  144.   std::vector <std::vector <Cell*>> playing_field;
  145.   VisualBoard();
  146.   void clean_board();
  147. };
  148.  
  149. VisualBoard our_visual_board;
  150.  
  151. //bool after_game_over_button_pushed;
  152. //int after_game_over_button_pushed_id;
  153.  
  154. bool game_is_over;
  155.  
  156. void start_game_cycle();
  157.  
  158. int cross_win_id = 10;//and put
  159. int zero_win_id = 20;//and put
  160. int draw_id = -1;
  161.  
  162. //main.cpp
  163. std::string message;
  164.  
  165. void game_over(int game_over_id) {
  166.   //after_game_over_button_pushed = false;
  167.   std::cout << "game over with id " << game_over_id << "\n";
  168.   /*
  169.   show_winner_button->redraw();
  170.   show_winner_button->show();
  171.   */
  172.   Fl_Window *window = new Fl_Window(200, 500, 300, 300 , "Show winner");
  173.   if (game_over_id == cross_win_id) {
  174.     message = "RED WINS\n";
  175.   } else if (game_over_id == zero_win_id) {
  176.     message = "BLUE WINS\n";
  177.   } else {
  178.     message = "DRAW\n";
  179.   }
  180.   Fl_Button *show_winner_button;
  181.   show_winner_button = new Fl_Button(10, 10, 50, 50, message.c_str());
  182.   window->show();
  183. }
  184.  
  185. //cell.cpp
  186. int cell_occupied_id = 0;
  187. int put_cross_id = 1;
  188. int put_zero_id = 2;
  189.  
  190. void Cell::cell_pushed(Fl_Widget *widget, void *data) {
  191.     std::pair <int, int> *xy = (std::pair <int, int>*)data;
  192.     std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\n";
  193.     int check_pos_id = our_board.check_pos((*xy).first, (*xy).second);
  194.     if (check_pos_id == cell_occupied_id) {
  195.       std::cout << "cell occupied\n";
  196.       return;
  197.     }
  198.     if (game_is_over) {
  199.       std::cout << "game_is_over\n";
  200.       return;
  201.     }
  202.     if (check_pos_id == put_cross_id) {
  203.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  204.       std::cout<<"put cross\n";
  205.       return;
  206.     } else if (check_pos_id == put_zero_id) {
  207.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  208.       std::cout<<"put zero\n";
  209.       return;
  210.     }
  211.     //game is over
  212.     int game_over_id = check_pos_id;
  213.     std::cout << "CASE GAME IS OVER\n";
  214.     std::cout << "game_over_id: " << game_over_id << "\n";
  215.    
  216.     if (game_over_id == draw_id) {
  217.       if (our_board.moved - 1 == 1) {
  218.         our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  219.       } else{
  220.         our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  221.       }
  222.     }
  223.     //cout << "game_o"
  224.     if (game_over_id == cross_win_id) {
  225.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  226.     } else if (game_over_id == zero_win_id) {
  227.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  228.     }
  229.     game_is_over = true;
  230.     game_over(game_over_id);
  231. }
  232.  
  233. //visualboard.cpp
  234. VisualBoard::VisualBoard() {
  235.   this->playing_field.resize(3);
  236.   for (int i = 0; i < 3; ++i) {
  237.     playing_field[i].resize(3);
  238.   }
  239.   for (int y = 0; y < 3; ++y) {
  240.     for (int x = 0; x < 3; ++ x) {
  241.       this->playing_field[y][x] = new Cell(x, y);
  242.     }
  243.   }
  244. }
  245.  
  246. void VisualBoard::clean_board() {
  247.   for (int i = 0; i < 3; ++i) {
  248.     for (int j = 0; j < 3; ++j) {
  249.       our_visual_board.playing_field[i][j]->button->color(FL_WHITE);
  250.       our_visual_board.playing_field[i][j]->button->redraw();
  251.     }
  252.   }
  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. Fl_Window *window;
  264.  
  265. //main.cpp
  266. void start_game_cycle() {
  267.   std::cout << "NEW GAME CYCLE STARTED\n";
  268.   game_is_over = false;
  269.   our_board.clean_board();
  270.   std::cout << "check board\n";
  271.   our_board.check_board();
  272.   our_visual_board.clean_board();
  273. }
  274.  
  275. void play_again_button_pushed(Fl_Widget *widget, void *data) {
  276.   //after_game_over_button_pushed = true;
  277.   //want_to_play = true;
  278.   std::cout << "play_again_button_pushed\n";
  279.   start_game_cycle();
  280. }
  281.  
  282. void close_program_button_pushed(Fl_Widget *widget, void *data) {
  283.   //after_game_over_button_pushed = true;
  284.   //want_to_play = false;
  285.   std::cout << "close_program_button_pushed\n";
  286.   exit(0);
  287. }
  288.  
  289.  
  290.  
  291. //InterfaceButtons.cpp
  292. InterfaceButtons::InterfaceButtons() {
  293.   this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
  294.   this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
  295.   this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
  296.   //this->button->callback(cell_pushed, (void*)&(this->crd));
  297.   this->play_again_button->callback(play_again_button_pushed);
  298.   this->close_program_button->callback(close_program_button_pushed);
  299. }
  300.  
  301. struct Game{
  302.   bool game_is_over = false;
  303.     Board our_board;
  304.     int cross_win_id = 10;//and put
  305.     int zero_win_id = 20;//and put
  306.     int draw_id = -1;
  307.     Game();
  308.     void game_over(int game_over_id);
  309.     void start_game_cycle();
  310.     Game(const Game & ) = delete;
  311.     ~Game();
  312. };
  313.  
  314.  
  315.  
  316. int main ()
  317. try
  318. {
  319.   //std::cout << "EVERYTHINS HAS FALLEN\n";
  320.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  321.   InterfaceButtons our_interface_buttons;
  322.   our_board = *new Board();
  323.   our_visual_board = *new VisualBoard();
  324.   start_game_cycle();
  325.   // window->end();
  326.   window->show();
  327.   /*Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  328.   InterfaceButtons our_interface_buttons;
  329.   our_board = *new Board();
  330.   our_visual_board = *new VisualBoard();
  331.   window->end();
  332.   window->show();*/
  333.   return Fl::run();
  334.   /*
  335.   This is how it will look at the end:
  336.   int window_x_len = 600;
  337.   int window_y_len = 400;
  338.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  339.   InterfaceButtons our_interface_buttons;
  340.   Game our_game;
  341.   our_game.start_game_cycle();
  342.   window->show();
  343.   //return Fl::run();
  344.   */
  345. }
  346. catch (std::exception& e)
  347. {
  348.   std::cerr << e.what() << std::endl;
  349.   return 1;
  350. }
  351. catch (...)
  352. {
  353.   std::cerr << "Oops, something went wrong..." << std::endl;
  354.   return 2;
  355. }
Add Comment
Please, Sign In to add comment