/*************************************************
/*
/* This code © 1989 by W. Roger uzun
/* This code is for Lattice 5.0X
/* compile using - lc -v -cf -L -O Rubik (for 68000 version)
/* or lc -v -cf -L -m2 -O -dCPU020 Rubik (for 68020 version)
/*
/*************************************************/

#include <proto/exec.h>
#include <exec/memory.h>
#ifdef CPU020
#include <exec/execbase.h>
#endif
#include <proto/graphics.h>
#include <proto/intuition.h>
#include <proto/dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define WINDOWSIGNAL (1L<<(wG->UserPort->mp_SigBit))


/* data for cube */

int aborted=0;
char q[50];
BPTR fh;
int PlotOn=1;
ULONG speed=0L;
ULONG *xchg1,*xchg2;
ULONG upperface[9],leftface[9],frontface[9],rightface[9],backface[9];
ULONG downface[9],x[9],y[9];
int onepoint=0;
int a,c,e,k;

UWORD chip PickPtr[]={
0x0000,0x0000,      /* Position pad bytes */
0x0000,0x7090,0x0000,0x4810,0x0000,0x4892,0x0000,0x7094,
0x0000,0x4098,0x0000,0x4094,0x0000,0x4092,0x0000,0x0000,

0x0000,0x0000,      /* Linkage pad bytes */

};

/* sizebody=           0x20 HEX     ....32 decimal */
/* width=              0x10 HEX     ....16 decimal */
/* Width in WORDS=     0x1 HEX      ....1 decimal */
/* height=             0x8 HEX     ....8 decimal */
/* number of planes=   0x2 HEX */

ULONG __regargs *MatchBox(int,int);
void DoALayer(void);
void SolveCorners(void);
void Parse(void);
void DoVertical(void);
void DoHorizontal(void);
void Space2(void);
void FinishLayers(void);
void FaceRotate(void);
void Plot(void);
void __regargs PrintText(int,int,char *);
void Init(void);
void main(void);
void __regargs HandleEvent(APTR);
void DoQuit(void);
void DoSolve(void);
void DoRight(void);
void ClearUpperCorners(void);
void DoLeft(void);
void DoUp(void);
void DoDown(void);
void DoAbout(void);
void DoForward(void);
void DoBack(void);

int done=0;
struct RastPort *rpG;
struct TextFont *myTextFont;
struct Screen *sC;
struct Window *wG;

struct TextAttr TOPAZ80 = {
	(STRPTR)"topaz.font",
	TOPAZ_EIGHTY,0,0
};

struct NewScreen NewScreenStructure = {
	0,0,	/* screen XY origin relative to View */
	320,200,	/* screen width and height */
	3,	/* screen depth (number of bitplanes) */
	0,1,	/* detail and block pens */
	NULL,	/* display modes for this screen */
	CUSTOMSCREEN,	/* screen type */
	&TOPAZ80,	/* pointer to default screen font */
	"Rubiks Cube",	/* screen title */
	NULL,	/* first in list of custom screen gadgets */
	NULL	/* pointer to custom BitMap structure */
};

#define NEWSCREENSTRUCTURE NewScreenStructure

USHORT Palette[] = {
	0x0777,	/* color #0 */
	0x0000,	/* color #1 */
	0x00B0,	/* color #2 */
	0x0F82,	/* color #3 */
	0x0FFF,	/* color #4 */
	0x006F,	/* color #5 */
	0x0F33,	/* color #6 */
	0x0FF0	/* color #7 */
#define PaletteColorCount 8
};

#define PALETTE Palette

UBYTE MyMyGadget3SIBuff[20]={'2','5','5','\0'};
struct StringInfo MyMyGadget3SInfo = {
	MyMyGadget3SIBuff,	/* buffer where text will be edited */
	NULL,	/* optional undo buffer */
	0,	/* character position in buffer */
	4,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

SHORT MyBorderVectors1[] = {
	0,0,
	71,0,
	71,13,
	0,13,
	0,0
};
struct Border MyBorder1 = {
	-1,-1,	/* XY origin relative to container TopLeft */
	3,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	MyBorderVectors1,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

struct Gadget MyGadget3 = {
	NULL,	/* next gadget */
	42,65,	/* origin XY of hit box relative to window TopLeft */
	70,12,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	STRGADGET,	/* gadget type flags */
	(APTR)&MyBorder1,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	NULL,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&MyMyGadget3SInfo,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

UBYTE MyMyGadget2SIBuff[20];
struct StringInfo MyMyGadget2SInfo = {
	MyMyGadget2SIBuff,	/* buffer where text will be edited */
	NULL,	/* optional undo buffer */
	0,	/* character position in buffer */
	20,	/* maximum number of characters to allow */
	0,	/* first displayed character buffer position */
	0,0,0,0,0,	/* Intuition initialized and maintained variables */
	0,	/* Rastport of gadget */
	0,	/* initial value for integer gadgets */
	NULL	/* alternate keymap (fill in if you set the flag) */
};

SHORT MyBorderVectors2[] = {
	0,0,
	147,0,
	147,13,
	0,13,
	0,0
};
struct Border MyBorder2 = {
	-1,-1,	/* XY origin relative to container TopLeft */
	3,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	MyBorderVectors2,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

struct Gadget MyGadget2 = {
	&MyGadget3,	/* next gadget */
	8,33,	/* origin XY of hit box relative to window TopLeft */
	146,12,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	STRGADGET,	/* gadget type flags */
	(APTR)&MyBorder2,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	NULL,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	(APTR)&MyMyGadget2SInfo,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

SHORT MyBorderVectors3[] = {
	0,0,
	46,0,
	46,22,
	0,22,
	0,0
};
struct Border MyBorder3 = {
	-1,-1,	/* XY origin relative to container TopLeft */
	3,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	MyBorderVectors3,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

struct IntuiText MyIText1 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	14,7,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"OK",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct Gadget MyGadget1 = {
	&MyGadget2,	/* next gadget */
	53,106,	/* origin XY of hit box relative to window TopLeft */
	45,21,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&MyBorder3,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&MyIText1,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

#define MyGadgetList1 MyGadget1

struct IntuiText MyIText3 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	56,54,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"Speed",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct IntuiText MyIText2 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	41,17,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"File Name",	/* pointer to text */
	&MyIText3	/* next IntuiText structure */
};

#define MyIntuiTextList1 MyIText2

struct NewWindow MyNewWindowStructure1 = {
	80,30,	/* window XY origin relative to TopLeft of screen */
	160,135,	/* window width and height */
	0,1,	/* detail and block pens */
	GADGETUP,	/* IDCMP flags */
	ACTIVATE+NOCAREREFRESH,	/* other window flags */
	&MyGadget1,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	"Solve The Cube!",	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	5,5,	/* minimum width and height */
	-1,-1,	/* maximum width and height */
	CUSTOMSCREEN	/* destination screen type */
};

struct IntuiText IText1 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Quit",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem MenuItem4 = {
	NULL,	/* next MenuItem structure */
	0,24,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	80,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText1,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'Q',	/* alternate command-key */
	NULL,	/* SubItem list */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText2 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Solve",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem MenuItem3 = {
	&MenuItem4,	/* next MenuItem structure */
	0,16,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	80,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText2,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'S',	/* alternate command-key */
	NULL,	/* SubItem list */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText3b = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Vert",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem8 = {
	NULL, /* next SubItem structure */
	65,48,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText3b,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'C',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};


struct IntuiText IText3a = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Horiz",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem7 = {
	&SubItem8, /* next SubItem structure */
	65,40,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText3a,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'H',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};



struct IntuiText IText3 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Back",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem6 = {
	&SubItem7, /* next SubItem structure */
	65,32,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText3,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'B',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText4 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Forward",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem5 = {
	&SubItem6,	/* next SubItem structure */
	65,24,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText4,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'F',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText5 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Down",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem4 = {
	&SubItem5,	/* next SubItem structure */
	65,16,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText5,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'D',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText6 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Up",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem3 = {
	&SubItem4,	/* next SubItem structure */
	65,8,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText6,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'U',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText7 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Left",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem2 = {
	&SubItem3,	/* next SubItem structure */
	65,0,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText7,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'L',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText8 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Right",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem SubItem1 = {
	&SubItem2,	/* next SubItem structure */
	65,-8,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	96,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText8,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'R',	/* alternate command-key */
	NULL,	/* no SubItem list for SubItems */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText9 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"Move",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem MenuItem2 = {
	&MenuItem3,	/* next MenuItem structure */
	0,8,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	80,8,	/* hit box width and height */
	ITEMTEXT+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText9,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	NULL,	/* alternate command-key */
	&SubItem1,	/* SubItem list */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct IntuiText IText10 = {
	3,1,COMPLEMENT,	/* front and back text pens, drawmode and fill byte */
	0,0,	/* XY origin relative to container TopLeft */
	&TOPAZ80,	/* font pointer or NULL for default */
	"About",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct MenuItem MenuItem1 = {
	&MenuItem2,	/* next MenuItem structure */
	0,0,	/* XY of Item hitbox relative to TopLeft of parent hitbox */
	80,8,	/* hit box width and height */
	ITEMTEXT+COMMSEQ+ITEMENABLED+HIGHCOMP,	/* Item flags */
	0,	/* each bit mutually-excludes a same-level Item */
	(APTR)&IText10,	/* Item render  (IntuiText or Image or NULL) */
	NULL,	/* Select render */
	'A',	/* alternate command-key */
	NULL,	/* SubItem list */
	MENUNULL	/* filled in by Intuition for drag selections */
};

struct Menu Menu1 = {
	NULL,	/* next Menu structure */
	0,0,	/* XY origin of Menu hit box relative to screen TopLeft */
	63,0,	/* Menu hit box width and height */
	MENUENABLED,	/* Menu flags */
	"Project",	/* text of Menu name */
	&MenuItem1	/* MenuItem linked list pointer */
};

#define MenuList1 Menu1

struct NewWindow NewWindowStructure1 = {
	0,0,	/* window XY origin relative to TopLeft of screen */
	320,200,	/* window width and height */
	0,1,	/* detail and block pens */
	MOUSEBUTTONS|MENUPICK,	/* IDCMP flags */
	BACKDROP+BORDERLESS+ACTIVATE+NOCAREREFRESH,	/* other window flags */
	NULL,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	"Rubiks Cube",	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	320,200,	/* minimum width and height */
	-1,-1,	/* maximum width and height */
	CUSTOMSCREEN	/* destination screen type */
};

SHORT BorderVectors1[] = {
	0,0,
	51,0,
	51,25,
	0,25,
	0,0
};
struct Border Border1 = {
	-1,-1,	/* XY origin relative to container TopLeft */
	3,0,JAM1,	/* front pen, back pen and drawmode */
	5,	/* number of XY vectors */
	BorderVectors1,	/* pointer to XY vectors */
	NULL	/* next border in list */
};

struct IntuiText IText11 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	16,10,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"OK",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct Gadget Gadget1 = {
	NULL,	/* next gadget */
	54,101,	/* origin XY of hit box relative to window TopLeft */
	50,24,	/* hit box width and height */
	NULL,	/* gadget flags */
	RELVERIFY,	/* activation flags */
	BOOLGADGET,	/* gadget type flags */
	(APTR)&Border1,	/* gadget border or image to be rendered */
	NULL,	/* alternate imagery for selection */
	&IText11,	/* first IntuiText structure */
	NULL,	/* gadget mutual-exclude long word */
	NULL,	/* SpecialInfo structure */
	NULL,	/* user-definable data */
	NULL	/* pointer to user-definable data */
};

#define GadgetList2 Gadget1

struct IntuiText IText14 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	33,40,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"Version 0.0",	/* pointer to text */
	NULL	/* next IntuiText structure */
};

struct IntuiText IText13 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	19,61,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"By W. Roger Uzun",	/* pointer to text */
	&IText14	/* next IntuiText structure */
};

struct IntuiText IText12 = {
	3,0,JAM2,	/* front and back text pens, drawmode and fill byte */
	31,15,	/* XY origin relative to container TopLeft */
	NULL,	/* font pointer or NULL for default */
	"Rubiks Solve",	/* pointer to text */
	&IText13	/* next IntuiText structure */
};

#define IntuiTextList2 IText12

struct NewWindow NewWindowStructure2 = {
	80,30,	/* window XY origin relative to TopLeft of screen */
	160,135,	/* window width and height */
	0,1,	/* detail and block pens */
	GADGETUP,	/* IDCMP flags */
	ACTIVATE+NOCAREREFRESH,	/* other window flags */
	&Gadget1,	/* first gadget in gadget list */
	NULL,	/* custom CHECKMARK imagery */
	"About Rubiks Solve",	/* window title */
	NULL,	/* custom screen pointer */
	NULL,	/* custom bitmap */
	160,135,	/* minimum width and height */
	-1,-1,	/* maximum width and height */
	CUSTOMSCREEN	/* destination screen type */
};


void __regargs HandleEvent(object)
APTR object;
{
  if (object == (APTR)&MenuItem1) { DoAbout(); return; }
  if (object == (APTR)&SubItem1) { DoRight(); return; }
  if (object == (APTR)&SubItem2) { DoLeft(); return; }
  if (object == (APTR)&SubItem3) { DoUp(); return; }
  if (object == (APTR)&SubItem4) { DoDown(); return; }
  if (object == (APTR)&SubItem5) { DoForward(); return; }
  if (object == (APTR)&SubItem6) { DoBack(); return; }
  if (object == (APTR)&SubItem7) { DoHorizontal(); return; }
  if (object == (APTR)&SubItem8) { DoVertical(); return; }
  if (object == (APTR)&MenuItem3) { DoSolve(); return; }
  if (object == (APTR)&MenuItem4) { DoQuit(); return; }
}
#define HANDLEEVENT HandleEvent

/* end of PowerWindows source generation */



ULONG __regargs *MatchBox(x,y)
int x,y;
{
 ULONG *temp;
 
 if ((x >= 90)&&(x <= 109)&&(y >= 16)&&(y <= 32))
  temp = &upperface[0];
 else if ((x >= 111)&&(x <= 130)&&(y >= 16)&&(y <= 32))
  temp = &upperface[1];
 else if ((x >= 132)&&(x <= 151)&&(y >= 16)&&(y <= 32))
  temp = &upperface[2];
 else if ((x >= 90)&&(x <= 109)&&(y >= 34)&&(y <= 50))
  temp = &upperface[3];
 else if ((x >= 111)&&(x <= 130)&&(y >= 34)&&(y <= 50))
  temp = &upperface[4];
 else if ((x >= 132)&&(x <= 151)&&(y >= 34)&&(y <= 50))
  temp = &upperface[5];
 else if ((x >= 90)&&(x <= 109)&&(y >= 52)&&(y <= 68))
  temp = &upperface[6];
 else if ((x >= 111)&&(x <= 130)&&(y >= 52)&&(y <= 68))
  temp = &upperface[7];
 else if ((x >= 132)&&(x <= 151)&&(y >= 52)&&(y <= 68))
  temp = &upperface[8];
 else if ((x >= 20)&&(x <= 39)&&(y >= 76)&&(y <= 92))
  temp = &leftface[0];
 else if ((x >= 41)&&(x <= 60)&&(y >= 76)&&(y <= 92))
  temp = &leftface[1];
 else if ((x >= 62)&&(x <= 81)&&(y >= 76)&&(y <= 92))
  temp = &leftface[2];
 else if ((x >= 20)&&(x <= 39)&&(y >= 94)&&(y <= 110))
  temp = &leftface[3];
 else if ((x >= 41)&&(x <= 60)&&(y >= 94)&&(y <= 110))
  temp = &leftface[4];
 else if ((x >= 62)&&(x <= 81)&&(y >= 94)&&(y <= 110))
  temp = &leftface[5];
 else if ((x >= 20)&&(x <= 39)&&(y >= 112)&&(y <= 128))
  temp = &leftface[6];
 else if ((x >= 41)&&(x <= 60)&&(y >= 112)&&(y <= 128))
  temp = &leftface[7];
 else if ((x >= 62)&&(x <= 81)&&(y >= 112)&&(y <= 128))
  temp = &leftface[8];
 else if ((x >= 90)&&(x <= 109)&&(y >= 76)&&(y <= 92))
  temp = &frontface[0];
 else if ((x >= 111)&&(x <= 130)&&(y >= 76)&&(y <= 92))
  temp = &frontface[1];
 else if ((x >= 132)&&(x <= 151)&&(y >= 76)&&(y <= 92))
  temp = &frontface[2];
 else if ((x >= 90)&&(x <= 109)&&(y >= 94)&&(y <= 110))
  temp = &frontface[3];
 else if ((x >= 111)&&(x <= 130)&&(y >= 94)&&(y <= 110))
  temp = &frontface[4];
 else if ((x >= 132)&&(x <= 151)&&(y >= 94)&&(y <= 110))
  temp = &frontface[5];
 else if ((x >= 90)&&(x <= 109)&&(y >= 112)&&(y <= 128))
  temp = &frontface[6];
 else if ((x >= 111)&&(x <= 130)&&(y >= 112)&&(y <= 128))
  temp = &frontface[7];
 else if ((x >= 132)&&(x <= 151)&&(y >= 112)&&(y <= 128))
  temp = &frontface[8];
 else if ((x >= 160)&&(x <= 179)&&(y >= 76)&&(y <= 92))
  temp = &rightface[0];
 else if ((x >= 181)&&(x <= 200)&&(y >= 76)&&(y <= 92))
  temp = &rightface[1];
 else if ((x >= 202)&&(x <= 221)&&(y >= 76)&&(y <= 92))
  temp = &rightface[2];
 else if ((x >= 160)&&(x <= 179)&&(y >= 94)&&(y <= 110))
  temp = &rightface[3];
 else if ((x >= 181)&&(x <= 200)&&(y >= 94)&&(y <= 110))
  temp = &rightface[4];
 else if ((x >= 202)&&(x <= 221)&&(y >= 94)&&(y <= 110))
  temp = &rightface[5];
 else if ((x >= 160)&&(x <= 179)&&(y >= 112)&&(y <= 128))
  temp = &rightface[6];
 else if ((x >= 181)&&(x <= 200)&&(y >= 112)&&(y <= 128))
  temp = &rightface[7];
 else if ((x >= 202)&&(x <= 221)&&(y >= 112)&&(y <= 128))
  temp = &rightface[8];
 else if ((x >= 230)&&(x <= 249)&&(y >= 76)&&(y <= 92))
  temp = &backface[0];
 else if ((x >= 251)&&(x <= 270)&&(y >= 76)&&(y <= 92))
  temp = &backface[1];
 else if ((x >= 272)&&(x <= 291)&&(y >= 76)&&(y <= 92))
  temp = &backface[2];
 else if ((x >= 230)&&(x <= 249)&&(y >= 94)&&(y <= 110))
  temp = &backface[3];
 else if ((x >= 251)&&(x <= 270)&&(y >= 94)&&(y <= 110))
  temp = &backface[4];
 else if ((x >= 272)&&(x <= 291)&&(y >= 94)&&(y <= 110))
  temp = &backface[5];
 else if ((x >= 230)&&(x <= 249)&&(y >= 112)&&(y <= 128))
  temp = &backface[6];
 else if ((x >= 251)&&(x <= 270)&&(y >= 112)&&(y <= 128))
  temp = &backface[7];
 else if ((x >= 272)&&(x <= 291)&&(y >= 112)&&(y <= 128))
  temp = &backface[8];
 else if ((x >= 90)&&(x <= 109)&&(y >= 136)&&(y <= 152))
  temp = &downface[0];
 else if ((x >= 111)&&(x <= 130)&&(y >= 136)&&(y <= 152))
  temp = &downface[1];
 else if ((x >= 132)&&(x <= 151)&&(y >= 136)&&(y <= 152))
  temp = &downface[2];
 else if ((x >= 90)&&(x <= 109)&&(y >= 154)&&(y <= 170))
  temp = &downface[3];
 else if ((x >= 111)&&(x <= 130)&&(y >= 154)&&(y <= 170))
  temp = &downface[4];
 else if ((x >= 132)&&(x <= 151)&&(y >= 154)&&(y <= 170))
  temp = &downface[5];
 else if ((x >= 90)&&(x <= 109)&&(y >= 172)&&(y <= 188))
  temp = &downface[6];
 else if ((x >= 111)&&(x <= 130)&&(y >= 172)&&(y <= 188))
  temp = &downface[7];
 else if ((x >= 132)&&(x <= 151)&&(y >= 172)&&(y <= 188))
  temp = &downface[8];
 else
  temp = 0L;
 return(temp);
}

void DoQuit()
{
 done = 1;
}


void Space2()
{
 SetAPen(rpG,0L);
 RectFill(rpG,0L,190L,319L,199L);
 Move(rpG,0L,197L);
 SetAPen(rpG,1L);
}

void Parse()
{
 int i;
 int j,count;
 char c;

 PlotOn = 0;
 for(i=0;i<strlen(q);i++)
  {
   c = q[i];
   count = 1;
   if (q[i+1] == 39)
    {
     i++;
     count = 3;
    }
   switch(c)
    {
     case 'r' : for(j=0;((j<count)&&(!aborted));j++)
		 DoRight();
		break; 
     case 'l' : for(j=0;((j<count)&&(!aborted));j++)
		 DoLeft();
		break; 
     case 'u' : for(j=0;((j<count)&&(!aborted));j++)
		 DoUp();
		break; 
     case 'd' : for(j=0;((j<count)&&(!aborted));j++)
		 DoDown();
		break; 
     case 'f' : for(j=0;((j<count)&&(!aborted));j++)
		 DoForward();
		break; 
     case 'b' : for(j=0;((j<count)&&(!aborted));j++)
		 DoBack();
		break; 
     case 'h' : for(j=0;((j<count)&&(!aborted));j++)
		 DoHorizontal();
		break; 
     case 'c' : for(j=0;((j<count)&&(!aborted));j++)
		 DoVertical();
		break; 
    }
  if (aborted)
   {
    PlotOn = 1;
    return;
   }
  }
 PlotOn = 1;
 Plot();
 if (speed > 15L)
  Delay((speed/4L));
}

void ClearUpperCorners()
{
 char str[80];

   Space2();
   strcpy(str,"No Solved Sides - clear upper corners");
   Text(rpG,str,strlen(str));
   strcat(str,"\n");
   if (fh)
    Write(fh,str,strlen(str));
   for(k=0;k<4;k++)
    {
a1660:
     if ((upperface[8] != a)&&(frontface[2] != a)&&(rightface[0] != a))
      {
       strcat(q,"c\n");
       if (fh)
        Write(fh,q,strlen(q));
       goto a1820;
      }
     else
      {/* Move out of top layer */
a1720:
       q[0] = '\0';
       if ((frontface[8] == a)||(rightface[6] == a)||(downface[2] == a))
        {
         strcpy(q,"d");
        }
       if (q[0] != '\0')
        {
         if (fh)
          Write(fh,q,strlen(q));
         Parse();if (aborted) return;
         goto a1720;
        }
a1790:
       strcpy(q,"r'd'rd");
       if (fh)
        Write(fh,q,strlen(q));
       Parse();if (aborted) return;
       q[0] = '\0';
       goto a1660;
      }
a1820:
       Parse();if (aborted) return;
       q[0] = '\0';
    }
}


void SolveCorners()
{
 int temp;

 k = 0;
a1850:
 temp = 0;
 a = upperface[4];
 c = frontface[4];
 e = rightface[4];
 if ((downface[2] == e)&&(frontface[8] == c))
  strcpy(q,"dfd'f'");
 else if ((frontface[8] == e)&&(rightface[6] == c))
  strcpy(q,"r'drfddf'");
 else if ((rightface[6] == e)&&(downface[2] == c))
  strcpy(q,"d'r'dr");
 if (q[0] != '\0')
  k++;
 else
  {
   temp = 1;
   strcpy(q,"d'");
  }
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 if (!(temp))
  {
   strcpy(q,"c");
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   if (k == 4)
    {
     return;
    }
  }
 q[0] = '\0';
 goto a1850;
}

void DoALayer()
{ /* 2340 stuff here */
 char str[80];
 int l;

 strcpy(str,"Layer unfinished, Clear edges");
 Space2();
 Text(rpG,str,strlen(str));
 strcat(str,"\n");
 if (fh)
  Write(fh,str,strlen(str));
 strcpy(str,"begin by removing centers from upper layer\n");
 if (fh)
  Write(fh,str,strlen(str));
 q[0] = '\0';
 for(l=1;l<5;l++)
  {
a2390:
   if ((upperface[4] == upperface[7])||(upperface[4] == frontface[1]))
    strcpy(q,"f'u'dcf");
   if (q[0] == '\0')
    {
     strcpy(q,"c");
    }
   else
    {
     if (fh)
      Write(fh,q,strlen(q));
     Parse();if (aborted) return;
     q[0] = '\0';
     goto a2390;
    }
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';
  }
 strcpy(str,"\nNow replace centers from center layer\n");
 if (fh)
  Write(fh,str,strlen(str));
 strcpy(str,"Edges Cleared - Solve Edges");
 Space2();
 Text(rpG,str,strlen(str));
 strcat(str,"\n");
 if (fh)
  Write(fh,str,strlen(str));
 q[0] = '\0';
 for(l=1;l<5;l++)
  {
   if ((upperface[4] == upperface[7])&&(frontface[1] == frontface[0]))
    {
     goto a2600;
    }
a2520:
   if ((upperface[4] == leftface[5])&&(frontface[3] == frontface[0]))
    strcpy(q,"u'r'ud'f");
   if ((upperface[4] == frontface[3])&&(leftface[5] == frontface[0]))
    strcpy(q,"u'ruuddl'u'");
   if ((upperface[4] == downface[1])&&(frontface[7] == frontface[0]))
    strcpy(q,"rl'fflr'");
   if ((upperface[4] == frontface[7])&&(downface[1] == frontface[0]))
    strcpy(q,"drl'f'lr'");
   if (q[0] != '\0')
    goto a2590;
   strcpy(q,"cu'");
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';
   goto a2520;
a2590:
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
a2600:
   strcpy(q,"c");
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';   
  } 
}

void FinishLayers()
{
 char str[80];
 int i,temp,l;

 temp = 0;
 for(i=0;i<9;i++)
  {
    if (upperface[i] != upperface[4])
     temp = 1;
  }
 if (temp)
  DoALayer(); /* this is 2340 */
 else
  {
    for(i=0;i<3;i++)
     if ((leftface[i] != leftface[0])||(rightface[i] != rightface[0])||
        (backface[i] != backface[0]))
      {
       temp = 1;
      }
    if (temp)
     DoALayer(); 
  }
 if (!temp)
  {
   strcpy(str,"Top Layer Finished - Go to Middle Layer\n");
   if (fh)
    Write(fh,str,strlen(str));
   strcpy(str,"Go To Middle Layer");
   Space2();
   Text(rpG,str,strlen(str));
  }
/* 2630 stuff goes here */
/* missing a4070 and a4080 routines! */
 Space2();
 strcpy(str,"\n");
 if (fh)
  Write(fh,str,1L);
 strcpy(str,"First Layer Complete");
 Text(rpG,str,strlen(str));
 strcat(str,"\n");
 if (fh)
  Write(fh,str,strlen(str));
 strcpy(str,"Color align cubes of middle layer with top layer\n");
 if (fh)
  Write(fh,str,strlen(str));
 q[0] = '\0';
a2660:
 if (frontface[0]  != frontface[4])
  {
   strcpy(q,"cu'");
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';
   goto a2660;
  }
 Space2();
 strcpy(str,"\nTop and Middle Layers aligned - is middle layer finished?\n");
 if (fh)
  Write(fh,str,strlen(str));
 strcpy(str,"- is middle layer finished?");
 Text(rpG,str,strlen(str));
 for(l=3;l<6;l++)
  {
   if ((leftface[l] != leftface[4])||(frontface[l] != frontface[4])||
      (rightface[l] != rightface[4])||(backface[l] != backface[4]))
    {
     goto a2790;
    }
  }
 goto a3120;
a2790:
 strcpy(str,"Middle Layer Unfinished - clear middle layer edge cubes\n");
 if (fh)
  Write(fh,str,strlen(str));
 Space2();
 strcpy(str,"- clear middle layer edge cubes");
 Text(rpG,str,strlen(str));
 q[0] = '\0';
 for(k=1;k<5;k++)
  {
a2830:
   if ((frontface[3] == downface[4])||(leftface[5] == downface[4]))
    goto a2900;
   if ((downface[5] == downface[4])||(rightface[7] == downface[4]))
    strcpy(q,"ldl'd'f'd'f");
   else
    {
     strcpy(q,"d");
     if (fh)
      Write(fh,q,strlen(q));
     Parse();if (aborted) return;
     q[0] = '\0';
     goto a2830;
    }
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
 a2900:
   q[1] = '\0';
   q[0] = 'c';
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';
  }
 /* Middle Layer Edge Destoryed */
 Space2();
 strcpy(str,"Edge Cubes Cleared - replace them");
 Text(rpG,str,strlen(str));
 strcat(str,"\n");
 if (fh)
  Write(fh,str,strlen(str));
 for(l=1;l<5;l++)
  {
   q[0] = '\0';
a2960:
   if ((downface[5] == leftface[4])&&(rightface[7] == frontface[4]))
    strcpy(q,"ldl'd'f'd'f");
   if ((downface[5] == frontface[4])&&(rightface[7] == leftface[4]))
    strcpy(q,"ldl'd'f'd'f");
   if (q[0] == '\0')
    {
     strcpy(q,"d");
     if (fh)
      Write(fh,q,strlen(q));
     Parse();if (aborted) return;
     q[0] = '\0';
     goto a2960;
    }
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = 'c';
   q[1] = '\0';
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';
  }
  /* Top and Middle layers are complete */
 strcpy(str,"\nTop and Middle Layers are complete\n");
 if (fh)
  Write(fh,str,strlen(str));
 Space2();
 strcpy(str,"Color Align edge cubes");
 Text(rpG,str,strlen(str));
 strcpy(str,"edge cubes replaced - color align them\n");
 if (fh)
  Write(fh,str,strlen(str));
 for(k=1;k<5;k++)
  {
   q[0] = '\0';
   if ((leftface[5] == leftface[4])&&(frontface[3] == frontface[4]))
    strcpy(q,"c");
   else
    {
     strcpy(q,"drdu'rdu'bdu'lld'ubd'urd'uffr'd'");
     if (fh)
      Write(fh,q,strlen(q));
     Parse();if (aborted) return;
     strcpy(q,"c");
    }
   if (fh)
    Write(fh,q,strlen(q));
   Parse();if (aborted) return;
   q[0] = '\0';
  }
 /* Middle Layer finished */
a3120:
 Space2();
 strcpy(str,"- place corners in bottom layer");
 Text(rpG,str,strlen(str));
 strcpy(str,"\nMiddle Layer finished - place corners in bottom layer\n");
a3140:
 if ((downface[2] == rightface[4])&&(rightface[6] == frontface[4]))
  goto a3180;
 if ((rightface[6] == rightface[4])&&(frontface[8] == frontface[4]))
  goto a3180;
 if ((frontface[8] == rightface[4])&&(downface[2] == frontface[4]))
  goto a3180;
 q[0] = 'd';
 q[1] = '\0';
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3140;
a3180:
 if ((downface[0] == frontface[4])&&(frontface[6] == leftface[4]))
  goto a3300;
 if ((frontface[6] == frontface[4])&&(leftface[8] == leftface[4]))
  goto a3300;
 if ((leftface[8] == frontface[4])&&(downface[0] == leftface[4]))
  goto a3300;
 if ((downface[6] == leftface[4])&&(leftface[6] == backface[4]))
  goto a3340;
 if ((leftface[6] == leftface[4])&&(backface[8] == backface[4]))
  goto a3340;
 if ((backface[8] == leftface[4])&&(downface[6] == backface[4]))
  goto a3340;
 if ((downface[8] == rightface[4])&&(rightface[8] == backface[4]))
  goto a3350;
 if ((rightface[8] == rightface[4])&&(backface[6] == backface[4]))
  goto a3350;
 if ((backface[6] == rightface[4])&&(downface[8] == backface[4]))
  goto a3350;
 strcpy(q,"dfd'b'df'd'b");
a3290:
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3180;
a3300:
 if ((downface[6] == leftface[4])&&(leftface[6] == backface[4]))
  goto a3360;
 if ((leftface[6] == leftface[4])&&(backface[8] == backface[4]))
  goto a3360;
 if ((backface[8] == leftface[4])&&(downface[6] == backface[4]))
  goto a3360;
 strcpy(q,"fd'b'df'd'bdd");
 goto a3290;
a3340:
 strcpy(q,"dldbd'b'l'");
 goto a3290;
a3350:
 strcpy(q,"rd'l'dr'd'ldd");
  goto a3290;
a3360:
 /* corners are in place */
  strcpy(str,"Corners in place align corner colors");
  Space2();
  Text(rpG,str,strlen(str));
  strcat(str,"\n");
  if (fh)
   Write(fh,"\n",1L);
  if (fh)
   Write(fh,str,strlen(str));
a3380:
  a = 0;
  if (downface[0] == downface[4])
   a++;
  if (downface[2] == downface[4])
   a++;
  if (downface[6] == downface[4])
   a++;
  if (downface[8] == downface[4])
   a++;
  if (a==1)
   goto a3500;
  if (a != 0)
   goto a3530;
a3450:
 if (downface[4] != backface[8])
  {
   q[0] = 'c';
   q[1] = '\0';
   if (fh)
    Write(fh,q,1L);
   Parse();if (aborted) return;
   goto a3450;
  }
a3480:
 strcpy(q,"ldl'dlddl'dd");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3380;
a3500:
 if (downface[2] == downface[4])
  goto a3480;
 strcpy(q,"c");
 if (fh)
  Write(fh,q,1L);
 Parse();if (aborted) return;
 goto a3500;
a3530:
 if (a==4)
  goto a3610;
 if (a==2)
  goto a3570;
 if (downface[0] != downface[4])
  goto a3480;
 goto a3590;
a3570:
 if ((downface[6] == downface[4])&&(downface[8] == downface[4]))
  goto a3480;
 if ((downface[2] == downface[4])&&(downface[6] == downface[4]))
  goto a3480;
a3590:
 q[0] = 'c';
 q[1] = '\0';
 if (fh)
  Write(fh,q,1L);
 Parse();if (aborted) return;
 goto a3380;
a3610:
 strcpy(str,"- color align bottom face");
 Space2();
 Text(rpG,str,strlen(str));
 strcpy(str,"\nCorners are now aligned - color align bottom face\n");
 if (fh)
  Write(fh,str,strlen(str));
 for(k=1;k<5;k++)
  {
   if (downface[1] != downface[7])
    goto a3750;
a3650:
    if (downface[1] != downface[3])
     goto a3830;
a3660:
    q[0] = 'c';
    q[1] = '\0';
    if (fh)
     Write(fh,q,strlen(q));
    Parse();if (aborted) return;
  }
  strcpy(str,"- check edges for color alignment");
  Space2();
  Text(rpG,str,strlen(str));
  strcpy(str,"\nBottom face aligned - check edges for color alignment\n");
  if (fh)
   Write(fh,str,strlen(str));
a3680:
 if ((frontface[7] != frontface[4]))
  goto a3870;
 if (leftface[7] != leftface[4])
  goto a4030;
 if (rightface[7] != rightface[4])
  goto a4070;
 if (backface[7] != backface[4])
  goto a4080;
 strcpy(str,"\n- - Edges OK!\n");
 if (fh)
  Write(fh,str,strlen(str));
 strcpy(str,"Solution to Rubiks Cube completed\n");
 if (fh)
  Write(fh,str,strlen(str));
 Space2();
 strcpy(str,"Cube Solved!");
 Text(rpG,str,strlen(str));
 return;
a3750:
 if ((downface[1] != downface[4])&&(downface[7] != downface[4]))
  goto a3770;
 goto a3650;
a3770:
 q[0] = 'c';
 q[1] = '\0';
 if (fh)
  Write(fh,q,1L);
 Parse();if (aborted) return;
 strcpy(q,"hdu'rdu'bdu'lld'ubd'urd'uffh'");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 strcpy(q,"c'");
 if (fh)
  Write(fh,q,2L);
 Parse();if (aborted) return;
 goto a3650;
a3830:
 if ((downface[1] != downface[4])&&(downface[3] != downface[4]))
  goto a3850;
 goto a3660;
a3850:
 strcpy(q,"f'r'hdu'rdu'bdu'lld'ubd'urd'uffh'rf");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3660;
a3870:
 if (leftface[7] == leftface[4])
  goto a3920;
 if (rightface[7] == rightface[4])
  goto a3930;
 if (backface[7] == backface[4])
  goto a3970;
 q[0] = 'c';
 q[1] = '\0';
 if (fh)
  Write(fh,q,1L);
 Parse();if (aborted) return;
 strcpy(q,"rrdbf'rrfb'drr");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3680;
a3920:
 strcpy(q,"rrdbf'rrfb'drr");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3680;
a3930:
 strcpy(q,"cc");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 strcpy(q,"rrdbf'rrfb'drr");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 strcpy(q,"cc");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 goto a3680;
a3970:
 strcpy(q,"c'");
 if (fh)
  Write(fh,q,2L);
 Parse();if (aborted) return;
 strcpy(q,"rrdbf'rrfb'drr");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 strcpy(q,"c");
 if (fh)
  Write(fh,q,1L);
 Parse();if (aborted) return;
 goto a3680;
a4030:
 strcpy(q,"c");
 if (fh)
  Write(fh,q,1L);
 Parse();if (aborted) return;
 strcpy(q,"rrdbf'rrfb'drr");
 if (fh)
  Write(fh,q,strlen(q));
 Parse();if (aborted) return;
 strcpy(q,"c'");
 if (fh)
  Write(fh,q,2L);
 Parse();if (aborted) return;
 goto a3680;
a4070:
a4080:
 strcpy(str,"Cannot Solve Cube");
 Space2();
 Text(rpG,str,strlen(str));
 strcpy(str,"\nCannot solve cube because of its structure!\n");
 if (fh)
  Write(fh,str,strlen(str));
}

void DoSolve()
{
 register int i;
 struct Window *wG3;
 struct IntuiMessage *message;
 int done2=0;
 UWORD code;
 ULONG class,s1,s2,m1,m2;
 APTR object;
 char str[80];
 

 aborted = 0;
 i = 255 - speed;
 sprintf(MyMyGadget3SIBuff,"%d",i);
 if (!(wG3 = OpenWindow(&MyNewWindowStructure1)))
  return;
 PrintIText(wG3->RPort,&MyIntuiTextList1,0L,0L);
 Delay(10L);
 ActivateGadget(&MyGadget2,wG3,NULL);
 while (!done2)
  {
   WaitPort(wG3->UserPort);
   while(message = (struct IntuiMessage *)GetMsg(wG3->UserPort))
    {
     class = message->Class;
     code = message->Code;
     object = message->IAddress;
     ReplyMsg((struct Message *)message);
     if (object == (APTR)&MyGadget1)
      done2 = 1;
     else if (object == (APTR)&MyGadget2)
      ActivateGadget(&MyGadget3,wG3,NULL);
     else if (object == (APTR)&MyGadget3)
      ActivateGadget(&MyGadget2,wG3,NULL);
    }
  }
 CloseWindow(wG3);
 speed = atol(MyMyGadget3SIBuff);
 for(done2=0;done2<strlen(MyMyGadget3SIBuff);done2++)
  {
   if ((MyMyGadget3SIBuff[done2] > '9')||(MyMyGadget3SIBuff[done2] < '0'))
    {
     speed = 255L;
    }
  }
 if (speed > 255L)
  speed = 255L;
 speed = 255L - speed; /* speed is the delay factor from 0 - 255 */
 fh = 0L;
 if (strlen(MyMyGadget2SIBuff) > 0)
  {
   if (!(fh = Open(MyMyGadget2SIBuff,MODE_NEWFILE)))
    {
     DisplayBeep(0L);
     Delay(30L);
     DisplayBeep(0L);
    }
  }
 CurrentTime(&s1,&m1);
/* solve cube goes here */
 SetAPen(rpG,1L); /* Black */
 Space2();
 strcpy(str,"check for one side solved");
 Text(rpG,str,strlen(str));
 if (fh)
  {
   strcat(str,"\n");
   Write(fh,str,strlen(str));
  }
 done2 = 1;
 for(i=0;i<9;i++)
  if (upperface[i] != upperface[4])
   done2 = 0;
 if (!done2)
  {
   done2 = 2;
   for(i=0;i<9;i++)
    if (leftface[i] != leftface[4])
     done2 = 0;
  }
 if (!done2)
  {
   done2 = 3;
   for(i=0;i<9;i++)
    if (frontface[i] != frontface[4])
     done2 = 0;
  }
 if (!done2)
  {
   done2 = 4;
   for(i=0;i<9;i++)
    if (rightface[i] != rightface[4])
     done2 = 0;
  }
 if (!done2)
  {
   done2 = 5;
   for(i=0;i<9;i++)
    if (backface[i] != backface[4])
     done2 = 0;
  }
 if (!done2)
  {
   done2 = 6;
   for(i=0;i<9;i++)
    if (downface[i] != downface[4])
     done2 = 0;
  }
 if (done2)
  {/* a side is done */
   switch(done2)
    {
     case 1 :	Space2();
		strcpy(str,"Upper Side Solved");
		Text(rpG,str,strlen(str));
		strcat(str,"\n");
		if (fh)
		 Write(fh,str,strlen(str));
		break;
     case 2 :	Space2();
		strcpy(str,"Left Side Solved");
		Text(rpG,str,strlen(str));
		strcat(str,"\n");
		if (fh)
		 Write(fh,str,strlen(str));
		strcpy(q,"c'h");
		if (fh)
		 Write(fh,q,strlen(q));
		Parse();if (aborted) return;
		if (aborted)
		 return;
		break;
     case 3 :	Space2();
		strcpy(str,"Front Side Solved");
		Text(rpG,str,strlen(str));
		strcat(str,"\n");
		if (fh)
		 Write(fh,str,strlen(str));
		strcpy(q,"h");
		if (fh)
		 Write(fh,q,strlen(q));
		Parse();if (aborted) return;
		break;
     case 4 :	Space2();
		strcpy(str,"Right Side Solved");
		Text(rpG,str,strlen(str));
		strcat(str,"\n");
		if (fh)
		 Write(fh,str,strlen(str));
		strcpy(q,"ch");
		if (fh)
		 Write(fh,q,strlen(q));
		Parse();if (aborted) return;
		break;
     case 5 :	Space2();
		strcpy(str,"Back Side Solved");
		Text(rpG,str,strlen(str));
		strcat(str,"\n");
		if (fh)
		 Write(fh,str,strlen(str));
		strcpy(q,"h'");
		if (fh)
		 Write(fh,q,strlen(q));
		Parse();if (aborted) return;
		break;
     case 6 :	Space2();
		strcpy(str,"Down Side Solved");
		Text(rpG,str,strlen(str));
		strcat(str,"\n");
		if (fh)
		 Write(fh,str,strlen(str));
		strcpy(q,"hh");
		if (fh)
		 Write(fh,q,strlen(q));
		Parse();if (aborted) return;
		break;
    }
  }
 else
  {
   q[0] = '\0';
   a = upperface[4];
   c = frontface[4];
   e = rightface[4];
   ClearUpperCorners();
   strcpy(str,"Corners are Cleared, solve Corners");
   Space2();
   Text(rpG,str,strlen(str));
   strcat(str,"\n");
   if (fh)
    Write(fh,str,strlen(str));
   SolveCorners();
   strcpy(str,"\nCorners Finished\n");
   if (fh)
    Write(fh,str,strlen(str));
   strcpy(str,"Check For Finished Layers");
   Space2();
   Text(rpG,str,strlen(str));
   strcat(str,"\n");
   if (fh)
    Write(fh,str,strlen(str));
  }
 strcpy(str,"Now finish Centers of Upper layer\n");
 if (fh)
 Write(fh,str,strlen(str));
 FinishLayers(); /* this is sub 2240 */
 CurrentTime(&s2,&m2);
 s2 -= s1;
 sprintf(str,"Cube solved in %d secs.",s2);
 Space2();
 Text(rpG,str,strlen(str));
 strcat(str,"\n");
 if (fh)
  Write(fh,str,strlen(str));
 if (fh)
  Close(fh);
 fh = 0L;
}



void DoHorizontal()
{
 int i;

 DoRight();
 DoLeft();
 DoLeft();
 DoLeft();
 for(i=0;i<9;i++)
  x[i] = frontface[i];
 frontface[1] = downface[1];
 frontface[4] = downface[4];
 frontface[7] = downface[7];
 downface[1] = backface[7];
 downface[4] = backface[4];
 downface[7] = backface[1];
 backface[1] = upperface[7];
 backface[4] = upperface[4];
 backface[7] = upperface[1];
 upperface[1] = x[1];
 upperface[4] = x[4];
 upperface[7] = x[7];
 Plot();
}

void DoVertical()
{
 int i;

 DoUp();
 DoDown();
 DoDown();
 DoDown();
 for(i=0;i<9;i++)
  x[i] = leftface[i];
 for(i=3;i<6;i++)
  {
   leftface[i] = frontface[i];
   frontface[i] = rightface[i];
   rightface[i] = backface[i];
   backface[i] = x[i];
  }
 Plot();
}

void DoForward()
{
 int i;

 for(i=0;i<9;i++)
  x[i] = frontface[i];
 FaceRotate();
 for(i=0;i<9;i++)
  frontface[i] = x[i];
 for(i=0;i<9;i++)
  x[i] = upperface[i];
 upperface[6] = leftface[8];
 upperface[7] = leftface[5];
 upperface[8] = leftface[2];
 leftface[2] = downface[0];
 leftface[5] = downface[1];
 leftface[8] = downface[2];
 downface[0] = rightface[6];
 downface[1] = rightface[3];
 downface[2] = rightface[0];
 rightface[0] = x[6];
 rightface[3] = x[7];
 rightface[6] = x[8];
 Plot();
}

void DoBack()
{
 int i;
 
 for(i=0;i<9;i++)
  x[i] = backface[i];
 FaceRotate();
 for(i=0;i<9;i++)
  backface[i] = x[i];
 for(i=0;i<9;i++)
  x[i] = upperface[i];
 upperface[0] = rightface[2];
 upperface[1] = rightface[5];
 upperface[2] = rightface[8];
 rightface[2] = downface[8];
 rightface[5] = downface[7];
 rightface[8] = downface[6];
 downface[6] = leftface[0];
 downface[7] = leftface[3];
 downface[8] = leftface[6];
 leftface[0] = x[2];
 leftface[3] = x[1];
 leftface[6] = x[0];
 Plot();
}

void DoRight()
{
 int i;
 
 for(i=0;i<9;i++)
  x[i] = rightface[i];
 FaceRotate();
 for(i=0;i<9;i++)
  rightface[i] = x[i];
 for(i=0;i<9;i++)
  x[i] = upperface[i];
 for(i=2;i<9;i+=3)
  {
   upperface[i] = frontface[i];
   frontface[i] = downface[i];
  }
 downface[2] = backface[6];
 downface[5] = backface[3];
 downface[8] = backface[0];
 backface[0] = x[8];
 backface[3] = x[5];
 backface[6] = x[2];
 Plot();
}

void DoLeft()
{
 int i;

 for(i=0;i<9;i++)
  x[i] = leftface[i];
 FaceRotate();
 for(i=0;i<9;i++)
  leftface[i] = x[i];
 for(i=0;i<9;i++)
  x[i] = upperface[i];
 upperface[0] = backface[8];
 upperface[3] = backface[5];
 upperface[6] = backface[2];
 backface[2] = downface[6];
 backface[5] = downface[3];
 backface[8] = downface[0];
 for(i=0;i<7;i+=3)
  {
   downface[i] = frontface[i];
   frontface[i] = x[i];
  }
 Plot();
}

void FaceRotate()
{
 int i;

 y[0] = x[6];
 y[1] = x[3];
 y[2] = x[0];
 y[3] = x[7];
 y[4] = x[4];
 y[5] = x[1];
 y[6] = x[8];
 y[7] = x[5];
 y[8] = x[2];
 for(i=0;i<9;i++)
  x[i] = y[i];
}

void DoUp()
{
 register long i;

 for(i=0;i<9;i++)
  x[i] = upperface[i];
 FaceRotate();
 for(i=0;i<9;i++)
  upperface[i] = x[i];
 for(i=0;i<9;i++)
  x[i] = leftface[i];
 for(i=0;i<3;i++)
  {
   leftface[i] = frontface[i];
   frontface[i] = rightface[i];
  }
 for(i=0;i<3;i++)
  {
   rightface[i] = backface[i];
   backface[i] = x[i];
  }
 Plot();
}

void DoDown()
{
 int i;
 for(i=0;i<9;i++)
  x[i] = downface[i];
 FaceRotate();
 for(i=0;i<9;i++)
  downface[i] = x[i];
 for(i=0;i<6;i++)
  x[i] = 0;
 x[6] = leftface[6];
 x[7] = leftface[7];
 x[8] = leftface[8];
 for(i=6;i<9;i++)
  {
   leftface[i] = backface[i];
   backface[i] = rightface[i];
  }
 for(i=6;i<9;i++)
  {
   rightface[i] = frontface[i];
   frontface[i] = x[i];
  }
 Plot();
}

void DoAbout()
{
 struct Window *wG3;
 struct IntuiMessage *message;

 if (!(wG3 = OpenWindow(&NewWindowStructure2)))
  return;
 PrintIText(wG3->RPort,&IntuiTextList2,0L,0L);
 WaitPort(wG3->UserPort);
 while(message = (struct IntuiMessage *)GetMsg(wG3->UserPort))
  ReplyMsg((struct Message *)message);
 CloseWindow(wG3);
}

void __regargs PrintText(Line,Row,str)
int Line,Row;
char *str;
{

 Move(rpG,(Row*7L),(Line*10L+2L));
 Text(rpG,str,strlen(str));
}

void Plot()
{
 int i;
 UWORD code;
 ULONG class;
 char str[80];
 struct IntuiMessage *message;

 if ((message = (struct IntuiMessage *)GetMsg(wG->UserPort)))
  {
   class = message->Class;
   code = message->Code;
   ReplyMsg((struct Message *)message);
   if ((class == MOUSEBUTTONS)&&(code == SELECTDOWN))
    {
     aborted = 1;
     for(i=0;i<9;i++)
      {
       upperface[i] = 2;
       leftface[i] = 3;
       frontface[i] = 4;
       rightface[i] = 5;
       backface[i] = 6;
       downface[i] = 7;
      }
    strcpy(str,"\nAborted\n");
    if (fh)
     Write(fh,str,strlen(str));
    PlotOn = 1;
   }
  }
 if (!PlotOn)
  return;
 SetAPen(rpG,upperface[0]);
 RectFill(rpG,90L,16L,109L,32L);
 SetAPen(rpG,upperface[1]);
 RectFill(rpG,111L,16L,130L,32L);
 SetAPen(rpG,upperface[2]);
 RectFill(rpG,132L,16L,151L,32L);
 SetAPen(rpG,upperface[3]);
 RectFill(rpG,90L,34L,109L,50L);
 SetAPen(rpG,upperface[4]);
 RectFill(rpG,111L,34L,130L,50L);
 SetAPen(rpG,upperface[5]);
 RectFill(rpG,132L,34L,151L,50L);
 SetAPen(rpG,upperface[6]);
 RectFill(rpG,90L,52L,109L,68L);
 SetAPen(rpG,upperface[7]);
 RectFill(rpG,111L,52L,130L,68L);
 SetAPen(rpG,upperface[8]);
 RectFill(rpG,132L,52L,151L,68L);
 SetAPen(rpG,leftface[0]);
 RectFill(rpG,20L,76L,39L,92L);
 SetAPen(rpG,leftface[1]);
 RectFill(rpG,41L,76L,60L,92L);
 SetAPen(rpG,leftface[2]);
 RectFill(rpG,62L,76L,81L,92L);
 SetAPen(rpG,leftface[3]);
 RectFill(rpG,20L,94L,39L,110L);
 SetAPen(rpG,leftface[4]);
 RectFill(rpG,41L,94L,60L,110L);
 SetAPen(rpG,leftface[5]);
 RectFill(rpG,62L,94L,81L,110L);
 SetAPen(rpG,leftface[6]);
 RectFill(rpG,20L,112L,39L,128L);
 SetAPen(rpG,leftface[7]);
 RectFill(rpG,41L,112L,60L,128L);
 SetAPen(rpG,leftface[8]);
 RectFill(rpG,62L,112L,81L,128L);
 SetAPen(rpG,frontface[0]);
 RectFill(rpG,90L,76L,109L,92L);
 SetAPen(rpG,frontface[1]);
 RectFill(rpG,111L,76L,130L,92L);
 SetAPen(rpG,frontface[2]);
 RectFill(rpG,132L,76L,151L,92L);
 SetAPen(rpG,frontface[3]);
 RectFill(rpG,90L,94L,109L,110L);
 SetAPen(rpG,frontface[4]);
 RectFill(rpG,111L,94L,130L,110L);
 SetAPen(rpG,frontface[5]);
 RectFill(rpG,132L,94L,151L,110L);
 SetAPen(rpG,frontface[6]);
 RectFill(rpG,90L,112L,109L,128L);
 SetAPen(rpG,frontface[7]);
 RectFill(rpG,111L,112L,130L,128L);
 SetAPen(rpG,frontface[8]);
 RectFill(rpG,132L,112L,151L,128L);
 SetAPen(rpG,rightface[0]);
 RectFill(rpG,160L,76L,179L,92L);
 SetAPen(rpG,rightface[1]);
 RectFill(rpG,181L,76L,200L,92L);
 SetAPen(rpG,rightface[2]);
 RectFill(rpG,202L,76L,221L,92L);
 SetAPen(rpG,rightface[3]);
 RectFill(rpG,160L,94L,179L,110L);
 SetAPen(rpG,rightface[4]);
 RectFill(rpG,181L,94L,200L,110L);
 SetAPen(rpG,rightface[5]);
 RectFill(rpG,202L,94L,221L,110L);
 SetAPen(rpG,rightface[6]);
 RectFill(rpG,160L,112L,179L,128L);
 SetAPen(rpG,rightface[7]);
 RectFill(rpG,181L,112L,200L,128L);
 SetAPen(rpG,rightface[8]);
 RectFill(rpG,202L,112L,221L,128L);
 SetAPen(rpG,backface[0]);
 RectFill(rpG,230L,76L,249L,92L);
 SetAPen(rpG,backface[1]);
 RectFill(rpG,251L,76L,270L,92L);
 SetAPen(rpG,backface[2]);
 RectFill(rpG,272L,76L,291L,92L);
 SetAPen(rpG,backface[3]);
 RectFill(rpG,230L,94L,249L,110L);
 SetAPen(rpG,backface[4]);
 RectFill(rpG,251L,94L,270L,110L);
 SetAPen(rpG,backface[5]);
 RectFill(rpG,272L,94L,291L,110L);
 SetAPen(rpG,backface[6]);
 RectFill(rpG,230L,112L,249L,128L);
 SetAPen(rpG,backface[7]);
 RectFill(rpG,251L,112L,270L,128L);
 SetAPen(rpG,backface[8]);
 RectFill(rpG,272L,112L,291L,128L);
 SetAPen(rpG,downface[0]);
 RectFill(rpG,90L,136L,109L,152L);
 SetAPen(rpG,downface[1]);
 RectFill(rpG,111L,136L,130L,152L);
 SetAPen(rpG,downface[2]);
 RectFill(rpG,132L,136L,151L,152L);
 SetAPen(rpG,downface[3]);
 RectFill(rpG,90L,154L,109L,170L);
 SetAPen(rpG,downface[4]);
 RectFill(rpG,111L,154L,130L,170L);
 SetAPen(rpG,downface[5]);
 RectFill(rpG,132L,154L,151L,170L);
 SetAPen(rpG,downface[6]);
 RectFill(rpG,90L,172L,109L,188L);
 SetAPen(rpG,downface[7]);
 RectFill(rpG,111L,172L,130L,188L);
 SetAPen(rpG,downface[8]);
 RectFill(rpG,132L,172L,151L,188L);
}

void Init()
{
 register ULONG i;
 char str[80];

 for(i=0;i<9;i++)
  {
   upperface[i] = 2;
   leftface[i] = 3;
   frontface[i] = 4;
   rightface[i] = 5;
   backface[i] = 6;
   downface[i] = 7;
  }
 SetDrMd(rpG,JAM1);
 SetAPen(rpG,1L);
 for(i=19;i<83;i+=21)
  {
   Move(rpG,i,75L);
   Draw(rpG,i,129L);
  }
 for(i=89;i<153;i+=21)
  {
   Move(rpG,i,75L);
   Draw(rpG,i,129L);
   Move(rpG,i,15L);
   Draw(rpG,i,69L);
   Move(rpG,i,135L);
   Draw(rpG,i,189L);
  }
 for(i=159;i<223;i+=21)
  {
   Move(rpG,i,75L);
   Draw(rpG,i,129L);
  }
 for(i=229;i<293;i+=21)
  {
   Move(rpG,i,75L);
   Draw(rpG,i,129L);
  }
 for(i=75;i<130;i+=18)
  {
   Move(rpG,19L,i);
   Draw(rpG,82L,i);
   Move(rpG,89L,i);
   Draw(rpG,152L,i);
   Move(rpG,159L,i);
   Draw(rpG,222L,i);
   Move(rpG,229L,i);
   Draw(rpG,292L,i);
  }
 for(i=15;i<70;i+=18)
  {
   Move(rpG,89L,i);
   Draw(rpG,152L,i);
  }
 for(i=135;i<190;i+=18)
  {
   Move(rpG,89L,i);
   Draw(rpG,152L,i);
  }
 strcpy(str,"Up");
 PrintText(3,10,str);
 strcpy(str,"Left");
 PrintText(7,5,str);
 strcpy(str,"Right");
 PrintText(7,24,str);
 strcpy(str,"Back");
 PrintText(7,33,str);
 strcpy(str,"Down");
 PrintText(18,8,str);
 strcpy(str,"Front");
 PrintText(4,23,str);
 Move(rpG,152L,75L);
 Draw(rpG,175L,47L);
 Plot();
}


void main()
{
 int Mx,My;
 UWORD code;
 ULONG class,temp;
 APTR object;
 ULONG signals;
 struct IntuiMessage *message;	/* the message the IDCMP sends us */

#ifdef CPU020

 struct ExecBase **execbaseptr=(struct ExecBase **)4L;
 UWORD cpuid;
 struct ExecBase *execbase;

 execbase = *execbaseptr;
 cpuid = execbase->AttnFlags;
 if (!(cpuid & AFF_68020))
  {
   exit(0);
  }
#endif


 if (!(IntuitionBase = 
    (struct IntuitionBase *)OpenLibrary("intuition.library", 33L)))
  {
   exit(0);
  }
 if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 33L)))
  {
   CloseLibrary((struct Library *)IntuitionBase);
   exit(0);
  }
 if (!(myTextFont = OpenFont(&TOPAZ80)))
  {
   CloseLibrary((struct Library *)GfxBase);
   CloseLibrary((struct Library *)IntuitionBase);
   exit(0);
  }
 if (!(sC = OpenScreen(&NewScreenStructure)))
  {
   CloseFont(myTextFont);
   CloseLibrary((struct Library *)GfxBase);
   CloseLibrary((struct Library *)IntuitionBase);
   exit(0);
  }
 LoadRGB4(&(sC->ViewPort),Palette,PaletteColorCount);
 MyNewWindowStructure1.Screen = NewWindowStructure2.Screen = 
  NewWindowStructure1.Screen = sC;
 if (!(wG = OpenWindow(&NewWindowStructure1)))	/* open the window */
  {
   CloseScreen(sC);
   CloseFont(myTextFont);
   CloseLibrary((struct Library *)GfxBase);
   CloseLibrary((struct Library *)IntuitionBase);
   exit(0);
   }
  rpG = wG->RPort;	/* get a rastport pointer for the window */
  SetFont(rpG,myTextFont);
  SetMenuStrip(wG,&MenuList1);	/* attach any Menu */
  Init();
  do
   {
	signals = Wait(WINDOWSIGNAL);
	if (signals & WINDOWSIGNAL)
		while( (message = (struct IntuiMessage *)
		GetMsg(wG->UserPort) ) != NULL)
		{
			Mx = message->MouseX;
			My = message->MouseY;
			class = message->Class;
			code = message->Code;
			object = message->IAddress;  /* Gadget */
			ReplyMsg((struct Message *)message);
			if ( class == MENUPICK )	/* MenuItems */
			 HandleEvent((APTR)ItemAddress(&MenuList1,code));
                        else if ((class == MOUSEBUTTONS)&&(code == SELECTDOWN))
                         {
			  onepoint++;
			  if (onepoint == 1)
			   {
			    xchg1 = MatchBox(Mx,My);
		   	    if (xchg1)
 			     SetPointer(wG,PickPtr,8L,0x10L,0L,0L);
			    else
			     onepoint = 0;
			   }
                          else
                           {
			    onepoint = 0;
			    ClearPointer(wG);
			    xchg2 = MatchBox(Mx,My);
			    if (xchg2)
			     {
			      temp = *xchg1;
			      *xchg1 = *xchg2;
			      *xchg2 = temp;
			      Plot();
			     }
                           }
                         }
		}
   } while (!done);
  CloseWindow(wG);
  CloseFont(myTextFont);
  CloseScreen(sC);
  CloseLibrary((struct Library *)GfxBase);
  CloseLibrary((struct Library *)IntuitionBase);
}
