繁体   English   中英

C代码几乎不需要帮助

[英]Little help needed with C Code

您好,我正在研究带有单个IR传感器的线路跟随器机器人。

已为我提供了模拟客户端+硬件。 我现在正在尝试模拟部分,其中必须沿着有效路径移动机器人,直到在迷宫中找到令牌为止。 然后停下来,给出坐标x,y,就像这样:“在x,y上找到令牌”。 服务器应用程序基于全局坐标系(例如东,西...)给出的坐标具有一个局部坐标系(例如,左,右,前...)。 在启动时选择机器人的方向,并在屏幕上显示实际坐标。 在将所有动作发送到服务器应用程序之前,在屏幕上给出对机器人方向的必要校正(“向左”,“向前”,“向右”,“转身”),然后将坐标发送给服务器。 示例:欢迎来到Sim机器人,在交叉点:3,5上,方向为:向南机器人移动:向左机器人前进:4,5 ...在以下位置找到令牌:2,1

有两种方法可以在本地或在远程服务器上模拟程序,在本地计算机上我可以看到迷宫,但是在远程服务器上总是会给我未知的迷宫。

我有两个C档案

roboproxy.c

    #include <stdarg.h>
#include "../h/RobotProxy.h"

/// initialized with ROBOT_FAIL
int currentIntersection = ROBOT_FAIL;

#if !defined(vasprintf)
static int vasprintf(char **s, const char *format, va_list ap) {
    /* Guess we need no more than 100 bytes. */
    int n, size = 100;
    va_list save_ap;

    if ((*s = (char*) malloc(size)) == NULL)
        return -1;
    while (1) {
        /* work on a copy of the va_list because of a bug
         in the vsnprintf implementation in x86_64 libc
         */
#ifdef __va_copy
        __va_copy(save_ap, ap);
#else
        save_ap = ap;
#endif
        /* Try to print in the allocated space. */
#ifdef _vsnprintf
        n = _vsnprintf(*s, size, format, save_ap);
#else
        n = vsnprintf(*s, size, format, save_ap);
#endif
        va_end(save_ap);
        /* If that worked, return the string. */
        if (n > -1 && n < size) {
            return n;
        }
        /* Else try again with more space. */
        if (n > -1) { /* glibc 2.1 */
            size = n + 1; /* precisely what is needed */
        } else { /* glibc 2.0 */
            size *= 2; /* twice the old size */
        }
        if ((*s = (char*) realloc(*s, size)) == NULL) {
            return -1;
        }
    }
}
#endif

#if !defined(asprintf)
static int asprintf(char **s, const char *format, ...) {
    va_list vals;
    int result;

    va_start(vals, format);
    result = vasprintf(s, format, vals);
    va_end(vals);
    return result;
}
#endif

/// Set the robot to the specified position
/// @ returns ROBOT_SUCCESS, ROBOT_FAIL or ROBOT_TOKENFOUND
int Robot_Move(int x, int y) {
    char* buffer;
    asprintf(&buffer, "{\"x\":%d,\"y\":%d}", x, y);
    char* query = url_encode(buffer);
    free(buffer);
    char* response = sendAndRecieve(concat(URL, query));

    if (response == NULL) {
        puts("Connection to server failed!");
        return ROBOT_FAIL;
    }

    if (contains(response, "\"code\":1")) {
        puts("Connection declined!");
        return ROBOT_FAIL;
    }

    if (contains(response, "\"code\":2")) {
        puts("Connection blocked!");
        return ROBOT_FAIL;
    }

    if (contains(response, "\"code\":3")) {
        printf("Invalid position! (x=%d, y=%d)\n", x, y);
        return ROBOT_FAIL;
    }

    int foundIntersection = 0;
    bool token = false;

    if (contains(response, "\"north\":true"))
        foundIntersection |= D_N;
    if (contains(response, "\"east\":true"))
        foundIntersection |= D_E;
    if (contains(response, "\"south\":true"))
        foundIntersection |= D_S;
    if (contains(response, "\"west\":true"))
        foundIntersection |= D_W;

    if (contains(response, "\"token\":true"))
        token = true;

    free(query);

    currentIntersection = foundIntersection;
    if (token)
        return ROBOT_TOKENFOUND;

    return ROBOT_SUCCESS;
}

/// Get the intersections of the current node that the robot is at
/// @ returns always the intersection at position x=0,y=0 if Robot_Move was not called first
int Robot_GetIntersections() {
    if (currentIntersection == ROBOT_FAIL)
        Robot_Move(0, 0);
    return currentIntersection;
}

客户端软件

    #include "../h/Configuration.h"

int main(void) {

    printf("Token: %d\n", Robot_Move(1, 0));
    printf("Intersection: %d\n", Robot_GetIntersections());

    return EXIT_SUCCESS;
}

当我在控制台中运行程序时,它给我Token为1,然后在找到任何令牌后将变为2。

因此在roboclientsim.c中,我添加了“

 If (Robot_Move()==2) //as Robot_move is returning the %d value for Token
printf("another token found ");

但是我的编译器给出的问题是robot_move很少有人争论,

谁能帮我解决这个问题?

Robot_Move接受2个参数:

int Robot_Move(int x, int y);

而且您可以毫无争议地调用它。

If (Robot_Move()==2)

那是错误。 我怀疑您想做的是:

int main(void) {
    int token = Robot_Move(1, 0);
    printf("Token: %d\n", token);
    printf("Intersection: %d\n", Robot_GetIntersections());

    if (token == 2) 
       printf("another token found ");

    return EXIT_SUCCESS;
}

或者可能:

int main(void) {
    int token = Robot_Move(1, 0);

    if (token == 2) {
       printf("Token: %d\n", token);
       printf("Intersection: %d\n", Robot_GetIntersections());
    } else {
       printf("another token found ");
    }

    return EXIT_SUCCESS;
}

检查方法int Robot_Move(int x, int y)的原型。调用它时需要提供2个参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM