Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <stdexcept>
- #include <string>
- #include <vector>
- #include <FL/Fl.H>
- #include <FL/Fl_Window.H>
- #include <FL/Fl_Button.H>
- #include <Graph_lib/Graph.h>
- #include <Graph_lib/Simple_window.h>
- //#include "board.h"
- using namespace Graph_lib;
- //Board.h
- struct Board{
- std::vector <std::vector <int>> board;
- int moved;
- int moves;
- Board();
- int check_win();
- int check_pos(int x, int y);
- void check_board();
- void clean_board();
- };
- Board our_board;
- //Board.cpp
- Board::Board() {
- this->board.assign(3, std::vector <int> (3, 0));
- this->moved = 1;
- this->moves = 0;
- }
- int Board::check_win()
- {
- int winner=0;
- for (int i=0;i<3;i++)
- {
- if (board[0][i]==board[1][i] and board[1][i]==board[2][i] and board[0][i]!=0)
- winner = board[0][i];
- if (board[i][0]==board[i][1] and board[i][1]==board[i][2] and board[i][0]!=0)
- winner = board[i][0];
- }
- if (board[0][0]==board[1][1] and board[1][1]==board[2][2] and board[0][0]!=0)
- winner = board[0][0];
- if (board[2][0]==board[1][1] and board[1][1]==board[0][2] and board[2][0]!=0)
- winner = board[2][0];
- return winner;
- }
- void Board::check_board()
- {
- for (int i=0;i<3;i++)
- {
- for (int i1:board[i])
- {
- std::cout<<i1<<" ";
- }
- std::cout<<"\n";
- }
- std::cout<<"\n";
- }
- int Board::check_pos(int x, int y)
- {
- if (board[y][x]==0)
- {
- moves++;
- board[y][x]=moved;
- moved=1+moved%2;
- int win = check_win();
- //checkboard();
- if (win!=0)
- return win*10;
- if (win==0 and moves==9)
- return -1;
- return board[y][x];
- }
- else
- return 0;
- }
- void Board::clean_board() {
- this->board.assign(3, std::vector <int> (3, 0));
- this->moved = 1;
- this->moves = 0;
- }
- //still works
- //delete
- //перед тем, как удалить значение - надо удалить указатели на этот объект
- struct Game{
- bool game_is_over = false;
- Board our_board;
- VisualBoard our_visual_board;
- int cross_win_id = 10;//and put
- int zero_win_id = 20;//and put
- int draw_id = -1;
- Game();
- void game_over(int game_over_id);
- void start_game_cycle();
- static void play_again_button_pushed(Fl_Widget *widget, void *data);
- static void close_program_button_pushed(Fl_Widget *widget, void *data);
- Fl_Button *menu_button;
- Fl_Button *play_again_button;
- Fl_Button *close_program_button;
- Game(const Game & ) = delete;
- ~Game();
- };
- //cell.h
- struct Cell{
- std::pair <int, int> crd;
- Fl_Button *button;
- static void cell_pushed(Fl_Widget *widget, void *data);
- Game *pointer_to_our_game;
- Cell(int X, int Y, Game *pointer_to_our_game);
- Cell(const Cell & ) = delete;
- ~Cell();
- };
- //void Cell::cell_pushed(Fl_Widget *widget, void *data);
- /*void cell_pushed(Fl_Widget *widget, void *data) {
- std::cout << "HEY NIGGA!\n";
- }*/
- int window_x_len = 600;
- int window_y_len = 400;
- int step_from_edge = 50;
- int button_size = 100;
- Cell::Cell (int X, int Y, Game *pointer_to_our_game) {
- this->crd.first = X;
- this->crd.second = Y;
- this->button = new Fl_Button(step_from_edge + button_size * X, step_from_edge + button_size * Y, button_size, button_size, "");
- this->button->color(FL_WHITE);
- this->button->callback(this->cell_pushed, this);//(void*)&(this->crd)
- this->pointer_to_our_game = pointer_to_our_game;
- }
- Cell::~Cell() {
- delete this->button;
- }
- //visualboard.h
- struct VisualBoard{
- std::vector <std::vector <Cell*>> playing_field;
- VisualBoard();
- void clean_board();
- };
- VisualBoard our_visual_board;
- //bool after_game_over_button_pushed;
- //int after_game_over_button_pushed_id;
- bool game_is_over;
- int cross_win_id = 10;//and put
- int zero_win_id = 20;//and put
- int draw_id = -1;
- //main.cpp
- std::string message;
- void Game::game_over(int game_over_id) {
- //after_game_over_button_pushed = false;
- std::cout << "game over with id " << game_over_id << "\n";
- /*
- show_winner_button->redraw();
- show_winner_button->show();
- */
- Fl_Window *window = new Fl_Window(200, 500, 300, 300 , "Show winner");
- if (game_over_id == cross_win_id) {
- message = "RED WINS\n";
- } else if (game_over_id == zero_win_id) {
- message = "BLUE WINS\n";
- } else {
- message = "DRAW\n";
- }
- Fl_Button *show_winner_button;
- show_winner_button = new Fl_Button(10, 10, 50, 50, message.c_str());
- window->show();
- }
- //cell.cpp
- int cell_occupied_id = 0;
- int put_cross_id = 1;
- int put_zero_id = 2;
- void Cell::cell_pushed(Fl_Widget *widget, void *data) {
- //data = this
- //std::pair <int, int> *xy = (std::pair <int, int>*)data;
- auto pointer_to_cell = static_cast<Cell*>(data);
- auto xy = pointer_to_cell->crd;
- std::cout << "Pushed: " << xy.first << ' ' << xy.second << "\n";
- int check_pos_id = our_board.check_pos(xy.first, xy.second);
- if (check_pos_id == cell_occupied_id) {
- std::cout << "cell occupied\n";
- return;
- }
- if (game_is_over) {
- std::cout << "game_is_over\n";
- return;
- }
- if (check_pos_id == put_cross_id) {
- our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_RED);
- std::cout<<"put cross\n";
- return;
- } else if (check_pos_id == put_zero_id) {
- our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_BLUE);
- std::cout<<"put zero\n";
- return;
- }
- //game is over
- int game_over_id = check_pos_id;
- std::cout << "CASE GAME IS OVER\n";
- std::cout << "game_over_id: " << game_over_id << "\n";
- if (game_over_id == draw_id) {
- if (our_board.moved - 1 == 1) {
- our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_RED);
- } else{
- our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_BLUE);
- }
- }
- //cout << "game_o"
- if (game_over_id == cross_win_id) {
- our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_RED);
- } else if (game_over_id == zero_win_id) {
- our_visual_board.playing_field[xy.second][xy.first]->button->color(FL_BLUE);
- }
- game_is_over = true;
- pointer_to_cell->pointer_to_our_game->game_over(game_over_id);
- }
- //visualboard.cpp
- VisualBoard::VisualBoard() {
- this->playing_field.resize(3);
- for (int i = 0; i < 3; ++i) {
- playing_field[i].resize(3);
- }
- for (int y = 0; y < 3; ++y) {
- for (int x = 0; x < 3; ++ x) {
- this->playing_field[y][x] = new Cell(x, y);
- }
- }
- }
- void VisualBoard::clean_board() {
- for (int i = 0; i < 3; ++i) {
- for (int j = 0; j < 3; ++j) {
- our_visual_board.playing_field[i][j]->button->color(FL_WHITE);
- our_visual_board.playing_field[i][j]->button->redraw();
- }
- }
- }
- //InterfaceButtons.h
- struct InterfaceButtons {
- Fl_Button *menu_button;
- Fl_Button *play_again_button;
- Fl_Button *close_program_button;
- InterfaceButtons();
- };
- Fl_Window *window;
- //main.cpp
- void Game::start_game_cycle() {
- std::cout << "NEW GAME CYCLE STARTED\n";
- game_is_over = false;
- our_board.clean_board();
- std::cout << "check board\n";
- our_board.check_board();
- our_visual_board.clean_board();
- }
- void Game::play_again_button_pushed(Fl_Widget *widget, void *data) {
- //after_game_over_button_pushed = true;
- //want_to_play = true;
- auto pointer_to_our_game = static_cast<Game*>(data);
- std::cout << "play_again_button_pushed\n";
- pointer_to_our_game->start_game_cycle();
- }
- void Game::close_program_button_pushed(Fl_Widget *widget, void *data) {
- //after_game_over_button_pushed = true;
- //want_to_play = false;
- std::cout << "close_program_button_pushed\n";
- exit(0);
- }
- Game::Game() {
- this->our_board;
- this->our_visual_board;
- this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
- int p = 3;
- this->play_again_button->callback(play_again_button_pushed);
- this->menu_button = new Fl_Button(400, 50, 150, 50, "MENU");
- this->play_again_button = new Fl_Button(400, 150, 150, 50, "PLAY AGAIN");
- this->close_program_button = new Fl_Button(400, 250, 150, 50, "EXIT");
- //this->button->callback(cell_pushed, (void*)&(this->crd));
- this->play_again_button->callback(this->play_again_button_pushed, this);
- this->close_program_button->callback(this->close_program_button_pushed, this);
- }
- int main ()
- try
- {
- //std::cout << "EVERYTHINS HAS FALLEN\n";
- Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
- InterfaceButtons our_interface_buttons;
- our_board = *new Board();
- our_visual_board = *new VisualBoard();
- start_game_cycle();
- // window->end();
- window->show();
- /*Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
- InterfaceButtons our_interface_buttons;
- our_board = *new Board();
- our_visual_board = *new VisualBoard();
- window->end();
- window->show();*/
- return Fl::run();
- /*
- This is how it will look at the end:
- int window_x_len = 600;
- int window_y_len = 400;
- Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
- InterfaceButtons our_interface_buttons;
- Game our_game;
- our_game.start_game_cycle();
- window->show();
- //return Fl::run();
- */
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- return 1;
- }
- catch (...)
- {
- std::cerr << "Oops, something went wrong..." << std::endl;
- return 2;
- }
Add Comment
Please, Sign In to add comment