微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

蛇和梯子算法中与增量有关的问题C

如何解决蛇和梯子算法中与增量有关的问题C

所以,我对函数turn()和game()有问题。每当我提示用户键盘上按下“ d”时,pspace(玩家的空间)的值就会重置为0。这是代码

int turns()
{
    int diceval;
    int pspace = 0;

    diceval = diceroll(diceval);

    pspace = pspace + diceval;
    printf("Die: %d\n",diceval);
    printf("%d\n",pspace);

    return pspace;
}

char promptrolldie()
{
    char droll;
    int diceval;
    int pspace = 0;
    printf("Press D to roll the die \n");
    scanf("%s",&droll);
    if (droll == 'd' || droll == 'D');
    turns();

    promptrolldie();
    return droll;
}

/* Void Game is the game itself. Yes,the game itself,packed in one function. */
void game()
{
    int snake1;
    int snake2;
    int snake3;
    int plspace;
    int playlives = 3;
    int ladder1,ladder2,ladder3;
        
    snake1 = snakepositions(snake1);
    snake2 = snakepositions(snake2);
    snake3 = snakepositions(snake3);
    ladder1 = ladderpositions(ladder1);
    ladder2 = ladderpositions(ladder2);
    ladder3 = ladderpositions(ladder3);
    printf("Snakes are located at %d,%d and %d\n",snake1,snake2,snake3);
    printf("Ladders are located at %d,ladder1,ladder3);

    promptrolldie();
    turns();
    
    plspace = turns(plspace);
    if (plspace == snake1 || plspace == snake2 || plspace == snake3)
    {
        playlives = playlives - 1;
        printf("You lost a life.\n");
        printf("Remaining lives = %d\n\n",playlives);
        plspace = (plspace - 3);
    } else if (plspace == ladder1 || plspace == ladder2 || plspace == ladder3)
    {
        plspace = plspace + 4;
    }
    if (playlives == 0)
    {
        printf("You ran out of lives.\n");
        printf("Game Over!");
    }
    if (plspace >= 50)
        printf("You win!");
    else
       printf("You landed on an empty block. \n");      
}

我粘贴了用于掷骰子(提示掷骰),增加玩家空间(回合)和调用回合功能的游戏功能函数。有什么办法可以保留以前的pspace值?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。