Advertisement
Korotkodul

tic_tac_toe/main.cpp_5_завис

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