Advertisement
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>
- 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));
- }
- //cell.h
- struct Cell{
- std::pair <int, int> crd;
- Fl_Button *button;
- Cell();
- Cell(int X, int Y);
- Cell(const Cell & );
- ~Cell();
- };
- //cell.cpp
- Cell::Cell() {
- this->crd.first = 0;
- this->crd.second = 0;
- this->button = new Fl_Button(0, 0, 0, 0, "");
- this->button->color(FL_WHITE);
- }
- Cell::Cell(const Cell & ) = delete;
- void cell_pushed(Fl_Widget *widget, void *data);
- 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) {
- 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(cell_pushed, (void*)&(this->crd));
- }
- Cell::~Cell() {
- delete this->button;
- }
- //visualboard.h
- struct VisualBoard{
- std::vector <std::vector <Cell*>> playing_field;
- VisualBoard();
- void clean_board();
- };
- VisualBoard our_visual_board;
- int cross_win_id = 10;//and put
- int zero_win_id = 20;//and put
- //InterfaceButtons.cpp
- void show_winner(int game_over_id) {
- Fl_Button *show_winner_button;
- //std::string show_winner_string;
- if (game_over_id == cross_win_id) {
- show_winner_button = new Fl_Button(50, 50, 300, 300, "RED WINS");
- //show_winner_string = "RED WINS";
- } else {
- show_winner_button = new Fl_Button(50, 50, 300, 300, "BLUE WINS");
- //show_winner_string = "BLUE WINS";
- }
- }
- bool want_to_play = true;
- bool after_game_over_button_pushed;
- int after_game_over_button_pushed_id;
- //main.cpp
- void game_over(int game_over_id) {
- after_game_over_button_pushed = false;
- std::cout << "game over with id " << game_over_id << "\n";
- std::cout << "now we will show winners\n";
- show_winner(game_over_id);
- while (!after_game_over_button_pushed) {
- //do nothing
- }
- want_to_play = (bool)after_game_over_button_pushed_id;
- }
- //cell.cpp
- int cell_occupied_id = 0;
- int draw_id = -1;
- int put_cross_id = 1;
- int put_zero_id = 2;
- void cell_pushed(Fl_Widget *widget, void *data) {
- std::pair <int, int> *xy = (std::pair <int, int>*)data;
- 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 (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
- 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"
- int game_over_id = check_pos_id;
- if (game_over_id == cross_win_id) {
- 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);
- }
- 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);
- }
- clean_board();
- }
- void VisualBoard::clean_board() {
- for (int y = 0; y < 3; ++y) {
- for (int x = 0; x < 3; ++ x) {
- playing_field[y][x] = new Cell(x, y);
- }
- }
- }
- //main.cpp
- void start_game_cycle() {
- our_board.clean_board();
- our_visual_board.clean_board();
- }
- void play_again_button_pushed(Fl_Widget *widget, void *data) {
- after_game_over_button_pushed = true;
- want_to_play = true;
- std::cout << "play_again_button_pushed\n";
- }
- void 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";
- }
- //InterfaceButtons.h
- struct InterfaceButtons {
- Fl_Button *menu_button;
- Fl_Button *play_again_button;
- Fl_Button *close_program_button;
- InterfaceButtons();
- };
- //InterfaceButtons.cpp
- InterfaceButtons::InterfaceButtons() {
- 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(play_again_button_pushed);
- this->close_program_button->callback(close_program_button_pushed);
- }
- int main ()
- try
- {
- Fl_Window *window = new Fl_Window(100, 400, window_x_len, window_y_len , "Window");
- InterfaceButtons our_interface_buttons;
- /*our_board.clean_board();
- our_visual_board.clean_board();*/
- start_game_cycle();
- window->end();
- window->show();
- return Fl::run();
- /*while (want_to_play) {
- start_game_cycle();
- }*/
- //exit(0);
- /*window->end();
- window->show();
- return Fl::run();*/
- /*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) {
- // Cell new_Cell(x, y);
- // Cell new_Cell = new Cell(x, y);
- playing_field[y][x] = new Cell(x, y);
- }
- }*/
- /*Board board;
- board.check_pos(0, 2);
- board.check_pos(2, 0);
- board.check_board();
- board.clean_board();
- board.check_board();*/
- }
- catch (std::exception& e)
- {
- std::cerr << e.what() << std::endl;
- return 1;
- }
- catch (...)
- {
- std::cerr << "Oops, something went wrong..." << std::endl;
- return 2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement