/* wmshuffle - The First Shuffle Game for WindowMaker =-=-=-=-=-= ====================================== Copyright (C) 2000 Adriel M. Ziesemer Jr Este programa é um software de livre distribuição, que pode ser copiado e distribuído sob os termos da Licença Pública Geral GNU, conforme publicada pela Free Software Foundation, versão 2 da licença ou (a critério do autor) qualquer versão posterior. Este programa é distribuído na expectativa de ser útil aos seus usuários, porém NÃO TEM NENHUMA GARANTIA, EXPLÍCITAS OU IMPLÍCITAS, COMERCIAIS OU DE ATENDIMENTO A UMA DETERMINADA FINALIDADE. Consulte a Licença Pública Geral GNU para maiores detalhes. Deve haver uma cópia da Licença Pública Geral GNU junto com este software em inglês ou português. Caso não haja escreva para Free Software Foundation, Inc. 675 Mass Ave, Cambridge, MA 02139, USA. adriel@atlas.ucpel.tche.br http://atlas.ucpel.tche.br/~adriel Adriel Mota Ziesemer Jr. Rua Alm. Barroso, 1359/102 Centro Pelotas/RS-Brasil CEP 96.010-280 */ #include #include #include #include #include #include #include #include #include "./wmgeneral/wmgeneral.h" #include "wmshuffle-master.xpm" // --------------------------------------------------------------- // definicoes #define WMSHUFFLE_VERSION "0.6" // --------------------------------------------------------------- // Variáveis Globais char ImageFile[1024]="none"; char *ProgName; int mute=0; int setskin=0; int quadrantes[16][4] = { {7, 7, 18, 18}, {20, 7, 31, 18}, {33, 7, 44, 18}, {46, 7, 57, 18}, {7, 20, 18, 31}, {20, 20, 31, 31}, {33, 20, 44, 31}, {46, 20, 57, 31}, {7, 33, 18, 44}, {20, 33, 31, 44}, {33, 33, 44, 44}, {46, 33, 57, 44}, {7, 46, 18, 57}, {20, 46, 31, 57}, {33, 46, 44, 57}, {46, 46, 57, 57}}; int tabuleiro[4][4] = { {0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; int buraco[2] = {3,3}; // mascara char wmshuffle_mask_bits[64 * 64]; int wmshuffle_mask_width = 64; int wmshuffle_mask_height = 64; // ---------------------------------------------------------- // declaracao das funcoes do sistema void main (int argc, char *argv[]); void printversion (void); void desenhafigura(void); void principal (int, char **); void validaJogada (int quadrante); void embaralha(void); void mostra_tabuleiro(void); void printversion (void); void usage (void); // ------------------------------------------------------------------------------------------- // funcao: main() // descricao: funcao principal da linguagem // in: argc - numero de argumentos passados por linha d comando // argv - vetor com os argumentos // out: nada void main (int argc, char *argv[]) { int i; ProgName = argv[0]; if (strlen (ProgName) >= 9) ProgName += (strlen (ProgName) - 9); for (i = 1; i < argc; i++) { char *arg = argv[i]; if (*arg == '-') { switch (arg[1]) { case 's': strcpy(ImageFile, argv[i+1]); setskin=1; break; case 'm': mute = 1; break; case 'v': printversion (); exit (0); break; case 'h': usage (); exit (0); break; default: fprintf (stderr, "Invalid option %c%c ",*arg,arg[1]); usage (); exit (0); break; } } } if(mute == 0) printversion (); principal (argc, argv); exit(0); } // ------------------------------------------------------------------------------------------- // funcao: desenhafigura() // descricao: desenha as pecas nos respectivos lugares // in: nada // out: nada void desenhafigura(void) { int x,y,c; for(y=0;y<=3;y++){ for(x=0;x<=3;x++){ c = (y*4)+x; copyXPMArea(quadrantes[tabuleiro[y][x]][0],quadrantes[tabuleiro[y][x]][1],12,12,quadrantes[c][0],quadrantes[c][1]+60); } } } // ------------------------------------------------------------------------------------------- // funcao: mostra_tabuleiro() // descricao: mostra os numeros das pecas // in: nada // out: nada void mostra_tabuleiro(void) { int x,y,c; for(y=0;y<=3;y++){ for(x=0;x<=3;x++){ c = (y*4)+x; copyXPMArea(quadrantes[tabuleiro[y][x]][0]+60,quadrantes[tabuleiro[y][x]][1],12,12,quadrantes[c][0],quadrantes[c][1]+60); } } RedrawWindowXY (0,60); usleep (3000000); desenhafigura(); RedrawWindowXY (0,60); } // ---------------------------------------------------------------------------------- // funcao: principal // descricao: funcao principal do jogo // in: argc - numero de argumentos passados por main() // argv - matriz de strings com os argumentos passador por main() // out: nada void principal (int argc, char **argv) { int i; int Width, Height; XEvent Event; Pixmap NewPixmap, NewShapeMask; XpmAttributes Attributes; Colormap cmap; Attributes.nalloc_pixels = 0; NewShapeMask = 0; createXBMfromXPM (wmshuffle_mask_bits, wmshuffle_master_xpm, wmshuffle_mask_width, wmshuffle_mask_height); openXwindow(argc, argv, wmshuffle_master_xpm, wmshuffle_mask_bits, wmshuffle_mask_width,wmshuffle_mask_height); // createXBMfromXPM (wmshuffle_mask_bits, wmshuffle_master_xpm, wmshuffle_mask_width, wmshuffle_mask_height); // openXwindow(argc, argv, wmshuffle_master_xpm, wmshuffle_mask_bits, wmshuffle_mask_width, wmshuffle_mask_height); if (setskin==1){ cmap = DefaultColormap(display, DefaultScreen(display)); if (!Attributes.nalloc_pixels) XFreeColors(display, cmap, Attributes.alloc_pixels, Attributes.nalloc_pixels,0); Attributes.valuemask = XpmExactColors | XpmCloseness | XpmReturnAllocPixels; Attributes.exactColors = 0; Attributes.closeness = 40000; XpmReadFileToPixmap(display, Root, ImageFile, &NewPixmap, &NewShapeMask, &Attributes); Height = Attributes.height; Width = Attributes.width; XCopyArea(display, NewPixmap, wmgen.pixmap, NormalGC, 0, 0, Width, Height, 0, 0); } for (i = 0;i < MAX_MOUSE_REGION; i++) AddMouseRegion (i, quadrantes[i][0], quadrantes[i][1], quadrantes[i][2], quadrantes[i][3]); desenhafigura(); while(1) { usleep(100000); i = ((float) random () / (float) RAND_MAX * 4); while (XPending (display)) { XNextEvent (display, &Event); switch (Event.type) { case Expose: RedrawWindowXY (0,60); break; case DestroyNotify: XCloseDisplay (display); exit (0); break; case ButtonRelease: i = CheckMouseRegion (Event.xbutton.x, Event.xbutton.y); switch (Event.xbutton.button) { case 1: validaJogada(i); break; case 2: embaralha (); break; case 3: mostra_tabuleiro (); break; } } } } } // ---------------------------------------------------------------------------------- // funcao: validaJogada // descricao: Verifica se a peca pressionada eh valida e executa o movimento // in: quadrante // out: nada void validaJogada (int quadrante) { int x,y; x = quadrante%4; y = quadrante/4; if(y==buraco[0]){ if(x==buraco[1]+1||x==buraco[1]-1){ tabuleiro[buraco[0]][buraco[1]]=tabuleiro[y][x]; tabuleiro[y][x] = 15 ; buraco[0] = y; buraco[1] = x; } } else if(x==buraco[1]){ if(y==buraco[0]+1||y==buraco[0]-1){ tabuleiro[buraco[0]][buraco[1]]=tabuleiro[y][x]; tabuleiro[y][x] = 15 ; buraco[0] = y; buraco[1] = x; } } desenhafigura(); RedrawWindowXY (0,60); } // ---------------------------------------------------------------------------------- // funcao: embaralha() // descricao: embaralha o tabuleiro // in: nada // out: nada void embaralha(void) { int x, i,a=4,n=4; for(x=0;x<=100;x++){ volta: i = ((float) random () / (float) RAND_MAX * 4); if((i==0&&a==1)||(i==1&&a==0)) i = ((float) random () / (float) RAND_MAX * 2) + 2; else if((i==2&&a==3)||(i==3&&a==2)) i = ((float) random () / (float) RAND_MAX * 2); if(n == i) goto volta; if(i==0&&buraco[0]!=0){ tabuleiro[buraco[0]][buraco[1]]=tabuleiro[buraco[0]-1][buraco[1]]; tabuleiro[buraco[0]-1][buraco[1]] = 15 ; //para cima buraco[0]--; n = 4; a = i; } else if(i==1&&buraco[0]!=3){ tabuleiro[buraco[0]][buraco[1]]=tabuleiro[buraco[0]+1][buraco[1]]; tabuleiro[buraco[0]+1][buraco[1]] = 15 ; //para baixo buraco[0]++; n = 4; a = i; } else if(i==2&&buraco[1]!=0){ tabuleiro[buraco[0]][buraco[1]]=tabuleiro[buraco[0]][buraco[1]-1]; tabuleiro[buraco[0]][buraco[1]-1] = 15 ; //para esquerda buraco[1]--; n = 4; a = i; } else if(i==3&&buraco[1]!=3){ tabuleiro[buraco[0]][buraco[1]]=tabuleiro[buraco[0]][buraco[1]+1]; tabuleiro[buraco[0]][buraco[1]+1] = 15 ; //para direita buraco[1]++; n = 4; a = i; } else { n = i; x--; } desenhafigura(); RedrawWindowXY (0,60); usleep(3000); } } // ------------------------------------------------------------------------------ // funcao: printversion // descricao: imprime a versao da aplicacao // in: nada // out: nada void printversion (void) { if (!strcmp (ProgName, "wmshuffle")){ fprintf (stderr, "\nwmShuffle v%s - The First Shuffle Game for WindowMaker... :) \n", WMSHUFFLE_VERSION); fprintf (stderr, "Copyright © 2000 Adriel Mota Ziesemer Jr.\n\n"); } } // ------------------------------------------------------------------------------ // funcao: usage // descricao: help da aplicacao // in: nada // out: nada void usage (void) { fprintf (stderr, "\nwmShuffle v%s - The First Shuffle Game for WindowMaker... :) \n", WMSHUFFLE_VERSION); fprintf (stderr, "Copyright © 2000 Adriel Mota Ziesemer Jr.\n"); fprintf (stderr, "============================================================\n"); fprintf (stderr, "usage:\n"); fprintf (stderr, "\t-h\tthis screen\n"); fprintf (stderr, "\t-v\tprint the version number\n"); fprintf (stderr, "\t-m\tmute mode (default=off)\n"); fprintf (stderr, "\t-s name\tchoose a skin \n"); fprintf (stderr, "\nexample: wmshuffle -m -s skin-feiticeira.xpm\n"); fprintf (stderr, "\n"); }