當前位置:首頁 » 編程語言 » 猜字游戲c語言

猜字游戲c語言

發布時間: 2022-09-01 18:16:51

A. 設計一個猜字游戲的c語言程序

以下程序的功能是隨機產生數字,要求用戶猜測程序中產生的隨機數字,並輸入,根據猜測的結果程序給出不同的響應,如果15次沒猜對則退出。

源程序如下:
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
main()
{
int count;/*猜數字的次數*/
int number;/*系統產生的隨機數字*/
int guess;/*程序員輸入數字*/
char yes='Y';
clrscr();
printf("\nNow let us play the game.\n Guess the number:");
while (toupper(yes)=='Y')
{
count=0;
randomize();
number=random(100)+1;
do
{
do
{
printf("\nInput an integer number(1~100):");
scanf("%d",&guess);
}while(!(guess>=1&&guess<=100));/*結束第二層DO~WHILE循環*/
if (guess<number)
printf("\n Your answer is low,try again!");/*如果用戶輸入的數字小於系統隨機數,則輸出數字太小的提示信息*/

if (guess>number)
printf("\n Your answer is high,try again!");/*如果用戶輸入的數字大於系統隨機數,則輸出數字太小的提示信息*/

count++;/*猜測次數加一*/
if (count==15)
{
printf("\n This is the %d times! Think it hard next!",count);
exit(0);/*如猜測15次還沒猜對,則退出遊戲*/
}
}while (!(guess==number));
if (count<=7)/*猜測的次數小於7次*/
{
printf("\n You have got it in %d times.\n",count);
printf("\n you guess right,Congretulations!");/*游戲成功則提示祝賀信息*/
}
else
{
printf("\n You got it in %d times.\n",count);
printf("\n I bet you can do it better!");/*游戲失敗則提示鼓勵信息*/
}
printf("\n NEXT?(Y/N):");/*選擇是否重新游戲*/
scanf("%c",&yes);
}
}
運行程序時請用戶猜數字,該數字由系統隨機產生,用戶最多有七次猜測的機會,如果在七次內猜對數字,則程序顯示祝賀信息,如果用戶大於七次猜對數字,則程序顯示鼓勵信息,如果用戶連續15次都沒有猜對數字,則游戲自動退出。結束一次游戲後,系統詢問用戶進行下一次猜數字游戲,用戶輸入「Y」則開始下一次猜數字游戲,用戶如果輸入「N」則退出遊戲。

B. C語言用循環猜字游戲:使用隨機數生成一個三位數作為謎底進行猜字游戲,每次猜的時候給出提示,

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a,b,c=10;
srand(time(NULL));
a=rand()%1000;
printf("系統已經給出了一個0-999的數,請輸入一個數。\n");
while(1)
{ c--;
scanf("%d",&b);
if(b>a)
printf("猜大了,還剩%d次機會,請繼續\n",c);
if(b<a)
printf("猜小了,還剩%d次機會,請繼續\n",c);
if(b==a)
{ printf("恭喜你答對了\n");
printf("系統給的數是:%d",a);
break;}
if(c==0)
{ printf("你已經沒有機會了,請重新開始吧!\n");break;}
}

}

C. 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);

}

D. c語言編程:猜數字游戲

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define UI unsigned short int
void game()
{
UI answer;
UI input;
UI lower=1;
UI upper=100;
UI count=0;
srand(time(NULL));
do{answer=rand()%101;}
while(answer==0);
puts("Welcome to the number guessing game!");
do
{
puts("Please enter an integer from 1 to 100 (again):");
scanf("%lu",&input);
getchar();
count=count+1;
if(input==answer){puts("You succeeded!");printf("The number of time(s) you entered is %lu.\n",count);}
else
{
puts("You failed!");
if(input<answer){if(input>lower){lower=input;}puts("The answer is greater than your input.");}
else {if(input<upper){upper=input;}puts("The answer is less than your input.");}
printf("The answer is from %lu to %lu.\n",lower,upper);
}
}
while(input!=answer);
}
#undef UI
int main()
{
game();
system("Pause");
return 0;
}

E. C語言編寫猜數字游戲

#include<stdio.h>
#include<time.h>
#include<math.h>
int main()
{
int i,a,n;
srand((int)time(NULL));
n=rand()%100+1;
for(i=0; i<5; i++)
{
printf("請輸入一個介於1到100的整數:");
scanf("%d",&a);
if(a<n)
printf("猜小了!\n");
if(a>n)
printf("猜大了!\n");
if(a==n)
{
printf("猜對了!\n");
break;
}
}
return 0;
}

F. 猜數字游戲c語言編程一到五

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

intmain(){

srand((unsigned)time(NULL));

intguessNum=0;

while(1)

{

intrandNum=rand()%6;

printf("請輸入1-5的數字:");

scanf("%d",&guessNum);

if(guessNum<1||guessNum>5){

printf("輸入不合法! ");

continue;

}

if(randNum==guessNum){

printf("恭喜你,猜對了!");

break;

}else

{

printf("很遺憾,差一點,繼續努力! ");

}

}

}

G. 怎樣用C語言實現猜字游戲

//電腦隨機生成一個50以內的整數,然後從鍵盤輸入一個整數猜系統生成的數,直到猜對為止,並輸出所猜次數。
#include<stdio.h>
#include
<stdlib.h>
#include<time.h>
void
main()
{
int
count=0;
int
flag=1;
do
{
srand(time(NULL));
int
num1,num=rand()%51;
printf("請輸入您要猜的數(0-50):");
scanf("%d",&num1);
if(num1>50)
{
printf("輸入有誤,請重新輸入\n");
continue;
}
printf("隨機數為:%d\n",num);
count++;
if(num1==num)
{
flag=0;
}
printf("猜錯了!3請繼續\n\n\n");
}while(flag);
printf("%d",count);
}

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

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

熱點內容
圖中兩種配置哪個好 發布:2025-01-16 02:59:28 瀏覽:579
如何解開密保密碼 發布:2025-01-16 02:57:44 瀏覽:20
中國銀行查詢密碼是什麼 發布:2025-01-16 02:33:20 瀏覽:791
堅果pro錄音文件夾 發布:2025-01-16 02:31:46 瀏覽:939
支付寶的登錄密碼忘記了如何改 發布:2025-01-16 02:30:30 瀏覽:221
解壓作業泥 發布:2025-01-16 02:28:02 瀏覽:807
我的世界rpg伺服器空島 發布:2025-01-16 02:26:49 瀏覽:90
ps腳本函數 發布:2025-01-16 02:15:28 瀏覽:481
android顯示行數據 發布:2025-01-16 02:07:40 瀏覽:963
壓縮皇冠 發布:2025-01-16 01:51:27 瀏覽:275