/*
                GNU GO - the game of Go (Wei-Chi)
                Version 1.1   last revised 3-1-89
           Copyright (C) Free Software Foundation, Inc.
                      written by Man L. Li
                      modified by Wayne Iba
                    documented by Bob Webber
                    NeXT version by John Neil
*/
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation - version 1.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License in file COPYING for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

Please report any bug/fix, modification, suggestion to

mail address:   Man L. Li
                Dept. of Computer Science
                University of Houston
                4800 Calhoun Road
                Houston, TX 77004

e-mail address: manli@cs.uh.edu         (Internet)
                coscgbn@uhvax1.bitnet   (BITNET)
                70070,404               (CompuServe)

For the NeXT version, please report any bug/fix, modification, suggestion to

mail address:   John Neil
                Mathematics Department
                Portland State University
                PO Box 751
                Portland, OR  97207

e-mail address: neil@math.mth.pdx.edu  (Internet)
                neil@psuorvm.bitnet    (BITNET)
*/

/* Generated by Interface Builder */

#import <appkit/appkit.h>
#import <appkit/PrintInfo.h>

#include <string.h>
#import "GoApp.h"
#import "Board.h"

int saveNeXTGoFile(const char *fileName)
{
  FILE *NGoFile;
  int i, j;

  if ((NGoFile = fopen(fileName, "w")) == NULL)
    return 1;

  fprintf(NGoFile, "%d\n", handicap);
  fprintf(NGoFile, "%d\n", whiteSide);
  fprintf(NGoFile, "%d\n", blackSide);
  fprintf(NGoFile, "%d\n", MAXX);
  fprintf(NGoFile, "%d\n", MAXY);
  fprintf(NGoFile, "%d\n", currentStone);
  fprintf(NGoFile, "%d\n", opposingStone);
  fprintf(NGoFile, "%d\n", blackCaptured);
  fprintf(NGoFile, "%d\n", whiteCaptured);
  fprintf(NGoFile, "%d\n", blackCapturedKoI);
  fprintf(NGoFile, "%d\n", blackCapturedKoJ);
  fprintf(NGoFile, "%d\n", whiteCapturedKoI);
  fprintf(NGoFile, "%d\n", whiteCapturedKoJ);
  fprintf(NGoFile, "%d\n", bothSides);
  fprintf(NGoFile, "%d\n", neitherSide);
  fprintf(NGoFile, "%d\n", blackPassed);
  fprintf(NGoFile, "%d\n", whitePassed);
  for (i = 0; i < 9; i++)
    fprintf(NGoFile, "%d\n", opn[i]);
  for (i = 0; i < MAXX; i++)
    for (j = 0; j < MAXY; j++)
      fprintf(NGoFile, "%d\n", p[i][j]);
      
  fclose(NGoFile);

  return 0;
}

@implementation GoApp

- appDidInit:app 
{
    [self SetPreferences:self];
    [self NewGame:self];
    [[[NXApp printInfo] setHorizCentered:YES] setVertCentered:YES];

    openReq = [OpenPanel new];
    saveReq = [SavePanel new];
    
    return self;
}

- showError: (const char *)errorMessage
{
  NXRunAlertPanel(NULL, errorMessage,"OK", NULL, NULL);
  return self;
}

- UserPass:sender
{
    [MainGoView passMove];

    return self;
}

- stopGame:sender
{
    [MainGoView stop:self];
    
    return self;
}

- startGame:sender
{
    [MainGoView go:self];
    
    return self;
}

- NewGame:sender
{	    
    [MainGoView startNewGame];
    [MainGoView display];
	    
    return self;
}

- SetPreferences:sender
{
    handicap = [handicapSlider intValue];
    MAXX = MAXY = [sizeSlider intValue];
    blackSide = [BlackPlayer selectedRow];
    whiteSide = [WhitePlayer selectedRow];
    
    neitherSide = 0;
    bothSides = 0;
    
    if ((blackSide == 0) && (whiteSide == 0))
      neitherSide++;
    if ((blackSide == 1) && (whiteSide == 1))
      bothSides++;
	
    [self NewGame:self];
    
    [prefPanel close];
    
    return self;
}

-openNeXTGoFileRequest:sender
{
  FILE *NGoFile;
  int i, j, t;
  const char *fileName;
  const char *const types[2] = {"nextgo", NULL};

  if ([openReq runModalForTypes:types] && (fileName = [openReq filename])) {

    if ((NGoFile = fopen(fileName, "r")) == NULL)
      return self;

    fscanf(NGoFile, "%d", &handicap);
    fscanf(NGoFile, "%d", &whiteSide);
    fscanf(NGoFile, "%d", &blackSide);
    fscanf(NGoFile, "%d", &MAXX);
    fscanf(NGoFile, "%d", &MAXY);
    [MainGoView startNewGame];
    fscanf(NGoFile, "%d", &opposingStone);
    fscanf(NGoFile, "%d", &currentStone);
    fscanf(NGoFile, "%d", &blackCaptured);
    fscanf(NGoFile, "%d", &whiteCaptured);
    fscanf(NGoFile, "%d", &blackCapturedKoI);
    fscanf(NGoFile, "%d", &blackCapturedKoJ);
    fscanf(NGoFile, "%d", &whiteCapturedKoI);
    fscanf(NGoFile, "%d", &whiteCapturedKoJ);
    fscanf(NGoFile, "%d", &bothSides);
    fscanf(NGoFile, "%d", &neitherSide);
    fscanf(NGoFile, "%d", &blackPassed);
    fscanf(NGoFile, "%d", &whitePassed);
    for (i = 0; i < 9; i++)
      {
        fscanf(NGoFile, "%d", &t);
	opn[i] = t;
      }
    for (i = 0; i < MAXX; i++)
      for (j = 0; j < MAXY; j++)
        {
          fscanf(NGoFile, "%d", &t);
	  p[i][j] = t;
	}
    [MainGoView updateInfo];

    fclose(NGoFile);
    
    [MainGoView lockFocus];
    [[MainGoView window] flushWindow];
    [MainGoView display];
    [MainGoView unlockFocus];    
  } else
    [self showError:"Error on Open Request"];

  NXPing();

  return self;
}


-saveNeXTGoFileRequest:sender
{
  const char *fileName;
  const char *const types[2] = {"nextgo", NULL};

  [saveReq setRequiredFileType:types[0]];

  if (([saveReq runModalForDirectory:""
      file:""]) && (fileName = [saveReq filename]))
    {
      saveNeXTGoFile(fileName);
    }
    
  NXPing();
  
  return self;
}

@end

