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.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 & ) = delete;
- 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 button_callback(Fl_Widget *widget, void *data) {
- //std::string *label = (std::string*)data;
- //std::cout << "Pushed: " << *label << std::endl;
- //std::cout << "CB " << data << std::endl;
- std::pair <int, int> *xy = (std::pair <int, int>*)data;
- std::cout << "Pushed: " << (*xy).first << ' ' << (*xy).second << "\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(button_callback, (void*)&(this->crd));
- }
- /*struct Cell {
- Cell (int X, int Y) {
- //this->x = X;
- //this->y = Y;
- //this->state = "free";
- 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(button_callback, (void*)&(this->crd));
- //std::cout << "Cell " << X << Y << std::endl;
- }
- ~Cell() {
- delete this->button;
- }
- };*/
- std::vector <std::vector <Cell*>> playing_field;
- int main ()
- try
- {
- /*Fl_Window *window = new Fl_Window(100,400,window_x_len, window_y_len , "Window");
- 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();*/
- //window->end();
- //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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement