/* Ein Solitär-Spiel */
/* 11.4.93 die Version 0.8 by gum@assi.s-link.de */
/* am 10.6.94 zur Version 1.0 erweitert */

#include<stdlib.h>
#include<intuition/intuition.h>
#include<graphics/gfxmacros.h>
#include<workbench/startup.h>
#include<proto/graphics.h>
#include<proto/intuition.h>
#include<proto/dos.h>
#include<proto/exec.h>
#include<proto/icon.h>
#include<stdio.h>

#define DX(x) ((x)+10)
#define DY(y) ((y)+15)
#define SX(x) ((x)+win->LeftEdge-8)
#define SY(y) ((y)+win->TopEdge-8)

/* IMAGE DATA */

UWORD chip holeImg_data[] = {
	/* Plane 0 */
	0x07e0, 0x0000, 0x1818, 0x0000, 0x2004, 0x0000, 0x4000, 0x0000, 
	0x8000, 0x0000, 0x8000, 0x0000, 0x8000, 0x0000, 0x8000, 0x0000, 
	0x8000, 0x0000, 0x8000, 0x0000, 0x4000, 0x0000, 0x2000, 0x0000, 
	0x1000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
	0x0000, 0x0000, 
	/* Plane 1 */
	0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0002, 0x0000, 
	0x0001, 0x0000, 0x0001, 0x0000, 0x0001, 0x0000, 0x0001, 0x0000, 
	0x0001, 0x0000, 0x0001, 0x0000, 0x0002, 0x0000, 0x0004, 0x0000, 
	0x0818, 0x0000, 0x07e0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 
	0x0000, 0x0000
};

UWORD chip pinImg_data[2 * 16 * 2] = {
2016,0,6264,0,12412,0,25087,0,50175,32768,51199,49152,53247,
24576,53247,40960,65535,24576,65535,40960,32767,24576,16382,
40960,8189,16384,2026,32768,855,0,252,0,
0,0,2016,0,8184,0,16380,0,32766,0,32766,0,32766,0,32766,0,
32766,0,32766,0,16380,0,8184,0,2016,0,0,0,0,0,0,0};

UWORD chip pinSprite_data[] = {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

UWORD chip pinLeer[] = {
        0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

UWORD chip pinUp[] = {
	0x0000, 0x0000,	0x3c00, 0x0000,	0x4e00, 0x3c00,	0x9f00, 0x7e00,
	0xbf00, 0x7e00,	0xff00, 0x7e00,	0x7e00, 0x3c00,	0x3c00, 0x0000,
	0x0000, 0x0000,	0xffff, 0xffff
};


/* Images */

struct Image holeImg = {
   0, 0, 20, 17, 2, &holeImg_data[0], 3, 0, NULL};
struct Image pinImg = {
   0, 0, 19, 16, 2, &pinImg_data[0], 3, 0, NULL};
struct SimpleSprite pinSprite = {
   &pinSprite_data[0], 8, 0, 0, 0};

USHORT Palette[4]={
	0xccc, 0x000, 0xfff, 0x68a
};

static struct IntuiText ItemsText[] = {
  1,0,JAM1,1,1,NULL,(UBYTE *)"New Game",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Quit",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Cross",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Plus",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Fireplace",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Up Arrow",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Pyramid",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Diamond",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Solitaire",NULL,
  1,0,JAM1,1,1,NULL,(UBYTE *)"Undo",NULL };

static struct MenuItem Items[] = {
  &Items[9],0,0,108,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[0],NULL,78,NULL,0,
  NULL,0,18,108,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[1],NULL,81,NULL,0,
  &Items[3],0,0,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[2],NULL,49,NULL,0,
  &Items[4],0,9,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[3],NULL,50,NULL,0,
  &Items[5],0,18,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[4],NULL,51,NULL,0,
  &Items[6],0,27,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[5],NULL,52,NULL,0,
  &Items[7],0,36,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[6],NULL,53,NULL,0,
  &Items[8],0,45,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[7],NULL,54,NULL,0,
  NULL,0,54,116,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[8],NULL,55,NULL,0,
  &Items[1],0,9,108,9,ITEMENABLED+HIGHCOMP+ITEMTEXT+COMMSEQ,0,(APTR)&ItemsText[9],NULL,90,NULL,0 };

static struct Menu MenuStrip[] = {
  &MenuStrip[1],0,0,72,8,MENUENABLED,(BYTE *)"Project",&Items[0],0,0,0,0,
  NULL,72,0,56,8,MENUENABLED,(BYTE *)"Games",&Items[2],0,0,0,0 };


#define GADX 40
#define GADY 11
#define REQX 120
#define REQY 40

static SHORT okGadBorderPairs0[] = {
  -1,GADY,-1,-1,GADX-1,-1,GADX-2,-1,GADX-2,0,0,0,0,GADY-2 };

static SHORT okGadBorderPairs1[] = {
  GADX,-1,GADX,GADY,0,GADY,0,GADY-1,GADX-1,GADY-1,GADX-1,0 };

static SHORT ReqBorderPairs0[] = {
  -1,REQY,-1,-1,REQX-1,-1 };

static SHORT ReqBorderPairs1[] = {
  0,REQY,REQX,REQY,REQX,-1 };

static SHORT ReqBorderPairs2[] = {
  4,18,4,4,REQX-4,4 };

static SHORT ReqBorderPairs3[] = {
  5,18,REQX-4,18,REQX-4,5 };

static struct Border okGadBorder1 = {
  0,0,1,0,JAM1,6,(SHORT *)&okGadBorderPairs1[0],NULL };

static struct Border okGadBorder = {
  0,0,2,0,JAM1,7,(SHORT *)&okGadBorderPairs0[0],&okGadBorder1 };

static struct Border ReqBorder3 = {
  0,0,2,0,JAM1,3,(SHORT *)&ReqBorderPairs3[0],NULL };

static struct Border ReqBorder2 = {
  0,0,1,0,JAM1,3,(SHORT *)&ReqBorderPairs2[0],&ReqBorder3 };

static struct Border ReqBorder1 = {
  -1,-1,1,0,JAM1,3,(SHORT *)&ReqBorderPairs1[0],&ReqBorder2 };

static struct Border ReqBorder = {
  1,1,2,0,JAM1,3,(SHORT *)&ReqBorderPairs0[0],&ReqBorder1 };

static struct IntuiText RenderTexts = {
  1,0,JAM1,9,7,NULL,(UBYTE *)"No more Moves",NULL };

static struct IntuiText okGadget_text = {
  1,0,JAM1,12,2,NULL,(UBYTE *)"OK",NULL };

static struct Gadget okGadget = {
  NULL,45,26,GADX,GADY,
  GADGHCOMP,
  RELVERIFY|ENDGADGET,
  BOOLGADGET+REQGADGET,
  (APTR)&okGadBorder,NULL,
  &okGadget_text,NULL,NULL,0,NULL };

static struct Requester Requester = {
  NULL,60,34,REQX,REQY,0,0,&okGadget,&ReqBorder,&RenderTexts,
  NULL,0,NULL,NULL,NULL,NULL,NULL
};


/* Arrays */

short white[] = {
        0,139, 0,57,   1,139, 1,57,     1,56, 64,56,    1,57, 65,57,
        64,56, 64,1,   65,57, 65,1,     65,0, 161,0,    65,1, 161,1,
        163,56,226,56, 163,57,226,57,   64,140,64,195,  65,140,65,195,
        -1,-1
};
short black[] = {
        162,1,162,57,    163,1,163,55,    226,58,226,140,  227,58,227,141,
        226,140,162,140, 227,141,163,141, 162,140,162,196, 163,141,163,197,
        162,196,65,196,  163,197,65,197,  2,140,63,140,    2,141,63,141,
        -1,-1
};
short holesB[7][7] = {
        {-1,-1,1,1,1,-1,-1},  {-1,-1,1,1,1,-1,-1},  {1,1,1,1,1,1,1},
        {1,1,1,1,1,1,1},  {1,1,1,1,1,1,1},  {-1,-1,1,1,1,-1,-1},
        {-1,-1,1,1,1,-1,-1}
};
short holesX[] = {
        10,42,74,106,138,170,202
};
short holesY[] = {
        8,36,64,92,120,148,176
};

short sol[7][7];

short solMenu[7][7][7] = {
       {{-1,-1,0,0,0,-1,-1}, {-1,-1,0,1,0,-1,-1}, {0,0,1,1,1,0,0}, /* cross */
        {0,0,0,1,0,0,0}, {0,0,0,1,0,0,0}, {-1,-1,0,0,0,-1,-1},
        {-1,-1,0,0,0,-1,-1}},
       {{-1,-1,0,0,0,-1,-1}, {-1,-1,0,1,0,-1,-1}, {0,0,0,1,0,0,0}, /* plus */
        {0,1,1,1,1,1,0}, {0,0,0,1,0,0,0}, {-1,-1,0,1,0,-1,-1},
        {-1,-1,0,0,0,-1,-1}},
       {{-1,-1,1,1,1,-1,-1}, {-1,-1,1,1,1,-1,-1}, {0,0,1,1,1,0,0}, /* Fireplace */
        {0,0,1,0,1,0,0}, {0,0,0,0,0,0,0}, {-1,-1,0,0,0,-1,-1},
        {-1,-1,0,0,0,-1,-1}},
       {{-1,-1,0,1,0,-1,-1}, {-1,-1,1,1,1,-1,-1}, {0,1,1,1,1,1,0}, /*uparrow */
        {0,0,0,1,0,0,0}, {0,0,0,1,0,0,0}, {-1,-1,1,1,1,-1,-1},
        {-1,-1,1,1,1,-1,-1}},
       {{-1,-1,0,0,0,-1,-1}, {-1,-1,0,1,0,-1,-1}, {0,0,1,1,1,0,0}, /* pyramid */
        {0,1,1,1,1,1,0}, {1,1,1,1,1,1,1}, {-1,-1,0,0,0,-1,-1},
        {-1,-1,0,0,0,-1,-1}},
       {{-1,-1,0,1,0,-1,-1}, {-1,-1,1,1,1,-1,-1}, {0,1,1,1,1,1,0}, /*diamond */
        {1,1,1,0,1,1,1}, {0,1,1,1,1,1,0}, {-1,-1,1,1,1,-1,-1},
        {-1,-1,0,1,0,-1,-1}},
       {{-1,-1,1,1,1,-1,-1}, {-1,-1,1,1,1,-1,-1}, {1,1,1,1,1,1,1}, /*solitaire */
        {1,1,1,0,1,1,1}, {1,1,1,1,1,1,1}, {-1,-1,1,1,1,-1,-1},
        {-1,-1,1,1,1,-1,-1}}
};

/* vars */

#define WINFLAGS MOUSEBUTTONS|CLOSEWINDOW|MENUPICK

struct NewWindow nw = {
        0,0, 250,222, 1,0,
        WINFLAGS,
        WINDOWDEPTH|WINDOWCLOSE|WINDOWDRAG|SMART_REFRESH|REPORTMOUSE,
        NULL, NULL,
        "Pegged",
        NULL, NULL,
        30,30, 250,222,
        WBENCHSCREEN
};

struct undo_arr {
   short xalt,yalt,xneu,yneu;
} _undo_arr[34],*undo_ptr;

struct Window *win = 0L;
struct RastPort *rp = 0L;
int sprite = -1,hasPin = 0,altX,altY,aktSol = 6;
char *ver = "\0$VER: Pegged 1.0 (10.6.94) by Gunther Mannigel «gum@assi.s-link.de»";

/* Funktionen */

void setSpriteColors(struct ViewPort *vp,int sprite)
{
        int nr;
        
        if (sprite < 0) return;
        nr = 16 + (sprite>>1)*4;
        SetRGB4(vp,nr++,Palette[0]>>8,(Palette[0]>>4)&15,Palette[0]&15);
        SetRGB4(vp,nr++,Palette[1]>>8,(Palette[1]>>4)&15,Palette[1]&15);
        SetRGB4(vp,nr++,Palette[2]>>8,(Palette[2]>>4)&15,Palette[2]&15);
        SetRGB4(vp,nr++,Palette[3]>>8,(Palette[3]>>4)&15,Palette[3]&15);
}

void copySol(short to[7][7],short from[7][7])
{
        int x,y;
        
        for(y=0;y<7;y++)
           for(x=0;x<7;x++)
              to[x][y] = from[y][x];
              /* da hab' ich doch glatt die Indizes verwechselt */
}

void drawLines(struct RastPort *rp,short lines[],int color)
{
        SetAPen(rp,color);
        while(lines[0] >= 0 & lines[1] >= 0) {
                Move(rp,DX(lines[0]),DY(lines[1]));
                Draw(rp,DX(lines[2]),DY(lines[3]));
                lines += 4;
        }
}

void drawHoles(struct RastPort *rp)
{
        int x,y;
        
        for (y=0;y<7;y++) {
                for(x=0;x<7;x++) {
                   if (sol[x][y] >= 0)
                        if (sol[x][y])
                                DrawImage(rp,&pinImg,DX(holesX[x]),DY(holesY[y]));
                        else
                                DrawImage(rp,&holeImg,DX(holesX[x]),DY(holesY[y]));
                }
        }
}

int testeZug(int xa,int ya,int xe,int ye)
{
        if (xe < 0 || xe > 6 || ye < 0 || ye > 6) return(0);
        if (xe < 2 && (ye < 2 || ye > 4)) return(0);
        if (xe > 4 && (ye < 2 || ye > 4)) return(0);
        if (xa == xe) {
                if (abs(ya-ye) != 2) return(0);
                if (!sol[xa][(ya+ye)/2] || sol[xe][ye]) return(0);
                return(1);
        }
        if (ya == ye) {
                if (abs(xa-xe) != 2) return(0);
                if (!sol[(xa+xe)/2][ya] || sol[xe][ye]) return(0);
                return(1);
        }
        return(0);
}

int testeZug1(int x,int y)
{
        if (testeZug(x,y,x-2,y)) return(1);
        if (testeZug(x,y,x+2,y)) return(1);
        if (testeZug(x,y,x,y-2)) return(1);
        if (testeZug(x,y,x,y+2)) return(1);
        return(0);
}

int getHole(int x,short tab[],int lg,int rg) /* linke Grenze rechte Grenze */
{
        int i;
        
        for (i=0;i<7;i++) if (x > tab[i]+lg && x < tab[i]+rg) return(i);
        return(-1);
}

int testeFertig(void)
{
        int anzPins = 0;
        int x,y;
        
        /* hoffe, daß das wirklich schneller ist */
        for(x=2;x<5;x++)
                for(y=2;y<5;y++)
                        if (sol[x][y]) {
                                anzPins++;
                                if (testeZug1(x,y)) return(0);
                        }
        for(x=0;x<2;x++) {
                for(y=2;y<5;y++) {
                        if (sol[x][y]) {
                                anzPins++;
                                if (testeZug1(x,y)) return(0);
                        }
                        if (sol[6-x][y]) {
                                anzPins++;
                                if (testeZug1(6-x,y)) return(0);
                        }
                        if (sol[y][x]) {
                                anzPins++;
                                if (testeZug1(y,x)) return(0);
                        }
                        if (sol[y][6-x]) {
                                anzPins++;
                                if (testeZug1(y,6-x)) return(0);
                        }
                }
        }
        return((anzPins == 1 && sol[3][3])?2:1);
}

void LMBDown(int x,int y)
{
        int x1,y1;
        
        /* Überprüfen, ob akt Mauspos über einem Lock ist*/
        x1 = getHole(x,holesX,DX(-8),DX(24));
        y1 = getHole(y,holesY,DY(-8),DY(20));
        if (x1 < 0 || y1 < 0 || (x1 < 2 || x1 > 4)&&(y1 < 2 || y1 > 4)) return;
        /* Überprüfen, ob Loch besetzt */
        if (!sol[x1][y1]) return;
        /* MoveSprite an Mauspos */
        MoveSprite(&win->WScreen->ViewPort,&pinSprite,SX(x),SY(y));
        /* ChangeSprite von Leer auf Pin */
        ChangeSprite(&win->WScreen->ViewPort,&pinSprite,(APTR)&pinUp);
        /* DrawImage für das jetzte leere Loch */
        DrawImage(rp,&holeImg,DX(holesX[x1]),DY(holesY[y1]));
        hasPin = 1;
        sol[x1][y1] = 0;
        altX = x1;
        altY = y1;
        ModifyIDCMP(win,WINFLAGS|MOUSEMOVE);
}

void LMBUp(int x,int y)
{
        int x1,y1,t;
        
        /* hasPin = true ? */
        if (!hasPin) return;
        /* Überprüfen, ob aktuelle Mauspos über einem Loch ist */
        /* teste, ob Zug legal */
        x1 = getHole(x,holesX,DX(-8),DX(24));
        y1 = getHole(y,holesY,DY(-8),DY(20));
        if (!(x1 < 0 || y1 < 0 || (x1 < 2 || x1 > 4)&&(y1 < 2 || y1 > 4))) {
           if (testeZug(altX,altY,x1,y1)) {
        /* legal */
              /* DrawImage() für das Übersprungene Loch */
              DrawImage(rp,&holeImg,DX(holesX[(altX+x1)>>1]),DY(holesY[(altY+y1)>>1]));
              /* sol[][] = 0 dito */
              sol[(altX+x1)>>1][(altY+y1)>>1] = 0;
              /* DrawImage() für Zielloch */
              DrawImage(rp,&pinImg,DX(holesX[x1]),DY(holesY[y1]));
              /* sol[][] = 1 dito */
              sol[x1][y1] = 1;
              /* ChangeSprite zum Löschen */
              ChangeSprite(&win->WScreen->ViewPort,&pinSprite,(APTR)&pinLeer);
              hasPin = 0;
              ModifyIDCMP(win,WINFLAGS);
              /* in die Undo-Liste eintragen */
              undo_ptr->xalt = altX;
              undo_ptr->yalt = altY;
              undo_ptr->xneu = x1;
              undo_ptr->yneu = y1;
              undo_ptr++;
              /* test auf Ende des Spieles */
              if (t = testeFertig()) {
                RenderTexts.IText = ((t == 2)?"  You Win !":"no more moves");
                Request(&Requester,win);
                Wait(1L<<win->UserPort->mp_SigBit);
              }
              return;
           }
        }
        /* illegal */
           sol[altX][altY] = 1;
           /* DrawImage() dito */
           DrawImage(rp,&pinImg,DX(holesX[altX]),DY(holesY[altY]));
           hasPin = 0;
           /* ChangeSprite z. Löschen */
           ChangeSprite(&win->WScreen->ViewPort,&pinSprite,(APTR)&pinLeer);
           ModifyIDCMP(win,WINFLAGS);
        /* */
}

void newSol(int t)
{
        aktSol = t;
        copySol(sol,solMenu[aktSol]);
        drawHoles(rp);
        undo_ptr = _undo_arr;
}

void do_undo(void)
{
   if (hasPin || undo_ptr == _undo_arr) return; /* nur zur Sicherheit */
   undo_ptr--;
   /* Pin an Endposition löschen */
   sol[undo_ptr->xneu][undo_ptr->yneu] = 0;
   DrawImage(rp,&holeImg,DX(holesX[undo_ptr->xneu]),DY(holesY[undo_ptr->yneu]));
   /* übersprungenen Pin wieder setzen */
   sol[(undo_ptr->xneu+undo_ptr->xalt)>>1][(undo_ptr->yneu+undo_ptr->yalt)>>1] = 1;
   DrawImage(rp,&pinImg,DX(holesX[(undo_ptr->xneu+undo_ptr->xalt)>>1]),DY(holesY[(undo_ptr->yneu+undo_ptr->yalt)>>1]));
   /* Pin an Ursprungsposition wieder setzen */
   sol[undo_ptr->xalt][undo_ptr->yalt] = 1;
   DrawImage(rp,&pinImg,DX(holesX[undo_ptr->xalt]),DY(holesY[undo_ptr->yalt]));
}

void doMenu(int code)
{
        switch(code) {
                case 0xf801: newSol(0);
                             break;
                case 0xf821: newSol(1);
                             break;
                case 0xf841: newSol(2);
                             break;
                case 0xf861: newSol(3);
                             break;
                case 0xf881: newSol(4);
                             break;
                case 0xf8a1: newSol(5);
                             break;
                case 0xf8c1: newSol(6);
                             break;
                case 0xf800: /* new */
                             newSol(aktSol);
                             break;
                case 0xf820: /* Undo */
                             if (!hasPin) do_undo();
                             break;
                case 0xf840: /* Quit */
                             break;
        }
}

int askClose(void)
{
        return(0);
}

int event(void)
{
        struct IntuiMessage *imsg;
        int mx,my,endep = 1;
        ULONG class;
        USHORT code;
        
        Wait(1<<win->UserPort->mp_SigBit);
        while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort)) {
                class = imsg->Class;
                code = imsg->Code;
                mx = imsg->MouseX;
                my = imsg->MouseY;
                ReplyMsg((struct Message *)imsg);
                switch(class) {
                        case MOUSEBUTTONS: if (code == SELECTDOWN) LMBDown(mx,my);
                                           if (code == SELECTUP) LMBUp(mx,my);
                                           break;
                        case CLOSEWINDOW:  endep = askClose();
                                           break;
                        case MENUPICK:     doMenu(code);
                                           if (code == 0xf840) endep = 0;
                                           break;
                        case MOUSEMOVE:    if (sprite != -1)
                                                 MoveSprite(&win->WScreen->ViewPort,&pinSprite,SX(mx),SY(my));
                                           break;
                }
        }
        if (!endep)
                while(imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
                        ReplyMsg((struct Message *)imsg);
        return(endep);
}

int openWin(void)
{
        win = OpenWindow(&nw);
        if (!win) return(-1);
        rp = win->RPort;
        drawLines(rp,white,2);
        drawLines(rp,black,1);
        drawHoles(rp);
        SetMenuStrip(win,MenuStrip);
        sprite = GetSprite(&pinSprite,2);
        setSpriteColors(&win->WScreen->ViewPort,sprite);
        return(0);
}

void closeWin(void)
{
        struct Message *msg;
        
        /* IntuiMessages löschen */
        if (win) {
                while(msg = GetMsg(win->UserPort)) ReplyMsg(msg);
        }
        /* Window schliessen */
        if (sprite != -1) FreeSprite(sprite);
        sprite = -1;
        CloseWindow(win);
        win = 0L; rp = 0L;
}

char *liesNum(char *p,int *n)
{
        int i=0;
        
        while (*p >= '0' && *p <= '9') i = 10*i + *p++ -'0';
        *n = i;
        return(p);
}

struct IntuitionBase *IntuitionBase = 0L;
struct Library *IconBase = 0L;

void setGame(int argc,char *argv[])
{
        struct WBStartup *wb;
        struct DiskObject *db;
        char *p;
        int flag = 0,x,y;
        
        if (argc) {
                if (argc == 2 && *argv[1] >= '0' && *argv[1] < '7')
                     aktSol = *argv[1]-'0';
                else flag = 1;
        } else {
            flag = 1;
            wb = (struct WBStartup *)argv;
            if (IconBase = OpenLibrary("icon.library",33L)) {
               if (db = GetDiskObject(wb->sm_ArgList->wa_Name)) {
                  if (p = FindToolType(db->do_ToolTypes,"WINDOW")) {
                        p = liesNum(p,&x);
                        if (*p == ',') {
                                p++;
                                p = liesNum(p,&y);
                                nw.LeftEdge = x;
                                nw.TopEdge = y;
                        }
                  }
                  if (p = FindToolType(db->do_ToolTypes,"GAME"))
                    if (*p >= '0' && *p < '7') {
                        aktSol = *p-'0';
                        flag = 0;
                    }
                  FreeDiskObject(db);
               }
               CloseLibrary(IconBase);
            }
        }
        copySol(sol,solMenu[aktSol]);
}
        
void main(int argc,char *argv[])
{
        IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",33L);
        undo_ptr = _undo_arr;
        setGame(argc,argv);
        if (!IntuitionBase) exit(10);
        if (openWin()) exit(10);
        while (event()) ;
        closeWin();
}
