猜拳遊戲

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

//宏定義
#define NAME_SIZE 20
#define PASSWD_SIZE 20
#define NAME "zhangsan"
#define PASSWD "123456"
#define TRUE 1
/*行緩衝*/
#define MFLUSH {int ch = 0; while((ch = getchar()) != '\n'  &&  ch != EOF) ;}

#define CONTINUE(X)  {printf("%s",X); getchar();}

//玩家結構體
typedef struct player
{
    char name[NAME_SIZE];
    char passwd[PASSWD_SIZE];
    int total;
    int victory;

} player_t;

player_t *player;

/*********/
/*           */
/*********/

player_t *creat_player(void)
{
    player = (player_t *)malloc(sizeof (player_t)*1);
    if (NULL == player)
    {
        return NULL;
    }
    memset(player,0,sizeof (player_t));
    strcpy(player->name,NAME );
    strcpy(player->passwd,PASSWD);
    player->total = 0;
    player->victory = 0;
}

//銷燬玩家
void destory_player()
{
    if (NULL != player)
    {
        free(player);
        player = NULL;//如果沒置空,別人直接用就崩潰
    }
}

/*
創建菜單
*/
void meue()
{
    system("cls");//windows
    printf("welcome \n");
    printf("**********\n");
    printf("1. 石頭   2. 剪刀   3. 布   0.退出\n");
    printf("請出拳: ");
}

int myrand()
{
    int chose = 0;
    srand(time(NULL));
    chose = rand()%3 + 1;
    return chose;
}

/*打印出拳詳情*/
void out_win(int playerChose,int computerChose)
{
    if (1 == playerChose)
    {
        printf("你出了石頭!\n");
    }
    else if (2 == playerChose)
    {
        printf("你出了剪刀!\n");
    }
    else if (3 == playerChose)
    {
        printf("你出了布!\n");
    }
    if (1 == computerChose)
    {
        printf("電腦出了石頭!\n");
    }
    else if (2 == computerChose)
    {
        printf("電腦出了剪刀!\n");
    }
    else if (3 == computerChose)
    {
        printf("電腦出了布!\n");
    }
}

void load(void)
{
    int i = 0;
    for ( i = 2; i >=0 ;i--)
    {
        system("cls");
        printf("電腦出拳中:%d",i);
        fflush(stdout);
        Sleep(1000);
    }
    printf("\n");
}


/*
控制菜單
*/
void meue_ctr()
{
    int player_chose = 0;
    int computer_chose = 0;
    int win = 0;

    while (TRUE)
    {
        do
        {
            meue();
            scanf("%d", &player_chose);
            MFLUSH;
        } while (player_chose > 3 || player_chose < 0);

        if (0 == player_chose)
        {
            return;
        }

        computer_chose = myrand();

        printf("******出拳詳情******\n");

        out_win(player_chose,computer_chose);

        win = player_chose - computer_chose;

        player->total++;
        switch (win)
        {
            case -1:
            case 2 :
                   printf("恭喜!\n");
                   player->victory++;
                   CONTINUE("按回車繼續…");
                   break;
            case 0:
                   printf("平局!\n");
                   CONTINUE("按回車繼續…");
                   break;
            default :
                   printf("很遺憾!\n");
                   CONTINUE("按回車繼續…");
                   break;
        }
    }
}

void victory_display()
{
    double win = 0.0;
    printf("\n\n\t\t     排行榜\n");
    printf("\t\t*********\n");
    printf("%10s %10s %10s %10s\n","姓名","總局數","勝利數","勝率\n");
    if (0 != player->total)
    {
        win = (double)player->victory/player->total * 100;
    }
    printf("%10s %d %10d %5.12f\n ",player->name,player->total,player->victory,win);
}


int main(void)
{
    player = creat_player();
    if (NULL == player)
    {
        return 1;
    }

    meue_ctr();
    victory_display();

    destory_player();
    return 0;
}
發佈了35 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章