0

Sorry I am new to c++ but I tried to follow a game tutorial in youtube and I think the logic is a bit wrong because it seems that the position of the enemy creates a pattern when it runs even though the srand and rand function are used, I think the same random number is being used.

#include <iostream>
#include <curses.h>
#include <time.h>
using namespace std;

void gotoxy (int x, int y){
  move (y, x);
}

bool exitGame;
bool isMoving;
int mycatX;
int mycatY;
int enemyX[4];
int enemyY[4] = {-9, -19, -29,-39};
int enemyPositionX[3] = {3, 18 ,32};

void startUp () {
  mycatX = 18;
  mycatY = 15;
  srand(time(NULL));
  for(int i = 0; i < 4; i++){
    enemyX[i] = enemyPositionX[rand() % 3];
  }
}

void layout () {
  if (isMoving){
      gotoxy (0, 0); printw ("##                                       ##");
      gotoxy (0, 1); printw ("##            ||            ||           ##");
      gotoxy (0, 2); printw ("##                                       ##");
      gotoxy (0, 3); printw ("##            ||            ||           ##");
      gotoxy (0, 4); printw ("##                                       ##");                     
      gotoxy (0, 5); printw ("##            ||            ||           ##");
      gotoxy (0, 6); printw ("##                                       ##");
      gotoxy (0, 7); printw ("##            ||            ||           ##");
      gotoxy (0, 8); printw ("##                                       ##");
      gotoxy (0, 9); printw ("##            ||            ||           ##");
      gotoxy (0, 10);printw ("##                                       ##");
      gotoxy (0, 11);printw ("##            ||            ||           ##");
      gotoxy (0, 12);printw ("##                                       ##");
      gotoxy (0, 13);printw ("##            ||            ||           ##");
      gotoxy (0, 14);printw ("##                                       ##");
      gotoxy (0, 15);printw ("##            ||            ||           ##");
      gotoxy (0, 16);printw ("##                                       ##");
      gotoxy (0, 17);printw ("##            ||            ||           ##");
      gotoxy (0, 18);printw ("##                                       ##");
      gotoxy (0, 19);printw ("##            ||            ||           ##");
      gotoxy (0, 20);printw ("##                                       ##");
      isMoving = false;
    }else{
      gotoxy (0, 0); printw ("##            ||            ||           ##");
      gotoxy (0, 1); printw ("##                                       ##");
      gotoxy (0, 2); printw ("##            ||            ||           ##");
      gotoxy (0, 3); printw ("##                                       ##");
      gotoxy (0, 4); printw ("##            ||            ||           ##");
      gotoxy (0, 5); printw ("##                                       ##");
      gotoxy (0, 6); printw ("##            ||            ||           ##");
      gotoxy (0, 7); printw ("##                                       ##");
      gotoxy (0, 8); printw ("##            ||            ||           ##");
      gotoxy (0, 9); printw ("##                                       ##");
      gotoxy (0, 10);printw ("##            ||            ||           ##");
      gotoxy (0, 11);printw ("##                                       ##");
      gotoxy (0, 12);printw ("##            ||            ||           ##");
      gotoxy (0, 13);printw ("##                                       ##");
      gotoxy (0, 14);printw ("##            ||            ||           ##");
      gotoxy (0, 15);printw ("##                                       ##");
      gotoxy (0, 16);printw ("##            ||            ||           ##");
      gotoxy (0, 17);printw ("##                                       ##");
      gotoxy (0, 18);printw ("##            ||            ||           ##");
      gotoxy (0, 19);printw ("##                                       ##");
      gotoxy (0, 20);printw ("##            ||            ||           ##");
      isMoving= true;
        
    }
}

void mycat ()
{
  gotoxy (mycatX, mycatY);     printw ("   ^^   ");
  gotoxy (mycatX, mycatY +1);  printw ("=( ^ ^)=");
  gotoxy (mycatX, mycatY + 2); printw ("  o  o ");
  gotoxy (mycatX, mycatY + 3); printw ("  (  )  ");
  gotoxy (mycatX, mycatY + 4); printw ("    \\   ");

}

void enemymouse ()
{
  for(int i = 0; i < 4; i++){
      if(enemyY[i] >= 0){
        gotoxy (enemyX[i], enemyY[i]);       printw ("  \\/ ");
        }
      if(enemyY[i]+1 >= 0){gotoxy (enemyX[i], enemyY[i] + 1); printw ("  ()  ");}
      if(enemyY[i]+2 >= 0){gotoxy (enemyX[i], enemyY[i] + 2); printw (" /  \\");}
      if(enemyY[i]+3 >= 0){gotoxy (enemyX[i], enemyY[i] + 3); printw ("((__))");}
      if(enemyY[i]+4 >= 0){gotoxy (enemyX[i], enemyY[i] + 4); printw ("   |  ");}    
         
         
          
      if(enemyY[i] > 20){gotoxy (enemyX[i], enemyY[i]);       printw ("         ");}
      if(enemyY[i]+1 > 20){gotoxy (enemyX[i], enemyY[i] + 1); printw ("         ");}
      if(enemyY[i]+2 > 20){gotoxy (enemyX[i], enemyY[i] + 2); printw ("         ");}
      if(enemyY[i]+3 > 20){gotoxy (enemyX[i], enemyY[i] + 3); printw ("         ");}
      if(enemyY[i]+4 > 20){gotoxy (enemyX[i], enemyY[i] + 4); printw ("         ");}
      if(enemyY[i] > 20) enemyY[i] = -10;
      enemyY[i]++;  
    }    
}
 

void controller (){
  nodelay (stdscr, TRUE);
  switch (getch ()) {
    case 'a':
      mycatX = mycatX - 13;
      break;
    case 'd':
      mycatX = mycatX + 13;
      break;
    case 'w':
      mycatY = mycatY - 2;
      break;
    case 's':
      mycatY = mycatY + 2;
      break;


    }

}

int main (){
  initscr ();
  startUp ();
  noecho ();
  scrollok (stdscr, TRUE);
  nodelay (stdscr, TRUE);
  while (!exitGame){
      layout ();
      mycat ();
      enemymouse ();
      controller ();
      napms(200);
      curs_set(0);
    }

  return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
  • Srand sets the seed. You set the seed as your time then used rand() so nothing abnormal happened. – Client Jun 03 '21 at 07:07
  • 2
    If you're using C++ of any modern ilk and not using [``](https://en.cppreference.com/w/cpp/numeric/random) for your random generator needs, you need to look into it. You'll be far more satisfied with the options available to you. – WhozCraig Jun 03 '21 at 07:16
  • The way to figure out if the same random number is being used is to write a (much simpler) program that just calls `rand()` a few times and displays the values. If you see a pattern with that program, you've identified the problem. – Pete Becker Jun 03 '21 at 12:14

1 Answers1

1

rand() doesn't actually generate true random numbers, rather pseudo-random. So it is kinda okay if you eventually start noticing a pattern, because there actually is a rule by which "random" numbers are being chosen.

For more info refer to other posts, like:

srand(time(NULL)) generating similar results

c++ random doesn't work(returns same value always)

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
A_Mel
  • 21
  • 2
  • 7
  • Do you know how I can remove that pattern? I tried to use with the mersenne twister, but it still didn't work. – The Aircrafters Jun 03 '21 at 09:44
  • You can't "remove" that pattern. Not a single program runs coincidentally - programs function as they are told. There are ways to make it more complex and less predictable, but it will never achieve true randomness. – A_Mel Jun 03 '21 at 09:57
  • actually... if you seed with a truly random number (whatever that may mean- usually more random means less predictable), you can introduce some true randomness into the system. For some people, true randomness means checking in on a potentially dead cat. For most people, a decision as to what millisecond they tell something to run is usually enough. – Abel Jun 03 '21 at 12:50