1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include "GUI.h"
#include "fltk.h"
#include "std_lib_facilities.h"
#include "Graph.h"
#include <stdlib.h>
using namespace Graph_lib;
using namespace std;
//Create a new kind of button
class Button2 : public Button {
public:
Address p;
Point pt;
Button2(Point xy, int ww, int hh, const string& s, Callback cb)
:Button(xy,ww,hh,s,cb),pt(xy.x,xy.y){}
virtual void attach(Window& win)
{
pw = new Fl_Button(loc.x, loc.y, width, height, label.c_str());
pw->callback(reinterpret_cast<Fl_Callback*>(do_it), this); // pass the window
own = &win;
p = &win;
}
};
struct Checker
{
bool WC;
bool active;
Point p;
Circle inner;
Circle outer;
Checker(bool w_c, Point pt):WC(w_c),p(pt),inner(p,25),outer(p,48),active(true){}
};
//Class Main_window is a new kind of window where the game takes place
struct Main_window: Graph_lib::Window {
Main_window(Point p, int w, int h, const string& title);
Vector_ref<Rectangle> table;
Vector_ref<Checker> checkers;
Vector_ref< Vector_ref<Button2> > buttons;
Vector_ref<Player> players;
//Buttons used in the game
private:
Text* instruction;
Button quit_button;
Button start_button;
Button help_button;
Out_box move_box; //displays number of moves
static void cb_quit(Address,Address pw);
static void cb_click(Address,Address pw);
static void cb_start(Address, Address pw);
static void cb_help(Address, Address pw);
static void refresh(bool endgame);
void click(Address active_button);
void quit();
void start();
void help();
};
int moves=0;
bool gameover=false;
//Initialize the main window
Main_window::Main_window(Point p, int w, int h, const string& title):Window(p,w,h,title),
quit_button(Point(x_max()-80,y_max()/2),70,20,"Quit",cb_quit),help_button(Point(x_max()-80,y_max()/2-60),70,20,"Help",cb_help),start_button(Point(x_max()-80,y_max()/2 -30),70,20,"Start",cb_start),
move_box(Point(600,50),70,20,"Number of Moves")
{
instruction = new Text(Point(20,550),"Instruction: You may move one square diagonally in any direction.");
for (int i=0;i<8;i++)
{
for (int j=0;j<8;j++)
{
table.push_back(new Rectangle(Point(j*100,i*100),100,100));
buttons[i].push_back(new Button2(Point(j*100,i*100),100,100,"",Main_window::cb_click));
if ((j-i)%2==0)
{
table[table.size()-1].set_color(Color::white);
table[table.size()-1].set_fill_color(Color::white);
}
else
{
table[table.size()-1].set_color(Color::black);
table[table.size()-1].set_fill_color(Color::black);
}
attach(table[table.size()-1]);
attach(buttons[i][j]);
}
}
attach(quit_button);
attach(start_button);
attach(help_button);
attach(move_box);
}
void Main_window::start()
{
refresh(gameover);
}
void Main_window::cb_quit(Address, Address pw)
{
reference_to<Main_window>(pw).quit();
}
void Main_window::cb_start(Address, Address pw)
{
reference_to<Main_window>(pw).start();
}
void Main_window::cb_help(Address, Address pw)
{
reference_to<Main_window>(pw).help();
}
void Main_window::cb_click(Address active_button, Address pw)
{
reference_to<Button2>(pw).hide();
reference_to<Main_window>(reference_to<Button2>(pw).p).click(pw);
}
void Main_window::click(Address pw)
{
int index=-1;
int ind_x;
int ind_y;
for (int i=0; i<checkers.size(); i++)
if(&checkers[i].WC == false && checkers[i].active==true)
index=i;
for (int i=0; i<8; i++)
{
for (int j=0; j<8; j++)
{
if (&buttons[i][j]==pw)
{
ind_y=50+(i*100);
ind_x=50+(j*100);
}
}
}
if(index>=0)
{
detach(checkers[index].inner);
detach(checkers[index].outer);
}
checkers.push_back(new Checker(false,Point(ind_x,ind_y)));
checkers[checkers.size()-1].inner.set_style(Line_style(Line_style::solid,50));
checkers[checkers.size()-1].inner.set_color(Color::white);
checkers[checkers.size()-1].outer.set_style(Line_style(Line_style::solid,5));
attach(checkers[checkers.size()-1].inner);
attach(checkers[checkers.size()-1].outer);
move_box.put(moves);
moves++;
}
void Main_window::help()
{
attach(*instruction);
}
void Main_window::quit()
{
hide();
}
void Main_window::refresh(bool endgame)
{
if (endgame==1)
exit(0);
else
{
Fl::wait();
Fl::redraw();
refresh(gameover);
}
}
|