❶ c語言編寫一個猜數字游戲
同學你好!
下面是我自己做了一個,運行是成功的。希望對你有所幫助!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
void
main()
{
int
i,j,cnt,guard=1;
while(guard)/*利用guard來判斷是否繼續進行游戲*/
{
srand((unsigned)time(NULL));
i=rand()%100;/*隨機生成整數*/
cnt=0;/*比較次數置零*/
for(;;)
{
printf("Please
input
the
data:");
scanf("%d",&j);
cnt++;/*次數累加*/
if(j>i)
printf("Too
high!\n");
else
if(j<i)
printf("Too
low!\n");
else
{
printf("Right!\n");
printf("The
cnt
is:%d\n",cnt);
printf("Wanna
continue?
1.continue
0.exit\n");
scanf("%d",&guard);/*選擇是否繼續*/
break;
}
}
}
}
希望我的回答對你有幫助,祝你的C成績進步
❷ c語言猜數字游戲
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
char*getHint(char*secret,char*guess)
{
inti,countA=0,countB=0;
intcount[10]={0};
char*result=(char*)malloc(strlen(secret));
if(strlen(secret)!=strlen(guess))
{
printf("UnequalLength!");
returnNULL;
}
for(i=0;i<4;i++)
{
if(secret[i]==guess[i])
countA++;
else
{
count[secret[i]-'0']++;
if(count[secret[i]-'0']<=0)
{
countB++;
}
count[guess[i]-'0']--;
if(count[guess[i]-'0']>=0)
{
countB++;
}
}
}
sprintf(result,"%d%c%d%c",countA,'A',countB,'B');
returnresult;
}
intmain()
{
inti=0,j=0;
charnum[4]={0},input[5]={0};
srand((unsigned)time(NULL));//初始化隨機數
for(i=0;i<4;i++)//生成4個隨機數
{
num[i]=(rand()%10)+48;
}
for(j=0;j<10;j++)
{
printf("pleaseinput4-digtalsnumber[%d]times:",j+1);
fgets(input,sizeof(input),stdin);
fflush(stdin);
if(strlen(input)!=4)
{
continue;
}
printf("input[%s] ",input);
strcpy(input,getHint(num,input));
printf("ret=[%s] ",input);
if(strcmp(input,"4A0B")==0)
{
printf("YouareWin! ");
break;
}
}
return0;
}
❸ C語言編程,猜數游戲
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int a;
srand(time(NULL));
a=rand()%101;
int n,count=0;
printf("input the number you want guess:");
while(n!=a)
{
count++;
if(count>8)
break;
scanf("%d",&n);
if(n>a)
printf("too big,try again:");
if(n<a)
printf("tow low,try again:");
}
if(n==a)
printf("you are right\n");
else
printf("you are lose\n");
}
這個游戲肯定贏啊
❹ c語言猜數字游戲源代碼
小游戲2048:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int jsk( ); //計算空格數
void rsgm( ); //重置游戲
void inkey( ); //按鍵輸入
void left( ); //向左移動
void right( ); //向右移動
void up( ); //向上移動
void down( ); //向下移動
void show( ); //輸出界面
void adnum( ); //添加隨機數
void yes( ); //游戲是否結束(1是0否)
void gtxy(int x, int y); //控制游標位置的函數
int a[4][4]; //存儲16個格子中的數字
int score = 0; //每局得分
int best = 0; //最高得分
int ifnum; //是否需要添加數字(1是0否)
int over; //游戲結束標志(1是0否)
int i,j,k;
int main( )
{ rsgm( ); //重置游戲
inkey( ); //按鍵輸入
return 0;
}
void Color(int a) //設定字元顏色的函數(a應為1-15)
{ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a); }
void rsgm( ) //重置游戲
{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //啟動隨機數發生器
int n = rand( ) % 16; //隨機函數產生0-15的數字
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }
else { a[i][j] = 4; } n--; }
else { a[i][j] = 0; n--; }
}
}
adnum( );
system("cls");
CONSOLE_CURSOR_INFO gb={1,0}; //以下兩行是隱藏游標的設置,gb代指游標
SetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), &gb );
Color(14); //設置字體淡黃色
printf(" 2048小游戲"); Color(7); //恢復白字黑底
printf(" ┌──────┬──────┬──────┬──────┐");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" ├──────┼──────┼──────┼──────┤");
printf(" │ │ │ │ │");
printf(" └──────┴──────┴──────┴──────┘");
show( );
}
void show( ) //輸出界面
{ for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是游標到指定位置輸出數字
if(a[i][j]==0){printf(" "); Color(7); printf("│");}
else if(a[i][j]<10){ if (a[i][j] == 2) { Color(14); }
else if (a[i][j] == 4) { Color(13); }
else if (a[i][j] == 8) { Color(12); }
printf(" %d ", a[i][j]); Color(7 ); printf("│");
}
else if (a[i][j] < 100){if (a[i][j] == 16) { Color(12); }
else if (a[i][j] == 32) { Color(10); }
else if (a[i][j] == 64) { Color(2 ); }
printf(" %d ", a[i][j]); Color(7); printf("│");
}
else if (a[i][j] < 1000) {if (a[i][j] == 128) { Color(9); }
else if (a[i][j] == 256) { Color(1); }
else if (a[i][j] == 512) { Color(13); }
printf(" %d ", a[i][j]); Color(7); printf("│");
}
else if (a[i][j] < 10000) {if (a[i][j] == 1024) { Color(5); }
else { Color(15); }
printf(" %d ", a[i][j]); Color(7); printf("│");
}
}
if (jsk( ) == 0)
{ yes( ); if (over) { gtxy(9,12); Color(10);
printf(" 游戲結束!是否繼續? [ Y/N ]:"); }
}
}
void inkey( ) //按鍵輸入
{ int key;
while (1)
{ key = getch( );
if (over) { if (key == 89|| key == 121) { rsgm( ); continue; }
else if (key == 78|| key == 110) { return; }
else continue; }
ifnum = 0;
if(key==224)key=getch( );
switch (key)
{ case 75: left( ); break;
case 77: right( ); break;
case 72: up( ); break;
case 80: down( );break;
}
if (score > best) { best = score; }
if (ifnum) { adnum( ); show( ); }
}
}
int jsk( ) //計算空格數
{ int n = 0;
for (i = 0; i < 4; i++)
{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }
return n;
}
void left( ) //向左移動
{ for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{ if (a[i][j] > 0)
{ if ( a[i][k] == a[i][j])
{ a[i][k] *= 2; k++;
score = score + 2 * a[i][j];
a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }
k++; }
}
}
}
}
void right( ) //向右移動
{for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[i][j] > 0)
{ if (a[i][k] == a[i][j])
{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }
}
}
}
}
void up( ) //向上移動
{for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{if (a[j][i] > 0)
{if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];
a[j][i] = 0; ifnum = 1; }
else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }
k++; }
}
}
}
}
void down( ) //向下移動
{ for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[j][i] > 0)
{if (a[k][i] == a[j][i])
{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }
else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else {a[k - 1][i] = a[j][i];
if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }
}
}
}
}
void adnum( ) //添加隨機數
{ srand(time(0)); int n = rand( ) % jsk( );
for (int i = 0; i < 4; i++)
{for (int j = 0; j < 4; j++)
{ if (a[i][j] == 0) {if (n != 0) { n--; }
else {int k = rand( ) % 3;
if (k == 0 || k == 1) {a[i][j] = 2; return; }
else {a[i][j] = 4; return; } }
}
}
}
}
void yes( ) //游戲是否結束
{ for (int i = 0; i < 4; i++)
{for (int j = 0; j < 3; j++)
{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}
}
over = 1;
}
void gtxy(int x, int y) //控制游標位置的函數
{ COORD zb; //zb代指坐標
zb.X = x;
zb.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), zb);
}
❺ c語言:猜數字游戲代碼
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain()
{
intnum,n,i,cnt=0,finish=0;
srand((unsignedint)time(NULL));
num=rand()%100;
printf("請猜數字,0~100之間 ");
do{
scanf("%d",&i);
cnt++;
if(i<0&&i>=100)
{
printf("GameOver ");
finish=1;
}
elseif(i>num)printf("Toobig ");
elseif(i<num)printf("Toosmall ");
else
{
printf("你用了%d次機會猜中!",cnt);
finish=1;
}
}while(!finish);
return0;
}
❻ 用C語言編寫一個猜數字游戲的程序(最好附帶說明)
有償,先程序後cash ,有意加我
❼ c語言 簡單猜數游戲 急
#include
<stdio.h>
#include<stdlib.h>
#include
<time.h>
main()
{
int
a,b,i=1;
srand((unsigned)time(NULL));//產生隨機數種子
a=rand()%100+1;//產生隨機數
printf("\n\t-------------------
歡迎進入猜數游戲
-------------------\n\n");
printf("
@游戲開始@
請猜一個數(1~100):");
scanf("%d",&b);
while(i<8)//判斷的循環體
{
if(a==b)
{printf("
o(≥v≤)o
恭喜你答對了!\n");break;}
else
if(a>b)
printf("
@對不起@
你猜小了...\n");
else
if(a<b)
printf("
@對不起@
你猜大了...\n");
printf("
@沒關系@
再猜一次吧:");
scanf("%d",&b);
i++;
}
if(i==8)printf("-_-!!!對不起,你猜錯了8次,你輸了\n");
}
這個簡單點,基本功能實現
❽ c語言編程 編一個猜數字游戲
源碼如下:
/*File:guess.c*/
#include<stdio.h>/*standardinput&outputsupport*/
#include<stdlib.h>/*srand()rand()*/
#include<time.h>/*time()*/
/*宏定義*/
#defineNUMBER_LENGTH5/*隨機數長度*/
#defineNUMBER_LIMIT10/*隨機數限制,每一位0-9*/
#defineINPUT_LENTH128/*輸入緩沖區大小*/
chargoal[NUMBER_LENGTH]={0};/*保存隨機數*/
charflag[NUMBER_LIMIT]={0};/*保存隨機數標志,保證不重復*/
charinput[INPUT_LENTH]={0};/*保存輸入*/
/*初始化用於保存數據的數組*/
voidinitData()
{
inti=0;
while(i<NUMBER_LENGTH)
goal[i++]=0;
i=0;
while(i<NUMBER_LIMIT)
{
flag[i++]=0;
}
}
/*初始化用於保存緩沖區的數組*/
voidinitBuffer()
{
inti=0;
while(i<INPUT_LENTH)
input[i++]=0;
}
/*顯示猜測結果*/
voiddisplay()
{
intcount=0;
inti=0;
while(i<NUMBER_LENGTH)
{
if(input[i]==goal[i])
{
printf("%c",'o');
count++;
}
else
{
printf("%c",'x');
}
i++;
}
printf(" RIGHT:%dbit(s) ",count);
if(count==NUMBER_LENGTH)
{
printf("Youwin!Thenumberis%s. ",goal);
exit(0);
}
}
/*生成隨機數*/
voidgeneral()
{
/*以時間作為時間種子保證生成的隨機數真正具有隨機性質*/
srand((unsignedint)time(NULL));
inti=0;
while(i<NUMBER_LENGTH)
{
chartmp;
do
{
tmp='0'+((i!=0)?(rand()%10):(1+rand()%9));
}while(flag[tmp]!=0);
flag[tmp]=1;
goal[i++]=tmp;
}
}
/*輸入方法,用於猜測*/
voidguess()
{
printf(": ");
scanf("%s",input);
display();
initBuffer();
}
/*主函數,程序主框架*/
intmain(intargc,constchar*argv[])
{
initData();
initBuffer();
general();
while(1)guess();
return0;
}
==============================================
運行結果見附圖,希望我的回答能夠對你有所幫助。