Korotkodul

tic_tac_toe/main.cpp_12

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