求C语言猜价格游戏代码

求C语言猜价格游戏代码,第1张

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

// 格式化时间函数

char __format_time__[40]

char* getFormatTime(long t)

{

strftime(__format_time__,sizeof(__format_time__)/sizeof(char),"%Y.%m.%d %H:%M:%S",gmtime(&t))

return __format_time__

}

// 保存玩家源宏竖信息的文件

char PLAYER_DATA_FILE_NAME[] = "player.txt"

// 系统默认的用户名

const char PALYER_DEFAULT_SYSTEM_NAME[] = "player"

// 用户名最大长度

const int PLAYER_MAX_NAME_LENGTH = 20

// 密码最大长度

const int PLAYER_MAX_PASS_LENGTH = 20

// 玩家信息

struct Player

{

// 唯一的账号名称

char name[PLAYER_MAX_NAME_LENGTH+1]

// 登陆密码

char pass[PLAYER_MAX_PASS_LENGTH+1]

struct Player *next

Player()

{

strcpy(name,"")

strcpy(pass,"")

next = NULL

}

}

// 全局雹大变量:游戏玩家链表

struct Player* _players = NULL

// 全局变量:当前登陆的游戏玩家

struct Player _loginPlayer

// 保存游戏信息的文件

char GAME_DATA_FILE_NAME[] = "game.txt"

// 排行榜上记录保存个数

const int GAME_MAX_TOP_PLAYERS = 5

// 猜价格最多尝试次数

const int GAME_MAX_TRY_TIME = 3

// 猜价格的最小值

const int GAME_MIN_PRICE = 1000

// 猜价格的最大值

const int GAME_MAX_PRICE = 9999

// 一局游绝灶戏的信息

struct Game

{

// 游戏开始时间

long startTime

// 游戏结束时间

long endTime

// 玩家账号

char playerName[PLAYER_MAX_NAME_LENGTH+1]

// 真实价格

int realPrice

// 是否成功猜出

int isSuccess

// 玩家一共猜的次数

int guessTime

// 玩家猜了 guessTime 次,每次才的价格

int guessPrice[GAME_MAX_TRY_TIME]

struct Game *next

Game()

{

startTime = time(NULL)

strcpy(playerName,"")

srand(time(NULL))

isSuccess = 0

realPrice = rand()%(GAME_MAX_PRICE-GAME_MIN_PRICE)+GAME_MIN_PRICE

guessTime = 0

next = NULL

}

}

// 全局变量:猜价格游戏链表

struct Game* _games = NULL

// 初始化 游戏玩家链表

void initPlayer()

{

FILE * playerFile = fopen(PLAYER_DATA_FILE_NAME,"r")

struct Player *p,tempPlayer

if(playerFile)

{

while(fscanf(playerFile,"%s%s",tempPlayer.name,tempPlayer.pass)!=EOF)

{

if(_players == NULL)

{

_players = (struct Player*)malloc(sizeof(struct Player))

*_players = tempPlayer

p = _players

}

else

{

p->next = (struct Player*)malloc(sizeof(struct Player))

*p->next = tempPlayer

p = p->next

}

}

fclose(playerFile)

}

}

// 保存 游戏玩家链表

void savePlayer()

{

FILE * playerFile = fopen(PLAYER_DATA_FILE_NAME,"w")

struct Player *p

if(playerFile)

{

p = _players

while(p)

{

fprintf(playerFile,"%s %s\n",p->name,p->pass)

p = p->next

}

fclose(playerFile)

}

}

// 初始化 猜价格游戏链表

void initGame()

{

FILE * gameFile = fopen(GAME_DATA_FILE_NAME,"r")

struct Game *p,tempGame

int i

if(gameFile)

{

while(

fscanf(gameFile,"%ld%ld%s%d%d%d",

&tempGame.startTime,

&tempGame.endTime,

&tempGame.playerName,

&tempGame.realPrice,

&tempGame.isSuccess,

&tempGame.guessTime

)!=EOF)

{

for(i=0i<tempGame.guessTimei++)

{

fscanf(gameFile,"%d",&tempGame.guessPrice[i])

}

if(_games == NULL)

{

_games = (struct Game*)malloc(sizeof(struct Game))

*_games = tempGame

p = _games

}

else

{

p->next = (struct Game*)malloc(sizeof(struct Game))

*p->next = tempGame

p = p->next

}

}

fclose(gameFile)

}

}

// 保存 猜价格游戏链表

void saveGame()

{

FILE * gameFile = fopen(GAME_DATA_FILE_NAME,"w")

struct Game *p

int i

if(gameFile)

{

p = _games

while(p)

{

fprintf(gameFile,"%ld %ld %s %d %d %d",

p->startTime,

p->endTime,

p->playerName,

p->realPrice,

p->isSuccess,

p->guessTime

)

for(i=0i<p->guessTimei++)

{

fprintf(gameFile," %d",p->guessPrice[i])

}

fprintf(gameFile,"\n")

p = p->next

}

fclose(gameFile)

}

}

// 猜价格游戏主菜单

int printMainMenu()

{

char strChoice[100]

int intChoice

do

{

printf("\n*******************************************\n")

printf("****主菜单\n")

printf("*******************************************\n")

printf("****1)查看排行榜\n")

printf("****2)清除排行榜\n")

printf("****3)登录账号开始游戏\n")

printf("****4)申请账号开始游戏\n")

printf("****5)退出游戏\n")

printf("*******************************************\n")

scanf("%s",strChoice)

intChoice = atoi(strChoice)

if(intChoice<1 || intChoice >5)

{

printf("****命令错误!请重新输入命令!")

}

}

while(intChoice<1 || intChoice >5)

return intChoice

}

/*

对已有的游戏记录根据【猜价格的次数】递增排序

如果【猜价格的次数】相等,

根据【使用的时间】递增排序,

如果【使用的时间】相等,

根据【开始的时间】递增排序,

*/

void sortGames()

{

struct Game *p , *q , tempGame1,tempGame2

p = _games

while(p)

{

q = p->next

while(q)

{

if

(

p->guessTime >q->guessTime ||

(p->guessTime == q->guessTime &&(p->endTime - p->startTime) >(q->endTime - q->startTime)) ||

(p->guessTime == q->guessTime &&(p->endTime - p->startTime) == (q->endTime - q->startTime) &&p->startTime >q->startTime)

)

{

tempGame1 = *p

tempGame2 = *q

// 注意交换的时候,next 指针不能交换

*p = tempGame2

p->next = tempGame1.next

// 注意交换的时候,next 指针不能交换

*q = tempGame1

q->next = tempGame2.next

}

q = q->next

}

p = p->next

}

}

// 查看排行榜

void printRankList()

{

int counter = 0

struct Game *p

printf("\n*******************************************\n")

printf("****玩家排名\n")

printf("*******************************************\n")

// 没有记录

if(!_games)

{

printf("****排名:%4d 用户:%10s(default) 用了%4d次\n",1,PALYER_DEFAULT_SYSTEM_NAME,GAME_MAX_TRY_TIME)

}

else

{

// 排序

sortGames()

p = _games

counter = 0

// 查看前 GAME_MAX_TOP_PLAYERS 名(如果有那么多),并且成功猜出价格的

while(p &&counter <GAME_MAX_TOP_PLAYERS &&p->isSuccess)

{

counter ++

printf("****排名:%4d 用户:%10s用了%4d次\n",counter,p->playerName,p->guessTime)

printf(" 开始时间:%s\n",getFormatTime(p->startTime))

printf(" 结束时间:%s\n\n",getFormatTime(p->endTime))

p = p->next

}

}

printf("*******************************************\n")

}

// 清除排行榜

void clearRankList()

{

struct Game *p = _games , *q

while(p)

{

q = p->next

free(p)

p=q

}

_games = NULL

printf("\n*******************************************\n")

printf("****清空排名榜\n")

printf("*******************************************\n")

printf("****排名榜已经清空!\n")

printf("*******************************************\n")

}

// 猜价格游戏用户登陆菜单

int printLoginMenu()

{

struct Player *p

char name[PLAYER_MAX_NAME_LENGTH+1]

char pass[PLAYER_MAX_PASS_LENGTH+1]

int i

char ch

printf("\n*******************************************\n")

printf("****登录菜单\n")

printf("*******************************************\n")

printf("****输入用户名称:")

scanf("%s",name)

printf("****输入用户密码:")

scanf("%s",pass)

/*

i=0

while((ch=getch())!='\n')

{

pass[i++]=ch

}

pass[i]='\n'

*/

p=_players

while(p)

{

if(strcmp(p->name,name) == 0 &&strcmp(p->pass,pass) == 0)

{

break

}

else

{

p = p->next

}

}

if(p)

{

printf("****登陆成功!\n")

printf("*******************************************\n")

strcpy(_loginPlayer.name,name)

strcpy(_loginPlayer.pass,pass)

return 1

}

else

{

printf("****登陆失败!用户名不存在或者密码错误!\n")

printf("*******************************************\n")

return 0

}

}

// 猜价格游戏用户注册菜单

int printRegisterMenu()

{

struct Player *p

char name[PLAYER_MAX_NAME_LENGTH+1]

char pass[PLAYER_MAX_PASS_LENGTH+1]

int i

char ch

printf("\n*******************************************\n")

printf("****注册菜单\n")

printf("*******************************************\n")

do

{

printf("****输入要注册的用户名称:")

scanf("%s",name)

if(strcmp(PALYER_DEFAULT_SYSTEM_NAME,name) == 0)

{

printf("****%s 是系统默认的用户名,不能被注册!\n",PALYER_DEFAULT_SYSTEM_NAME)

}

else if(strlen(name)==0)

{

printf("****用户名不能为空!\n")

}

}

while(strlen(name)==0 || strcmp(PALYER_DEFAULT_SYSTEM_NAME,name)== 0 )

printf("****输入要注册的用户密码:")

scanf("%s",pass)

/*

i=0

while((ch=getch())!='\n')

{

pass[i++]=ch

}

pass[i]='\n'

*/

p=_players

while(p)

{

if(strcmp(p->name,name) == 0 &&strcmp(p->pass,pass) == 0)

{

break

}

else

{

p = p->next

}

}

if(!p)

{

printf("****注册成功!\n")

printf("*******************************************\n")

// 更新当前登陆的用户

strcpy(_loginPlayer.name,name)

strcpy(_loginPlayer.pass,pass)

// 添加一个新的玩家到玩家列表

p = (struct Player*)malloc(sizeof(struct Player))

*p = _loginPlayer

p->next = _players

_players = p

return 1

}

else

{

printf("****注册失败!该用户名已经被注册!\n")

printf("*******************************************\n")

return 0

}

}

void play()

{

struct Game newGame, *p

char strChoice[100]

int intChoice

int guessPrice

// 当前游戏的玩家为当前登陆的玩家

strcpy(newGame.playerName,_loginPlayer.name)

printf("\n*******************************************\n")

printf("****猜价格菜单\n")

printf("*******************************************\n")

printf("****您好:%s\n",_loginPlayer.name)

// 下面这句 printf 可以根据需要注释掉

printf("*******************************************\n")

while(newGame.guessTime <GAME_MAX_TRY_TIME)

{

printf("[第%2d次]输入您猜的价格:",newGame.guessTime+1)

scanf("%d",&guessPrice)

newGame.guessPrice[newGame.guessTime++] = guessPrice

if(guessPrice == newGame.realPrice)

{

newGame.isSuccess = 1

break

}

else if(guessPrice <newGame.realPrice)

{

printf("****低了!\n")

}

else

{

printf("****高了!\n")

}

}

// 记录猜价格游戏结束时间

newGame.endTime = time(NULL)

// 判断猜价格结果

if(newGame.isSuccess == 1)

{

printf("****恭喜你猜出了真实价格!\n")

}

else

{

printf("****很遗憾,您没有猜出真实价格!真实价格为:%6d!\n",newGame.realPrice)

}

// 记录将本轮猜价格游戏

p = (struct Game*)malloc(sizeof(struct Game))

*p = newGame

p->next = _games

_games = p

printf("*******************************************\n")

do

{

printf("****1)继续\n")

printf("****2)返回\n")

printf("*******************************************\n")

scanf("%s",&strChoice)

intChoice = atoi(strChoice)

if(intChoice<1 || intChoice>2)

{

printf("****命令错误!请重新输入命令!\n")

}

}

while(intChoice<1 || intChoice>2)

// 继续下一轮

if(intChoice==1)

{

play()

}

//返回之前注销登录的账号

else if(intChoice==2)

{

strcpy(_loginPlayer.name,"")

strcpy(_loginPlayer.pass,"")

}

}

void startGame()

{

int choice

initPlayer()

initGame()

while(1)

{

choice = printMainMenu()

switch(choice)

{

// 打印排行榜

case 1:

{

printRankList()

break

}

// 清空排行榜

case 2:

{

clearRankList()

break

}

// 用户登陆并开始游戏

case 3:

{

if(printLoginMenu())

{

play()

}

break

}

// 用户注册并开始游戏

case 4:

{

if(printRegisterMenu())

{

play()

}

break

}

// 用户退出游戏

case 5:

{

savePlayer()

saveGame()

exit(0)

break

}

default:

{

printf("****命令错误!请重新输入命令!\n")

break

}

}

}

}

int main(int argc, char *argv[])

{

startGame()

return 0

}

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

void introduction(){

printf("猜价格游戏,。。。。。\n")

}

int startgame(){

int price,player

int i

time_t t

srand((unsigned)time(&t))

price=rand()%101+100

printf("%d\n",price)

for(i=0i<6i++){

printf("请输入价格:")

scanf("%d",&player)

if(player>price)

printf("高\n")

else if(player<price)

printf("低\n")

else{

printf("恭喜你!猜对了!\n")

return i

break

}

}

printf("很遗憾!没雹衫有猜对!\n")

return -1

}

double winingper(int winnum,int total){

double per,prinper

if(total==0){

printf("你还没有进行游戏!\n")

return -1

}

per=(double)winnum/total

prinper=per*100

printf("你的胜率为:%.1lf % \n",prinper)

return prinper

}

int average(int score,int num){

if(num==0){

printf("你还没有进行游戏!\n")

return -1

}

printf("你的平均分为:%d\n",score/num)

return score/num

}

int gameover(){

exit(1)

}

void menu(){

printf("***********menu*************\n")

printf("1.游戏介绍\n")

printf("2.开始游戏\n")

printf("3.输出胜率\n")

printf("4.输出平均成绩源悔腔\n")

printf("5.\n")

printf("6.退出游戏\n")

printf("****************************\n")

}

void main(){

int i,select,flag

int count=0,wincount=0,score=0

label:

menu()

scanf("%d"前腊,&select)

switch(select){

case 1:introduction()goto label

break

case 2:flag=startgame()count++

if(flag!=-1){

wincount++

score+=(5-flag)*2

}

goto label

break

case 3:winingper(wincount,count)goto label

break

case 4:average(score,count)goto label

break

case 5:goto label

break

case 6:exit(1)

break

default:

printf("错误的选择项!\n")

break

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12557343.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存