home *** CD-ROM | disk | FTP | other *** search
- /* LORD OF HOSTS - board.c --- Spielfeld generieren */
-
- #include "Lord.h"
-
- extern int NumPieces[2];
- extern struct Move Moves[MAXUNDO];
- extern int undopos,redotop,undobot,status,last_error;
-
- int type_of_board;
- UBYTE pdesc [16][4] = /* 16 Steine, 4 Werte, die jeden Stein
- kennzeichnen. Die vier Werte sind die Schrittwerte, die die Steine auf den
- Feldern (0,0),(1,0),(4,0) und (5,0) haben. Daraus lassen sich alle anderen
- ableiten. Drehung des Feldes um 90° bewirkt Vertauschung der Wertepaare */
- {
- { 1,1, 2,2 }, /* roter King */
- { 4,1, 3,2 }, /* roter Knight 1 */
- { 4,1, 3,2 }, /* " " 2 */
- { 3,2, 4,1 }, /* etc. */
- { 3,2, 4,1 },
- { 2,4, 3,1 },
- { 2,4, 3,1 },
- { 2,4, 3,1 },
- { 2,2, 1,1 }, /* weißer King */
- { 3,1, 2,4 }, /* etc. */
- { 3,1, 2,4 },
- { 3,1, 2,4 },
- { 4,1, 3,2 },
- { 4,1, 3,2 },
- { 3,2, 4,1 },
- { 3,2, 4,1 },
- };
-
- UBYTE fval[16][8][8]; /* Werte der Spielfelder für die Steine */
- UBYTE kval[16][8][8]; /* Werte, die (durch Daraufziehen) bereits den
- Spielern bzw. dem Computer bekannt sind. 16=Anzahl der Steine, 8,8= x,y */
- UBYTE whatsonboard[8][8];
- struct Piece ThePieces[16]; /* Spielsteine */
-
- void setup_stdboard(BOOL Rotated) /* BOOL=TRUE : Brett um 90° gedreht */
- {
- int p,i,rot= (Rotated) ? 4 : 0;
- clear_board();
- for (p=0; p<=15; ++p) /* Spielsteine 0 - 15 */
- {
- for (i=0; i<=3; i+=2) /* y-Koord. 0 - 3 */
- {
- /* Zeilen 0 und 2 sind identisch */
- fval[p][0+rot][i] = fval[p][2+rot][i] = pdesc[p][0];
- fval[p][1+rot][i] = fval[p][3+rot][i] = pdesc[p][1];
- fval[p][4-rot][i] = fval[p][6-rot][i] = pdesc[p][2];
- fval[p][5-rot][i] = fval[p][7-rot][i] = pdesc[p][3];
- /* Zeilen 1 und 3 auch */
- fval[p][0+rot][i+1] = fval[p][2+rot][i+1] = pdesc[p][3];
- fval[p][1+rot][i+1] = fval[p][3+rot][i+1] = pdesc[p][2];
- fval[p][4-rot][i+1] = fval[p][6-rot][i+1] = pdesc[p][1];
- fval[p][5-rot][i+1] = fval[p][7-rot][i+1] = pdesc[p][0];
- }
- for (i=4; i<=7; i+=2) /* y-Koord. 4 - 7 */
- {
- /* Zeilen 4 und 6 sind identisch */
- fval[p][0+rot][i] = fval[p][2+rot][i] = pdesc[p][2];
- fval[p][1+rot][i] = fval[p][3+rot][i] = pdesc[p][3];
- fval[p][4-rot][i] = fval[p][6-rot][i] = pdesc[p][0];
- fval[p][5-rot][i] = fval[p][7-rot][i] = pdesc[p][1];
- /* Zeilen 5 und 7 auch */
- fval[p][0+rot][i+1] = fval[p][2+rot][i+1] = pdesc[p][1];
- fval[p][1+rot][i+1] = fval[p][3+rot][i+1] = pdesc[p][0];
- fval[p][4-rot][i+1] = fval[p][6-rot][i+1] = pdesc[p][3];
- fval[p][5-rot][i+1] = fval[p][7-rot][i+1] = pdesc[p][2];
- }
- }
- InitPiece(0,RKING,GetValue(0,4,7),4,7);
- for (i=1; i<=7; ++i)
- {
- do p = rand() % 8; while (IsOccupied(p,7) != NOT_OCCUPIED);
- InitPiece(i,RKNIGHT,GetValue(i,(UBYTE)p,7),(UBYTE)p,7);
- }
-
- InitPiece(8,WKING,GetValue(8,3,0),3,0);
- for (i=9; i<=15; ++i)
- {
- do p = rand() % 8; while (IsOccupied(p,0) != NOT_OCCUPIED);
- InitPiece(i,WKNIGHT,GetValue(i,(UBYTE)p,0),(UBYTE)p,0);
- }
- for (i=0; i<=15; ++i)
- SetPiece(ThePieces[i],FALSE,FALSE);
- if (!Rotated) type_of_board = STD_NORM;
- else type_of_board = STD_ROTD;
- }
-
- void setup_rndboard(BOOL Balanced)
- {
- int p,i,j,top,r;
- UBYTE nums[64];
- clear_board();
- if (! Balanced) /* reiner Zufall */
- {
- for (p=0; p<=15; ++p)
- for (i=0; i<=7; ++i)
- for (j=0; j<=7; ++j)
- if (p % 8) /* Kein King */
- fval[p][i][j] = (rand() % 4) +1;
- else
- fval[p][i][j] = (rand() % 2) +1;
- }
- else /* balancierter Zufall, d.h. jede Nummer kommt in jedem */
- { /* Stein gleich oft vor */
- for (p=0; p<=15; ++p)
- {
- for (i=0; i<=15; ++i) /* Nummerntabelle */
- {
- nums[i] = 1;
- nums[i+16] = 2;
- nums[i+32] = (p % 8) ? 3 : 1; /* King kann 1 oder 2 ziehen */
- nums[i+48] = (p % 8) ? 4 : 2;
- }
- top = 63;
- for (i=0; i<=7; ++i)
- for (j=0; j<=7; ++j)
- {
- r = rand() % (top+1); /* Zufallszahl zw. 0 und top */
- fval[p][i][j] = nums[r];
- nums[r] = nums[top]; /* Tabelle erneuern */
- --top;
- }
- }
- }
- InitPiece(0,RKING,GetValue(0,4,7),4,7);
- for (i=1; i<=7; ++i)
- {
- do p = rand() % 8; while (IsOccupied(p,7) != NOT_OCCUPIED);
- InitPiece(i,RKNIGHT,GetValue(i,(UBYTE)p,7),(UBYTE)p,7);
- }
-
- InitPiece(8,WKING,GetValue(8,3,0),3,0);
- for (i=9; i<=15; ++i)
- {
- do p = rand() % 8; while (IsOccupied(p,0) != NOT_OCCUPIED);
- InitPiece(i,WKNIGHT,GetValue(i,(UBYTE)p,0),(UBYTE)p,0);
- }
- for (i=0; i<=15; ++i)
- SetPiece(ThePieces[i],FALSE,FALSE);
- if (Balanced) type_of_board = RND_BAL;
- else type_of_board = RND_UNB;
- }
-
- UBYTE GetValue(int Num, UBYTE x, UBYTE y)
- {
- return fval[Num][x][y];
- }
-
- UBYTE IsOccupied(UBYTE x, UBYTE y)
- {
- if (whatsonboard[x][y]<16) return whatsonboard[x][y];
- else return(NOT_OCCUPIED);
- }
-
- void InitPiece(int Num, int ID, int Val, UBYTE x, UBYTE y)
- {
- ThePieces[Num].Number = Num;
- ThePieces[Num].ID = ID;
- ThePieces[Num].Value = Val;
- ThePieces[Num].x = x;
- ThePieces[Num].y = y;
- ThePieces[Num].StillAlive = TRUE;
- whatsonboard[x][y] = Num;
- kval[Num][x][y] = Val; /* bekannter Zugwert */
- }
-
- void clear_board(void) /* Alles für Spielbeginn herrichten */
- {
- int h,i,j;
- for (h=0; h<=15; ++h)
- for (i=0; i<=7; ++i)
- for (j=0; j<=7; ++j)
- kval[h][i][j] = 0; /* Feld mit bekannten Zugwerten löschen */
- for (h=0; h<=7; ++h)
- for (i=0; i<=7; ++i)
- whatsonboard[h][i] = NOT_OCCUPIED;
- NumPieces[0] = NumPieces[1] = 8;
- undopos = redotop = undobot = 0;
- for (i=0; i<MAXUNDO; ++i)
- {
- Moves[i].fromx = Moves[i].tox = Moves[i].fromy = Moves[i].toy = 0;
- Moves[i].x_first = FALSE;
- Moves[i].Beaten = NOBODY;
- }
- status = IDLE|BOARD_UNTOUCHED; last_error = 0;
- }