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