A. 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);
}
B. C語言中猜數字的代碼
再定義兩個變數,在游戲說明後面套個while循環,條件你想要退出輸入的鍵例如:(x=='y')繼續游戲,否者退出!至於統計次數在選者外套用一個for循環語句,在答對的情況下i 自增最後輸出i的值就可以了!
C. 請用c語言編寫猜數字游戲
沒時間來寫符合你題目的,這個是我以前寫過的一個猜數字游戲,系統會給出一個的1~99之間的隨機數,你來猜,看多少次可以猜中. 你看用得上不.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
int main (int argc, char **argv)
{
int d, c;
struct timeval tv;
gettimeofday (&tv, NULL);
srand (tv.tv_usec);
d = rand () % 100;
printf ("Please input a number:\n");
scanf ("%d", &c);
printf ("c=%d\n", c);
int min_num = 1;
int max_num = 99;
int num=1;
while (c != d)
{
if (c > d)
{
if( c < max_num )
max_num = c;
printf ("%d - %d\n", min_num ,max_num);
}
else if (c < d)
{
if( c > min_num )
min_num = c;
printf ("%d - %d\n", min_num ,max_num);
}
else
break;
scanf ("%d", &c);
num++;
printf("c = %d \n",c);
}
printf ("it's true:%d,num=%d\n", c,num);
return 0;
}
D. 猜數游戲c語言
#include <stdio.h>
#include <stdlib.h>
main()
{
int n,t,g;
char c;
while(1)
{
n = 1 + rand() % 100;
printf("請猜一個1~100的數字:");
scanf("%d",&g);
t=1;
while(g!=n)
{
if(t==10) break;
if(g<n) printf("猜小了哦\n");
else printf("猜大了哦\n");
scanf("%d",&g);
t++;
}
if(t==1) printf("太棒了,一次就猜對了!\n");
else if(t>2 && t<5) printf("猜了%d次,也不錯哦。\n",t);
else if(t>4 && t<8) printf("猜了%d次,還可以\n",t);
else if(t==8 || t==9) printf("猜了%d次才對,你亂猜的吧\n",t);
else if(t==10) printf("怎麼猜了10次還不對啊,算了,猜下一個吧。\n");
if((c=getchar())=='q') break;
}
}
E. 猜數字游戲 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]);
}
F. C語言 猜數字程序
原因分析:
「scanf("%d",&a);」這一行,輸入完一個數字後,你還必須按下回車鍵,所以這行執行完畢後緩沖區就還留著一個'\n'字元即回車字元。執行到「}while(ch=getchar()!='n');」時,這字元就直接賦給了ch(驗證方法:把「while(ch=getchar()!='n');」改為「while(ch=getchar()!='\n');」,你會發現輸入一個數字後程序就直接結束了)。
對症下葯:
方案一:「scanf("%d",&a);」這一行後加上「fflush(stdin);」(作用:清空輸入流)。但這樣改有個麻煩,就是你每次輸入一個數字後你都還得因要執行getchar()而再輸入一個字元。
方案二:「}while(ch=getchar()!='n');」改為「}while(ch=getch()!
='n');」(getch()會自動跳過前導的回車字元),這樣改也有個類似方案一的麻煩,就是輸入一個數字後你還得按一個鍵後方能再輸入數字。
方案三:進行代碼優化:(順便說下,你的代碼可讀性有些問題,和下面的對照下吧)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
char ch;
int i,a;
printf("猜數字游戲\n\n");
srand(time(NULL));
i=rand()%10;
printf("請輸入數字\n\n");
while(1)
{
scanf("%d",&a);
if(i>a) printf("小了\n");
if(i<a) printf("大了\n");
if(i==a)
{
printf("你猜對了!\n");
printf("按n退出,按y繼續\n");
fflush(stdin);//清空輸入流
if(getchar()=='n') break;
i=rand()%10;//重新隨機答案
}
}
}
給分吧,花了我老半天時間呢!
G. 如何用c語言寫一個猜數字游戲,我輸入正確數字,讓電腦自動去猜
網頁鏈接
與上述引用同一種問題。
//二分法查找數據,1-100內查找一個數據,查找一次需要判斷一下YesorNo.
#include<stdio.h>
intmain()
{
//初始化
intlow=1,high=100,guess,mid=low,k=0;
chara='n';
//主體
printf("Pleaseinputintegerfrom1to100,Iwilltrytoguessit. ");
scanf("%d",&guess);
printf("Ifiguesstrueinput'y',elseinput'n'. ");
while(1)
{
printf("測試循環了%d次 ",k++);
mid=(low+high)/2;
printf("Um...isyournumberis%d? ",mid);
getchar();//作用:「吃掉」『 』,否則下一行的scanf函數會讀入回車符號。
scanf("%c/n",&a);
if(a!='y')
{
if(mid<guess)//猜測數在mid--high之間
{
low=mid;
}
elseif(mid>guess)//猜測數在low--mid之間
{
high=mid;
}
}
elsebreak;
}
return0;
}
H. 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");
}