Advertisement
Korotkodul

tic_tac_toe/main.cpp_6_завис

Nov 27th, 2024
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.20 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 & ) = delete;
  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) {//(50, 50, 300, 300, "RED WINS")
  151.     show_winner_button = new Fl_Button(50, 50, 50, 50, "RED WINS");
  152.     //show_winner_string = "RED WINS";
  153.   } else if  (game_over_id == zero_win_id){
  154.     show_winner_button = new Fl_Button(50, 50, 50, 50, "BLUE WINS");
  155.     //show_winner_string = "BLUE WINS";
  156.   } else {
  157.     show_winner_button = new Fl_Button(50, 50, 50, 50, "DRAW");
  158.   }
  159. }*/
  160.  
  161. bool want_to_play = true;
  162.  
  163. bool after_game_over_button_pushed;
  164. int after_game_over_button_pushed_id;
  165.  
  166. bool game_is_over;
  167.  
  168. //main.cpp
  169. void game_over(int game_over_id) {
  170.   after_game_over_button_pushed = false;
  171.   std::cout << "game over with id " << game_over_id << "\n";
  172.   std::cout << "now we will show winners\n";
  173.   //show_winner(game_over_id);
  174.   /*while (!after_game_over_button_pushed) {
  175.     //do nothing
  176.   } */
  177.   //want_to_play = (bool)after_game_over_button_pushed_id;
  178.   want_to_play = false;
  179.   return;
  180. }
  181.  
  182. //cell.cpp
  183. int cell_occupied_id = 0;
  184. int draw_id = -1;
  185. int put_cross_id = 1;
  186. int put_zero_id = 2;
  187.  
  188. void cell_pushed(Fl_Widget *widget, void *data) {
  189.     std::pair <int, int> *xy = (std::pair <int, int>*)data;
  190.     std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\n";
  191.    
  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.     if (our_board.moved - 1 == 1) {
  212.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  213.     } else{
  214.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  215.     }
  216.     //cout << "game_o"
  217.     int game_over_id = check_pos_id;
  218.     if (game_over_id == cross_win_id) {
  219.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  220.     } else {
  221.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  222.     }
  223.     game_over(game_over_id);
  224.     game_is_over = true;
  225. }
  226.  
  227. //visualboard.cpp
  228. VisualBoard::VisualBoard() {
  229.   this->playing_field.resize(3);
  230.   for (int i = 0; i < 3; ++i) {
  231.     playing_field[i].resize(3);
  232.   }
  233.   clean_board();
  234. }
  235.  
  236. void VisualBoard::clean_board() {
  237.   for (int y = 0; y < 3; ++y) {
  238.     for (int x = 0; x < 3; ++ x) {
  239.       playing_field[y][x] = new Cell(x, y);
  240.     }
  241.   }
  242. }
  243.  
  244. //main.cpp
  245. void start_game_cycle() {
  246.   game_is_over = false;
  247.   our_board.clean_board();
  248.   our_visual_board.clean_board();
  249.   while (!game_is_over) {
  250.     std::cout << "we are playing\n";
  251.   }
  252.   std::cout << "now game is over\n";
  253. }
  254.  
  255. void play_again_button_pushed(Fl_Widget *widget, void *data) {
  256.   after_game_over_button_pushed = true;
  257.   want_to_play = true;
  258.   std::cout << "play_again_button_pushed\n";
  259.   start_game_cycle();
  260. }
  261.  
  262. void close_program_button_pushed(Fl_Widget *widget, void *data) {
  263.   after_game_over_button_pushed = true;
  264.   want_to_play = false;
  265.   std::cout << "close_program_button_pushed\n";
  266.   exit(0);
  267. }
  268.  
  269. //InterfaceButtons.h
  270. struct InterfaceButtons {
  271.   Fl_Button *menu_button;
  272.   Fl_Button *play_again_button;
  273.   Fl_Button *close_program_button;
  274.   InterfaceButtons();
  275. };
  276.  
  277. //InterfaceButtons.cpp
  278. InterfaceButtons::InterfaceButtons() {
  279.   this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
  280.   this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
  281.   this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
  282.   //this->button->callback(cell_pushed, (void*)&(this->crd));
  283.   this->play_again_button->callback(play_again_button_pushed);
  284.   this->close_program_button->callback(close_program_button_pushed);
  285. }
  286.  
  287.  
  288.  
  289. int main ()
  290. try
  291. {
  292.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  293.   InterfaceButtons our_interface_buttons;
  294.   /*our_board.clean_board();
  295.   our_visual_board.clean_board();*/
  296.   //start_game_cycle();
  297.   window->end();
  298.   window->show();
  299.   return Fl::run();
  300.   /*while (want_to_play) {
  301.     start_game_cycle();
  302.   }*/
  303.   //exit(0);
  304.   /*window->end();
  305.   window->show();
  306.   return Fl::run();*/
  307.   /*playing_field.resize(3);
  308.   for (int i = 0; i < 3; ++i) {
  309.     playing_field[i].resize(3);
  310.   }
  311.   for (int y = 0; y < 3; ++y) {
  312.     for (int x = 0; x < 3; ++x) {
  313.       // Cell new_Cell(x, y);
  314.       // Cell new_Cell = new Cell(x, y);
  315.       playing_field[y][x] = new Cell(x, y);
  316.     }
  317.   }*/
  318.   /*Board board;
  319.   board.check_pos(0, 2);
  320.   board.check_pos(2, 0);
  321.   board.check_board();
  322.   board.clean_board();
  323.   board.check_board();*/
  324. }
  325. catch (std::exception& e)
  326. {
  327.   std::cerr << e.what() << std::endl;
  328.   return 1;
  329. }
  330. catch (...)
  331. {
  332.   std::cerr << "Oops, something went wrong..." << std::endl;
  333.   return 2;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement