纯C语言五子棋-----(带登录界面)

这是大一上学期的实训作品,有许多不足之处,我也就睁只眼闭只眼了,对学习C语言感兴趣的小伙伴可以借鉴一下,本身也是初学者代码有点乱-------(实在有疑问可以加QQ:2862841130)---废话不多说直接上代码!!!

建议一个函数一个函数看清楚在看逻辑,里面由于编辑器的原因有些地方的符号和格式有不同,半角全角格式自己调一下

#include <stdio.h>                                            //标准输入输出函数
#include <stdlib.h>                                            //通用工具函数和一些宏
#include <windows.h>                                        //调用win32API
#include<conio.h>                                            //鼠标点击事件
#include<stdbool.h>                                            //引入bool类型
#include<string.h>                                            //引入字符串函数,对用户信息进行操作
#include<time.h>                                            //获取随机数种子
#include <mmsystem.h>                                        //播放音乐
#pragma comment(lib,"winmm.lib")                            //获取windows播放媒体接口


#define wideth    113                                            //设置屏幕界面显示字符串的宽度
#define hghit    33                                            //设置屏幕界面显示字符串的高度
#define ScreenWideeth 180                                    //窗口宽度
#define ScreenHghit   40                                    //窗口高度
int HaveManyMan = 0;                                        //定义玩家总数

struct userImag
{
    char name[32];
    int  victory;
    int  defeate;
    int    score;
    char pow[32];
};                                                            //储存用户信息的结构体
struct cood
{
    int x;
    int y;
}XY;                                        //定义一个结构体储存当前光标的位置
char map[hghit][wideth] =                                    //定义全局地图变量并初始化
{
    { "=================================================================================================================" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |" },
    { "|---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---|" },
    { "=================================================================================================================" }
};

/*

地图格式是这样的:

*/
char name1[32] = "x", name2[32] = "o";                        //定义两个字符数组,储存姓名
int isOne;                                                    //定义一个bool型变量是否玩家一走棋


void initWindow();                                             //设置窗口大小,初始化界面信息
void ShowMap();                                                 //刷新地图
void ShowLeader(char *name[32]);                             //打印提示信息
void gotoxy(int x, int y);                                     //光标定位函数
void moveXy();                                                 //光标移动
bool IfGoOut(int x, int y);                                     //判断光标是否越界
bool ifDown();                                                 //落下棋子
bool ifHaveChess();                                             //判断当前位置是否有棋子
void IfGameOver();                                             //判断游戏是否结束
void ShowOverImag();                                         //游戏结束后打印提示信息(那个玩家获胜)
void initUserImag();                                         //创建一个文件,初始化用户信息,并排名(使用于菜单界面)
void LoadUserImag();                                         //读取并分析文件中的信息,并打印
int loadMeanu();                                             //加载菜单界面,登录菜单设置
bool UserLoad();                                             //用户登录
void UserLogin();                                             //用户注册
void UserChose();                                             //游戏选项
void UserRankingList();                                         //用户排行榜(使用于游戏界面)
bool IfInitUserImag();                                         //判断用户信息是否初始化成功
void menuChose();                                             //菜单功能选择
void starGame();                                             //开始游戏
void OutGame();                                                 //退出游戏
bool ifTheMan(char *name[32]);                                 //判断用户存在
bool ifThePow(char *name[32], char *pow[32]);                 //判断用户密码是否正确
void ShowSqure();                                             //此函数仅用于美化游戏界面排行榜
void ShowWhoDown();                                             //显示提示信息,现在是那位玩家走棋
void loadPlayTwo();                                             //登录第二个玩家的信息
void WinNext();                                                 //游戏结束后选择
void playBgm();                                                 //播放游戏背景音乐
void ShowTeamLog();                                             //显示队伍的图标
void color(short x);                                         //自定义函根据参数改变颜色 

void main()
{
    playBgm();
    if (!IfInitUserImag())initUserImag();
    menuChose();
}

void ShowMap()
{
    //system("color 0B");
    system("cls");
    //访问map数组的每一个成员,并打印出来
    for (int i = 0; i < hghit; i++)//需要c99标准
    {
        for (int j = 0; j < wideth; j++)
        {
            printf("%c", map[i][j]);
        }
        printf("\n");
    }
}

void ShowLeader(char *name[32])
{
    //system("color 06");
    printf("                                          操作说明:方向键实现移动光标\n");
    printf("                                                现在是玩家%s走棋\n", name);
    printf("                    目前实现了光标移动方向键控制(利用的是:GetAsyncKeyState()函数判断按键所处的状态)");
    //system("color 0");
    gotoxy(XY.x, XY.y);
}

void gotoxy(int x, int y)
{
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

void moveXy()
{
    if (GetAsyncKeyState(VK_DOWN) & 1)//&上1表示当前按键处于按下状态(位运算低位全部都变为0)
    {
        if (!IfGoOut(XY.x, XY.y + 2))
        {
            gotoxy(XY.x, XY.y + 2);
            XY.y += 2;
            return true;
        }
    }
    else if (GetAsyncKeyState(VK_UP) & 1)
    {
        if (!IfGoOut(XY.x, XY.y - 2))
        {
            gotoxy(XY.x, XY.y - 2);
            XY.y -= 2;
            return true;
        }
    }
    else if (GetAsyncKeyState(VK_LEFT) & 1)
    {
        if (!IfGoOut(XY.x - 4, XY.y))
        {
            gotoxy(XY.x - 4, XY.y);
            XY.x -= 4;
            return true;
        }
    }
    else if (GetAsyncKeyState(VK_RIGHT) & 1)
    {
        if (!IfGoOut(XY.x + 4, XY.y))
        {
            gotoxy(XY.x + 4, XY.y);
            XY.x += 4;
            return true;
        }
    }
    else return false;
}

bool IfGoOut(int x, int y)
{
    if (x > 3 && x < wideth - 4 && y>0 && y < hghit - 1)return false;
    else return true;
}

void initWindow()
{
    system("color FC");
    //设置界面大小
    system("mode con cols=180 lines=40");
    //初始化数据
    isOne = 1;//表示玩家一先走
    ShowMap();//初始化游戏地图

              //ShowLeader(name1);
}

bool ifDown()
{
    if (GetAsyncKeyState(' ') && !ifHaveChess())//如果按下空格键就实现落子
    {
        if (isOne == 1)//判断是否是玩家一下棋,并且当前位置没有棋子
        {
            map[XY.y][XY.x] = 'o';//玩家一用’o‘表示
            gotoxy(XY.x, XY.y);//移动光标到坐标上
            printf("o");//输出棋子
            isOne = 2;//表示下一步棋不是玩家一走
            return true;
        }
        else if (isOne == 2)//表示现在是玩家二走棋,并且当前位置没有棋子
        {
            map[XY.y][XY.x] = 'x';//玩家二用’x‘表示
            gotoxy(XY.x, XY.y);//移动光标到坐标上
            printf("x");//输出棋子
            isOne = 1;//表示下一步棋是玩家一走
            return true;
        }
    }
    return false;
}

bool ifHaveChess()
{
    if (map[XY.y][XY.x] == 'o' || map[XY.y][XY.x] == 'x')
        return true;
    else return false;
}

void IfGameOver()
{
    for (int j = 5; j <= hghit - 6; j += 2)
        for (int i = 12; i <= wideth - 12; i += 4)
        {
            if (map[j][i] == 'o')
            {
                if (map[j][i] == 'o'&&map[j][i - 4] == 'o'&& map[j][i + 4] == 'o'&& map[j][i - 8] == 'o'&&map[j][i + 8] == 'o')//横排全部相同
                {
                    isOne = 3;//代表玩家一获胜
                }
                else if (map[j - 2][i] == 'o'&& map[j + 2][i] == 'o'&& map[j - 4][i] == 'o'&&map[j + 4][i] == 'o'&& map[j][i] == 'o')//竖排全部相等
                {
                    isOne = 3;//代表玩家一获胜
                }
                else if (map[j][i] == 'o'&& map[j - 4][i - 8] == 'o'&&map[j - 2][i - 4] == 'o'&& map[j + 2][i + 4] == 'o'&&map[j + 4][i + 8] == 'o')//左上右下全部相等
                {
                    isOne = 3;//代表玩家一获胜
                }
                else if (map[j][i] == 'o'&&map[j + 4][i - 8] == 'o'&& map[j + 2][i - 4] == 'o'&&map[j - 2][i + 4] == 'o'&& map[j - 4][i + 8] == 'o')//左下右上全部相等
                {
                    isOne = 3;//代表玩家一获胜
                }
            }
            else if (map[j][i] == 'x')
            {
                if (map[j][i] == 'x'&&map[j][i - 4] == 'x'&& map[j][i + 4] == 'x'&& map[j][i - 8] == 'x'&&map[j][i + 8] == 'x')//横排全部相同
                {
                    isOne = 4;//代表玩家二获胜
                }
                else if (map[j - 2][i] == 'x'&& map[j + 2][i] == 'x'&& map[j - 4][i] == 'x'&&map[j + 4][i] == 'x'&& map[j][i] == 'x')//竖排全部相等
                {
                    isOne = 4;//代表玩家二获胜
                }
                else if (map[j][i] == 'x'&& map[j - 4][i - 8] == 'x'&&map[j - 2][i - 4] == 'x'&& map[j + 2][i + 4] == 'x'&&map[j + 4][i + 8] == 'x')//左上右下全部相等
                {
                    isOne = 4;//代表玩家二获胜
                }
                else if (map[j][i] == 'x'&&map[j + 4][i - 8] == 'x'&& map[j + 2][i - 4] == 'x'&&map[j - 2][i + 4] == 'x'&& map[j - 4][i + 8] == 'x')//左下右上全部相等
                {
                    isOne = 4;//代表玩家二获胜
                }
            }
        }

}

void ShowOverImag()
{
    if (isOne == 4)
    {
        gotoxy(wideth / 2 - 5, ScreenHghit - 2);
        printf("玩家<%s>[x]获胜", name1);
        gotoxy(wideth + 24, ScreenHghit - 17);
        printf("恭喜玩家<<<%s>>>获胜", name1);
        WinNext();
    }
    else if (isOne == 3)
    {
        gotoxy(wideth / 2 - 5, ScreenHghit - 2);
        printf("玩家<%s>[o]获胜", name2);
        gotoxy(wideth + 24, ScreenHghit - 16);
        printf("恭喜玩家<<<%s>>>获胜", name2);
        WinNext();
    }
    gotoxy(XY.x, XY.y);
}

void initUserImag()
{
    FILE *p;
    p = fopen("user.txt", "w");
    if (p == NULL)
    {
        printf("文件打开失败");
        exit(1);//结束程序
    }
    fputs("1 1 1 1\n", p);
    fputs("小豆芽 10000 0 123456\n", p);
    fputs("张三 130 0 123456\n", p);
    fputs("李四 0 99 123456\n", p);
    fputs("王五 22 66 123456\n", p);
    fputs("陈传豪 10000 632 123456\n", p);
    fputs("彭全通 10000 765 123456\n", p);
    fclose(p);
}

void LoadUserImag()
{
    FILE *p;
    p = fopen("user.txt", "r");
    if (p == NULL)
    {
        printf("文件打开失败");
        exit(1);
    }
    struct userImag    user[100];
    if (user == NULL)
    {
        printf("内存分配失败");
        exit(1);
    }
    int UserNub = 0;
    while (!feof(p))
    {
        fscanf(p, "%s", user[UserNub].name);
        fscanf(p, "%d", &user[UserNub].victory);
        fscanf(p, "%d", &user[UserNub].defeate);
        fscanf(p, "%s", &user[UserNub].pow);
        UserNub++;
    }
    HaveManyMan = UserNub;
    //对用户统计分数
    for (int i = 1; i < UserNub - 1; i++)
    {
        user[i].score = user[i].victory * 10 - user[i].defeate * 5;
    }
    //对用户重新排名
    for (int i = 1; i<UserNub - 1; i++)
        for (int j = 1; j < UserNub - 1; j++)
        {
            if (user[i].score < user[j].score)
            {
                struct userImag temp;
                temp = user[i];
                user[i] = user[j];
                user[j] = temp;
            }
        }
    //输出排行榜信息
    int hgh = 4, num = 1;
    for (int i = UserNub - 2; i >= 1; i--)
    {
        gotoxy(ScreenWideeth / 4 * 3 - 43, hgh);
        printf("第%d名 用户名:%s\t胜场:%d 场\t败场:%d场\t分数:%d分\n", num, user[i].name, user[i].victory, user[i].defeate, user[i].score);
        hgh += 2; num++;
    }
}

int loadMeanu()
{
    //设置界面大小
    system("mode con cols=180 lines=40");
    system("color F0");
    for (int i = 0; i < 40; i++)
    {
        gotoxy(0, i);
        printf("|*");
        gotoxy(ScreenWideeth - 2, i);
        printf("*|");
    }
    for (int i = 0; i < ScreenWideeth - 1; i++)
    {
        gotoxy(i, 0);
        printf("*");
        gotoxy(i, ScreenHghit - 1);
        printf("*");
    }
    for (int i = 0; i < ScreenWideeth / 2; i++)
    {
        gotoxy(i, ScreenHghit - 16);
        printf("*");
    }
    for (int i = 0; i < ScreenHghit - 1; i++)
    {
        gotoxy(ScreenWideeth / 2, i);
        printf("*");
    }
    gotoxy(ScreenWideeth / 4 * 3 - 8, 2);
    printf("::当前用户天梯排行榜::");
    LoadUserImag();//加载用户排行榜
    gotoxy(ScreenWideeth / 3 - 16, ScreenHghit / 2 - 10);
    printf("1,老用户登录");
    gotoxy(ScreenWideeth / 3 - 16, ScreenHghit / 2 - 8);
    printf("2,新用户注册");
    gotoxy(ScreenWideeth / 3 - 16, ScreenHghit / 2 - 6);
    printf("3,选择功能区");
    gotoxy(ScreenWideeth / 3 - 16, ScreenHghit / 2 - 4);
    printf("4,游戏不好玩");
    gotoxy(ScreenWideeth / 3 - 24, ScreenHghit / 2);
    printf("请输入序号,进行相应的操作:");
    gotoxy(2, 1);
    printf("||操作选项栏:->");
    gotoxy(ScreenWideeth / 2 + 1, 1);
    printf("||展示栏:->");
    gotoxy(2, ScreenHghit - 15);
    printf("游戏制作简介:->");
    gotoxy(2 + 4, ScreenHghit - 12);
    printf("游戏制作总负责人:---------赵智林");
    gotoxy(2 + 4, ScreenHghit - 10);
    printf("游戏背景音乐设计:---------彭全通");
    gotoxy(2 + 4, ScreenHghit - 8);
    printf("游戏界面布局设计:---------陈传豪");
    gotoxy(ScreenWideeth / 3 - 20, ScreenHghit / 2 + 2);
    printf("请输入1-4功能选项:");
    gotoxy(ScreenWideeth / 3 - 1, ScreenHghit / 2 + 2);
    int i = 0;
    while (scanf("%d", &i) == 1 && i > 0 && i < 5)
    {
        switch (i)
        {
        case 1: return 1; break;
        case 2:return 2; break;
        case 3:return 3; break;
        case 4:return 4; break;
        default:return 0; break;
        }
    }
}

bool UserLoad()
{
    int flag = 0;
    //清除操作区的内容
    for (int i = 6; i<ScreenHghit / 2 + 3; i++)
        for (int j = 6; j < ScreenWideeth / 2 - 2; j++)
        {
            gotoxy(j, i);
            printf(" ");
        }
    //打印提示信息
    gotoxy(30, 4);
    printf("        老用户登录       ");
    gotoxy(30, 5);
    printf("  (现在登录第一个玩家信息)   ");
    gotoxy(25, 10);
    printf("请输入你的用户名:(回车结束输入):");
    char name[36];
    gets(name);
    gets(name);
    char pow[36];
    fflush(stdin);
    while (1)//判断用户名是否存在
    {
        if (ifTheMan(name))break;
        gotoxy(25, 10);
        printf("抱歉!您输入的用户名不存在 !!!!!!!!   ");
        Sleep(3000);
        gotoxy(25, 10);
        printf("                                      ");
        gotoxy(25, 10);
        printf("请重新输入您的用户名:(回车结束输入):          ");
        gotoxy(25 + 40, 10);
        gets(name);
    }
    gotoxy(25, 12);
    printf("请输入你的的密码:(回车结束输入):");
    gets(pow);
    while (1)//判断用户密码是否正确
    {
        if (ifThePow(name, pow))break;
        gotoxy(25, 12);
        printf("抱歉!您输入的用户密码不对 !!!!!!!!   ");
        Sleep(3000);
        gotoxy(25, 12);
        printf("                                      ");
        gotoxy(25, 12);
        printf("请重新输入您的用户密码:(回车结束输入):          ");
        gotoxy(25 + 40, 12);
        gets(pow);
    }
    strcpy(name1, name);
    loadPlayTwo();
    starGame();
}

void UserLogin()
{
    //清除操作区的内容
    for (int i = 6; i<ScreenHghit / 2 + 3; i++)
        for (int j = 6; j < ScreenWideeth / 2 - 2; j++)
        {
            gotoxy(j, i);
            printf(" ");
        }
    //打印提示信息
    gotoxy(30, 7);
    printf("        新用户注册       ");
    gotoxy(25, 10);
    printf("请输入你要注册的用户名:(回车结束输入):");
    char name[36];
    gets(name);
    gets(name);
    
    
    while (1)
    {
        //if (ifTheMan(name))
            gotoxy(25, 10);
            printf("抱歉!您输入的用户名已存在,请重新输入:          ");
            Sleep(2000);
            gotoxy(25, 10);
            printf("                                                  ");
            gotoxy(25, 10);
            printf("请重新输入你要注册的用户名:(回车结束输入):      ");
            gotoxy(25 + 40 + 4, 10);
            gets(name);
            if (!ifTheMan(name))break;
    }
    gotoxy(25, 12);
    printf("请输入你需要设置的密码:(回车结束输入):");
    char pow[36];
    gets(pow);
    FILE *p;
    p = fopen("user.txt", "a");
    if (p == NULL)
    {
        exit(3);
    }
    fprintf(p, "%s 0 0 %s\n", name, pow);


    fclose(p);
    menuChose();
}

void UserRankingList()
{
    //由于时间有限加上我个人的原计划实施时的原因,就不加参数修改输出的起始坐标了
    //哈哈,可以说在偷懒,我想把功能做的更加的完善,所以就直接改567行的坐标了
    FILE *p;
    p = fopen("user.txt", "r");
    if (p == NULL)
    {
        printf("文件打开失败");
        exit(1);
    }
    struct userImag    user[100];
    if (user == NULL)
    {
        printf("内存分配失败");
        exit(1);
    }
    int UserNub = 0;
    while (!feof(p))
    {
        fscanf(p, "%s", user[UserNub].name);
        fscanf(p, "%d", &user[UserNub].victory);
        fscanf(p, "%d", &user[UserNub].defeate);
        fscanf(p, "%s", &user[UserNub].pow);
        UserNub++;
    }
    HaveManyMan = UserNub;
    //对用户统计分数
    for (int i = 1; i < UserNub - 1; i++)
    {
        user[i].score = user[i].victory * 10 - user[i].defeate * 5;
    }
    //对用户重新排名
    for (int i = 1; i<UserNub - 1; i++)
        for (int j = 1; j < UserNub - 1; j++)
        {
            if (user[i].score < user[j].score)
            {
                struct userImag temp;
                temp = user[i];
                user[i] = user[j];
                user[j] = temp;
            }
        }
    //输出用户信息和排名
    int hgh = 4, num = 1;
    for (int i = UserNub - 2; i >= 1; i--)
    {
        gotoxy(ScreenWideeth / 4 * 3 - 18, hgh);
        printf("第%d名 用户名:%s  \t\t分数:%d分\n", num, user[i].name, user[i].score);
        hgh += 2; num++;
    }
    ShowSqure();
    XY.x = wideth / 2;
    XY.y = hghit / 2 - 1;
    gotoxy(XY.x, XY.y);
}

bool IfInitUserImag()
{
    FILE *p;
    p = fopen("user.txt", "a");
    fclose(p);
    p = fopen("user.txt", "a+");
    if (p == NULL)
    {
        exit(4);
    }
    int temp[4];
    for (int i = 0; i<4; i++)
        fscanf(p, "%d", &temp[i]);
    fclose(p);
    int flag = 0;
    for (int i = 0; i < 4; i++)
    {
        if (temp[i] == 1)flag++;
    }
    if (flag == 4)return true;
    else return false;
}

void UserChose()
{
    //清除操作区的内容
    for (int i = 6; i<ScreenHghit / 2 + 3; i++)
        for (int j = 6; j < ScreenWideeth / 2 - 2; j++)
        {
            gotoxy(j, i);
            printf(" ");
        }
    //打印提示信息
    gotoxy(28, 4);
    printf("           背景音乐操作       ");
    gotoxy(28, 8);
    printf("        关闭背景音乐请按<F1>   ");
    gotoxy(28, 10);
    printf("        恢复背景音乐请按<F2>   ");
    gotoxy(28, 12);
    printf("         返回主菜单请按<F6> ");
    getchar();
    while (1)
    {
        if (GetAsyncKeyState(VK_F1)&1)
        {
            mciSendString(L"pause mysong", 0, 0, 0);//暂停播放
        }
        if (GetAsyncKeyState(VK_F2)&1)
        {
            mciSendString(L"resume  mysong", 0, 0, 0);//恢复播放
        }
        if (GetAsyncKeyState(VK_F6)&1)
        {
            
            break;
        }
    }
    getch();
    menuChose();
}

void menuChose()
{
    switch (loadMeanu())
    {
    case 1:UserLoad(); break;
    case 2:UserLogin(); break;
    case 3:UserChose(); break;
    case 4:OutGame(); break;
    }
}

void starGame()
{
    initWindow();
    UserRankingList();
    initUserImag();
    while (1)
    {
        fflush(stdin);
        moveXy();
        if (ifDown())
        {
            IfGameOver();
            ShowWhoDown();
            ShowOverImag();
        }
    }
    return 0;
}

void OutGame()
{
    //设置界面大小
    system("mode con cols=180 lines=40");
    system("color F0");
    for (int i = 0; i < 40; i++)
    {
        gotoxy(0, i);
        printf("|*");
        gotoxy(ScreenWideeth - 2, i);
        printf("*|");
    }
    for (int i = 0; i < ScreenWideeth - 1; i++)
    {
        gotoxy(i, 0);
        printf("*");
        gotoxy(i, ScreenHghit - 1);
        printf("*");
    }
    for (int i = 0; i < ScreenWideeth / 2; i++)
    {
        gotoxy(i, ScreenHghit - 16);
        printf("*");
    }
    for (int i = 0; i < ScreenHghit - 1; i++)
    {
        gotoxy(ScreenWideeth / 2, i);
        printf("*");
    }
    gotoxy(ScreenWideeth / 3 - 48, ScreenHghit / 2 - 10);
    printf("                小老弟真的觉得不好玩吗?");
    gotoxy(ScreenWideeth / 3 - 48, ScreenHghit / 2 - 6);
    printf("              如果是请按(1)不是请按(2)");
    int flag = 0;
    ShowTeamLog();
}

bool ifTheMan(char *name[32])
{
    FILE *p;
    p = fopen("user.txt", "r");
    if (p == NULL)
    {
        printf("文件打开失败");
        exit(1);
    }
    //玩家赢一场为10分,输一场扣5分
    //ch = (char *)malloc(sizeof(char) * 100);
    //在堆空间开辟内存
    struct userImag    user[100];
    //user = (struct userImag* )malloc(sizeof(struct userImag) * 66);
    //检查内存是否分配成功
    if (user == NULL)
    {
        printf("内存分配失败");
        exit(1);
    }
    int UserNub = 0;
    while (!feof(p))
    {
        fscanf(p, "%s", user[UserNub].name);
        fscanf(p, "%d", &user[UserNub].victory);
        fscanf(p, "%d", &user[UserNub].defeate);
        fscanf(p, "%s", &user[UserNub].pow);
        UserNub++;
    }
    //判断输入的用户信息是否正确
    int flag = 0;
    for (int i = 1; i < UserNub - 1; i++)
    {
        if (!strcmp(name, user[i].name))
            flag = 1;
    }
    fclose(p);
    //返回结果
    if (flag == 1)return true;
    else return false;
}

bool ifThePow(char *name[32], char *pow[32])
{
    FILE *p;
    p = fopen("user.txt", "r");
    if (p == NULL)
    {
        printf("文件打开失败");
        exit(1);
    }
    struct userImag    user[100];
    //user = (struct userImag* )malloc(sizeof(struct userImag) * 66);
    if (user == NULL)
    {
        printf("内存分配失败");
        exit(1);
    }
    int UserNub = 0;
    while (!feof(p))
    {
        fscanf(p, "%s", user[UserNub].name);
        fscanf(p, "%d", &user[UserNub].victory);
        fscanf(p, "%d", &user[UserNub].defeate);
        fscanf(p, "%s", &user[UserNub].pow);
        UserNub++;
    }
    //判断输入的用户信息是否正确
    int flag = 0;
    for (int i = 1; i < UserNub - 1; i++)
    {
        if (!strcmp(name, user[i].name) && !strcmp(pow, user[i].pow))
            flag = 1;
    }
    fclose(p);
    //返回结果
    if (flag == 1)return true;
    else return false;
}

void ShowSqure()
{
    gotoxy(ScreenWideeth / 4 * 3, 2);
    printf(">>>现有用户数据<<<");
    for (int i = wideth; i < ScreenWideeth - 1; i++)
    {
        gotoxy(i, 0);
        printf("*");
        gotoxy(i, ScreenHghit - 1);
        printf("*");
    }
    for (int j = 0; j < ScreenHghit; j++)
    {
        gotoxy(wideth, j);
        printf("*");
        gotoxy(ScreenWideeth - 1, j);
        printf("*");
    }
}

void ShowWhoDown()
{
    if (isOne == 1)
    {
        gotoxy(wideth / 2 - 13, ScreenHghit - 4);
        printf(">>>现在是玩家<<%s>>[o]走棋<<<", name2);
    }
    else if (isOne == 2)
    {
        gotoxy(wideth / 2 - 13, ScreenHghit - 4);
        printf(">>>现在是玩家<<%s>>[x]走棋<<<", name1);
    }
}

void loadPlayTwo()
{
    //清除操作区的内容
    for (int i = 6; i<ScreenHghit / 2 + 3; i++)
        for (int j = 6; j < ScreenWideeth / 2 - 2; j++)
        {
            gotoxy(j, i);
            printf(" ");
        }
    //打印提示信息
    gotoxy(30, 4);
    printf("        老用户登录       ");
    gotoxy(30, 5);
    printf("  (现在登录第二个玩家信息)   ");
    gotoxy(25, 10);
    printf("请输入你的用户名:(回车结束输入):");
    char name[36];
    gets(name);
    char pow[36];
    fflush(stdin);
    while (1)//判断用户名是否存在
    {
        if (ifTheMan(name))break;
        gotoxy(25, 10);
        printf("抱歉!您输入的用户名不存在 !!!!!!!!   ");
        Sleep(3000);
        gotoxy(25, 10);
        printf("                                      ");
        gotoxy(25, 10);
        printf("请重新输入您的用户名:(回车结束输入):          ");
        gotoxy(25 + 40, 10);
        gets(name);
    }
    gotoxy(25, 12);
    printf("请输入你的的密码:(回车结束输入):");
    gets(pow);
    while (1)//判断用户密码是否正确
    {
        if (ifThePow(name, pow))break;
        gotoxy(25, 12);
        printf("抱歉!您输入的用户密码不对 !!!!!!!!   ");
        Sleep(3000);
        gotoxy(25, 12);
        printf("                                      ");
        gotoxy(25, 12);
        printf("请重新输入您的用户密码:(回车结束输入):          ");
        gotoxy(25 + 40, 12);
        gets(pow);
    }
    strcpy(name2, name);
    fflush(stdin);
}

void WinNext()
{
    gotoxy(wideth + 24, ScreenHghit - 14);
    printf(" 退出游戏请按<方向上键>");
    gotoxy(wideth + 24, ScreenHghit - 12);
    printf(" 返回菜单请按<方向下键>");
    gotoxy(wideth + 24, ScreenHghit - 10);
    printf(" 请输入你的指令>>>");
    while (1)
    {
        if (GetAsyncKeyState(VK_UP))exit(5);
        if (GetAsyncKeyState(VK_DOWN))loadMeanu();
    }
    
}

void playBgm()
{
    mciSendString(TEXT("open bg2.mp3 alias mysong"), NULL, 0, NULL);
    mciSendString(TEXT("play mysong"), NULL, 0, NULL);
}

void ShowTeamLog()
{
    char log[18][36] =
    {
        ("========*==========================="),
        ("-------***=========================="),
        ("======*****========================="),
        ("======*****========================="),
        ("======*****========================="),
        ("======*****-----图案设计:彭泉通----"),
        ("======*****========================="),
        ("======*****-----特效设计:小豆芽----"),
        ("======*****========================="),
        ("======*****========================="),
        ("======*****========================="),
        ("@*****xx*xx*****@==================="),
        ("@****xxx*xxx****@==================="),
        ("======(###)========================="),
        ("======(###)========================="),
        ("======(###)========================="),
        ("======(###)========================="),
        ("=======***=========================="),
    };
    //随机种子生成
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY);
    srand((unsigned)time(NULL));//返回一个无符号整型数
    while (1)
    {
        gotoxy(ScreenWideeth/4*3-20,ScreenHghit/4);
        for (int i = 0; i < 18; i++)
        {
            for (int j = 0; j < 36; j++)
            {
                color(rand()%16);
                //Sleep(2);
                printf("%c", log[i][j]);
            }
            gotoxy(ScreenWideeth / 4 * 3 - 20, ScreenHghit / 4+i);
            if (GetAsyncKeyState('1') )
            {
                Sleep(1000);
                exit(1);
            }
            else if (GetAsyncKeyState('2'))
            {
                getch();
                Sleep(1000);
                menuChose();
            }
        }
        gotoxy(ScreenWideeth / 4 * 3-20, ScreenHghit / 4 );
    }
        
}

void color(short x)    //自定义函根据参数改变颜色 
{    if(x>=0 && x<=15)//参数在0-15的范围颜色
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);    //只有一个参数,改变字体颜色  
    else//默认的颜色白色        
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
 

//背景音乐文件自己选择

/*这是运行的界面

*/

猜你喜欢

转载自blog.csdn.net/xiao_dou_ya_cool/article/details/86490882