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

关于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条回答 默认 最新

  • 爱音斯坦牛 全栈领域优质创作者 2022-11-30 14:30
    关注

    井字棋游戏可参考这个,十分详细,配套资源和视频
    http://t.csdn.cn/jvrmL

    #include<iostream> 
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<ctime>
    using namespace std;
    char play[9] = { '1','2','3','4','5','6','7','8','9' };
    void system()
    {
        system("color FD");
        system("date/t");
        system("time/t");
        system("title 井字棋 Ver:1.0.0");
    }
     
    void CSH()
    {
        cout << "######井字棋小游戏######" << endl;
        cout << "(*游戏规则:先掷骰子选出优先下子者,然后轮流下子,任一方若有三子连成一线,该方即获胜)" << endl;
        cout << "+---+---+---+\n"
                "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
                "+---+---+---+\n"
                 "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
                "+---+---+---+\n"
                "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
                "+---+---+---+\n"<<endl;
        cout<<"鸡你太美"<<endl;
        cout<<"你干嘛,哎呦"<<endl;
        cout<<"-------------------------------------------------------"<<endl;
    }
    void return_CSH()
    {
        memset(play,0,9);
        play[0] = '1';
        play[1] = '2';
        play[2] = '3';
        play[3] = '4';
        play[4] = '5';
        play[5] = '6';
        play[6] = '7';
        play[7] = '8';
        play[8] = '9';
    }
    class Player {
    public:
        Player() {}
        void Name(int i)
        {
            cout << "请为玩家" << i << "设置玩家名称:";
            cin >> name;
        }
        void Get_Name()
        {
            cout << name;
        }
        int Order()
        {
            cout << "请玩家 " << name << " 掷骰子:";
            system("pause");
            srand((unsigned)time(NULL));
            a = rand() % 6 + 1;
            cout << a << endl;
            return a;
        }
        int PD()
        {
            return a;
        }
        void XQ_1()
        {
            cout << "******游戏设置******" << endl;
            cout << "   1.O        2.X   " << endl;
            cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
            while (xq == 0)
            {
                cin >> xq;
                if (xq == 1)
                    q1 = 'O';
                else if (xq == 2)
                    q1 = 'X';
                else
                    cout << "命令错误!请重新输入合法选项:";
            }
        }
        void XQ_2()
        {
            cout << "请玩家 " << name << " 选择执什么棋(1 或者 2):";
            while (xq == 0)
            {
                cin >> xq;
                if (xq == 1)
                    q2 = 'O';
                else if (xq == 2)
                    q2 = 'X';
                else
                    cout << "命令错误!请重新输入合法选项:";
            }
        }
        void XS_Play()
        {
            int n;
            cout << "请玩家 " << name << " 输入下棋位置:";
            cin >> n;
            if (play[n - 1] != 'O' && play[n - 1] != 'X'&&n<10)
            {
                play[n - 1] = q1;
            }
            else
            {
                cout << "位置不合法!请重新选择:";
                cin >> n;
                if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
                {
                    play[n - 1] = q1;
                }
                else
                {
                    cout << "我感觉你是故意的";
                    exit(1);
                }
            }
            system("cls");
            cout << "+---+---+---+\n"
                    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
                    "+---+---+---+\n"
                    "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
                    "+---+---+---+\n"
                    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
                    "+---+---+---+\n";
        }
        void HS_Play()
        {
            int n;
            cout << "请玩家 " << name << " 输入下棋位置:";
            cin >> n;
            if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
            {
                play[n - 1] = q2;
            }
            else
            {
                cout << "位置不合法!请重新选择:";
                cin >> n;
                if (play[n - 1] != 'O' && play[n - 1] != 'X' && n < 10)
                {
                    play[n - 1] = q2;
                }
                else
                {
                    cout << "你又找茬是吧?";
                    exit(1);
                }
            }
            system("cls");
            cout << "+---+---+---+\n"
                    "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
                     "+---+---+---+\n"
                    "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
                     "+---+---+---+\n"
                    "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
                    "+---+---+---+\n";
        }
        friend int Get_Q1(Player& p1);
        friend int Get_Q2(Player& p2);
    private:
        string name;
        int a = 0;
        int xq = 0;
        char q1, q2;
    };
     
     
    int Get_Q1(Player& p1)
    {
        return p1.q1;
    }
    int Get_Q2(Player& p2)
    {
        return p2.q2;
    }
     
    int End()
    {
        if (play[0] == play[1] && play[1] == play[2] && play[0] != ' ')
        {
            if (play[0] == 'X')
            {
                return 1;
            }
            if (play[0] == 'O')
            {
                return 2;
            }
        }
        if (play[3] == play[4] && play[4] == play[5] && play[3] != ' ')
        {
            if (play[3] == 'X')
            {
                return 1;
            }
            if (play[3] == 'O')
            {
                return 2;
            }
        }
        if (play[6] == play[7] && play[7] == play[8] && play[6] != ' ')
        {
            if (play[6] == 'X')
            {
                return 1;
            }
            if (play[6] == 'O')
            {
                return 2;
            }
        }
     
        if (play[0] == play[3] && play[3] == play[6] && play[0] != ' ')
        {
            if (play[0] == 'X')
            {
                return 1;
            }
            if (play[0] == 'O')
            {
                return 2;
            }
        }
        if (play[1] == play[4] && play[4] == play[7] && play[1] != ' ')
        {
            if (play[1] == 'X')
            {
                return 1;
            }
            if (play[1] == 'O')
            {
                return 2;
            }
        }
        if (play[2] == play[5] && play[5] == play[8] && play[2] != ' ')
        {
            if (play[1] == 'X')
            {
                return 1;
            }
            if (play[1] == 'O')
            {
                return 2;
            }
        }
        if (play[0] == play[4] && play[4] == play[8] && play[0] != ' ')
        {
            if (play[0] == 'X')
            {
                return 1;
            }
            if (play[0] == 'O')
            {
                return 2;
            }
        }
        if (play[2] == play[4] && play[4] == play[6] && play[2] != ' ')
        {
            if (play[2] == 'X')
            {
                return 1;
            }
            if (play[2] == 'O')
            {
                return 2;
            }
        }
        return 0;
    }
     
    int main()
    {
        system();
        CSH();
        Player p1;
        Player p2;
        int xz;
        int j = 0;
        int xs = 0;
        p1.Name(1);
        p2.Name(2);
        while (xs == 0)
        {
            p1.Order();
            p2.Order();
            if (p1.PD() > p2.PD())
                cout << "\n玩家 ", p1.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 1;
            else if (p1.PD() < p2.PD())
                cout << "\n玩家 ", p2.Get_Name(), cout << " 先手\n" << endl, system("pause"), system("cls"), xs = 2;
            else
                cout << "双方点数一样大,请重新掷点数:" << endl;
        }
        if (xs == 1)
        {
            p1.XQ_1(), p2.XQ_2();
        }
        else if (xs == 2)
        {
            p2.XQ_1(), p1.XQ_2();
        }
        CSH();
        while (j < 5)
        {
            if (xs == 1)
            {
                p1.XS_Play();
                End();
                if (End() != 0)
                {
                    break;
                }
                p2.HS_Play();
                End();
                if (End() != 0)
                {
                    break;
                }
            }
            if (xs == 2)
            {
                p2.XS_Play();
                End();
                if (End() != 0)
                {
                    break;
                }
                p1.HS_Play();
                End();
                if (End() != 0)
                {
                    break;
                }
            }
            j++;
        }
        system("cls");
        cout<<"******游戏结束******"<<endl;
        cout << "+---+---+---+\n"
                "| " << play[0] << " | " << play[1] << " | " << play[2] << " |\n"
                "+---+---+---+\n"
                "| " << play[3] << " | " << play[4] << " | " << play[5] << " |\n"
                "+---+---+---+\n"
                "| " << play[6] << " | " << play[7] << " | " << play[8] << " |\n"
                "+---+---+---+\n";
        if (End() == 1)
        {
            if (Get_Q1(p1) == 'x')
                cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<<endl;
            else
                cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<<endl;
        }
        if (End() == 2)
        {
            if (Get_Q1(p1) == 'O')
                cout << "\n玩家 ", p1.Get_Name(), cout << " 获得胜利!"<<endl;
            else
                cout << "\n玩家 ", p2.Get_Name(), cout << " 获得胜利!"<<endl;
        }
        cout<<"------------------------------"<<endl; 
        cout<<"********是否继续游戏?********"<<endl;
        cout<<"1.重新开始        2.退出游戏"<<endl;
        cin>>xz;
        switch(xz)
        {
            case 1:return_CSH(),main();break;
            case 2:return 0;break;
            default:cout<<"指令错误,游戏终止!";break; 
        }
    }
    
    评论 编辑记录

报告相同问题?

问题事件

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

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料