码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-10-26 19:03:49      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:c 猜拳游戏

#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;
}

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

标签:c 猜拳游戏

原文地址:http://10739845.blog.51cto.com/10729845/1706372

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!