Korotkodul

Game_new/main.cpp_5

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