예제 #1
0
//generate all possible moves
void Gen(void)
{
	short i, j, k, n, p, x, y, t, fcannon;

	gen_end[ply] = gen_begin[ply];

	for (i=0; i < BOARD_SIZE; i++){
		if (color[i]==side)
		{
			p = piece[i];//piece kind
			for (j=0; j<8; j++)
			{
				if (!offset[p][j]) break;//find possible next position
				x = mailbox90[i]; //offset in mailbox128
				fcannon = 0;
				if (p==ROOK || p==CANNON) n = 9; else n = 1;//
				for (k=0; k<n; k++)
				{
					//  get offset result for (p==PAWN && side==LIGHT)
					//there is no offset table for it
					if (p==PAWN && side==LIGHT) x -= offset[p][j]; else x += offset[p][j];

					y = mailbox182[x];
					//  t for the position in the board of this piece ,
					//according which side the piece is 
					if (side == DARK) t = y; else t = 89-y;
					if (y==-1 || (legalposition[t] & maskpiece[p])==0) break;

					if (!fcannon)
					{
						if (color[y]!=side)
							switch (p)
						{
							case KNIGHT: if (color[i+knightcheck[j]]==EMPTY) Gen_push(i, y); break;
							case ELEPHANT:if (color[i+elephancheck[j]]==EMPTY) Gen_push(i, y); break;
							case CANNON: if (color[y]==EMPTY) Gen_push(i, y); break;
							default: Gen_push(i, y);
						}
						if (color[y]!=EMPTY) { if (p==CANNON) fcannon++; else break; }
					}
					else   /* CANNON switch */
					{
						if (color[y] != EMPTY)
						{
							if (color[y]==xside) Gen_push(i, y);
							break;
						}
					}
				} /* for k */
			} /* for j */
		}
	}
	gen_end[ply+1] = gen_end[ply]; gen_begin[ply+1] = gen_end[ply];
	brandtotal += gen_end[ply] - gen_begin[ply]; gencount++;
}
void Gen(void)
{
    short i, j, k, n, p, x, y, t, fcannon;

    gen_end[ply] = gen_begin[ply];

    for (i=0; i < BOARD_SIZE; i++)
        if (color[i]==side)
        {
            p = piece[i];
            for (j=0; j<8; j++)
            {
                if (!offset[p][j]) break;
                x = mailbox90[i]; fcannon = 0;
                if (p==ROOK || p==CANNON) n = 9; else n = 1;
                for (k=0; k<n; k++)
                {
                    if (p==PAWN && side==LIGHT) x -= offset[p][j]; else x += offset[p][j];

                    y = mailbox182[x];
                    if (side == DARK) t = y; else t = 89-y;
                    if (y==-1 || (legalposition[t] & maskpiece[p])==0) break;
                    if (!fcannon)
                    {
                        if (color[y]!=side)
                            switch (p)
                            {
                            case KNIGHT: if (color[i+knightcheck[j]]==EMPTY) Gen_push(i, y); break;
                            case ELEPHAN:if (color[i+elephancheck[j]]==EMPTY) Gen_push(i, y); break;
                            case CANNON: if (color[y]==EMPTY) Gen_push(i, y); break;
                            default: Gen_push(i, y);
                            }
                        if (color[y]!=EMPTY) { if (p==CANNON) fcannon++; else break; }
                    }
                    else   /* CANNON switch */
                    {
                        if (color[y] != EMPTY)
                        {
                            if (color[y]==xside) Gen_push(i, y);
                            break;
                        }
                    }
                } /* for k */
            } /* for j */
        }

    gen_end[ply+1] = gen_end[ply]; gen_begin[ply+1] = gen_end[ply];
    brandtotal += gen_end[ply] - gen_begin[ply]; gencount++;
}