// This may look like C code, but it is really -*- C++ -*-
///////////////////////////////////////////////////////////////////////////
//
//  AMIGA Minesweeper - the driver-routine
//
//  (c) 1992 Hubert Feyrer (c9020@rrzc1.rz.uni-regensburg.de)
//
///////////////////////////////////////////////////////////////////////////

/*
** Diverse "C"-Header
*/
#define class _class
#define template _template
#define IntuitionBase IntuiBase
#define GfxBase GraphBase
extern "C" {
# include <stdio.h>
# include <stdlib.h>
# include <exec/types.h>
# include <exec/exec.h>
# include <exec/Ports.h>
# include <devices/timer.h>
# include <utility/tagitem.h>
# include <intuition/intuition.h>
# include <intuition/intuitionbase.h>
# include <intuition/gadgetclass.h>
# include <graphics/gfxbase.h>
# include <clib/gadtools_protos.h>
# include <clib/intuition_protos.h>
# include <clib/exec_protos.h>
# include <math.h>
}    
#undef IntuitionBase
#undef GfxBase 
#undef class
#undef template

/*
** C++-Header
*/
#include "mine.h"
#include "field.h"

Field ***minefield;                    // minefield[x][y]->check();
int lenx=8;                            // Minenfeld-Größe x (max: 57)
int leny=8;                            // Minenfeld-Größe y (max: 36)
int playtime;                          // Verspielte Zeit in Sekunden


//*
//* Spiel gewonnen!
//*
void game_won(void)
{
    SetWindowTitles(win,"You WIN!",-1);
}


//*
//* Spiel verloren!
//*
void game_lost(void)
{
    showmines();
    SetWindowTitles(win,"You blew it!",-1);
}


//*
//* Hauptprogramm
//*
int main(int argc, char *argv[])
{
    argv0=argv[0];
    int pr=15;

    if(argc>2){
	if((lenx=atoi(argv[1]))<2) lenx=8;             // Anzahl x-Felder
	if((leny=atoi(argv[2]))<2) leny=8;             // Anzahl y-Felder
	if(argc>3){
	    if((pr=atoi(argv[3]))<0 || pr>99) pr=15;   // Wieviel Prozent vermint
	}
    }

    init(pr);
    hidemines(pr);

    int idle=0;                    // Schleife in play() wartet auf Closewindow
    while(1){
	GameStat r=play(idle);

	if(r==GAME_QUIT)
	  shutdown(0);
	  
	switch(r){
	  case GAME_WON:
	    game_won();
	    idle=1;
	    break;
	  case GAME_LOST:
	    game_lost();
	    idle=1;
	    break;
	  case GAME_RESTART:
	    removemines();
	    hidemines(pr);
	    playtime=0;
	    sprintf(ttimegad,"%04d",playtime);
	    GT_SetGadgetAttrs(timegad,win,NULL,
			      GTTX_Text,ttimegad,
			      TAG_END);
	    RefreshGList(timegad,win,NULL,1);
	    countdown=lenx*leny;
	    idle=0;
	    break;
	  default:
	    shutdown(20,"Unknown returncode from play()");
	}
    }
    /*NOTREACHED*/
}
