丙壬交辉755 2022-11-30 00:35 采纳率: 80%
浏览 201
已结题

关于C语言设计井字棋游戏问题的实现

任务
用C语言设计一个井字棋游戏,包含以下内容:
1.用结构体设计一个可以创建账户名称,密码累积输赢数字且账户相关信息能够存储在数据文件中,并由程序访问
2.设计一个3*3井字棋游戏,包括游戏开始菜单设计,棋盘的生成与初始化与棋盘打印,玩家下棋和电脑下棋,判断输赢和统计输赢次数,且在开始游戏后可以选择玩家先手或电脑先手
本人写了一段C语言代码,但是代码无法运行,求各位帮我看看我的程序的问题在哪以及如何改正,以及创建账户和密码和统计输赢次数的部分如何实现?

本人代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


typedef struct The_users
{
    char id[15];
    char pwd[20];
}users;

void Create_File()
{
    FILE *fp;
    if ((fp = fopen("users.txt","rb"))==NULL)
    {
         if ((fp = fopen("users.txt","wb+"))==NULL)
         {
             printf("Unable to create a file!\n");
             exit(0);
         }
    }
}

void registers()
     {
         users c,d;
         FILE *fp;
         char temp[20];
         int count = 0;
         printf("Welcome to the registration screen!\n");
         Sleep(1000);
         fp = fopen("users.txt","r");

         fread(&d, sizeof(struct The_users), 1, fp);
          printf("Please enter your account number\n");
         scanf("%s",&c.id);



         while (1)
         {
             if (strcmp(c.id, d.id))
             {
                 if (!feof(fp))


                 {
                     fread(&d, sizeof(struct The_users), 1, fp);
                 }
                 else
                     break;
             }
             else

             {
                 printf("This username already exists!\n");
                 Sleep(1000);
                 fclose(fp);
                 return;
             }
         }
         printf("Please enter your password\n");
         scanf("%s",&c.pwd);
         printf("Please confirm the password\n");
         scanf("%s",&temp);
         do{
             if(!strcmp(c.pwd,temp)){
                 fp = fopen("users.txt","c");
                 fwrite(&c, sizeof(struct The_users), 1, fp);
                 printf("Account registration successful!\n");
                 Sleep(500);
                 fclose(fp);
                 return;
             }else{
                 printf("Password does not match, please retype£¡\n");
                 scanf("%s",&c.pwd);
                 printf("Please confirm the password\n");
                 scanf("%s",&temp);
             }
         }while(1);
     }

void  Input_login()
 {
    users c,d;

    FILE *fp;
    printf("Welcome to the login screen!\n");
         Sleep(1000);
    fp = fopen("users.txt","r");

    fread(&d, sizeof(struct The_users), 1, fp);
    printf("Please enter your account number\n");
    scanf("%s",&c.id);

            while (1)
            {
               if (strcmp(c.id, d.id)==0)
               {
                   break;
               }

               else
               {
                   if (!feof(fp))

                   {
                       fread(&d, sizeof(struct The_users), 1, fp);
                   }

                   else
                   {
                       printf("User name does not exist, please re-enter£¡\n");
                       Sleep(500);
                       fclose(fp);
                       return;
                   }
               }
            }
            printf("Please enter your password\n");
           scanf("%s",&c.pwd);
    do{

        if (strcmp(c.pwd, d.pwd)==0)

           {
              fclose(fp);
              printf("Login successfully!");
              Sleep(500);
              return;
              }
           else
           {    printf("The password is incorrect, please re-enter the password\n");
                   scanf("%s",&c.pwd);
           }
    }while(strcmp(c.pwd, d.pwd)==0);

}

void menu()
{
    printf("****************************\n");
    printf("**       1.Start the game       **\n");
    printf("****************************\n");
    printf("**       0.Exit the game       **\n");
    printf("****************************\n");
}

int whom_act()
{
    int input = 0;
    printf("****************************\n");
    printf("**       1.Computer first chess       **\n");
    printf("****************************\n");
    printf("**       0.Players play chess first      **\n");
    printf("****************************\n");
    while (1)
    {
        printf("Please choose who plays first:>");
        scanf_s("%d", &input);
        if (input == 1)
            return 1;
        else if (input == 0)
            return 0;
        else
            printf("An error occurred, please reselect\n");
    }
}

void init_chess(char arr[3][3])
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            arr[i][j] = ' ';
        }
    }
}

void print_chess(char arr[3][3])
{
    printf("\n");
    for (int i = 0; i < 3; i++)
    {
        printf("%c | %c| %c\n", arr[i][0], arr[i][1], arr[i][2]);
        if (i < 2)
        {
            printf("—+—+—\n");
        }
    }
    printf("\n");
}

int chess_full(char arr[3][3])
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if (arr[i][j] == ' ')
                return 0;
        }
    }
    return 1;
}


char judge_win(char arr[3][3])
{
    for (int i = 0; i < 3; i++)
    {
        if ((arr[i][0] == arr[i][1]) && (arr[i][1] == arr[i][2]) && (arr[i][0] != ' '))
            return arr[i][0];
        else if ((arr[0][i] == arr[1][i]) && (arr[1][i] == arr[2][i]) && (arr[0][i] != ' '))
            return arr[0][i];
        else if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2]) && (arr[1][1] != ' '))
            return arr[1][1];
        else if ((arr[0][2] == arr[1][1]) && (arr[1][1] == arr[2][0]) && (arr[1][1] != ' '))
            return arr[1][1];
    }
    return ' ';
}

void player_turn(char arr[3][3])
{
    int x = 0;
    int y = 0;
    while (1)
    {
        printf("Select the position to play (enter the corresponding x,y coordinates):>");
        scanf_s("%d,%d", &x, &y);
        if (arr[--x][--y] == ' ')
        {
            arr[x][y] = 'X';
            break;
        }
        else
        {
            printf("There is already a piece in that position, please reselect it.\n");
        }
    }
}

void computer_turn(char arr[3][3])
{


    srand((unsigned)time(NULL));
    while (1)
    {
        int x = rand() % 3;
        int y = rand() % 3;
        if (arr[x][y] == ' ')
        {
            arr[x][y] = 'O';
            break;
        }
    }
}



int main()
{
    int input = 0;
    int a = 0;
    int b = 0;
    char arr[3][3] = { 0 };
    do
    {
        menu();
        printf("Please select the action you want to perform:>");
        scanf_s("%d", &input);
        switch (input)
        {
        case 1:
            init_chess(arr);
            if (whom_act())
            {
                while (1)
                {
                    computer_act(arr);
                    print_chess(arr);
                    if ((jud_win(arr)) == 'X')
                    {
                        printf("You won the competition!\n");
                        a++;
                        break;
                    }
                    else if ((jud_win(arr)) == 'O')
                    {
                        printf("You lose!\n");
                        b++;
                        break;
                    }
                    player_act(arr);
                    print_chess(arr);
                    if ((jud_win(arr)) == 'X')
                    {
                        printf("You won the competition!\n");
                        a++;
                        break;
                    }
                    else if ((jud_win(arr)) == 'O')
                    {
                        printf("You lose!\n");
                        b++;
                        break;
                    }
                    else if (chess_full(arr))
                    {
                        printf("Tie!\n");
                        break;
                    }
                }
            }
            else
            {
                while (1)
                {
                    print_chess(arr);
                    player_act(arr);
                    print_chess(arr);
                    if ((jud_win(arr)) == 'X')
                    {
                        printf("You won the competition!\n");
                        a++;
                        break;
                    }
                    else if ((jud_win(arr)) == 'O')
                    {
                        printf("You lose!\n");
                        b++;
                        break;
                    }
                    computer_act(arr);
                    print_chess(arr);
                    if ((jud_win(arr)) == 'X')
                    {
                        printf("You won the competition!\n");
                        a++;
                        break;
                    }
                    else if ((jud_win(arr)) == 'O')
                    {
                        printf("You lose!\n");
                        b++;
                        break;
                    }
                    else if (chess_full(arr))
                    {
                        printf("Tie!\n");
                        break;
                    }
                }
            }
            break;
        case 0:
            printf("The numbers of wins are %d ",a);
            printf("The numbers of losses are %d ",b);
            printf("Exiting the game\n");
            break;
        }
    } while (input);
    return 0;
}


  • 写回答

9条回答

      报告相同问题?

      相关推荐 更多相似问题

      问题事件

      • 已结题 12月7日
      • 赞助了问题酬金15元 11月30日
      • 修改了问题 11月30日
      • 修改了问题 11月30日
      • 展开全部

      悬赏问题

      • ¥15 uniapp实现钉钉【发起审批实例】上传文件到钉盘,后端为C#.Net处理
      • ¥15 求深信服VDI基准测试工具Sangfor DTP
      • ¥20 使用python中pandas进行数据分析
      • ¥20 使用python中pandas进行数据分析
      • ¥15 导入数据,并且以utf-8为编码格式,但是以上代码报错,求解答
      • ¥15 关于#游戏程序#的问题:端 玩wy服务器10几分钟闪退一次没有任何提示,连启动器一起闪退 本人电脑5800h 3060 16运内 win11 求解答
      • ¥15 做一个手游私服需要什么条件?
      • ¥15 关于企业微信,调用官方 微盘api 时候access_token过期的问题?
      • ¥15 请问这个C语言代码的界面怎样修改
      • ¥15 QT for Android 开发的App后台切换后卡死,没有响应