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));
- }
- //still works
- //cell.h
- struct Cell{
- std::pair <int, int> crd;
- Fl_Button *button;
- Cell();
- Cell(int X, int Y);
- Cell(const Cell & ) = delete;
- ~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);
- }
- void 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) {
- 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
- bool want_to_play = true;
- bool after_game_over_button_pushed;
- int after_game_over_button_pushed_id;
- bool game_is_over;
- //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;
- want_to_play = false;
- return;
- }
- //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::cout << "func cell_pushed\n";
- 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 (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
- 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_is_over = true;
- 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) {
- this->playing_field[y][x] = new Cell(x, y);
- }
- }
- }
- int main ()
- try
- {
- std::cout << "EVERYTHINS HAS FALLEN\n";
- }
- 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