File: AI.C | Size: 3,406 bytes | Download file | Back to directory listing | BWPOW's homepage
// This file is by Marian Dvorsky
#include <stdio.h>
#include <allegro.h>
#include "sachy.h"
 
//extern int max_depth;
int max_depth;
extern int konec;
float maxskore;
int bestmove;
MOVE best_move;
 
float think(int depth, float alpha, float beta)
{
  MOVE now_possible[108];
  int now_poss,x;
  int wt;
  float sk;
  float skore;
  char ocastled[2];
  int z;
  MOVE prmove;
  POLICKO_XY sved[8];
  int ps=0;
  char oflags[6];
  wt = whose_turn;
  skore = -9999;
  if (depth==0){return score(wt);}
  now_poss = generate_possible_moves(now_possible,whose_turn);
  if (now_poss == 0)
  {
    if (depth == max_depth){
      konec = 1;
      return;
    }
    else
    {
      if (check_for_chess(wt))
        return -999+(max_depth-depth);
      else
        return 0;
    }
  }
  x=0;
  while (x<now_poss && skore < beta)
  {
    prmove = prev_move;
    for (z=0; z<=5;z++)
      oflags[z] = flags[z];
    for (z=0; z<2;z++)
      ocastled[z] = castled[z];
    whose_turn = wt;
    poc_saved = 0;
    check_move(now_possible[x].x1,now_possible[x].y1,now_possible[x].x2,now_possible[x].y2,1,sved);
    ps = poc_saved;
    if (skore > alpha) alpha = skore;
    sk = -think(depth-1,-beta,-alpha);
    poc_saved = ps;
    load_pos(sved);
    prev_move = prmove;
    for (z=0; z<=5;z++)
      flags[z] = oflags[z];
    for (z=0; z<2;z++)
      castled[z] = ocastled[z];
    if (sk > skore)
    {
      skore = sk;
      if (depth == max_depth) bestmove = x;
    }
    x++;
  }
  if (depth == max_depth)
  {
    best_move.x1 = now_possible[bestmove].x1;
    best_move.y1 = now_possible[bestmove].y1;
    best_move.x2 = now_possible[bestmove].x2;
    best_move.y2 = now_possible[bestmove].y2;
  }
  return skore;
}
 
int check_in_book()
{
  FILE *f;
  char poz[65];
  char ws;
  char x1,y1;
  char computed[64];
  char aa,x2,y2;
  long a;
  int gener;
  MOVE tahy[10];
  int poctahov=0;
  aa = 0;
  for (y2= 0; y2 <= 7; y2++)
    for (x2= 0; x2 <= 7; x2++,aa++)
    {
      computed[aa]=(sachovnica[x2][y2].figurka+(sachovnica[x2][y2].farba*6))+1;
    }
  computed[aa] = '\0';
  f = fopen("openingz.dat","rb");
  if(f==NULL){
    allegro_exit();
    printf("Error loading OPENINGZ.DAT!\n");
    exit(11);
  }
 
  aa=0;
  a = filelength(fileno(f));
  while (aa*70<a)
  {
    strcpy(poz,"");
    fread(poz,64,1,f);
    fscanf(f,"%c%c%c%c%c",&ws,&x1,&y1,&x2,&y2);
    aa++;
    fseek(f,70*aa,SEEK_SET);
    poz[64] = '\0';
    if (strcmp(poz,computed)==0 && ws == computer && a > -1)
    {
/*      gener = 0;
      while (gener<=64)
      {
        if (poz[gener]!=computed[gener])
          goto goout;
        gener++;
      }*/
      tahy[poctahov].x1 = x1;
      tahy[poctahov].y1 = y1;
      tahy[poctahov].x2 = x2;
      tahy[poctahov].y2 = y2;
      poctahov++;
//      goout:
    }
  }
  if (poctahov > 0)
  {
    gener = random()%poctahov;
    best_move.x1 = tahy[gener].x1;
    best_move.y1 = tahy[gener].y1;
    best_move.x2 = tahy[gener].x2;
    best_move.y2 = tahy[gener].y2;
    fclose(f);
    return 1;
  }
  fclose(f);
  return 0;
}
 
void go(void)
{
  maxskore = -9999;
  if (!check_in_book())
    think(max_depth,-9999,9999);
  whose_turn = computer;
  if (konec==0)
  {
    check_move(best_move.x1,best_move.y1,best_move.x2,best_move.y2);
  }
}