[問題] 變數亂跑?
我在寫一個簡單的打地鼠
可是分數 num 每次在空跑了幾輪後就會莫名地變成極大的數
我和同學找了很久都找不到原因
不知道是什麼原因
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
void creat(void);
void display(void);
void hit(void);
int num=0; // 分數跑過幾輪後會變很大
char mouse[8]={0};
int main(void)
{
int count=0;
srand(time(NULL));
while(1)
{
creat(); //建立圖形
while(1)
{
hit(); //檢查是否按鍵
count++;
if(count==1)//原定40000
{
system("cls");
display(); //顯示
count=0;
break;
}
}
system("cls");
}
return 0;
}
void creat(void)
{
int number;
int a,b,c,d;
memset(mouse,'0',8);
number=rand()%5; //隨機取地鼠隻數
a=rand()%8; //隨機取地鼠出現的地點
b=( (a*2501+35)-37 )%8; //亂取
c=( (b*2083+90)-36 )%8;
d=( (c*1779+11)-39 )%8;
if(number==1)
mouse[a]='1';
else if(number==2)
{
mouse[a]='1';
mouse[b]='1';
}
else if(number==3)
{
mouse[a]='1';
mouse[b]='1';
mouse[c]='1';
}
else if(number==4)
{
mouse[a]='1';
mouse[b]='1';
mouse[c]='1';
mouse[d]='1';
}
display();
}
void display()
{
int i;
for(i=0;i<8;i++)
{
if(i==4)
printf("\n");
if(mouse[i]=='0') //沒地鼠
printf("○ ");
else if(mouse[i]=='1') //有地鼠
printf(" § ");
else if(mouse[i]=='2') //打到地鼠
printf("hit! ");
}
printf("\n\n得分:%d\n",num);
}
void hit(void)
{
char down=' ';
if( kbhit() ) //檢查是否按鍵
{
down=getch();
if(down=='g' && mouse[0]=='1')
{
num+=10;
mouse[0]='2';
system("cls");
display();
}
if(down=='h' && mouse[1]=='1')
{
num+=10;
mouse[1]='2';
system("cls");
display();
}
if(down=='j' && mouse[2]=='1')
{
num+=10;
mouse[2]='2';
system("cls");
display();
}
if(down=='k' && mouse[3]=='1')
{
num+=10;
mouse[3]='2';
system("cls");
display();
}
if(down=='v' && mouse[4]=='1')
{
num+=10;
mouse[4]='2';
system("cls");
display();
}
if(down=='b' && mouse[5]=='1')
{
num+=10;
mouse[5]='2';
system("cls");
display();
}
if(down=='n' && mouse[6]=='1')
{
num+=10;
mouse[6]='2';
system("cls");
display();
}
if(down=='m' && mouse[7]=='1')
{
num+=10;
mouse[7]='2';
system("cls");
display();
}
down=' ';
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.116.13.191
推
02/15 23:57, , 1F
02/15 23:57, 1F
推
02/16 00:00, , 2F
02/16 00:00, 2F
→
02/16 00:00, , 3F
02/16 00:00, 3F
→
02/16 00:04, , 4F
02/16 00:04, 4F
→
02/16 00:05, , 5F
02/16 00:05, 5F
→
02/16 00:05, , 6F
02/16 00:05, 6F
→
02/16 00:15, , 7F
02/16 00:15, 7F
→
02/16 00:15, , 8F
02/16 00:15, 8F
推
02/16 02:27, , 9F
02/16 02:27, 9F
→
02/16 02:27, , 10F
02/16 02:27, 10F
→
02/16 02:28, , 11F
02/16 02:28, 11F
→
02/16 02:28, , 12F
02/16 02:28, 12F
→
02/16 02:28, , 13F
02/16 02:28, 13F
推
02/16 09:52, , 14F
02/16 09:52, 14F
→
02/16 10:21, , 15F
02/16 10:21, 15F
→
02/16 10:44, , 16F
02/16 10:44, 16F
推
02/17 11:25, , 17F
02/17 11:25, 17F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章