FLTK buttons

closed account (91q28vqX)
Howdy I am writing a program that acts as a 15 puzzle game. I am having trouble with the function that switches the labels of the buttons. I confused on how to access the vector that initializes the array in the function and changing it to fit my needs .

#include <iostream>
#include <FL/Fl.H>
#include "Window.h"
#include <FL/Fl_Button.H>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void button_cb( Fl_Widget*, void* );

void make_window()
{
Fl_Window* win= new Fl_Window( 500,400, "Buttons everywhere" );
win->begin();
const int arraySize = 16;


const char * v[]={"1","2","3","4","5","6","7","8" ,"9","10","11","12" ,"13","14","15"," " };

int spaceposition=15; //original position of the empty name of v


int pointsx[arraySize]={0,40,80,120,0,40,80,120,0,40,80,120,0,40,80,120};
int pointsy[arraySize]={0,0,0,0,40,40,40,40,80,80,80,80,120,120,120,120};
/



for (int j = 0; j < arraySize; j++){

Fl_Button* but = new Fl_Button( pointsx[j], pointsy[j], 40, 40, v[j] );

but -> callback( ( Fl_Callback* ) button_cb );

}
win->end();

win->show();
}

void button_cb( Fl_Widget* obj , void* )
{
//switch label with the label empty label button
//int spaceposition=15; original position of empty label.

std::vector<int> myvec(v, v + sizeof(v) / sizeof(v[0]) );
int pos = std::find(myvec.begin(), myvec.end(), obj->label) - myvec.begin();
if (pos=spaceposition4
||pos=spaceposition+4||pos=spacepositon+1||pos=spaceposition-1)

//code that switches labels or positions of the buttons curisponding //to value


obj->redraw();

}

int main() //int argc, char* argv[] )
{
make_window();
return Fl::run();
}

-------------------------------------------------------------------------------
Bellow is the code im having trouble with.
Im getting erros such as vector v is not defined in the scope. and
vector is not defined in std.


void button_cb( Fl_Widget* obj , void* )
{
//switch label with the label empty label button
//int spaceposition=15; original position of empty label.

std::vector<int> myvec(v, v + sizeof(v) / sizeof(v[0]) );
int pos = std::find(myvec.begin(), myvec.end(), obj->label) - myvec.begin();
//code above should give me the position of the label in my original v vector.

if (pos=spaceposition4
||pos=spaceposition+4||pos=spacepositon+1||pos=spaceposition-1)

//code that switches labels or positions of the buttons corresponding // values and ubdates the vector position of the space.


obj->redraw();

}
Overall I am asking how does one access each object and switch their labels while updating the original vector v with the condition that the object is 4 or one spaces away from the empty space " ".
but -> callback( ( Fl_Callback* ) button_cb );
Why do you call this function ?
Normally this callback function is called from the button when it is clicked.

No this is actually correct.

std::vector<int> myvec(v, v + sizeof(v) / sizeof(v[0]) );
v is defined in make_window() so you can't access it outside the function. If you need it outside you can declare it global - global constants are ok.

1
2
if (pos=spaceposition4
||pos=spaceposition+4||pos=spacepositon+1||pos=spaceposition-1)

= is the assignment operator, do you mean == ?

It seems you want to find the button in the vector, but why when you have it already?
void button_cb( Fl_Widget* obj , void* )
obj is the button that was clicked.
Last edited on
Topic archived. No new replies allowed.