Beispiel #1
0
bool whiteCheck() {
	int x, y;
	for (x = 0; x < 8; x++) {
		for (y = 0; y < 8; y++)
			if (table[x][y] == 'K')
				break;
		if (table[x][y] == 'K')
			break;
	}

	int i, j;
	for (i = 0; i < 8; i++)
		for (j = 0; j < 8; j++)
			if (table[i][j] == 'p' && blackPawn(i, j, x, y))
				return true;
			else if (table[i][j] == 'n' && knight(i, j, x, y))
				return true;
			else if (table[i][j] == 'b' && bishop(i, j, x, y))
				return true;
			else if (table[i][j] == 'r' && rook(i, j, x, y))
				return true;
			else if (table[i][j] == 'q' && queen(i, j, x, y))
				return true;
			else if (table[i][j] == 'k' && king(i, j, x, y))
				return true;
	return false;
}
Beispiel #2
0
bool blackCheck() {
	int x, y;
	for (x = 0; x < 8; x++) {
		for (y = 0; y < 8; y++)
			if (table[x][y] == 'k')
				break;
		if (table[x][y] == 'k')
			break;
	}

	int i, j;
	for (i = 0; i < 8; i++)
		for (j = 0; j < 8; j++)
			if (table[i][j] == 'P' && whitePawn(i, j, x, y))
				return true;
			else if (table[i][j] == 'N' && knight(i, j, x, y))
				return true;
			else if (table[i][j] == 'B' && bishop(i, j, x, y))
				return true;
			else if (table[i][j] == 'R' && rook(i, j, x, y))
				return true;
			else if (table[i][j] == 'Q' && queen(i, j, x, y))
				return true;
			else if (table[i][j] == 'K' && king(i, j, x, y))
				return true;
	return false;
}
Beispiel #3
0
int main()
{
	cout<<"Hi there! Nicolas programmed me to do cool things. This program simulates an 8x8 chess board where you control the knight. The program will tell you what to do. If you get bored and wish to leave enter quit whenever prompted for an input!\n";
	
	chess knight(8,1);

	knight.level1();

	return 0;
}
Beispiel #4
0
// Parses Donna chess format string for one side. Besides [K]ing, [Q]ueen, [R]ook,
// [B]ishop, and k[N]ight the following pseudo pieces could be specified:
//
// [M]ove:      specifies the right to move along with the optional move number.
//              For example, "M42" for Black means the Black is making 42nd move.
//              Default value is "M1" for White.
//
// [C]astle:    specifies castle right squares. For example, "Cg1" and "Cc8" encode
//              allowed kingside castle for White, and queenside castle for Black.
//              By default all castles are allowed, i.e. defult value is "Cc1,Cg1"
//              for White and "Cc8,Cg8" for Black. The actual castle rights are
//              checked during position setup to make sure they do not violate
//              chess rules. If castle rights are specified incorrectly they get
//              quietly ignored.
//
// [E]npassant: specifies en-passant square if any. For example, "Ed3" marks D3
//              square as en-passant. Default value is no en-passant.
//
//------------------------------------------------------------------------------
Position *setup_side(Position *self, char *side, uint8 color) {
    char *buffer = (char *)calloc(strlen(side) + 1, 1); // strdup() the string so we could tokenize it.
    strcat(buffer, side);

    char *move = strtok(buffer, ",");
    while(move) {
        if (move[0] == 'M') { // TODO: parse move number.
            self->color = color;
        } else {
            int sq, size = strlen(move);
            assert(size == 2 || size == 3); // Don't panic.

            if (size == 2) { // Pawn, ex. "e4".
                sq = square(move[1] - '1', move[0] - 'a');
            } else { // Piece, ex. "Qd1".
                sq = square(move[2] - '1', move[1] - 'a');
            }

            switch(move[0]) {
            case 'K':
                self->pieces[sq] = king(color);
                break;
            case 'Q':
                self->pieces[sq] = queen(color);
                break;
            case 'R':
                self->pieces[sq] = rook(color);
                break;
            case 'B':
                self->pieces[sq] = bishop(color);
                break;
            case 'N':
                self->pieces[sq] = knight(color);
                break;
            case 'E':
                self->enpassant = (uint8)sq;
                break;
            case 'C':
                if (sq == C1 + (int)color || sq == C8 + (int)color) {
                    self->castles |= castleQueenside[color];
                } else if (sq == G1 + (int)color || sq == G8 + (int)color) {
                    self->castles |= castleKingside[color];
                }
                break;
            default:
                // When everything else fails, read the instructions.
                self->pieces[sq] = pawn(color);
            }
        }
        move = strtok(NULL, ","); // <-- Get next move token.
    }
    free(buffer);

    return self;
}
Beispiel #5
0
void coins()                            // draw coins
{ for(int i=0;i<8;i++)
   for(int j=0;j<8;j++)
   { if(A[i][j].coin=='R')
      rook(A[i][j].x,A[i][j].y,A[i][j].color);
     if(A[i][j].coin=='H')
      knight(A[i][j].x,A[i][j].y,A[i][j].color);
     if(A[i][j].coin=='B')
      bishop(A[i][j].x,A[i][j].y,A[i][j].color);
     if(A[i][j].coin=='K')
      king(A[i][j].x,A[i][j].y,A[i][j].color);
     if(A[i][j].coin=='Q')
      queen(A[i][j].x,A[i][j].y,A[i][j].color);
     if(A[i][j].coin=='P')
      pawn(A[i][j].x,A[i][j].y,A[i][j].color);  }}
//------------------------------------------------------------------------------
Score threats(uint8 color, Bitmask hisAttacks, Bitmask herAttacks) {
    Score score = {};
    Position *p = eval.position;

    // Find weak enemy pieces: the ones under attack and not defended by
    // pawns (excluding a king).
    Bitmask weak = p->outposts[color^1] & hisAttacks & ~eval.attacks[pawn(color^1)];
    weak &= ~p->outposts[king(color^1)];

    if (weak) {

        // Threat bonus for strongest enemy piece attacked by our pawns,
        // knights, or bishops.
        Bitmask targets = weak & (eval.attacks[pawn(color)] | eval.attacks[knight(color)] | eval.attacks[bishop(color)]);
        if (targets) {
            Piece piece = strongest_piece(p, color^1, targets);
            score = add(score, bonusMinorThreat[kind(piece) / 2]);
        }

        // Threat bonus for strongest enemy piece attacked by our rooks
        // or queen.
        targets = weak & (eval.attacks[rook(color)] | eval.attacks[queen(color)]);
        if (targets) {
            Piece piece = strongest_piece(p, color^1, targets);
            score = add(score, bonusMajorThreat[kind(piece) / 2]);
        }

        // Extra bonus when attacking enemy pieces that are hanging. Side
        // having the right to move gets bigger bonus.
        int hanging = count(weak & ~herAttacks);
        if (hanging > 0) {
            if (p->color == color) {
                hanging++;
            }
            score = add(score, times(hangingAttack, hanging));
        }
    }

    return score;
}
Beispiel #7
0
void main()
{
 int gdriver = DETECT, gmode;
 initgraph(&gdriver, &gmode, "\\tc\\bgi");
 cleardevice();
// char str[1]; str[1]='\0';
 file.open("Info.TXT",ios::in|ios::binary);
 if(!file)
  cout<<"File couldnt be opened";
 x=20; y=getmaxy()-70;
 settextstyle(2,0,5);
 setvalues();
 board();
 coins();
 getch();
 typeon();
 getch();
 cleardevice();
 x=20; y=200;
 pic("ch pic");
 typeon();
 y+=20;
 typeon();
 getch();
 cleardevice();
 pic("ch pic2");
 x=20; y=200;
 typeon();
 getch();
 cleardevice();
 king(getmaxx()/2-30,30,1);
 x=20; y=100;
 typeon();
 getch();
 cleardevice();
 rook(getmaxx()/2-30,30,1);
 x=20; y=100;
 typeon();
 getch();
 cleardevice();
 bishop(getmaxx()/2-30,30,1);
 x=20; y=100;
 typeon();
 getch();
 cleardevice();
 queen(getmaxx()/2-30,30,1);
 x=20; y=100;
 typeon();
 getch();
 cleardevice();
 knight(getmaxx()/2-30,30,1);
 x=20; y=100;
 typeon();
 getch();
 cleardevice();
 pawn(getmaxx()/2-30,30,1);
 x=20; y=100;
 typeon();
 getch();
 cleardevice();
 pic("ch pic3");
 x=20; y=50;
 typeon();
 getch();
 file.close();
 closegraph();
}
Beispiel #8
0
int main(void){
	int i, j;
	int game;
	char board[BOARD_SIZE][BOARD_SIZE+1];
	char white[BOARD_SIZE+2+2][BOARD_SIZE+2+2];
	char black[BOARD_SIZE+2+2][BOARD_SIZE+2+2];

	char *pboard[BOARD_SIZE];
	char *pblack[BOARD_SIZE+4];
	char *pwhite[BOARD_SIZE+4];
	int bk_row;
	int bk_col;
	int wk_row;
	int wk_col;

#ifdef _DEBUG_
	FILE *f;
	f = fopen("in.txt", "r");
#endif

	for(i=0; i<BOARD_SIZE; i++){
#ifdef _DEBUG_
		fscanf(f, "%s", &board[i][0]);
#else
		scanf("%s", &board[i][0]);
#endif
	}

	for(i=0; i<BOARD_SIZE; i++)pboard[i] = &board[i][0];
	for(i=0; i<BOARD_SIZE+4; i++){
		pwhite[i] = &white[i][0];
		pblack[i] = &black[i][0];
	}


	game = 0;
	while(boardIsNotEmpty(&board[0][0])){
		game++;
		memset(white, 0, sizeof(white));
		memset(black, 0, sizeof(black));

		for(i=0; i<BOARD_SIZE; i++){
			for(j=0; j<BOARD_SIZE; j++){
				if(board[i][j] == 'k'){
					bk_row = i;
					bk_col = j;
				}
				if(board[i][j] == 'K'){
					wk_row = i;
					wk_col = j;
				}
			}
		}

		for(i=0; i<BOARD_SIZE; i++){
			for(j=0; j<BOARD_SIZE; j++){
				switch(board[i][j]){
				case('k'):	king(pboard, i, j, pblack, BOARD_SIZE, 'K');
							break;
				case('K'):	king(pboard, i, j, pblack, BOARD_SIZE, 'k');
							break;
				case('p'):	black[2+i+1][2+j-1]++;
							black[2+i+1][2+j+1]++;
							break;
				case('P'):	white[2+i-1][2+j-1]++;
							white[2+i-1][2+j+1]++;
							break;
				case('r'):	rook(pboard, i, j, pblack, BOARD_SIZE, 'K');
							break;
				case('R'):	rook(pboard, i, j, pwhite, BOARD_SIZE, 'k');
							break;
				case('b'):	bishop(pboard, i, j, pblack, BOARD_SIZE, 'K');
							break;
				case('B'):	bishop(pboard, i, j, pwhite, BOARD_SIZE, 'k');
							break;
				case('q'):	rook(pboard, i, j, pblack, BOARD_SIZE, 'K');
							bishop(pboard, i, j, pblack, BOARD_SIZE, 'K');
							break;
				case('Q'):	rook(pboard, i, j, pwhite, BOARD_SIZE, 'k');
							bishop(pboard, i, j, pwhite, BOARD_SIZE, 'k');
							break;
				case('n'):	knight(i, j, pblack);
							break;
				case('N'):	knight(i, j, pwhite);
							break;
				default:	break;
				}
			}
		}

#ifdef _DEBUG_
		printf("---------------------\n");
		printf("bk: %d %d  wk: %d %d\n", bk_row, bk_col, wk_row, wk_col);
		for(i=0; i<BOARD_SIZE; i++){
			for(j=0; j<BOARD_SIZE; j++){
				printf("%01d", white[2+i][2+j]);
			}
			printf("    ");
			for(j=0; j<BOARD_SIZE; j++){
				printf("%01d", black[2+i][2+j]);
			}
			printf("    ");
			for(j=0; j<BOARD_SIZE; j++){
				printf("%c", board[i][j]);
			}
			printf("\n");
		}
		printf("---------------------\n");
#endif

		if((white[2+bk_row][2+bk_col]==0)&&(black[2+wk_row][2+wk_col]==0)){
			printf("Game #%d: no king is in check.\n", game);
		}
		if(white[2+bk_row][2+bk_col] != 0){
			printf("Game #%d: black king is in check.\n", game);
		}
		if(black[2+wk_row][2+wk_col] != 0){
			printf("Game #%d: white king is in check.\n", game);
		}

		for(i=0; i<BOARD_SIZE; i++){
#ifdef _DEBUG_
			fscanf(f, "%s", &board[i][0]);
#else
			scanf("%s", &board[i][0]);
#endif
		}
	}

	return 0;
}
Beispiel #9
0
// Decodes a string in long algebraic notation and returns a move. If the
// notation is not recognized the move is returned as (Move)0.
//------------------------------------------------------------------------------
Move new_move_from_string(Position *self, char *e2e4) {
    bool queen_side_castle = !strcmp(e2e4, "0-0-0");
    bool king_side_castle = !strcmp(e2e4, "0-0") && !queen_side_castle;

    if (king_side_castle || queen_side_castle) {
        bool king_side_ok = false, queen_side_ok = false;
        can_castle(self, self->color, &king_side_ok, &queen_side_ok);
        if (king_side_castle && king_side_ok) {
            int from = self->king[self->color];
            int to = G1 + self->color * A8;
            return validate(self, new_castle(self, from, to));
        }
        if (queen_side_castle && queen_side_ok) {
            int from = self->king[self->color];
            int to = C1 + self->color * A8;
            return validate(self, new_castle(self, from, to));
        }
        return (Move)0;
    }

    char notation[strlen(e2e4)];
    char *src = e2e4, *dst = notation;

    // Strip all optional characters.
    while (*src) {
        if (strchr("KkQqRrBbNn12345678acdefgh", *src)) {
            *dst++ = *src;            
        }
        src++;
    }
    *dst = '\0';

    if (strlen(notation) >= 4 && strlen(notation) <= 5) {
        // Validate optional piece character to make sure the actual piece it
        // represents is there.
        Piece piece = pawn(self->color);
        switch (notation[0]) {
        case 'K':
            piece = king(self->color);
            break;
        case 'Q':
            piece = queen(self->color);
            break;
        case 'R':
            piece = rook(self->color);
            break;
        case 'B':
            piece = bishop(self->color);
            break;
        case 'N':
            piece = knight(self->color);
        }

        int sq;
        if (is_pawn(piece)) {
            sq = square(notation[1] - '1', notation[0] - 'a');
        } else {
            sq = square(notation[2] - '1', notation[1] - 'a');
        }

        if (sq >= A1 && sq <= H8) {
            if (self->pieces[sq] == piece) {
                return validate(self, new_move_from_notation(self, is_pawn(piece) ? notation : notation + 1));
            }
        }
    }

    return (Move)0;
}
Beispiel #10
0
// Non-capturing checks.
//------------------------------------------------------------------------------
MoveGen *generate_checks(MoveGen *self) {
    Position *p = self->p;
    int color = p->color, enemy = p->color ^ 1;
    int square = (int)p->king[enemy];

    // Non-capturing Knight checks.
    Bitmask prohibit = maskNone;
    Bitmask checks = knightMoves[square];
    Bitmask outposts = p->outposts[knight(color)];
    while (any(outposts)) {
        int from = pop(&outposts);
        add_piece_move(self, from, knightMoves[from] & checks & ~p->board);
    }

    // Non-capturing Bishop or Queen checks.
    checks = targets_for(p, square, bishop(enemy));
    outposts = p->outposts[bishop(color)] | p->outposts[queen(color)];
    while (any(outposts)) {
        int from = pop(&outposts);
        Bitmask squares = targets_for(p, from, bishop(enemy)) & checks & ~p->outposts[enemy];
        while (any(squares)) {
            int to = pop(&squares);
            Piece piece = p->pieces[to];
            if (!piece) {
                // Empty square: simply move a bishop to check.
                append(self, new_move(p, from, to));
            } else if (color(piece) == color && any(maskDiagonal[from][square])) {
                // Non-empty square occupied by friendly piece on the same
                // diagonal: moving the piece away causes discovered check.
                switch (kind(piece)) {
                case Pawn:
                    // Block pawn promotions (since they are treated as
                    // captures) and en-passant captures.
                    prohibit = maskRank[0] | maskRank[7];
                    if (p->enpassant) {
                        prohibit |= bit[p->enpassant];
                    }
                    add_pawn_move(self, to, targets(p, to) & ~p->board & ~prohibit);
                    break;
                case King:
                    // Make sure the king steps out of attack diaginal.
                    add_king_move(self, to, targets(p, to) & ~p->board & ~maskBlock[from][square]);
                    break;
                default:
                    add_piece_move(self, to, targets(p, to) & ~p->board);
                }
            }
        }
        if (is_queen(p->pieces[from])) {
            // Queen could move straight as a rook and check diagonally as a bishop
            // or move diagonally as a bishop and check straight as a rook.
            Bitmask squares = (targets_for(p, from, rook(color)) & checks) |
                  (targets_for(p, from, bishop(color)) & targets_for(p, square, rook(color)));
            add_piece_move(self, from, squares & ~p->board);
        }
    }

    // Non-capturing Rook or Queen checks.
    checks = targets_for(p, square, rook(enemy));
    outposts = p->outposts[rook(color)] | p->outposts[queen(color)];
    while (any(outposts)) {
        int from = pop(&outposts);
        Bitmask squares = targets_for(p, from, rook(enemy)) & checks & ~p->outposts[enemy];
        while (any(squares)) {
            int to = pop(&squares);
            Piece piece = p->pieces[to];
            if (!piece) {
                // Empty square: simply move a rook to check.
                append(self, new_move(p, from, to));
            } else if (color(piece) == color) {
                if (any(maskStraight[from][square])) {
                    // Non-empty square occupied by friendly piece on the same
                    // file or rank: moving the piece away causes discovered check.
                    switch (kind(piece)) {
                    case Pawn:
                        // If pawn and rook share the same file then non-capturing
                        // discovered check is not possible since the pawn is going
                        // to stay on the same file no matter what.
                        if (col(from) == col(to)) {
                            continue;
                        }
                        // Block pawn promotions (since they are treated as captures)
                        // and en-passant captures.
                        prohibit = maskRank[0] | maskRank[7];
                        if (p->enpassant) {
                            prohibit |= bit[p->enpassant];
                        }
                        add_pawn_move(self, to, targets(p, to) & ~p->board & ~prohibit);
                        break;
                    case King:
                        // Make sure the king steps out of attack file or rank.
                        prohibit = maskNone;
                        if (row(from) == row(square)) {
                            prohibit = maskRank[row(from)];
                        } else {
                            prohibit = maskFile[col(square)];
                        }
                        add_king_move(self, to, targets(p, to) & ~p->board & ~prohibit);
                        break;
                    default:
                        add_piece_move(self, to, targets(p, to) & ~p->board);
                    }
                }
            }
        }
    }

    // Non-capturing Pawn checks.
    outposts = p->outposts[pawn(color)] & maskIsolated[col(square)];
    while (any(outposts)) {
        int from = pop(&outposts);
        Bitmask target = maskPawn[color][square] & targets(p, from);
        if (any(target)) {
            append(self, new_pawn_move(p, from, pop(&target)));
        }
    }

    return self;
}