【问题标题】:How to keep track of input如何跟踪输入
【发布时间】:2021-09-11 17:37:35
【问题描述】:

您好,我正在尝试编写图案游戏,但问题是我不知道如何跟踪用户之前的输入,因此每当新游戏开始时,您都无法输入之前的字母。有没有办法编码?

示例如下:

Round 1 :
User: inputs -> A
(New Game)
User : inputs -> A // again
Invalid.

Round 2 :
User : inputs -> A
(New Game)
User : inputs -> B 
Valid

Round 3 :
User : inputs -> A
(New Game)
User : inputs -> A
Invalid

代码在这里:

#include <stdio.h>
#include <string.h>
int main ()
{
int i=0;
int roundCount = 1;
int pos = 0;


int over = 0;
int f = 1;
char G[2];

printf("Game Start!\n");
do
{
  printf("Round %d!\n", roundCount++);
  printf("Input selection upon prompt.\n");
  printf("Player 1: ");
  scanf(" %c", &G[0] );
  printf("Player 2: ");
  scanf(" %c", &G[1]);

 
if((G[0] == 'L' && G[1] == 'V') || (G[0] == 'V' && G[1] == 'S') || (G[0] == 'S' && G[1] == 'P') || (G[0] == 'P' && G[1] == 'R') || (G[0] == 'R' && G[1] == 'L') || (G[0] == 'R' && G[1] == 'S') || (G[0] == 'P' && G[1] == 'V') || (G[0] == 'S' && G[1] == 'L') || (G[0] == 'V' && G[1] == 'R') || (G[0] == 'L' && G[1] == 'P') )
  {
    f++;
    pos--;
    printf("Uno Wins!   Pos[%d]\n\n", pos);
  }
else if ((G[0] == 'R' && G[1] == 'P' ) || (G[0] == 'L' && G[1] == 'R') || (G[0] == 'R'&& G[1] == 'V') || (G[0] == 'P'&& G[1] =='S')|| (G[0] == 'P'&& G[1] == 'L') || (G[0] == 'S' && G[1] == 'R') || (G[0] == 'S' && G[1] == 'V') || (G[0] == 'L' && G[1] == 'S' )|| (G[0] == 'V'&& G[1] == 'P') || (G[0] == 'V'&& G[1] == 'L'))
  {
    f++;
    pos++;
    printf("Dos Wins    Pos[%d]!\n\n", pos);
  }
 else if ((G[0] == 'R' && G[1] == 'R' ) || (G[0] == 'P' && G[1] == 'P') || (G[0] == 'S' && G[1] == 'S') || (G[0] == 'L' && G[1] == 'L') || (G[0] == 'V' && G[1] == 'V'))
  {
    f++;
    pos = pos;
  }
 if (pos == -3 || pos == 3){
    printf("Game over\n");
    break;
  }
 if(f == 5 && pos != -3 && pos != 3)
  {
     if((G[0] == 'L' && G[1] == 'V') || (G[0] == 'V' && G[1] == 'S') || (G[0] == 'S' && G[1] == 'P') || (G[0] == 'P' && G[1] == 'R') || (G[0] == 'R' && G[1] == 'L') || (G[0] == 'R' && G[1] == 'S') || (G[0] == 'P' && G[1] == 'V') || (G[0] == 'S' && G[1] == 'L') || (G[0] == 'V' && G[1] == 'R') || (G[0] == 'L' && G[1] == 'P') )
  {
 printf("Uno Wins!\n");
    break;
  }
 else if((G[0] == 'R' && G[1] == 'P' ) || (G[0] == 'L' && G[1] == 'R') || (G[0] == 'R'&& G[1] == 'V') || (G[0] == 'P'&& G[1] =='S')|| (G[0] == 'P'&& G[1] == 'L') || (G[0] == 'S' && G[1] == 'R') || (G[0] == 'S' && G[1] == 'V') || (G[0] == 'L' && G[1] == 'S' )|| (G[0] == 'V'&& G[1] == 'P') || (G[0] == 'V'&& G[1] == 'L'))
  {
    printf("Dos Win!\n");
    break;
  }
}


} while (f < 5);

return 0;
}

见上面的代码, 它再次在 G[0] 和 G[1] 处重置,我无法跟踪先前的输入,因为它会重置。有没有办法改进这段代码?我很乐意为您提供帮助。

【问题讨论】:

  • 您希望跨调用保留哪些变量?您可以将它们写入文件。在程序启动时,您可以从文件中读取它们(如果存在)。游戏完成后,您可以取消链接文件
  • 我用更多示例编辑了问题@TomM

标签: c


【解决方案1】:

警告:这不是一个完整的解决方案,而是一个错误修复和一些清理:

您的scanf 序列为G[1] 生成了错误的值。最好使用fgets 来干净地获得完整的线路。

您的if 语句不实用。可以通过 switch/case 和一些宏技巧大大简化它们。

这是一些清理后的代码:

#include <stdio.h>
#include <string.h>

#define SWITCH(_g0,_g1) \
    (_g0 << 8) | (_g1 << 0)
#define CASE(_g0,_g1) \
    case SWITCH(_g0,_g1)

int
getval(const char *prompt)
{
    char *cp;
    char buf[100];
    int val;

    while (1) {
        printf("%s: ",prompt);
        fflush(stdout);

        cp = fgets(buf,sizeof(buf),stdin);

        // handle end of file
        if (cp == NULL) {
            val = -1;
            break;
        }

        // get the first char on the line
        val = buf[0];
        if (val != '\n')
            break;
    }

    return val;
}

int
main()
{
    int i = 0;
    int roundCount = 1;
    int pos = 0;

    int over = 0;
    int f = 1;
    int G[2];

    printf("Game Start!\n");

    do {
        printf("Round %d!\n", roundCount++);
        printf("Input selection upon prompt.\n");

        G[0] = getval("Player 1");
        if (G[0] < 0)
            break;
        G[1] = getval("Player 2");
        if (G[1] < 0)
            break;

        printf("DEBUG: %2.2X %2.2X\n",G[0],G[1]);

        switch (SWITCH(G[0],G[1])) {
        CASE('L','V'):
        CASE('V','S'):
        CASE('S','P'):
        CASE('P','R'):
        CASE('R','L'):
        CASE('R','S'):
        CASE('P','V'):
        CASE('S','L'):
        CASE('V','R'):
        CASE('L','P'):
            f++;
            pos--;
            printf("Uno Wins!   Pos[%d]\n\n", pos);
            break;

        CASE('R','P'):
        CASE('L','R'):
        CASE('R','V'):
        CASE('P','S'):
        CASE('P','L'):
        CASE('S','R'):
        CASE('S','V'):
        CASE('L','S'):
        CASE('V','P'):
        CASE('V','L'):
            f++;
            pos++;
            printf("Dos Wins    Pos[%d]!\n\n", pos);
            break;

        CASE('R','R'):
        CASE('P','P'):
        CASE('S','S'):
        CASE('L','L'):
        CASE('V','V'):
            f++;
            pos = pos;
            break;
        }

        if (pos == -3 || pos == 3) {
            printf("Game over\n");
            break;
        }

        if (f == 5 && pos != -3 && pos != 3) {
            switch (SWITCH(G[0],G[1])) {
            CASE('L','V'):
            CASE('V','S'):
            CASE('S','P'):
            CASE('P','R'):
            CASE('R','L'):
            CASE('R','S'):
            CASE('P','V'):
            CASE('S','L'):
            CASE('V','R'):
            CASE('L','P'):
                printf("Uno:Wins!\n");
                break;

            CASE('R','P'):
            CASE('L','R'):
            CASE('R','V'):
            CASE('P','S'):
            CASE('P','L'):
            CASE('S','R'):
            CASE('S','V'):
            CASE('L','S'):
            CASE('V','P'):
            CASE('V','L'):
                printf("Dos Win!\n");
                break;
            }
        }
    } while (f < 5);

    return 0;
}

【讨论】:

  • 清理工作很多。好的。有趣的宏使用。 Nit,变量 overi 未使用。
【解决方案2】:

查看您的代码,您似乎遇到了持久性问题!

您可以通过多种方式解决此问题,但您似乎只希望每个“组”游戏的数据都存在。在这种情况下,您需要在游戏发生的循环之外定义保存用户输入的数组,否则您需要在每个游戏过程中重新初始化数组。那将需要是另一个数组,与 G[2] 分开,这是您期望数据的最大大小。从您的代码看来,您玩了 5 场比赛,所以 5x2=10,也许是一个名为 Q[10] 的缓冲区?

当你完成一个循环时,将 Q 的下 2 个成员设置为用户输入,这应该可以工作。

在运行之间保留数据的另一种方法是使用文件来维护数据。这有点复杂,但在这个问题中的布局很好:

Write to .txt file?

希望对您有所帮助!如果有任何不清楚的地方,请告诉我:)

【讨论】:

  • 我用更多示例编辑了问题,请检查
  • @user14997606,看看上面克雷格的回答——他们真的做得很好!我认为他们对您的结构的改进,并使用缓冲区变量在比赛之间保持应该让您到达那里!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 2010-11-25
相关资源
最近更新 更多