Korotkodul

Game_new/main.cpp

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