今天老师讲了C的数据类型,以及简单的运算等等.听的很简单.
但是我还是会把每句代码,没个程序都自己写一遍.
今天很开心,偶然上百度查资料的时候看有人说写了个猜大小的游戏.不知道写什么的我.瞬间决定也去写个.
知道大概游戏规则的我,于是开始了我的自由发挥.
游戏规则:
首先先输入游戏中用到的名字,
之后系统随机一个0-300之间的数,然后系统提示你输入一个数,
如果大了会提示你输入的有点大,反之亦然.
就这样直到你输入的和系统随机的那个数相同了之后,根据你猜了几次来你进行评分。
由于是在mac下写的系统清屏没用.
但是还有一些小小的BUG,没有去解决.
代码奉上:
//
// main.c
// GuessNumberGame
//
// Created by X's on 13-7-12.
// Copyright (c) 2013年 xxxx. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
char name[12]={0};
long int len=0;
char y_n[2];
char yes[] = "y";
char no[] = "n";
int value=0;
int count = 0;
long int ranNumber=0;
void welcome()
{
void cls();
void Gameing();
printf("************************************************************************************\n");
printf("* ************* **** **** ******* **** ***** ******** **** ********\n");
printf("** ***** ***** *** ********** ******* ******** ***** **** * ***** *** *************\n");
printf("*** *** * *** **** **** ******* ******** ***** **** ** *** * *** ********\n");
printf("**** * *** * ***** ********** ******* ******** ***** **** *** * ** *** *************\n");
printf("***** ***** ******** **** **** **** ***** **** *** ***** ********\n");
printf("************************************************************************************\n");
printf("\n\t\t\t\t\t欢迎来到猜大小游戏");
printf("\n请输入游戏名称:");
gets(name);
len = strlen(name);
printf("确定使用%s此名字吗?(y/n):",name);
scanf("%s",y_n);
if(strcmp(y_n, yes)==0)
{
cls();
printf(" %s 你好",name);
printf("\n\t\t\t\t\t欢迎来到猜大小游戏\n");
ranNumber = random();
Gameing();
}
else
{
cls();
welcome();
}
strcat(y_n,"\0");
}
long int random()
{
srand((unsigned)time(NULL));
ranNumber = rand()%300+1;
return ranNumber;
}
void star(int con)
{
int k = 0 ;
if(con==0)
{
k = 0;
}
else if(con<=5|| con>0)
{
k =1;
}
else if(con<=10 || con>5)
{
k = 2;
}
else
{
k=3;
}
switch(k)
{
case 0: printf("★★★★★\n");break;
case 1: printf("★★★☆\n");break;
case 2: printf("★☆\n");break;
case 3:printf("☆\n");break;
}
}
void sets()
{
void Gameing();
printf("请输入一个整数:");
scanf("%d",&value);
count++;
if(value==ranNumber && count==1)
{
printf("恭喜你只用了%d次就猜对了,\n你简直是天才!",count);
}
if(value ==ranNumber)
{
printf("恭喜你,终于猜对了哦!\n");
printf("你共用了%d次\n",count);
printf("你的评分为:\n");
star(count);
printf("再玩一次吗?(y/n)");
scanf("%s",y_n);
if(strcmp(y_n, yes)==0)
{
strcat(name, "\0");
count=0;
welcome();
}
else
{
printf("游戏结束");
}
}
if(value <ranNumber)
{
printf("你猜的有点 小 哦,再猜猜看哦!\n");
Gameing();
}
if(value>ranNumber)
{
printf("你猜的有点 大 哦,再猜猜看吧!\n");
Gameing();
}
if(count==5)
{
printf("不要放弃,马上就猜到了\n");
}
}
void Gameing()
{
sets();
}
void cls()
{
//system("cls");
for(int i=0;i<5;i++)
{
printf("\n");
}
}
void run()
{
welcome();
}
int main(int argc, const char * argv[])
{
run();
return 0;
}