Update: visualization for alien/space invaders
@@ -49,20 +49,17 @@ void NAR_Alien(long iterations)
double alien0X = 0.5;
double defenderX = 0.5;
double alienWidth = 0.18;
+ int batWidth = 1;
+ int szX = 20;
+ int szY = 6;
int hits = 0;
int shots = 0;
int t=0;
while(1)
{
- if(t++ > iterations && iterations != -1)
- {
- break;
- }
- if(iterations == -1 && t%10000 == 0)
- {
- getchar();
- }
- fputs("\033[1;1H\033[2J", stdout); //POSIX clear screen
+ bool shot = false;
+ bool hit = false;
+ //simulate
bool cond1 = (defenderX <= alien0X - alienWidth);
bool cond2 = (defenderX > alien0X + alienWidth);
if(cond1)
@@ -80,10 +77,12 @@ void NAR_Alien(long iterations)
NAR_AddInputGoal(Narsese_Term("s0"));
if(NAR_Alien_Shoot_executed)
{
+ shot = true;
NAR_Alien_Shoot_executed = false;
shots++;
if(!cond1 && !cond2)
{
+ hit = true;
hits++;
NAR_AddInputBelief(Narsese_Term("s0"));
alien0X = ((double)(myrand() % 1000)) / 1000.0;
@@ -99,8 +98,60 @@ void NAR_Alien(long iterations)
NAR_Alien_Right_executed = false;
defenderX = MIN(1.0, defenderX+0.1);
}
+ //visualize:
+ int batX = defenderX*10;
+ int ballY = 5;
+ int ballX = alien0X*10;
+ if(t++ > iterations && iterations != -1)
+ {
+ break;
+ }
+ if(iterations == -1 && t%10000 == 0)
+ {
+ getchar();
+ }
+ fputs("\033[1;1H\033[2J", stdout); //POSIX clear screen
+ for(int i=0; i<batX-batWidth+1; i++)
+ {
+ fputs(" ", stdout);
+ }
+ for(int i=0; i<batWidth*2-1 ;i++)
+ {
+ fputs(shot ? ":" : "'", stdout);
+ }
+ puts("");
+ for(int i=0; i<ballY; i++)
+ {
+ for(int k=0; k<szX; k++)
+ {
+ fputs(" ", stdout);
+ }
+ puts("|");
+ }
+ for(int i=0; i<ballX; i++)
+ {
+ fputs(" ", stdout);
+ }
+ fputs(hit ? "_" : "o", stdout);
+ for(int i=ballX+1; i<szX; i++)
+ {
+ fputs(" ", stdout);
+ }
+ puts("|");
+ for(int i=ballY+1; i<szY; i++)
+ {
+ for(int k=0; k<szX; k++)
+ {
+ fputs(" ", stdout);
+ }
+ puts("|");
+ }
printf("shots=%d hits=%d ratio=%f time=%d\n", shots, hits, (float) (((float) hits) / ((float) shots)), t);
//nanosleep((struct timespec[]){{0, 10000000L}}, NULL); //POSIX sleep
//NAR_Cycles(10);
+ if(iterations == -1)
+ {
+ nanosleep((struct timespec[]){{0, 20000000L}}, NULL); //POSIX sleep
+ }
}
}
