Пример #1
0
int main()
{
    char board[8][8];
    int kx = 1, ky = 7;   // Starting location for knight
 
    printf("x: %d, y: %d\n", kx, ky);   
    srand(time(NULL));    // Seed the random number generator
    
    int tx, ty;           // Target location
    
    do
    {
        tx = rand() % 8;  // Pick random target
        ty = rand() % 8;
    } while (tx == kx && ty == ky);
    
    init_board(board);
    board[ty][tx] = 'G';    // Place goal and knight
    board[ky][kx] = 'K';
    
    print_board(board);
    
    int moves = 0;
    
    while (tx != kx || ty != ky)
    {
        char move = get_move();
        board[ky][kx] = '_';
        int ret = move_knight(&kx, &ky, move); 
        board[ky][kx] = 'K';
        if (!ret)
        {
            printf("Invalid move.\n");
        }
        else
        {
            print_board(board);
            moves++;
        }
    }
    printf("You won in %d moves!\n", moves);
}
Пример #2
0
static int
find_move(char *board)
{
    char *piece;
    int x, y;

    if ((piece = strpbrk(board, "prnbqk")) == NULL)
        return EXIT_FAILURE;

    get_coords(board, piece, &x, &y);

    switch(*piece) {
    case 'p':
        move_pawn(board, x, y);
        break;
    case 'r':
        move_rook(board, x, y);
        break;
    case 'n':
        move_knight(board, x, y);
        break;
    case 'b':
        move_bishop(board, x, y);
        break;
    case 'q':
        move_queen(board, x, y);
        break;
    case 'k':
        move_king(board, x, y);
        break;
    default:
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}