當前位置:首頁 » 編程語言 » 多人猜數字游戲c語言編程
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

多人猜數字游戲c語言編程

發布時間: 2022-09-02 10:04:21

c語言編寫猜數字游戲

#include <stdlib.h>
#include <stdio.h>
//#include <iostream>
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!\r\n",i);
else
{
printf("%d is a correct data\r\n",getNum[i]);
}

}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\
input 4 data gess the random data,input h can get help\
");}
}
}
}

void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );

orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{

for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);

if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one num\r\n");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another\r\n"); break;}
}

if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?\r\n",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");

}

Ⅱ 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語言編程 人機猜數字

int password,a[4],b[4],flag=1,count1,count2,i,j,count3;
password=rand()%1000; //隨機是不是這樣寫。。我忘了,就是隨機個四位數
a[0]=password/1000;
a[1]=password%1000/100;
a[2]=password%100/10;
a[3]=password%10;
while(flag)
{
scanf("%d",&temp);
b[0]=password/1000;
b[1]=password%1000/100;
b[2]=password%100/10;
b[3]=password%10;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i]==b[j])
{ count1++; i++;}
printf(" %d ",count1);
for(i=0;i<4;i++)
if(a[i]==b[i])
count2++;
printf(" %d ",count2);
if(count2==4)
flag=0;
count3++;
}
printf("\n%d\n",count3);

Ⅳ 猜數字游戲 C語言簡單程序代碼

#include
#include
#include
#include
int
i,j=1;
int
scores[6];
void
main()
{
char
control='\0';
int
rand1,guess,score;
printf("開始游戲嗎
?(y?n)");
control=getchar();
while(control!='y'&&control!='y'&&control!='n'&&control!='n')//屏蔽其他按鍵
{
printf("無效字元!開始游戲嗎
?(y?n)");
fflush(stdin);
control=getchar();
printf("%c",control);
}
while((control=='y')||(control=='y'))
{
system("cls");
srand((unsigned)time(null));
rand1=rand()%10+1;
//printf("%d",rand1);
for(i=0;i<20;i++)
{
printf("請輸入你猜的數:");
scanf("%d",&guess);
if(guess>rand1)printf("大啦!\n");
else
if(guess
:猜對了\a\n");//響鈴\a
break;
}
i++;
}
i+=1;
if(i==1)
{
score=100;
scores[0]++;
}
else
if(i>=2&&i<=3)
{
score=90;
scores[1]++;
}
else
if(i>=4&&i<=6)
{
score=80;
scores[2]++;
}
else
if(i>=7&&i<=10)
{
score=70;
scores[3]++;
}
else
if(i>=11&&i<=15)
{
score=60;
scores[4]++;
}
else
{
score=0;
scores[5]++;
}
printf("第%d次得分是:%d\n",j,score);
scores[7]+=score;
j++;
printf("是否繼續(y?n)\n");
fflush(stdin);//請輸入緩沖區
control=getchar();
while(control!='y'&&control!='y'&&control!='n'&&control!='n')
{
printf("無效字元!只能按y或y,n或n是否繼續(y?n)\n");
fflush(stdin);
control=getchar();
printf("%c",control);
}
}
system("cls");//清屏
printf("+++++++++++++++-----------------以下是得分情況:---------------*************\n");
for(i=0;i<5;i++)
{
printf("+++++++++++++++------------------得%d分%d次:------------------*************\n",10*(10-i),scores[i]);
}
printf("+++++++++++++++------------------得%d分%d次:------------------*************\n",
0,scores[5]);
}

Ⅳ 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語言程序設計題《猜數字游戲》

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 4/*隨機抽N個數*/
#define NUM 8/*NUM次猜數的機會*/

void detect(char s[])
{
int i,j,num;
int a,b;
char ch[N];
for(num=0;num<NUM;num++)
{
a=b=0;
printf("第%d次機會:",num+1);
for(i=0;i<N;i++)
{
ch[i]=getch();
if(ch[i]>='0'&&ch[i]<='9')
{
for(j=0;j<i;j++)
if(ch[i]==ch[j]) break;
if(j<i) i--;
else
{
putchar(ch[i]);
for(j=0;j<N;j++)
{
if(ch[i]==s[j])
if(i==j) a++;
else b++;
}
}
}
else
i--;
}
printf(" %dA%dB\n",a,b);
if(a==N)
{
printf("恭喜你答對了!\n");
break;
}
}
if(num==NUM)
printf("很遺憾,正確答案為:%s\n",s);
}

main()
{
int i,j;
char s[N+1];
srand(time(0));
for(i=0;i<N;i++)
{
s[i]=rand()%10;
for(j=0;j<i;j++)
if(s[i]==s[j]) break;
if(j<i) i--;
else
{
s[i]+='0';
putchar('*');
}
}
s[i]='\0';
printf("\n總共%d次機會\n",NUM);
detect(s);
}

_______________________
運行結果:
-----------------------
****
總共8次機會
第1次機會:1234 0A2B
第2次機會:2345 0A2B
第3次機會:3456 1A0B
第4次機會:4567 0A1B
第5次機會:5678 0A1B
第6次機會:6789 1A1B
第7次機會:3792 0A2B
第8次機會:9482 1A3B
很遺憾,正確答案為:8429
請按任意鍵繼續. . .

Ⅶ C語言程序設計 猜數字游戲

看看這樣是不是符合你的要求。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 4/*隨機抽N個數*/
#define NUM 8/*NUM次猜數的機會*/

void detect(char s[])
{
int i,j,num;
int a,b;
char ch[N];
for(num=0;num<NUM;num++)
{
a=b=0;
printf("第%d次機會:",num+1);
for(i=0;i<N;i++)
{
ch[i]=getch();
if(ch[i]>='0'&&ch[i]<='9')
{
for(j=0;j<i;j++)
if(ch[i]==ch[j]) break;
if(j<i) i--;
else
{
putchar(ch[i]);
for(j=0;j<N;j++)
{
if(ch[i]==s[j])
if(i==j) a++;
else b++;
}
}
}
else
i--;
}
printf(" %dA%dB\n",a,b);
if(a==N)
{
printf("恭喜你答對了!\n");
break;
}
}
if(num==NUM)
printf("很遺憾,正確答案為:%s\n",s);
}

main()
{
int i,j;
char s[N+1];
srand(time(0));
for(i=0;i<N;i++)
{
s[i]=rand()%10;
for(j=0;j<i;j++)
if(s[i]==s[j]) break;
if(j<i) i--;
else
{
s[i]+='0';
putchar('*');
}
}
s[i]='\0';
printf("\n總共%d次機會\n",NUM);
detect(s);
}

Ⅷ 用C語言編寫一個猜數字游戲的程序(最好附帶說明)

有償,先程序後cash ,有意加我

Ⅸ 用C語言編寫猜數字(喜歡挑戰的人可以來看看哦)

// 猜數字.cpp : Defines the entry point for the console application.
//
#include <stdafx.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//隨機生成4位數,要求沒有重復數字
void ProceRandomNumber(int data[4])
{
int z;
/*隨機選取1-9999的數,放棄1-999的數,選擇1000-9999的數*/
do
{
srand( (unsigned)time( NULL ) );
z=(rand()%100)*(rand()%100);/*隨機選數*/
data[3]=z%10;/*把隨即數分成4個*/
data[2]=z/10%10;
data[1]=z/100%10;
data[0]=z/1000%10;
}while(z<1000||data[0]==data[1]||data[0]==data[2]||data[0]==data[3]||data[1]==data[2]||data[1]==data[3]||data

[2]==data[3]||data[0]==0);/*判斷每一位數是否相同,如果是則重新輸出*/
return ;
}
void Indata(int number,int data[4])
{
data[0]=number/1000%10;
data[1]=number/100%10;
data[2]=number/10%10;
data[3]=number%10;
}
int PosRight(int question[4],int answer[4])
{
int i,count=0;
for(i=0;i<=3;i++)
{
if(question[i]==answer[i])
{
count++;
}
}
return count;
}
int NumRight(int question[4],int answer[4])
{
int i,j,count=0;
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
if((question[i]==answer[j])&&(i!=j))
{
count++;
}
}
}
return count;
}

int main(int argc, char* argv[])
{
int answer,count=0,input[4],output[4],m=0,n=0,choice;
ProceRandomNumber(input);
printf(" 歡迎來玩猜數字游戲!\n 游戲開始啦!!!\n");
do
{
count++;
scanf("%d",&answer);
Indata(answer,output);
m=PosRight(input,output);
n=NumRight(input,output);
if(m==4)
{
printf("你真棒!!!\n這個數字就是%d%d%d%d\n你一共猜了%d次了!\n",input[0],input[1],input[2],input[3],count);
break;
}
else
{
printf("不好意思你錯了哦!! 提示: %dA%dB\n",m,n);
}
if(count==8)
{
printf("你已經猜了八次了!還要繼續嗎?(1:yes/0:false)\n");
scanf("%d",&choice);
if(choice==1)
printf("繼續吧!\n");
else
{printf("結束游戲!\n");
break;}

}
else if(count==15)
{
printf("你已經猜了15 次了,可能方法不對!下次繼續吧!!!\n");
break;
}
}while(1);
return 0;
}

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