8 Replies - 3845 Views - Last Post: 08 April 2009 - 07:47 PM Rate Topic: -----

#1 Beinss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 09

pick up sticks : game

Posted 08 April 2009 - 10:39 AM

thank you

This post has been edited by Beinss: 08 April 2009 - 01:54 PM

Is This A Good Question/Topic? 0
  • +

Replies To: pick up sticks : game

#2 Beinss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 09

Re: pick up sticks : game

Posted 08 April 2009 - 10:48 AM

The entire code is...

#include "fltk.h"
#include "GUI.h"
#include <time.h>
#include <iostream>
#include <iomanip>

namespace Graph_lib { //additions to graphics library posted on http://www.stroustrup.com/Programming/Graphics/
	bool line_segment_intersect(Point p1, Point p2, Point p3, Point p4, Point& intersection);

	string In_box::get_string()	//added by RGW 2/23/09
	{
		Fl_Input& pi = reference_to<Fl_Input>(pw);
		return string(pi.value());
	}
}
using namespace Graph_lib;
//------------------------------------------------------------------------------
const int		NUM_TOP_PLAYERS		=5;		//Keep history of 5 best scores
const int		MAX_STICKS			=14;		//Game has this many sticks
const int		STICK_HEIGHT		=5;		//Line width
const int		STICK_WIDTH			=300;		//Line length
const int		WINDOW_W			=800;		//Game Window size
const int		WINDOW_H			=600;
const int		SELECT_TOLERANCE	=2;		//Neighborhood size (in pixels) for stick selection
const int		GAME_TIME			=30;		//game time in seconds
const int		STICK_SCORE			=5;		//base score for a stick with no intersections
const double	PI					=3.14159265358979;

//------------------------------------------------------------------------------
void play_swosh(void);			//Helper functions to use Audiere library to play a sound
void play_crash(void);
void play_toobad(void);
void play_applause(void);

//------------------------------------------------------------------------------
bool game_is_over = false;		//boolean used to monitor stick selection time
bool invalid_stick;
unsigned long game_start_time;
unsigned long game_time = 0;	//current game time in seconds
static int static_color = 0;

//------------------------------------------------------------------------------
vector<Color::Color_type> color_list;

void init_color_list() {
	color_list.push_back(Color::red);
	color_list.push_back(Color::blue);
	color_list.push_back(Color::green);
	color_list.push_back(Color::yellow);
	color_list.push_back(Color::white);
	color_list.push_back(Color::black);
	color_list.push_back(Color::magenta);
	color_list.push_back(Color::cyan);
	color_list.push_back(Color::dark_red);
	color_list.push_back(Color::dark_green);
	color_list.push_back(Color::dark_yellow);
	color_list.push_back(Color::dark_blue);
	color_list.push_back(Color::dark_magenta);
	color_list.push_back(Color::dark_cyan);
}
//------------------------------------------------------------------------------
struct top_player {
	string name;
	int score;
};

//------------------------------------------------------------------------------
//function in_bounds(p1,p2) verifies a stick at p1,p2 is in the Game Window
bool in_bounds(Point p1, Point p2) {
	Point intrs;
	if( line_segment_intersect(p1, p2, Point(0,0), Point(WINDOW_W,0),intrs)) return false;
	if( line_segment_intersect(p1, p2, Point(0,0), Point(0, WINDOW_H),intrs)) return false;
	if( line_segment_intersect(p1, p2, Point(0,WINDOW_H), Point(WINDOW_W, WINDOW_H),intrs)) return false;
	return !line_segment_intersect(p1, p2, Point(WINDOW_W,0), Point(WINDOW_W, WINDOW_H),intrs);
}
//------------------------------------------------------------------------------
//Game Window callback in case the Game is exited by closing the Game Window
void window_callback(Fl_Widget*, void*) {
  
  exit(0);	//For now just exit, since there is no state to log
}

//------------------------------------------------------------------------------
//Game Window type
struct Pile_window : Window {
private:
	bool start_pushed, stick_selected, monitor_mouse, next_pushed, again_pushed;
	int stick_to_remove, garea, number_sticks;
	//int pile_connects[MAX_STICKS][MAX_STICKS]; //see enum connect_type
	//enum Connect_type {none, top_intersect, bottom_intersect, middle_intersect};
	//--------------------------------------------------------------------------
	//Stick type
	struct Stick : Shape{
		Stick(Point p1, Point p2):a(p1), b(p2), angle(0), status(false), draw_order(0) {add(p1); add(p2); }//maintain mirror points in Shape to leverage its draw() code
		Stick(Point p1, Point p2, double ang): a(p1), b(p2), status(false), draw_order(0) { add(p1); add(p2); set_angle(ang);}
		Stick(Point p1, Point p2, double ang, bool stat): a(p1), b(p2), draw_order(0) {add(p1); add(p2); set_angle(ang); set_status(stat);}
		~Stick(){}
	public:
		void set_point(int i,Point p){if(i==0) a=p; else if(i==1) b=p; else error("invalid point");Shape::set_point(i, p);}
		void set_angle(double ang) {angle = ang;}
		double get_angle() {return angle;}
		bool get_status() {return status;}
		void set_status(bool stat) {status = stat;}
		Point point_a() {return a;}
		Point point_b() {return b;}
		int draw_order;		
		Stick(const Stick& s){			   // permit copying
			this->a = s.a;
			this->b = s.b;
			if(this->number_of_points()==0) {this->add(a);this->add(b);}
			else {this->Shape::set_point(0,a); this->Shape::set_point(1,b);}
			this->angle = s.angle;
			this->status = s.status;
			this->draw_order = s.draw_order;
			this->set_color(s.color());
			this->set_style(s.style());
		}
		Stick& operator=(const Stick& s){
			if (this != &s) // protect against invalid self-assignment
			{
				this->a = s.a;
				this->add(a);
				this->b = s.b;
				this->add(b);
				if(this->number_of_points()==0) {this->add(a);this->add(b);}
				else {this->Shape::set_point(0,a); this->Shape::set_point(1,b);}
				this->angle = s.angle;
				this->status = s.status;
				this->draw_order = s.draw_order;
				this->set_color(s.color());
				this->set_style(s.style());
			}
			return *this;
		}
	private:
		Point a, b;
		double angle;
		bool status; //in pile: true, in gathering area: false
	};

	//--------------------------------------------------------------------------
	vector<Stick> sticks;	//Iconic sticks used in the splash screen
	vector<Stick> pile;		//Pile of sticks used to play the game


	//--------------------------------------------------------------------------	
	static void cb_start(Address, Address addr) // callback for start_button
	//	{ reference_to<Pile_window>(addr).next(); }
	{
		static_cast<Pile_window*>(addr)->start();
	}

	void start(void) { start_pushed = true; }

	static void cb_next(Address, Address addr) //callback for next_button
	{
		static_cast<Pile_window*>(addr)->next();
	}
	void next(void) {next_pushed = true;}

	static void cb_again(Address, Address addr) // callback for again_button
	{
		static_cast<Pile_window*>(addr)->again();
	}

	void again(void) { again_pushed = true; }

	static void cb_exit(Address, Address addr) //callback for exit_button
	{
		static_cast<Pile_window*>(addr)->myexit();
	}
	void myexit(void) {exit(0);}

	static void timer_callback(void*) {		//second counter
	  ++Sticks_clock;					//bump up our second counter by 1
	  Fl::repeat_timeout(1.0, timer_callback);
	}
	//--------------------------------------------------------------------------
public:
	
	Button start_button;
	Button next_button, again_button, exit_button;
	Text instr1, instr2, score_output, clock_output, game_over_msg;
	Text o0, o1, o2, o3, o4, o5;	//Vector_ref doesn't seem to work as advertized
	In_box initials_box;
	static unsigned long Sticks_clock;

	Pile_window(Point xy, int w, int h, const string& title )
	: Window(xy,w,h,title), garea(0),
	start_pushed(false),stick_selected(false), monitor_mouse(false), next_pushed(false),again_pushed(false),	//init flags
	instr1(Point(WINDOW_W/5+70,WINDOW_H/4+0),"Enter your initials, and click START to play the game."),
	instr2(Point(WINDOW_W/5+20,WINDOW_H/4+20),"Click sticks on top of others, or non-intersecting sticks to earn points!"),
	initials_box(Point(WINDOW_W/3+100,WINDOW_H/2+45),70,20,"(Default is 'UuU')"),
	start_button(Point(WINDOW_W/3+85,WINDOW_H/2+80), 100, 20, "START", cb_start),
	next_button(Point(WINDOW_W - 150,WINDOW_H/2+250), 100, 20, "NEXT >>", cb_next),
	again_button(Point(WINDOW_W/3+85,WINDOW_H/2+80), 100, 20, "PLAY AGAIN", cb_again),
	exit_button(Point(WINDOW_W - 150,WINDOW_H/2+250), 100, 20, "EXIT GAME", cb_exit),
	score_output(Point(10,150+STICK_WIDTH),"Your Score: "),
	clock_output(Point(10, 200+STICK_WIDTH),"Game Seconds: "),
	game_over_msg(Point(10, 250+STICK_WIDTH), "GAME OVER!"),
	o0(Point(WINDOW_W/5+50,WINDOW_H/4+20),""),
	o1(Point(WINDOW_W/3+80,WINDOW_H/4+50),""),
	o2(Point(WINDOW_W/3+80,WINDOW_H/4+70),""),
	o3(Point(WINDOW_W/3+80,WINDOW_H/4+90),""),
	o4(Point(WINDOW_W/3+80,WINDOW_H/4+110),""),
	o5(Point(WINDOW_W/3+80,WINDOW_H/4+130),"")
	{ 
		
		attach(instr1);
		attach(instr2);
		attach(initials_box);
		attach(start_button); 
		attach(next_button);
		next_button.hide();
		attach(exit_button);
		exit_button.hide();
		attach(again_button);
		again_button.hide();
		callback(window_callback);

		Sticks_clock = 0;
		Fl::add_timeout(1.0, timer_callback);
		//create "iconic" sticks for 1st screen
		for (int i=1; i<MAX_STICKS+1; i++){
			Stick stick(Point(WINDOW_W/5+80, 180+i*10), Point(WINDOW_W/5+80+STICK_WIDTH, 180+i*10));
			stick.set_color(color_list[(i-1)%color_list.size()]);//rand()%
			stick.set_style(Line_style(Line_style::solid,STICK_HEIGHT));
			sticks.push_back(stick);
		}
		//for (int i=0; i<sticks.size(); i++){//debugging output
		//	cout << sticks[i].point_a().x << sticks[i].point_a().y << endl;
		//}
		//put "icon" sticks on the Game Window
		for(unsigned int i=0; i<sticks.size();i++)
			attach(sticks[i]); // attach sticks to window instance
		Fl::redraw();			//show the splash window
	}

	//--------------------------------------------------------------------------
	//Helper functions

	Stick create_pile_stick(void) {
		Point a;
		double angle = (rand()%90)/57.2957795;	//chose random angle and convert to radian measure
		if (0==rand()%2) a = Point(WINDOW_W/2-(rand()%(STICK_WIDTH/8)),WINDOW_H/2-(rand()%(STICK_WIDTH/2)));//rotate based on its left end point
		else {
			angle=-angle;
			a = Point(WINDOW_W/2-(rand()%(STICK_WIDTH/8)),WINDOW_H/2+(rand()%(STICK_WIDTH/2)));//rotate based on its left end point

		}
		Point b(int(a.x+STICK_WIDTH*cos(angle)), int(a.y+STICK_WIDTH*sin(angle)));//pile[0].point_b();// //calc other end point
		Stick t(a, b, angle, true);		
		t.set_color(color_list[(static_color++)%color_list.size()]);
		t.set_style(Line_style(Line_style::solid,STICK_HEIGHT));
		return t;
	}

	void show_score(int score) {
		ostringstream os;
		os << score;
		score_output.set_label("Your Score: "+os.str());
		attach(score_output);
		Fl::redraw();
	}

	void show_clock(int clock) {
		update_clock(clock);
		attach(clock_output);
		Fl::redraw();
	}
	void update_clock(int clock) {
		ostringstream os;
		os << clock;
		clock_output.set_label("Game Seconds: " + os.str());
		Fl::redraw();
	}
	void show_game_over(bool more_sticks) {
		if(invalid_stick) play_crash();
		else if(more_sticks) play_toobad();
		else play_applause();

		if (!more_sticks)
			game_over_msg.set_label("CONGRATULATIONS! GREAT GAME!");
		else if (invalid_stick)
			game_over_msg.set_label("STICK NOT ON TOP--GAME OVER");
		else game_over_msg.set_label("PLAY TIME EXHAUSTED--GAME OVER");
		attach(game_over_msg);
		Fl::redraw();
	}
	void hide_game_over(void) {
		detach(game_over_msg);
	}
	void hide_score() {
		detach(score_output);
	}
	void hide_clock() {
		detach(clock_output);
	}
	void initialize_pile(void) {
		vector<int> draw_index;
		for (int i = 0; i < MAX_STICKS; i++)
			draw_index.push_back(i);
		pile.clear();
		//place sticks on the pile
		for (int i=0; i<MAX_STICKS; i++){
			Stick t = create_pile_stick();
			int k = int(rand()%draw_index.size());
			t.draw_order = draw_index[k];
			draw_index.erase(draw_index.begin()+k);
			pile.push_back(t);
		}
		number_sticks = MAX_STICKS;
		garea = 0; //offset index of location for gathering area (garea) of sticks removed from the pile
	}

	void draw_pile(void) {
			//also draw gathered sticks as well as the running score
		
		for(unsigned int i=0; i<pile.size();i++){
			int j = 0;
			while (i != pile[j].draw_order) ++j; //locate next stick to draw
			attach(pile[j]); // attach sticks to window instance
		}
		
		Fl::redraw(); // display sticks!
	}
	void hide_sticks(void) {
		for(unsigned int i=0; i<pile.size();i++)
			detach(pile[i]); // detach sticks from window instance
		//gathered sticks?
		
		Fl::redraw();
	}
	string get_initials(void) {

		return initials_box.get_string(); //get player initials
	}

	void hide_next_button(void) {
		next_button.hide();
	}
	void show_next_button(void) {
		next_button.show();
	}

	void hide_instr(bool first_screen) {
		if(first_screen) {
			//Hide START button, and remove ititials startup instructions
			start_button.hide();	//hide start button
			detach(instr1);			//hide the instructions
			detach(instr2);
			detach(initials_box);	//hide input box
			initials_box.label = "New Initials?";
			for(unsigned int i=0; i<sticks.size();i++)
				detach(sticks[i]); // detach sticks from window instance
		}
		else {

			detach(o0);				//hide score data
			detach(o1);
			detach(o2);
			detach(o3);
			detach(o4);
			detach(o5);
			detach(initials_box);
		}
		Fl::redraw();
	}
	bool valid_selection(int stick){
		bool status = false; //assume invalid
		Point intrs;
		for (int i = 0; i < MAX_STICKS; i++) 
			if(pile[i].get_status() && i != stick ) 		
				if (line_segment_intersect(pile[stick].point_a(), pile[stick].point_b(),
					pile[i].point_a(), pile[i].point_b(), intrs)) {
						//sticks intersect, so stick must be on top to be valid (drawn later)
						//assume stick is in the pile (gathering area does not intersect), check draw order 
							if (pile[stick].draw_order < pile[i].draw_order) return false;//if draw order is earlier, stick is invalid in game play
				}
		return true; //no intersections or on top according to draw order
	}

	int score_stick(int stick) {
		//base the score on the number of stick interesections
		int score = STICK_SCORE;
		for (int i = 0; i < MAX_STICKS; i++) {
			if(i != stick && pile[i].get_status()) {
				Point intrs;
				if (line_segment_intersect(pile[stick].point_a(), pile[stick].point_b(),
					pile[i].point_a(), pile[i].point_b(), intrs)) score += STICK_SCORE;
			}
		}
		return score;
	}
	void output_scores(string pname, int score, top_player tp[]) {
		//get rid of screen garbage
		hide_sticks();

		//build text strings using score data
		ostringstream os;
		os << score;
		o0.set_label("Congratulations "+ pname +"! You scored: "+ os.str() +". The top 5 scores are:");
		os.clear();os.str("");os << setw(6) << tp[0].score;
		o1.set_label("1. "+tp[0].name + " ... "+os.str());
		os.clear();os.str("");os << setw(6) << tp[1].score;
		o2.set_label("2. "+tp[1].name+ " ... "+os.str());
		os.clear();os.str("");os << setw(6) << tp[2].score;
		o3.set_label("3. "+tp[2].name+ " ... "+os.str());
		os.clear();os.str("");os << setw(6) << tp[3].score;
		o4.set_label("4. "+tp[3].name+ " ... "+os.str());
		os.clear();os.str("");os << setw(6) << tp[4].score;
		o5.set_label("5. "+tp[4].name+ " ... "+os.str());

		//show results
		attach(o0);
		attach(o1);
		attach(o2);
		attach(o3);
		attach(o4);
		attach(o5);
		attach(initials_box);
		again_button.show();
		//again_button.label = "PLAY AGAIN";
		Fl::redraw();
	}

	void wait_for_start(void)
	{
		while (!start_pushed) 
			Fl::wait();
		start_pushed = false;
		Fl::redraw();
	}
	void wait_for_next(void)
	{
		while (!next_pushed) 
			Fl::wait();
		next_pushed = false;
		Fl::redraw();
	}

	void wait_for_again(void)
	{
		while (!again_pushed) 
			Fl::wait();
		again_pushed = false;
		Fl::redraw();
	}

	int wait_for_stick_selected(void)
	{
		int score = 0;
		invalid_stick = false;
		game_is_over = false;//set to true by second counter, or by code below
		unsigned long temp = Sticks_clock; //current time in seconds
		while(!stick_selected && !game_is_over) {
			Fl::wait(); //wait for player or the clock
			game_is_over =  (Sticks_clock - game_start_time) > game_time;
			if (!game_is_over && Sticks_clock >= (temp + 1)) {//when Sticks_clock advances show new second value
				update_clock(game_time - (Sticks_clock - game_start_time));//show new remaining game time
				temp = Sticks_clock;//advance temp timer to current time
			}
			
		}
		if (stick_selected) {
			stick_selected = false;	//reset flag
			//validate selected stick (from a game play viewpoint)
			invalid_stick = !valid_selection(stick_to_remove);
			if (!invalid_stick) 
				score = score_stick(stick_to_remove);	//score value of selected stick
			else game_is_over = true;	//end the game because invalid stick selected
		}
		return score;
	}

	//--------------------------------------------------------------------------
	//Monitor mouse clicks in Game Window

	// references: fltk.pdf, p. 57; http://pages.cs.wisc.edu/~pingelm/handson.html
	int handle(int event) {
		Point intrs;
		int mouse_x, mouse_y;
		if(monitor_mouse)
			switch (event) {
				case FL_PUSH: 
				{//mouse button pushed
					//compare mouse coordinates to sticks and select the closest stick
					mouse_x = Fl::event_x();
					mouse_y = Fl::event_y();
					//cout << mouse_x << "," << mouse_y << endl; //debugging check
					//verify the closest stick is within an acceptable tolerance to make it a valid selection
					int j = int(pile.size()-1); //start hunt at the latest draw order stick
					while (j>=0 && !stick_selected) {//run this loop in "draw order" latest to earliest to avoid BUG at the intersection of two sticks.
						int i=0;
						while (j != pile[i].draw_order)
							++i;//point i to jth draw order stick
						//form a diagonal line thru the mouse click point that creates a tolerance
						//and see if this line intersects the current stick
						if(pile[i].get_status()){ //in pile?
							if (line_segment_intersect(pile[i].point_a(), pile[i].point_b(), 
								Point(mouse_x-SELECT_TOLERANCE,mouse_y-SELECT_TOLERANCE), 
								Point(mouse_x+SELECT_TOLERANCE,mouse_y+SELECT_TOLERANCE),intrs)||
								line_segment_intersect(pile[i].point_a(), pile[i].point_b(), 
								Point(mouse_x-SELECT_TOLERANCE,mouse_y+SELECT_TOLERANCE), 
								Point(mouse_x+SELECT_TOLERANCE,mouse_y-SELECT_TOLERANCE),intrs)){
								stick_selected = true; 
								stick_to_remove = i;
								//BUG--user clicks on stick on top of another stick, but this code finds the stick underneath		Fixed by RGW 2/25/09
								//BUG--user clicks at the intersecion point of two sticks, and the draw order of the top stick is not taken into account
							}
						}
						--j; //advance back to next earliest draw order stick
					}
					return Fl_Window::handle(event);
				}
				default:
					return Fl_Window::handle(event);
		}
		
		else return Fl_Window::handle(event);
	}
	//--------------------------------------------------------------------------
	//MOre Helper functions
	void clear_stick(){
		if(monitor_mouse){
			//we relocate stick to the gathering area
			if(pile[stick_to_remove].get_status()) {
				pile[stick_to_remove].set_point(0, Point(garea*10 + 10, 100));
				pile[stick_to_remove].set_point(1, Point(garea*10 + 10, 100+STICK_WIDTH));
				pile[stick_to_remove].set_status(false); //no longer in the pile
				garea++;
				number_sticks--;
			}
		}
	}
	
	bool more_sticks() {
		return number_sticks != 0;
	}
	void set_monitor_mouse () {
		monitor_mouse=true;
	}
	void reset_monitor_mouse () {
		monitor_mouse = false;
	}
};

unsigned long Pile_window::Sticks_clock = 0;

void update_top_scores(string name, int score, top_player tp[]) {

	//read previous scores
	string fn = "pickupsticks.dat";
	ifstream ifs(fn.c_str(), ios_base::in);
	if (ifs) {
		for(int i=0; i < NUM_TOP_PLAYERS; ++i) {
			ifs >> tp[i].name >> tp[i].score;
			if(tp[i].name.length()<3) tp[i].name += "  ";//pad with spaces
			tp[i].name.resize(3);	//keep only first 3 characters
		}
	}
	else for(int i=0; i < NUM_TOP_PLAYERS; ++i) {//assume the file is not there, so create initial list
		tp[i].name = "---"; 
		tp[i].score = 0;
	}
	ifs.close();
	//locate insertion point for the current score
	int i = 0;
	while (i < NUM_TOP_PLAYERS && tp[i].score > score ) ++i;
	if (i < NUM_TOP_PLAYERS) {
		//make room to insert new score
		for(int j=NUM_TOP_PLAYERS-1; j > i; --j) {
			tp[j].name = tp[j-1].name; 
			tp[j].score = tp[j-1].score;
		}
		//copy in new score
		tp[i].name = name; tp[i].score = score;
		
		//rewrite scores to disk only if it makes it into the top five
		ofstream ofs(fn.c_str(), ios_base::out|ios_base::trunc);
		if (ofs) {
			for(int i=0; i < NUM_TOP_PLAYERS; ++i) 
				ofs << tp[i].name << " " << tp[i].score << " ";
		}
		ofs.close();
	}
}
//--------------------------------------------------------------------------
//game main

 int main(int argc, char **argv)
 
	try
	{
		srand (/*11*/ unsigned int(time(NULL)) );	//use current time to seed rand() to make play unpredictable
		init_color_list();
		//Basic pick up sticks game
		Point tl((Fl::w()-WINDOW_W)/2,(Fl::h()-WINDOW_H)/2); //position the sticks window
		Pile_window win(tl,WINDOW_W,WINDOW_H,"PICK UP STICKS"); // display initial sticks game window and instructions
		string player_name = "UuU";//set default initials		
		
		win.wait_for_start();//wait for START button
		win.hide_instr(true);//hide instructions
		while(true) {
			static_color = 0;
			//Play game rounds until the player exits the program
			string pname = win.get_initials(); //get player initials
			if(pname == "") pname = player_name;//set default initials
			else if(pname.length()<3) pname += "  ";//pad with spaces
			pname.resize(3);	//keep only first 3 characters
			player_name = pname;
			//cout << player_name << endl;//output initials to background console for debugging purposes
			game_start_time = win.Sticks_clock;	//mark start point for game time
			win.set_monitor_mouse ();
			win.initialize_pile();// generate initial pile of sticks
			int player_score = 0;
			game_time = GAME_TIME; // start the game with GAME_TIME seconds, increase when sticks are selected
			win.show_clock(game_time);
			bool more_sticks;
			while (more_sticks = win.more_sticks()) {	
				win.draw_pile();
				win.show_score(player_score);
				player_score += win.wait_for_stick_selected();
				if(game_is_over) break;		//play game_time seconds, or or stop if invalid stick selected
				//cout << "click in game window" << endl;//output to background console for debugging purposes
				play_swosh();
				win.clear_stick();
				//++game_time; //give an additional second for valid stick selection
			}

			win.reset_monitor_mouse();
			win.draw_pile(); //show final pile
			win.show_score(player_score); //show final score
			win.show_game_over(more_sticks);
			//attach next button
			win.show_next_button(); 
			win.wait_for_next();//wait for NEXT button
			win.hide_next_button();
			win.hide_score();
			win.hide_clock();
			win.hide_game_over();
			//update top NUM_TOP_PLAYERS score data
			top_player tp[NUM_TOP_PLAYERS];
			update_top_scores(player_name, player_score, tp);
			//output current score as well as top player scores
			//cout << player_score << endl;//output score to backbground console for debugging purposes
			win.output_scores(player_name, player_score, tp);//hide the sticks, and display top 5 scores
			win.again_button.show();
			win.exit_button.show();
			win.wait_for_again();//wait for PLAY AGAIN button
			win.again_button.hide();
			win.exit_button.hide();
			win.hide_instr(false); //hide data display
		}
		return 0;
	}
	catch(exception& e) {
		cerr << "exception: " << e.what() << '\n';
	exit (1);
	}
	catch (...) {
		cerr << "Some exception\n";
	exit (2);
}

Was This Post Helpful? 0
  • +
  • -

#3 Beinss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 09

Re: pick up sticks : game

Posted 08 April 2009 - 01:51 PM

My assignment is to add a border to a line for a pick up sticks game, so the sticks are outlined in black. The lines are rather thick so they arnt tiny lines but I was wondering if there is a line style or something close to it that would allow for the stick to be attached as a outlined line. If this isnt possible could anyone give me a clue as to how to create a border for a line. I fear making the lines into rectangles is my only option, which would invlove mass recreation of the code.

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: pick up sticks : game

Posted 08 April 2009 - 02:28 PM

To even allow someone to even think about this it would be advisable to let us all know which framework you are using to draw the "lines"; are you using MFC or Borlands OWL, or something else? From the sound of it you will need to use whichever framework and DRAW the lines with your own routines.
Was This Post Helpful? 0
  • +
  • -

#5 AndyH1963  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 22
  • View blog
  • Posts: 201
  • Joined: 22-June 08

Re: pick up sticks : game

Posted 08 April 2009 - 02:57 PM

To assist us in helping you I suggest that you:

(1) Explain your problem more,

(2) Rather than post the whole source code to your program, you target the specific area ofthe program that is the reason for your question.

If you are un-clear on what the problem is then how can you expect others to know?
Was This Post Helpful? 0
  • +
  • -

#6 Beinss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 09

Re: pick up sticks : game

Posted 08 April 2009 - 05:03 PM

View PostAndyH1963, on 8 Apr, 2009 - 01:57 PM, said:

To assist us in helping you I suggest that you:

(1) Explain your problem more,

(2) Rather than post the whole source code to your program, you target the specific area ofthe program that is the reason for your question.

If you are un-clear on what the problem is then how can you expect others to know?

I appreciate your response. I actually was trying to delete this post, I erased the question in the first post and forgot to erase the second post. I apologies, my question has been restated in another post on this forum.
Was This Post Helpful? 0
  • +
  • -

#7 Beinss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 09

Re: pick up sticks : game

Posted 08 April 2009 - 05:15 PM

View PostAndyH1963, on 8 Apr, 2009 - 01:28 PM, said:

To even allow someone to even think about this it would be advisable to let us all know which framework you are using to draw the "lines"; are you using MFC or Borlands OWL, or something else? From the sound of it you will need to use whichever framework and DRAW the lines with your own routines.


Im not sure what the framework is exactly. Im pretty new to C++ Im altering a Pick Up Sticks game in C++ to have the sticks outlined.
[code]
#include "fltk.h"
#include "GUI.h"
#include <time.h>
#include <iostream>
#include <iomanip>

namespace Graph_lib { //additions to graphics library posted on http://www.stroustrup.com/Programming/Graphics/
bool line_segment_intersect(Point p1, Point p2, Point p3, Point p4, Point& intersection);

string In_box::get_string() //added by RGW 2/23/09
{
Fl_Input& pi = reference_to<Fl_Input>(pw);
return string(pi.value());
}
}
using namespace Graph_lib;
//------------------------------------------------------------------------------
const int NUM_TOP_PLAYERS =5; //Keep history of 5 best scores
const int MAX_STICKS =14; //Game has this many sticks
const int STICK_HEIGHT =5; //Line width
const int STICK_WIDTH =300; //Line length
const int WINDOW_W =800; //Game Window size
const int WINDOW_H =600;
const int SELECT_TOLERANCE =2; //Neighborhood size (in pixels) for stick selection
const int GAME_TIME =30; //game time in seconds
const int STICK_SCORE =5; //base score for a stick with no intersections
const double PI =3.14159265358979;

//------------------------------------------------------------------------------
void play_swosh(void); //Helper functions to use Audiere library to play a sound
void play_crash(void);
void play_toobad(void);
void play_applause(void);

//------------------------------------------------------------------------------
bool game_is_over = false; //boolean used to monitor stick selection time
bool invalid_stick;
unsigned long game_start_time;
unsigned long game_time = 0; //current game time in seconds
static int static_color = 0;

//------------------------------------------------------------------------------
vector<Color::Color_type> color_list;

void init_color_list() {
color_list.push_back(Color::red);
color_list.push_back(Color::blue);
color_list.push_back(Color::green);
color_list.push_back(Color::yellow);
color_list.push_back(Color::white);
color_list.push_back(Color::black);
color_list.push_back(Color::magenta);
color_list.push_back(Color::cyan);
color_list.push_back(Color::dark_red);
color_list.push_back(Color::dark_green);
color_list.push_back(Color::dark_yellow);
color_list.push_back(Color::dark_blue);
color_list.push_back(Color::dark_magenta);
color_list.push_back(Color::dark_cyan);
}
//------------------------------------------------------------------------------
struct top_player {
string name;
int score;
};

//------------------------------------------------------------------------------
//function in_bounds(p1,p2) verifies a stick at p1,p2 is in the Game Window
bool in_bounds(Point p1, Point p2) {
Point intrs;
if( line_segment_intersect(p1, p2, Point(0,0), Point(WINDOW_W,0),intrs)) return false;
if( line_segment_intersect(p1, p2, Point(0,0), Point(0, WINDOW_H),intrs)) return false;
if( line_segment_intersect(p1, p2, Point(0,WINDOW_H), Point(WINDOW_W, WINDOW_H),intrs)) return false;
return !line_segment_intersect(p1, p2, Point(WINDOW_W,0), Point(WINDOW_W, WINDOW_H),intrs);
}
//------------------------------------------------------------------------------
//Game Window callback in case the Game is exited by closing the Game Window
void window_callback(Fl_Widget*, void*) {

exit(0); //For now just exit, since there is no state to log
}

//------------------------------------------------------------------------------
//Game Window type
struct Pile_window : Window {
private:
bool start_pushed, stick_selected, monitor_mouse, next_pushed, again_pushed, instructions_pushed, scores_pushed, reset_pushed; //BJT ", instruction_pushed, scores_pushed" KZ ", reset_pushed"
int stick_to_remove, garea, number_sticks;
//int pile_connects[MAX_STICKS][MAX_STICKS]; //see enum connect_type
//enum Connect_type {none, top_intersect, bottom_intersect, middle_intersect};
//--------------------------------------------------------------------------
//Stick type
struct Stick : Shape{
Stick(Point p1, Point p2):a(p1), b(p2), angle(0), status(false), draw_order(0) {add(p1); add(p2); }//maintain mirror points in Shape to leverage its draw() code
Stick(Point p1, Point p2, double ang): a(p1), b(p2), status(false), draw_order(0) { add(p1); add(p2); set_angle(ang);}
Stick(Point p1, Point p2, double ang, bool stat): a(p1), b(p2), draw_order(0) {add(p1); add(p2); set_angle(ang); set_status(stat);}
~Stick(){}
public:
void set_point(int i,Point p){if(i==0) a=p; else if(i==1) b=p; else error("invalid point");Shape::set_point(i, p);}
void set_angle(double ang) {angle = ang;}
double get_angle() {return angle;}
bool get_status() {return status;}
void set_status(bool stat) {status = stat;}
Point point_a() {return a;}
Point point_b() {return b;}
int draw_order;
Stick(const Stick& s){ // permit copying
this->a = s.a;
this->b = s.b;
if(this->number_of_points()==0) {this->add(a);this->add(b);}
else {this->Shape::set_point(0,a); this->Shape::set_point(1,b);}
this->angle = s.angle;
this->status = s.status;
this->draw_order = s.draw_order;
this->set_color(s.color());
this->set_style(s.style());
}
Stick& operator=(const Stick& s){
if (this != &s) // protect against invalid self-assignment
{
this->a = s.a;
this->add(a);
this->b = s.b;
this->add(b);
if(this->number_of_points()==0) {this->add(a);this->add(b);}
else {this->Shape::set_point(0,a); this->Shape::set_point(1,b);}
this->angle = s.angle;
this->status = s.status;
this->draw_order = s.draw_order;
this->set_color(s.color());
this->set_style(s.style());
}
return *this;
}
private:
Point a, b;
double angle;
bool status; //in pile: true, in gathering area: false
};

//--------------------------------------------------------------------------
vector<Stick> sticks; //Iconic sticks used in the splash screen
vector<Stick> pile; //Pile of sticks used to play the game


//--------------------------------------------------------------------------
static void cb_start(Address, Address addr) // callback for start_button
// { reference_to<Pile_window>(addr).next(); }
{
static_cast<Pile_window*>(addr)->start();
}

void start(void) { start_pushed = true; }

static void cb_next(Address, Address addr) //callback for next_button
{
static_cast<Pile_window*>(addr)->next();
}
void next(void) {next_pushed = true;}
static void cb_instructions(Address, Address addr) //calback for instructions_button //BJT all
{
static_cast<Pile_window*>(addr)->instructions();
}
void instructions(void){instructions_pushed = true;} //BJT
static void cb_scores(Address, Address addr) //calback for scores_button //BJT all
{
static_cast<Pile_window*>(addr)->scores();
}
void scores(void){scores_pushed = true;} //BJT
static void cb_reset(Address, Address addr) //calback for reset_button //KZ all
{
static_cast<Pile_window*>(addr)->reset();
}
void reset(void){reset_pushed = true;} //KZ
static void cb_again(Address, Address addr) // callback for again_button
{
static_cast<Pile_window*>(addr)->again();
}
void again(void) { again_pushed = true; }

static void cb_exit(Address, Address addr) //callback for exit_button
{
static_cast<Pile_window*>(addr)->myexit();
}
void myexit(void) {exit(0);}

static void timer_callback(void*) { //second counter
++Sticks_clock; //bump up our second counter by 1
Fl::repeat_timeout(1.0, timer_callback);
}
//--------------------------------------------------------------------------
public:

Button start_button;
Button next_button, again_button, exit_button, instructions_button, scores_button, reset_button; //BJT ", instructions_button, scores_button" KZ ", reset_button"
Text instr1, instr2, instr3, instr4, score_output, clock_output, game_over_msg; //BLS
Text o0, o1, o2, o3, o4, o5; //Vector_ref doesn't seem to work as advertized
In_box initials_box;
static unsigned long Sticks_clock;

Pile_window(Point xy, int w, int h, const string& title )
: Window(xy,w,h,title), garea(0),
start_pushed(false),stick_selected(false), monitor_mouse(false), next_pushed(false),again_pushed(false),instructions_pushed(false), reset_pushed(false), //init flags //BJT ", instructions_pushed(false)" KZ ", reset_pushed(false)"
instr1(Point(WINDOW_W/5+70,WINDOW_H/4-40),"Enter your initials, and click START to play the game."),
instr2(Point(WINDOW_W/5+10,WINDOW_H/4-20),"To earn points, click the stick that is on top of the others until all the sticks"), //improved instructions - BLS
instr3(Point(WINDOW_W/5+0,WINDOW_H/4+0),"are gone. The stick that is on top is a continuous line of a certain color. If you "), //improved instructions - BLS
instr4(Point(WINDOW_W/5+50,WINDOW_H/4+20),"fail to click the top stick you will lose. Good Luck and Gig 'Em!"), //improved instructions - BLS

initials_box(Point(WINDOW_W/3+100,WINDOW_H/2+45),70,20,"(Default is 'UuU')"),
start_button(Point(WINDOW_W/3+85,WINDOW_H/2+80), 100, 20, "START", cb_start),
next_button(Point(WINDOW_W - 150,WINDOW_H/2+250), 100, 20, "NEXT >>", cb_next),
again_button(Point(WINDOW_W/3+85,WINDOW_H/2+80), 100, 20, "PLAY AGAIN", cb_again),
instructions_button(Point(WINDOW_W/3+85,WINDOW_H/2+110), 100, 20, "INSTRUCTIONS", cb_instructions), //BJT
scores_button(Point(WINDOW_W/3+85,WINDOW_H/2+110), 100, 20, "SCORES", cb_scores), //BJT
reset_button(Point(WINDOW_W/3+85,WINDOW_H/2+140), 100, 20, "RESET", cb_reset), //KZ
exit_button(Point(WINDOW_W - 150,WINDOW_H/2+250), 100, 20, "EXIT GAME", cb_exit),
score_output(Point(10,150+STICK_WIDTH),"Your Score: "),
clock_output(Point(10, 200+STICK_WIDTH),"Game Seconds: "),
game_over_msg(Point(10, 250+STICK_WIDTH), "GAME OVER!"),
o0(Point(WINDOW_W/5+50,WINDOW_H/4+20),""),
o1(Point(WINDOW_W/3+80,WINDOW_H/4+50),""),
o2(Point(WINDOW_W/3+80,WINDOW_H/4+70),""),
o3(Point(WINDOW_W/3+80,WINDOW_H/4+90),""),
o4(Point(WINDOW_W/3+80,WINDOW_H/4+110),""),
o5(Point(WINDOW_W/3+80,WINDOW_H/4+130),"")
{

attach(instr1);
attach(instr2);
attach(instr3); //BLS
attach(instr4); //BLS
attach(initials_box);
attach(start_button);
attach(next_button);
next_button.hide();
attach(exit_button);
exit_button.hide();
attach(again_button);
again_button.hide();
attach(instructions_button); //BJT
instructions_button.hide(); //BJT
attach(scores_button); //BJT
scores_button.hide(); //BJT
attach(reset_button); //KZ
reset_button.hide(); //KZ
callback(window_callback);

Sticks_clock = 0;
Fl::add_timeout(1.0, timer_callback);
//create "iconic" sticks for 1st screen
for (int i=1; i<MAX_STICKS+1; i++){
Stick stick(Point(WINDOW_W/5+80, 180+i*10), Point(WINDOW_W/5+80+STICK_WIDTH, 180+i*10));
stick.set_color(color_list[(i-1)%color_list.size()]);//rand()%
stick.set_style(Line_style(Line_style::solid,STICK_HEIGHT));
sticks.push_back(stick);
}
//for (int i=0; i<sticks.size(); i++){//debugging output
// cout << sticks[i].point_a().x << sticks[i].point_a().y << endl;
//}
//put "icon" sticks on the Game Window
for(unsigned int i=0; i<sticks.size();i++)
attach(sticks[i]); // attach sticks to window instance
Fl::redraw(); //show the splash window
}

//--------------------------------------------------------------------------
//Helper functions

Stick create_pile_stick(void) {
Point a;
double angle = (rand()%90)/57.2957795; //chose random angle and convert to radian measure
if (0==rand()%2) a = Point(WINDOW_W/2-(rand()%(STICK_WIDTH/8)),WINDOW_H/2-(rand()%(STICK_WIDTH/2)));//rotate based on its left end point
else {
angle=-angle;
a = Point(WINDOW_W/2-(rand()%(STICK_WIDTH/8)),WINDOW_H/2+(rand()%(STICK_WIDTH/2)));//rotate based on its left end point

}
Point b(int(a.x+STICK_WIDTH*cos(angle)), int(a.y+STICK_WIDTH*sin(angle)));//pile[0].point_b();// //calc other end point
Stick t(a, b, angle, true);
t.set_color(color_list[(static_color++)%color_list.size()]);
t.set_style(Line_style(Line_style::solid,STICK_HEIGHT));
return t;
}

void show_score(int score) {
ostringstream os;
os << score;
score_output.set_label("Your Score: "+os.str());
attach(score_output);
Fl::redraw();
}

void show_clock(int clock) {
update_clock(clock);
attach(clock_output);
Fl::redraw();
}
void update_clock(int clock) {
ostringstream os;
os << clock;
clock_output.set_label("Game Seconds: " + os.str());
Fl::redraw();
}
void show_game_over(bool more_sticks) {
if(invalid_stick) play_crash();
else if(more_sticks) play_toobad();
else play_applause();

if (!more_sticks)
game_over_msg.set_label("CONGRATULATIONS! GREAT GAME!");
else if (invalid_stick)
game_over_msg.set_label("STICK NOT ON TOP--GAME OVER");
else game_over_msg.set_label("PLAY TIME EXHAUSTED--GAME OVER");
attach(game_over_msg);
Fl::redraw();
}
void hide_game_over(void) {
detach(game_over_msg);
}
void hide_score() {
detach(score_output);
}
void hide_clock() {
detach(clock_output);
}
void initialize_pile(void) {
vector<int> draw_index;
for (int i = 0; i < MAX_STICKS; i++)
draw_index.push_back(i);
pile.clear();
//place sticks on the pile
for (int i=0; i<MAX_STICKS; i++){
Stick t = create_pile_stick();
int k = int(rand()%draw_index.size());
t.draw_order = draw_index[k];
draw_index.erase(draw_index.begin()+k);
pile.push_back(t);
}
number_sticks = MAX_STICKS;
garea = 0; //offset index of location for gathering area (garea) of sticks removed from the pile
}

void draw_pile(void) {
//also draw gathered sticks as well as the running score

for(unsigned int i=0; i<pile.size();i++){
int j = 0;
while (i != pile[j].draw_order) ++j; //locate next stick to draw
attach(pile[j]); // attach sticks to window instance
}

Fl::redraw(); // display sticks!
}
void hide_sticks(void) {
for(unsigned int i=0; i<pile.size();i++)
detach(pile[i]); // detach sticks from window instance
//gathered sticks?

Fl::redraw();
}
string get_initials(void) {

return initials_box.get_string(); //get player initials
}

void hide_next_button(void) {
next_button.hide();
}
void show_next_button(void) {
next_button.show();
}

void hide_instr(bool first_screen) {
if(first_screen) {
//Hide START button, and remove ititials startup instructions
start_button.hide(); //hide start button
detach(instr1); //hide the instructions
detach(instr2);
detach(instr3); //BLS
detach(instr4); //BLS
detach(initials_box); //hide input box
initials_box.label = "New Initials?";
for(unsigned int i=0; i<sticks.size();i++)
detach(sticks[i]); // detach sticks from window instance
}
else {

detach(o0); //hide score data
detach(o1);
detach(o2);
detach(o3);
detach(o4);
detach(o5);
detach(initials_box);
}
Fl::redraw();
}
void show_instr(){ //BJT all
attach(instr1);
attach(instr2);
attach(instr3); //BLS
attach(instr4); //BLS
}
bool valid_selection(int stick){
bool status = false; //assume invalid
Point intrs;
for (int i = 0; i < MAX_STICKS; i++)
if(pile[i].get_status() && i != stick )
if (line_segment_intersect(pile[stick].point_a(), pile[stick].point_b(),
pile[i].point_a(), pile[i].point_b(), intrs)) {
//sticks intersect, so stick must be on top to be valid (drawn later)
//assume stick is in the pile (gathering area does not intersect), check draw order
if (pile[stick].draw_order < pile[i].draw_order) return false;//if draw order is earlier, stick is invalid in game play
}
return true; //no intersections or on top according to draw order
}

int score_stick(int stick) {
//base the score on the number of stick interesections
int score = STICK_SCORE;
for (int i = 0; i < MAX_STICKS; i++) {
if(i != stick && pile[i].get_status()) {
Point intrs;
if (line_segment_intersect(pile[stick].point_a(), pile[stick].point_b(),
pile[i].point_a(), pile[i].point_b(), intrs)) score += STICK_SCORE;
}
}
return score;
}
void output_scores(string pname, int score, top_player tp[]) {
//get rid of screen garbage
hide_sticks();

//build text strings using score data
ostringstream os;
os << score;
o0.set_label("Congratulations "+ pname +"! You scored: "+ os.str() +". The top 5 scores are:");
os.clear();os.str("");os << setw(6) << tp[0].score;
o1.set_label("1. "+tp[0].name + " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[1].score;
o2.set_label("2. "+tp[1].name+ " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[2].score;
o3.set_label("3. "+tp[2].name+ " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[3].score;
o4.set_label("4. "+tp[3].name+ " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[4].score;
o5.set_label("5. "+tp[4].name+ " ... "+os.str());

//show results
attach(o0);
attach(o1);
attach(o2);
attach(o3);
attach(o4);
attach(o5);
attach(initials_box);
again_button.show();
//again_button.label = "PLAY AGAIN";
Fl::redraw();
}
void output_scores(top_player tp[]) { //KZ all
//build text strings using score data
ostringstream os;
o0.set_label("You have reset the top 5 scores in the Pick Up Sticks Game");
os.clear();os.str("");os << setw(6) << tp[0].score;
o1.set_label("1. "+tp[0].name + " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[1].score;
o2.set_label("2. "+tp[1].name+ " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[2].score;
o3.set_label("3. "+tp[2].name+ " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[3].score;
o4.set_label("4. "+tp[3].name+ " ... "+os.str());
os.clear();os.str("");os << setw(6) << tp[4].score;
o5.set_label("5. "+tp[4].name+ " ... "+os.str());

//show results
attach(o0);
attach(o1);
attach(o2);
attach(o3);
attach(o4);
attach(o5);
Fl::redraw();
}

void wait_for_start(void)
{
while (!start_pushed)
Fl::wait();
start_pushed = false;
Fl::redraw();
}
void wait_for_next(void)
{
while (!next_pushed)
Fl::wait();
next_pushed = false;
Fl::redraw();
}

void wait_for_again(top_player tp[])
{

while (!again_pushed){
if(instructions_pushed) //BJT-all of if
{
hide_instr(false);
show_instr();
instructions_button.hide();
scores_button.show();
attach(initials_box);
for(unsigned int i=0; i<sticks.size();i++)
attach(sticks[i]); // attach sticks from window instance
instructions_pushed = false;
Fl::redraw();
}
if(scores_pushed) //BJT-all of if
{
hide_instr(true);
attach(o0);
attach(o1);
attach(o2);
attach(o3);
attach(o4);
attach(o5);
instructions_button.show();
scores_button.hide();
attach(initials_box);
for(unsigned int i=0; i<sticks.size();i++)
detach(sticks[i]); // attach sticks from window instance
scores_pushed = false;
Fl::redraw();
}
if(reset_pushed){ //KZ all
string fn = "pickupsticks.dat";
for(int i=0; i < NUM_TOP_PLAYERS; ++i) {//recreate initial list
tp[i].name = "---";
tp[i].score = 0;
}
//rewrite scores to disk only if it makes it into the top five
ofstream ofs(fn.c_str(), ios_base::out|ios_base::trunc);
if (ofs) {
for(int i=0; i < NUM_TOP_PLAYERS; ++i)
ofs << tp[i].name << " " << tp[i].score << " ";
}
ofs.close();
output_scores(tp);//display reset material
reset_pushed = false;
Fl::redraw();

}
Fl::wait();
}
again_pushed = false;
Fl::redraw();
}

int wait_for_stick_selected(void)
{
int score = 0;
invalid_stick = false;
game_is_over = false;//set to true by second counter, or by code below
unsigned long temp = Sticks_clock; //current time in seconds
while(!stick_selected && !game_is_over) {
Fl::wait(); //wait for player or the clock
game_is_over = (Sticks_clock - game_start_time) > game_time;
if (!game_is_over && Sticks_clock >= (temp + 1)) {//when Sticks_clock advances show new second value
update_clock(game_time - (Sticks_clock - game_start_time));//show new remaining game time
temp = Sticks_clock;//advance temp timer to current time
}

}
if (stick_selected) {
stick_selected = false; //reset flag
//validate selected stick (from a game play viewpoint)
invalid_stick = !valid_selection(stick_to_remove);
if (!invalid_stick)
score = score_stick(stick_to_remove); //score value of selected stick
else game_is_over = true; //end the game because invalid stick selected
}
return score;
}

//--------------------------------------------------------------------------
//Monitor mouse clicks in Game Window

// references: fltk.pdf, p. 57; http://pages.cs.wisc.edu/~pingelm/handson.html
int handle(int event) {
Point intrs;
int mouse_x, mouse_y;
if(monitor_mouse)
switch (event) {
case FL_PUSH:
{//mouse button pushed
//compare mouse coordinates to sticks and select the closest stick
mouse_x = Fl::event_x();
mouse_y = Fl::event_y();
//cout << mouse_x << "," << mouse_y << endl; //debugging check
//verify the closest stick is within an acceptable tolerance to make it a valid selection
int j = int(pile.size()-1); //start hunt at the latest draw order stick
while (j>=0 && !stick_selected) {//run this loop in "draw order" latest to earliest to avoid BUG at the intersection of two sticks.
int i=0;
while (j != pile[i].draw_order)
++i;//point i to jth draw order stick
//form a diagonal line thru the mouse click point that creates a tolerance
//and see if this line intersects the current stick
if(pile[i].get_status()){ //in pile?
if (line_segment_intersect(pile[i].point_a(), pile[i].point_b(),
Point(mouse_x-SELECT_TOLERANCE,mouse_y-SELECT_TOLERANCE),
 &n
Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6627
  • View blog
  • Posts: 30,868
  • Joined: 10-May 07

Re: pick up sticks : game

Posted 08 April 2009 - 06:45 PM

** Renamed title to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#9 Beinss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-April 09

Re: pick up sticks : game

Posted 08 April 2009 - 07:47 PM

View Postno2pencil, on 8 Apr, 2009 - 05:45 PM, said:

** Renamed title to be more descriptive **

Well thank you.

To explain my issue a bit more. Im not sure where to begin with outlining the sticks in the game. I tried looking up a line style where it would border the lines. However there doesnt seem to be anything about that on the internet. The only other thing I can think of is to change each line segment (stick) into a rectangle filled with the same colors. I know rectangles by default so that would offer the border but in this code I dont know how to replace the lines with rectangles and If there is a simplier way of outlining the sticks I would appreciate it if someone had some ideas.

thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1