欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个简单的c项目(未优化版)

程序员文章站 2022-05-18 14:33:33
include #include #include #include
include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#define size 20
#define passwd_size 20
#define name "player1"
#define passwd "123456"
#define ture 1
//结构体设计
typedef struct player
{
 char name[size];
 char passwd[passwd_size];
 int total;
 int victory;
}player_t;
player_t *player;
player_t *ceart_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;
 return player;
}

void destory_player()
{
 if (null != player)
 {
  free(player);
  player = null;
 }
}

void menu()
{
 printf("************************************************************\n");
 printf("猜拳游戏\n");
 printf("1.石头     2.剪刀     3.布     0.退出\n");
 printf("************************************************************\n");
}

int myrand()
{
 int chose = 0;
 srand((int)time(null));
 chose = rand() % 3 + 1;
 return chose;
}
void out_win                            //cls 清屏
void menu_ctr()
{
 int win;
 int player_choose = 0;
 int computer_choose = 0;
 while (ture)
 {
  menu();
  do
  {
   scanf("%d", &player_choose);
  } while(player_choose > 3 || player_choose < 0);
  if (0 == player_choose)
  {
   return; 
  }
  computer_choose = myrand();
  (player->total)++;
    win = player_choose - computer_choose;
  switch (win)
  {
  case -1:
  case  2:
   printf("恭喜你,你赢了\n");
   (player->victory)++;
   break;
  case 0:
   printf("平局\n");
   break;
  default:
   printf("你输了哈哈哈,再来?\n");
   break;
  }
 }
}
void show()
{
 printf("\t\t排行榜\n\n");
 printf("\t*********************\n");
 printf("姓名\t总局数\t赢场\t胜率\n");
 printf("%s\t%d\t%d\t%2.2f\n", player->name,player->total, player->victory, (double)player->victory/(double)player->total*100);
}
int main()
{
 player = ceart_player();
 if (null == player)
 {
  return 1;
 }
 menu_ctr();
 show();
 destory_player(player);
 return 0;
}