/[projet1]/users/barnsey123/HNEFATAFL/main.c
Defence Force logotype

Diff of /users/barnsey123/HNEFATAFL/main.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 771 by barnsey123, Fri Jan 6 14:11:00 2012 UTC revision 772 by barnsey123, Sat Jan 7 13:54:45 2012 UTC
# Line 88  Line 88 
88  // 05-01-2012 removed all local variables (replaced with globals) (34338)  // 05-01-2012 removed all local variables (replaced with globals) (34338)
89  // 05-01-2012 got rid of LookBackInAnger (plus some other changes) (33670)  // 05-01-2012 got rid of LookBackInAnger (plus some other changes) (33670)
90  // 06-01-2012 fixed bug in subcanbetaken2  // 06-01-2012 fixed bug in subcanbetaken2
91    // 07-01-2012 found out why some pieces commit suicide but the fix don't work...perhaps other factors are at work
92    
93  /* TODO LIST  /* TODO LIST
94  *** PROBLEM: Not blocking king under certain circumstances - related to changes in target selection...  *** 07/01/2012 - issue with suicide related to ns/ew and cantakepiece methinks...
95  *** Continue with endgame function to return a value determining victory conditions etc  *** Continue with endgame function to return a value determining victory conditions etc
96  *** routine to detect if all attackers have been captured  *** routine to detect if all attackers have been captured
97  *** routine to detect stalemate (a side cannot move)  *** routine to detect stalemate (a side cannot move)
# Line 102  Line 104 
104  #define SOUTH 1  #define SOUTH 1
105  #define EAST 2  #define EAST 2
106  #define WEST 3  #define WEST 3
107    //#define ENEMYWEIGHT 37
108  extern unsigned char PictureTiles[];    // standard graphics for pieces and backgrounds  extern unsigned char PictureTiles[];    // standard graphics for pieces and backgrounds
109  extern unsigned char ExplodeTiles[];    // extra graphics to "explode" a piece (animation)  extern unsigned char ExplodeTiles[];    // extra graphics to "explode" a piece (animation)
110  /******************* Function Declarations ************************/  /******************* Function Declarations ************************/
# Line 249  unsigned char* ptr_draw;       // ptr to board Line 252  unsigned char* ptr_draw;       // ptr to board
252  unsigned char* ptr_graph;       // pointer to byte values of loaded picture      unsigned char* ptr_graph;       // pointer to byte values of loaded picture    
253  unsigned char points;           // points around king  unsigned char points;           // points around king
254  char counter;                           // general purpose counter (*** DO NOT set to UNSIGNED *** NB 24/10/2011)  char counter;                           // general purpose counter (*** DO NOT set to UNSIGNED *** NB 24/10/2011)
255  unsigned char uncounter;        // general purpose counter (can be negative)  char uncounter;                         // *** MUST BE SIGNED *** general purpose counter (can be negative)
256  //unsigned char lookcol,lookrow;        // used in lookbackinanger  //unsigned char lookcol,lookrow;        // used in lookbackinanger
257  unsigned char origorient;                       // original orientation of a piece under test (which way he is heading)  unsigned char origorient;                       // original orientation of a piece under test (which way he is heading)
258  unsigned char takerow,takecol;                          // can I be taken if I stop here?  unsigned char takerow,takecol;                          // can I be taken if I stop here?
# Line 258  unsigned char surns,surew;                                     // count Line 261  unsigned char surns,surew;                                     // count
261  unsigned char takena,takenb,takenc,takend,takene;               // used in canbetaken routines  unsigned char takena,takenb,takenc,takend,takene;               // used in canbetaken routines
262  unsigned char ezns1,ezew1;                      // used in surroundcount/enemyzero to reset enemy[][] to zero if surrounded=3  unsigned char ezns1,ezew1;                      // used in surroundcount/enemyzero to reset enemy[][] to zero if surrounded=3
263  // WEIGHTS  // WEIGHTS
264  unsigned char enemyweight=37;           // >36. weight of "enemy could get here but piece occupied by attacker"  //unsigned char enemyweight=37;         // >36. weight of "enemy could get here but piece occupied by attacker"
265  //char defaulttakeweight=5;     // default weight assigned to a TAKE  //char defaulttakeweight=5;     // default weight assigned to a TAKE
266  unsigned char takeweight;                       // weight assigned to a TAKE (calculated in "calctakeweight")  unsigned char takeweight;                       // weight assigned to a TAKE (calculated in "calctakeweight")
267  unsigned char cbtweight=4;      // weight to be applied to escape position if can be taken  //unsigned char cbtweight=4;    // weight to be applied to escape position if can be taken
268  unsigned char pacpointsx,pacpointsy,pacpointsa,pacpointsb;              // used to calculate points in subpacmanx        unsigned char pacpointsx,pacpointsy,pacpointsa,pacpointsb;              // used to calculate points in subpacmanx      
269  unsigned char pcheckns1,pcheckns2;              // used in taking pieces and checking for takes  unsigned char pcheckns1,pcheckns2;              // used in taking pieces and checking for takes
270  unsigned char pcheckew1,pcheckew2;              // used in taking pieces and checking for takes                  unsigned char pcheckew1,pcheckew2;              // used in taking pieces and checking for takes                
# Line 294  unsigned char turn;                            // determines end Line 297  unsigned char turn;                            // determines end
297  unsigned char compass[4];       // used in cantake (if compass[NORTH]=1 then means canbetaken if i move here from NORTH  unsigned char compass[4];       // used in cantake (if compass[NORTH]=1 then means canbetaken if i move here from NORTH
298  //unsigned char funca,funcb,funcc,funcd,funce,funcf;    // general purpose variables used in functions  //unsigned char funca,funcb,funcc,funcd,funce,funcf;    // general purpose variables used in functions
299  /* end of playerturn variables */  /* end of playerturn variables */
300    unsigned char xplayers;
301  /****************** MAIN PROGRAM ***********************************/  /****************** MAIN PROGRAM ***********************************/
302  main()  main()
303  {  {
# Line 435  if ( foundpiece == 0 ) Line 439  if ( foundpiece == 0 )
439                  {                  {
440                  if (players[mkey][oew]==1)                  if (players[mkey][oew]==1)
441                          {                          {
442                          calccantake();                          orientation=origorient;calccantake();
443                          if (( cantake==0 )&&(surrounded<3)) canbetaken(); // if cannot take can I be taken?                          if (( cantake==0 )&&(surrounded<3)) canbetaken(); // if cannot take can I be taken?
444                          //if ((mkey==kingns)&&(targetns!=kingns)&&(kingattacker[origorient]==1)) compass[origorient]=1; // don't move from plane of king                          //if ((mkey==kingns)&&(targetns!=kingns)&&(kingattacker[origorient]==1)) compass[origorient]=1; // don't move from plane of king
445                          if (compass[origorient]==0)                          if (compass[origorient]==0)
# Line 443  if ( foundpiece == 0 ) Line 447  if ( foundpiece == 0 )
447                                  foundpiece=1;                                  foundpiece=1;
448                                  ons=mkey;                                  ons=mkey;
449                                  }                                  }
                         //else {target[targetns][targetew]=hightarget;} // if target=1 then reset it  
450                          }                          }
451                  if ((players[mkey][oew]==2)||(players[mkey][oew]==3)) {foundpiece=9;}                  if ((players[mkey][oew]==2)||(players[mkey][oew]==3)) {foundpiece=9;}
452                  }                  }
# Line 455  if ( foundpiece == 0 ) Line 458  if ( foundpiece == 0 )
458                  {                                {              
459                  if (players[ons][mkey]==1)                  if (players[ons][mkey]==1)
460                          {                          {
461                          calccantake();                          orientation=origorient;calccantake();
462                          if (( cantake==0 )&&(surrounded<3)) canbetaken(); // if cannot take can I be taken?                          if (( cantake==0 )&&(surrounded<3)) canbetaken(); // if cannot take can I be taken?
463                          //if ((mkey==kingew)&&(targetew!=kingew)&&(kingattacker[origorient]==1)) compass[origorient]=1; // don't move from plane of king                          //if ((mkey==kingew)&&(targetew!=kingew)&&(kingattacker[origorient]==1)) compass[origorient]=1; // don't move from plane of king
464                          if (compass[origorient]==0)                          if (compass[origorient]==0)
# Line 463  if ( foundpiece == 0 ) Line 466  if ( foundpiece == 0 )
466                                  foundpiece=1;                                  foundpiece=1;
467                                  oew=mkey;                                  oew=mkey;
468                                  }                                  }
                         //else{target[targetns][targetew]=hightarget;} // if target=1 then reset it  
469                          }                          }
470                                    
471                  if ((players[ons][mkey]==2)||(players[ons][mkey]==3)) {foundpiece=9;}                            if ((players[ons][mkey]==2)||(players[ons][mkey]==3)) {foundpiece=9;}          
# Line 476  void targetselect() Line 478  void targetselect()
478  //unsigned char xloop;  //unsigned char xloop;
479  NEWTARGET:  NEWTARGET:
480  hightarget=0;   // contains highest value target  hightarget=0;   // contains highest value target
 foundpiece=0;           // set findpiece to "piece not found"  
481  for (ctns=0;ctns<11;ctns++)     // find the highest value for target  for (ctns=0;ctns<11;ctns++)     // find the highest value for target
482          {          {
483          for (ctew=0;ctew<11;ctew++)          for (ctew=0;ctew<11;ctew++)
# Line 495  for (ctns=0;ctns<11;ctns++)    // find the Line 496  for (ctns=0;ctns<11;ctns++)    // find the
496          }          }
497  // having found target we need to select a piece to move  // having found target we need to select a piece to move
498  compass[NORTH]=0;compass[SOUTH]=0;compass[EAST]=0;compass[WEST]=0;      // initialize compass array  compass[NORTH]=0;compass[SOUTH]=0;compass[EAST]=0;compass[WEST]=0;      // initialize compass array
499  for (mkey=ons; mkey>-1; mkey--) {origorient=NORTH;findpiecens();}        fb=9;foundpiece=0;              // set findpiece to "piece not found"
500  if ( foundpiece != 1 ) { foundpiece=0;target[targetns][targetew]=hightarget; }                                                                                                            origorient=NORTH;
501  for (mkey=ons; mkey<11; mkey++) {origorient=SOUTH;findpiecens();}        for (mkey=ons-1; mkey>-1; mkey--){findpiecens();}      
502    if ( foundpiece != 1 ) { foundpiece=0;target[targetns][targetew]=hightarget; }
503    origorient=SOUTH;                                                                                                              
504    for (mkey=ons+1; mkey<11; mkey++){findpiecens();}      
505  if ( foundpiece != 1 ) { foundpiece=0;target[targetns][targetew]=hightarget; }  if ( foundpiece != 1 ) { foundpiece=0;target[targetns][targetew]=hightarget; }
506  for (mkey=oew; mkey<11; mkey++) {origorient=EAST;findpieceew();}          origorient=EAST;
507    for (mkey=oew+1; mkey<11; mkey++){findpieceew();}      
508  if ( foundpiece != 1 ) { foundpiece=0;target[targetns][targetew]=hightarget; }  if ( foundpiece != 1 ) { foundpiece=0;target[targetns][targetew]=hightarget; }
509  for (mkey=oew; mkey>-1; mkey--) {origorient=WEST;findpieceew();}          origorient=WEST;
510    for (mkey=oew-1; mkey>-1; mkey--){findpieceew();}      
511  if ( foundpiece != 1 ) {target[targetns][targetew]=1;goto NEWTARGET;}   // if can still be taken select new target  if ( foundpiece != 1 ) {target[targetns][targetew]=1;goto NEWTARGET;}   // if can still be taken select new target
512    
513  cx=1+boardx+(boxsize*oew);      // piece x screen position  cx=1+boardx+(boxsize*oew);      // piece x screen position
# Line 815  void printpossiblemoves() Line 821  void printpossiblemoves()
821  /************************************************/  /************************************************/
822  void printarrowsorblanks()      // used in printdestinations  void printarrowsorblanks()      // used in printdestinations
823  {  {
824  char xplayers;          // player type at square under test  //char xplayers;                // player type at square under test
825  //zns=ns;               // another copy of ns (for computer turn)  //zns=ns;               // another copy of ns (for computer turn)
826  //zew=ew;               // another copy of ew (for computer turn)  //zew=ew;               // another copy of ew (for computer turn)
827  origorient=orientation; // original orientation (for computer turn)  origorient=orientation; // original orientation (for computer turn)
# Line 829  arrow=1; Line 835  arrow=1;
835  //arrowsy=cy+1;  //arrowsy=cy+1;
836  // orientation 0,1,2,3 = N, S, E, W  // orientation 0,1,2,3 = N, S, E, W
837  takerow=ns;takecol=ew; // will set below to be the opposite of the orientation  takerow=ns;takecol=ew; // will set below to be the opposite of the orientation
838  if ( orientation == NORTH ) { xplayers=players[xns-1][xew];takerow=xns+1;} // check north  if ( orientation == NORTH ) { a=xns-1;b=xew;xplayers=players[a][b];takerow=xns+1;} // check north
839  if ( orientation == SOUTH ) { xplayers=players[xns+1][xew];takerow=xns-1;} // check south  if ( orientation == SOUTH ) { a=xns+1;b=xew;xplayers=players[a][b];takerow=xns-1;} // check south
840  if ( orientation == EAST ) { xplayers=players[xns][xew+1];takecol=xew-1;} // check east  if ( orientation == EAST ) { a=xns;b=xew+1;xplayers=players[a][b];takecol=xew-1;} // check east
841  if ( orientation == WEST ) { xplayers=players[xns][xew-1];takecol=xew+1;}  // check west  if ( orientation == WEST ) { a=xns;b=xew-1;xplayers=players[a][b];takecol=xew+1;}  // check west
842  if ( xplayers == 0 )    // if adjacent square is OK mode  //if ( xplayers == 0 )  // if adjacent square is OK mode
843          {  //      {
         //arrow=1;  
844          while ( arrow == 1 ) // keep checking until cannot move          while ( arrow == 1 ) // keep checking until cannot move
845                  {                  {
846                  if (( orientation == NORTH ) && ( xns ))  // check north                  if (( orientation == NORTH ) && ( xns ))  // check north
# Line 874  if ( xplayers == 0 )   // if adjacent squa Line 879  if ( xplayers == 0 )   // if adjacent squa
879                  if (( orientation == EAST ) && ( xew == 10 ))   { arrow=0;}     // check east                  if (( orientation == EAST ) && ( xew == 10 ))   { arrow=0;}     // check east
880                  if (( orientation == WEST ) && ( xew == 0 ))    { arrow=0;}     // check west                  if (( orientation == WEST ) && ( xew == 0 ))    { arrow=0;}     // check west
881                  }                                        }                      
882          }          //}
883    //else
884    //      {
885    //      if ((fb==4)&&(xplayers==1)){enemy[a][b]+=37;}   // update enemy array if attacker one square away from defender
886    //      }
887  if ((fb==7)&&(xplayers>1))      // check to see if an attacker can be caught if he stays where he is  if ((fb==7)&&(xplayers>1))      // check to see if an attacker can be caught if he stays where he is
888          {          {
889          if ((players[takerow][takecol]==0)&&(enemy[takerow][takecol])&&(enemy[takerow][takecol]<enemyweight))          if ((players[takerow][takecol]==0)&&(enemy[takerow][takecol])&&(enemy[takerow][takecol]<37))
890                  {                  {
891                  if (target[takerow][takecol]>1){target[takerow][takecol]+=4; } // update adjacent target to provide escape route or place for someone else to occupy                  if (target[takerow][takecol]>1){target[takerow][takecol]+=4; } // update adjacent target to provide escape route or place for someone else to occupy
892                  if (orientation < EAST) // if heading north or south                  if (orientation < EAST) // if heading north or south
# Line 1187  players[ns][ew]=piecetype;     // update squ Line 1196  players[ns][ew]=piecetype;     // update squ
1196  row=ns;  row=ns;
1197  col=ew;  col=ew;
1198  drawpiece();                            // draw piece at new location - 18-10-2011  drawpiece();                            // draw piece at new location - 18-10-2011
1199  if ( piecetype == 3 )   { kingns=ns;kingew=ew;} // update king position (used by AI)  if (piecetype==3)       { kingns=ns;kingew=ew;} // update king position (used by AI)
1200  // having moved piece we now need to check for, and implement any TAKES  // having moved piece we now need to check for, and implement any TAKES
1201  if (piecetype > 1 )     // if defender  if (piecetype > 1 )     // if defender
1202          {          {
# Line 1299  if ((targetns>0)&&(targetns<10)) Line 1308  if ((targetns>0)&&(targetns<10))
1308          }          }
1309                    
1310  if ((targetew>0)&&(targetew<10))  if ((targetew>0)&&(targetew<10))
1311          {                {
1312          takena=targetns;takenb=targetew+1;takenc=targetns;takend=targetew-1;takene=10;          takena=targetns;takenb=targetew+1;takenc=targetns;takend=targetew-1;takene=10;
1313          subcanbetaken2();          subcanbetaken2();
1314          takena=targetns;takenb=targetew-1;takenc=targetns;takend=targetew+1;takene=20;          takena=targetns;takenb=targetew-1;takenc=targetns;takend=targetew+1;takene=20;
# Line 1323  pcheckew1=ew; Line 1332  pcheckew1=ew;
1332  pcheckew2=ew;  pcheckew2=ew;
1333  piecetype=players[ns][ew];      // obtain type of piece  piecetype=players[ns][ew];      // obtain type of piece
1334  //if ( fb==3) { piecetype=players[ctns][ctew];} // if computer turn set piecetype to piece being checked  //if ( fb==3) { piecetype=players[ctns][ctew];} // if computer turn set piecetype to piece being checked
1335  if ( fb==3 ) { piecetype=1;}    // default = ATTACKER  if ((fb==3)||(fb==9)) { piecetype=1;}   // default = ATTACKER
1336  if (piecetype > 1 )     // if defender  if (piecetype > 1 )     // if defender
1337          {          {
1338          p1=2;          p1=2;
# Line 1381  void subarrows() Line 1390  void subarrows()
1390  if (players[xns][xew])  if (players[xns][xew])
1391          {          {
1392          arrow = 0;      // !ok if piece occupied or corner square          arrow = 0;      // !ok if piece occupied or corner square
1393          if ((fb==4)&&(players[xns][xew]==1)) {enemy[xns][xew]+=enemyweight;}    // means enemy could get here if attacker moved elsewhere          if ((fb==4)&&(players[xns][xew]==1)) {enemy[xns][xew]+=37;}     // means enemy could get here if attacker moved elsewhere
1394          }          }
1395  if (( players[ns][ew] == 3)&&(tiles[xns][xew] == 4)) { arrow = 1; } // corner ok if king          if (( players[ns][ew] == 3)&&(tiles[xns][xew] == 4)) { arrow = 1; } // corner ok if king        
1396  }  }
# Line 1648  if (players[takena][takenb]>1) Line 1657  if (players[takena][takenb]>1)
1657                  {                  {
1658                  if (players[takenc][takend]==0)                  if (players[takenc][takend]==0)
1659                          {                          {
1660                          if ((enemy[takenc][takend]-takene)&&((enemy[takenc][takend]<enemyweight)||(enemy[takenc][takend]-enemyweight))) // 23-12-2011                          if ((enemy[takenc][takend]-takene)&&((enemy[takenc][takend]<37)||(enemy[takenc][takend]-37))) // 23-12-2011
1661                                  {                                  {
1662                                  compass[origorient]=1;  // e.g. compass[NORTH]=1 means canbetaken here if moving from NORTH                                  compass[origorient]=1;  // e.g. compass[NORTH]=1 means canbetaken here if moving from NORTH
1663                                  }                                  }
1664                          }                          }
1665                  if (takena==ons)        // if potentially moved piece is on same plane as adjacent enemy piece North/South                  /*
1666                    if ((flag==0)&&(takena==mkey))  // if potentially moved piece is on same plane as adjacent enemy piece North/South
1667                          {                          {
1668                          if ((takena-ons==1)||(ons-takena==1))   // if only one space away                          if ((takena-ons==1)||(ons-takena==1))   // if only one space away
1669                                  {                                  {
1670                                  if (enemy[takenc][takend]-enemyweight) {compass[origorient]=1;}                                  if (enemy[takenc][takend]-37) {compass[origorient]=1;}
1671                                  }                                  }
1672                          }                          }
1673                  if (takenb==oew)        // if potentially moved piece is on same plane as adjacent enemy piece EAST/WEST                  if ((flag==1)&&(takenb==mkey))  // if potentially moved piece is on same plane as adjacent enemy piece EAST/WEST
1674                          {                          {
1675                          if ((takenb-oew==1)||(oew-takenb==1))   // if only one space away                          if ((takenb-oew==1)||(oew-takenb==1))   // if only one space away
1676                                  {                                  {
1677                                  if (enemy[takenc][takend]-enemyweight) {compass[origorient]=1;}                                  if (enemy[takenc][takend]-37) {compass[origorient]=1;}
1678                                  }                                  }
1679                          }                          }
1680                    */
1681                  }                  }
1682  }  }
1683  /****************************/  /****************************/

Legend:
Removed from v.771  
changed lines
  Added in v.772

  ViewVC Help
Powered by ViewVC 1.1.26