Advertisement
Korotkodul

tic_tac_toe/main.cpp_4

Nov 27th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.11 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. //InterfaceButtons.cpp
  144. void show_winner(int game_over_id) {
  145.  
  146. }
  147.  
  148. bool want_to_play = true;
  149.  
  150. bool after_game_over_button_pushed;
  151. int after_game_over_button_pushed_id;
  152.  
  153. //main.cpp
  154. void game_over(int game_over_id) {
  155.   after_game_over_button_pushed = false;
  156.   show_winner(game_over_id);
  157.   std::cout << "game over with id " << game_over_id << "\n";
  158.   //while (!after_game_over_button_pushed) {
  159.     //do nothing
  160.   //}
  161.   //want_to_play = (bool)after_game_over_button_pushed_id;
  162. }
  163.  
  164. //cell.cpp
  165. int cell_occupied_id = 0;
  166. int draw_id = -1;
  167. int put_cross_id = 1;
  168. int put_zero_id = 2;
  169. int cross_win_id = 10;//and put
  170. int zero_win_id = 20;//and put
  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.    
  175.     int check_pos_id = our_board.check_pos((*xy).first, (*xy).second);
  176.     if (check_pos_id == cell_occupied_id) {
  177.       std::cout << "cell occupied\n";
  178.       return;
  179.     }
  180.     if (check_pos_id == put_cross_id) {
  181.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  182.       std::cout<<"put cross\n";
  183.       return;
  184.     } else if (check_pos_id == put_zero_id) {
  185.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  186.       std::cout<<"put zero\n";
  187.       return;
  188.     }
  189.     //game is over
  190.     int game_over_id = check_pos_id;
  191.     if (game_over_id == cross_win_id) {
  192.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_RED);
  193.     } else {
  194.       our_visual_board.playing_field[(*xy).second][(*xy).first]->button->color(FL_BLUE);
  195.     }
  196.     game_over(game_over_id);
  197. }
  198.  
  199. //visualboard.cpp
  200. VisualBoard::VisualBoard() {
  201.   this->playing_field.resize(3);
  202.   for (int i = 0; i < 3; ++i) {
  203.     playing_field[i].resize(3);
  204.   }
  205.   clean_board();
  206. }
  207.  
  208. void VisualBoard::clean_board() {
  209.   for (int y = 0; y < 3; ++y) {
  210.     for (int x = 0; x < 3; ++ x) {
  211.       playing_field[y][x] = new Cell(x, y);
  212.     }
  213.   }
  214. }
  215.  
  216. //main.cpp
  217. void start_game_cycle() {
  218.   after_game_over_button_pushed = false;
  219.   our_board.clean_board();
  220.   our_visual_board.clean_board();
  221. }
  222.  
  223. void play_again_button_pushed(Fl_Widget *widget, void *data) {
  224.   after_game_over_button_pushed = true;
  225.   want_to_play = true;
  226.   std::cout << "play_again_button_pushed\n";
  227. }
  228.  
  229. void close_program_button_pushed(Fl_Widget *widget, void *data) {
  230.   after_game_over_button_pushed = true;
  231.   want_to_play = false;
  232.   std::cout << "close_program_button_pushed\n";
  233. }
  234.  
  235. //InterfaceButtons.h
  236. struct InterfaceButtons {
  237.   Fl_Button *menu_button;
  238.   Fl_Button *play_again_button;
  239.   Fl_Button *close_program_button;
  240.   InterfaceButtons();
  241. };
  242.  
  243. //InterfaceButtons.cpp
  244. InterfaceButtons::InterfaceButtons() {
  245.   this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
  246.   this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
  247.   this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
  248.   //this->button->callback(cell_pushed, (void*)&(this->crd));
  249.   this->play_again_button->callback(play_again_button_pushed);
  250.   this->close_program_button->callback(close_program_button_pushed);
  251. }
  252.  
  253.  
  254.  
  255. int main ()
  256. try
  257. {
  258.   Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
  259.   InterfaceButtons our_interface_buttons;
  260.   /*our_board.clean_board();
  261.   our_visual_board.clean_board();*/
  262.   start_game_cycle();
  263.   window->end();
  264.   window->show();
  265.   return Fl::run();
  266.   /*while (want_to_play) {
  267.     start_game_cycle();
  268.   }*/
  269.   //exit(0);
  270.   /*window->end();
  271.   window->show();
  272.   return Fl::run();*/
  273.   /*playing_field.resize(3);
  274.   for (int i = 0; i < 3; ++i) {
  275.     playing_field[i].resize(3);
  276.   }
  277.   for (int y = 0; y < 3; ++y) {
  278.     for (int x = 0; x < 3; ++x) {
  279.       // Cell new_Cell(x, y);
  280.       // Cell new_Cell = new Cell(x, y);
  281.       playing_field[y][x] = new Cell(x, y);
  282.     }
  283.   }*/
  284.   /*Board board;
  285.   board.check_pos(0, 2);
  286.   board.check_pos(2, 0);
  287.   board.check_board();
  288.   board.clean_board();
  289.   board.check_board();*/
  290. }
  291. catch (std::exception& e)
  292. {
  293.   std::cerr << e.what() << std::endl;
  294.   return 1;
  295. }
  296. catch (...)
  297. {
  298.   std::cerr << "Oops, something went wrong..." << std::endl;
  299.   return 2;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement