/* pairs2.c game of concentration CS691C lab1 Jon Squire */ /* using bitmaps in memory rather than from file */ /* I created deck_xbm.h and deck_xbm.c as general X card deck */ /* Started from O'reilly example */ /* change history: */ /* 9/19/95 JSS initial cut at display */ /* 9/22/95 JSS worked in toggle button */ /* 9/23/95 JSS did game logic in callback */ /* 9/25/95 JSS fixed game logic */ /* 7/06/04 JSS fix because bitmaps not displayed, XtAddCallback */ #include #include #include #include #include #include #include #include #include #include #include #include "deck_xbm.h" #define XMS(string) XmStringCreateSimple(string) #define nrows 4 #define ncols 5 #define nbutt 20 static Pixel fg, bg; static Pixmap back_pixmap; static int Xpos[nbutt]; static int Ypos[nbutt]; static char *cards[nbutt]; static float rsort[nbutt]; static double gen = 1.5; static int xincr = 100; static int yincr = 130; static int xoffs = 15; static int yoffs = 40; static int turn_num = 0; static Widget turned_on; static char *turned_card; static Display *display; static Window windowId; static XtAppContext app_context; /* statistics */ static int this_turn = 0; static int this_games = 0; static int this_best = 999; static int this_avg = 0; static int all_games = 0; static int all_best = 999; static int all_avg = 0; static int pairs_to_go; static int debug = 0; /* function prototypes */ static void get_stat(void); static void save_stat(void); static void PlaceIconButton(Widget *button, char *card_name, int k, int x, int y, void (*my_callback)()); static void next_deal(void); static void new_game(void); static void next_game(void); /* widgets indented according to heirarchy */ /* underscore b, "_b" are visible buttons that can be pushed */ /* underscore dia, "_dia" are dialog box widgets */ Widget toplevel; Widget main_w; Widget menu_bar; Widget pair_area; Widget text_output; /* single line text widget for messages */ Widget Tbuttons[nbutt]; /* array of cards as toggle buttons */ main(int argc, char *argv[]) { /* callback functions */ void file_cb(); void stat_cb(); printf("pairs2 starting, be patient if over network \n"); /* Initialize toolkit, open display and create application shell. */ toplevel = XtVaAppInitialize(&app_context, "Pairs2", NULL, 0, &argc, argv, NULL, NULL); /* Create main_w. */ main_w = XtVaCreateManagedWidget("Main", xmMainWindowWidgetClass, toplevel, XmNwidth, 510, XmNheight, 590, NULL); /* Create MenuBar in main_w. */ /* Create a simple MenuBar that contains two menus */ menu_bar = XmVaCreateSimpleMenuBar(main_w, "menubar", XmVaCASCADEBUTTON, XMS("File"), 'F', XmVaCASCADEBUTTON, XMS("Statistics"), 'S', NULL); /* Create menu, the "File" menu -- callback is file_cb() */ XmVaCreateSimplePulldownMenu(menu_bar, "file_menu", 0, (XtCallbackProc)(file_cb), XmVaPUSHBUTTON, XMS("New"), 'N', NULL, NULL, XmVaPUSHBUTTON, XMS("Clear"),'C', NULL, NULL, XmVaPUSHBUTTON, XMS("Exit"), 'E', "F3", XMS("F3"), NULL); /* Create menu, the "Statistics" menu -- callback is stat_cb() */ XmVaCreateSimplePulldownMenu(menu_bar, "file_menu", 1, (XtCallbackProc)(stat_cb), XmVaPUSHBUTTON, XMS("Show"), 'S', NULL, NULL, XmVaPUSHBUTTON, XMS("Clear"), 'C', NULL, NULL, NULL); XtManageChild(menu_bar); pair_area = XtVaCreateManagedWidget ("form", xmFormWidgetClass, main_w, NULL); text_output = XtVaCreateManagedWidget("text_out", xmTextWidgetClass, pair_area, XmNeditable, False, XmNcursorPositionVisible, False, XmNshadowThickness, 0, XmNsensitive, False, XmNx, 0, XmNy, 0, XmNwidth, 400, NULL); XmTextSetString(text_output, "watch this area for turn and best score \n"); XtManageChild(text_output); XtRealizeWidget(toplevel); display = XtDisplay(toplevel); windowId = XtWindow(toplevel); if(debug) printf("About to place bitmaps \n"); get_stat(); new_game(); XtRealizeWidget(toplevel); printf("pairs2 waiting for quit \n"); XtAppMainLoop(app_context); } /* end pairs2 */ static void get_stat(void) { /* read from file */ this_turn = 0; this_games = 0; this_best = 999; this_avg = 0; all_games = 0; all_best = 999; all_avg = 0; } static void save_stat(void) { if (this_games<=0) this_games = 1; if (this_best rsort[j]) { tf=rsort[i]; rsort[i]=rsort[j]; rsort[j]=tf; tc=cards[i]; cards[i]=cards[j]; cards[j]=tc; } } } } static void new_game(void) { void DoCB(); int i, j, k; if(debug) printf("new_game drawing back of cards\n"); reset_r(&gen); next_deal(); back_pixmap = XCreatePixmapFromBitmapData(display, windowId, card_xbm("back1"), card_width, card_height, BlackPixel(display, DefaultScreen(display)), WhitePixel(display, DefaultScreen(display)), DisplayPlanes(display, DefaultScreen(display))); k = 0; for (i=0; i=nbutt) { printf("bad index in DoCB %d",index); index = 0; } this_card = cards[index]; if(debug) printf("in DoCB index=%d, card=%s\n", index, this_card); if (pairs_to_go <= 0) { XmTextSetString(text_output, "Pick File,New or File,Quit \n"); return; } if (turn_num == 0) { turned_on = w; turned_card = this_card; turn_num = 1; return; } else if (turn_num == 1) { if (w == turned_on) { XmToggleButtonSetState(w, True, False); return; /* picked same card, keep it on */ } turn_num = 0; this_turn++; sprintf(buf, "%d turns %d best score\n", this_turn, this_best); XmTextSetString(text_output, buf); if (!strcmp(turned_card,this_card)) { /* have a match */ pairs_to_go--; if (!pairs_to_go) { if (this_turn < this_best) this_best = this_turn; if (this_turn < all_best) all_best = this_turn; this_games++; if (this_games<=0) this_games = 1; this_avg = (this_avg*this_games + this_turn)/(this_games+1); sprintf(buf, "%d turns %d best\n", this_turn, this_best); XmTextSetString(text_output, buf); /* next_game(); not automatic */ } return; } else { XmToggleButtonSetState(w, False, False); XmToggleButtonSetState(turned_on, False, False); turned_on = NULL; } } }