文字マトリックス10x10を表示し、その中に文字を表示するプログラムを作成しようとしています。600ミリ秒ごとに、文字はランダムに移動します。しかし、私の問題は、プログラムを実行するたびに、それが同じ動きであるということです。
Random_move関数を見ると、Rand()関数を使用しています... srand(time(null));も試してみました。以前は、aとbの両方の文字が常に同じ方向に移動するだけです。誰か助けてくれませんか。
#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
class mapa
{
private :
char map[10][10];
char background[10][10];
public :
mapa();
void cpy_btom();
void copy_to_map(int, int, char);
void print();
};
class character
{
private :
int posx;
int posy;
char type;
public :
character(char,int,int);
void send_print(mapa &);
void random_move(mapa &);
void delay(int);
};
int main()
{
character C1('A', 5, 5);
character C2('B', 8, 2);
mapa Mapa;
while(!GetAsyncKeyState(VK_ESCAPE))
{
Mapa.print();
C1.delay(750);
C1.random_move(Mapa);
C2.random_move(Mapa);
}
}
void mapa :: cpy_btom()
{
for(int a = 0;a < 10;a++)
{
for(int b = 0;b<10;b++)
{
map[a][b] = background[a][b];
}
}
}
void mapa :: print()
{
system("cls");
for(int a = 0;a<10;a++)
{
for(int b = 0;b<10;b++)
{
cout << map[a][b];
}
cout << endl;
}
cpy_btom();
}
character :: character(char kind = 'a', int x = 5, int y = 5)
{
type = kind;
posx = x;
posy = y;
}
void character :: send_print(mapa & mapa)
{
mapa.copy_to_map(posx, posy, type);
}
void character :: random_move(mapa & MAP)
{
int a = rand() % 5;
int b = rand() % 50;
if(a == 0) //x --
{
if(b < 45)
{
if(posx > 0)
posx--;
}
else
{
if(posx > 1)
posx = posx - 2;
}
}
else if(a == 1)
{
if(b < 45)
{
if(posx < 10)
posx++;
}
else
{
if(posx < 9)
posx = posx + 2;
}
}
else if(a == 2)
{
if(b < 45)
{
if(posy > 0)
posy--;
}
else
{
if (posy > 1)
posy = posy - 2;
}
}
else if(a == 3)
{
if(b < 45)
{
if(posy < 10)
posy++;
}
else
{
if(posy < 9)
posy = posy + 2;
}
}
send_print(MAP);
}
void character :: delay(int time)
{
int a = clock();
int b = clock() + time;
while(a < b)
{
a = clock();
}
}
mapa :: mapa()
{
for(int a = 0;a < 10;a++)
{
for(int b = 0;b < 10;b++)
{
map[a][b] = ' ';
background[a][b] = ' ';
}
}
}
void mapa :: copy_to_map(int x, int y, char kind)
{
map[x][y] = kind;
}