0

I'm creating a simple game of tennis, where the players are lines and the ball is a point. I'm having trouble moving the ball using rand. I want the ball to move randomly on the y-axis as it increments by one on the x-axis. Here is the procedure.

Libraries I'm using:

 #include <stdio.h>     
 #include <stdlib.h>    
 #include <time.h>   

void punto::viajar (char dir,int largo,int anch)// direction, length and width
{
    if (dir=='d') // if direction is to the right
    {
        x++; // moves to the right by 1
        srand(time(NULL)); // here is my problem
        if (rand()%2==0 && y<largo) // if ball is within the court's borders
        {
            y++;
        }
    }
    else
    {
        y--;
    }
    if (dir=='i') // to the left
    {
        x--;
        srand(time(NULL));
        if (rand()%2==0 && y<largo)
        {
            y--;
        }
    }
    else
    {
        y++;
    }
}

How can I move the ball?

EDIT: This is where I call viajar:

void juego:: funcionar()
 {
dibujar(); // draws ball, court, and players
char diraux='d'; // auxiliary direction
char tecla; // char key
while (1==1)
{
  while (pel.GetX()>0 && pel.GetX()<ancho) // while the ball is within court's range
  {
    pel.viajar(diraux,30,60); // ball travels to right, and court's length=30, width=60
    if (kbhit()) // if any of the two players presses a key
    {
       tecla=getch();
       moverraquetas(tecla); // if s/b is pressed, player 1 moves. if 2/8 is pressed, player 2 moves.
    }
    if (r1.toca(pel)=='S') //if player 1 touches the ball,
        diraux='d'; // ball moves to the right
    if (r2.toca(pel)=='S') // if player 2 touches the ball,
      diraux='i'; // ball moves to the left
  }
}

I'm sorry for the confusing explanation!

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
user3053042
  • 99
  • 2
  • 16
  • 1
    You have suggested *where* the problem is, but you have not told us *what* the problem is. Don't make us guess - the code posted has a number of issues one might comment on, many of which may not be related to the question you asked, but in the absence of guidance form you may attract comments, – Clifford Apr 19 '14 at 21:03
  • 1
    It's not directly related to the randomization question, but are you aware that your movement algorithm doesn't really resemble any ball i've ever seen? – Leeor Apr 19 '14 at 21:04
  • Contrary to the title of the post, you seem to have figured out how to get random numbers out of rand(). The question is how to turn that into movement, which we can't answer without knowing what graphics library you're using. SDL? Plain Windows calls? Carbon? – Max Lybbert Apr 19 '14 at 21:20
  • @MaxLybbert, I'm using the program codeblocks and lines and points for everything. So the ball is an O, and the lines are formed of #. – user3053042 Apr 19 '14 at 21:26
  • I noticed the players move in while (1==1) - move with moverrraquetas(tecla)- only when pel.viajar(diraux,30,60) isn't included. Otherwise, they don't move anymore. Any idea why? – user3053042 Apr 19 '14 at 21:30

4 Answers4

2

srand initializes the entire random stream - you shouldn't call it everytime you call rand, otherwise you'll always get the same value (the first in the stream, given srands' argument). Call it only once when you start the program.

In addition, rand() % small_int is not a very reliable random method as it doesn't guarantee uniform distribution. see here for e.g. - Why does rand() % 7 always return 0?

Community
  • 1
  • 1
Leeor
  • 19,260
  • 5
  • 56
  • 87
2

Call srand() once and only once when your program starts. Setting the random number seed every time you call rand() is unnecessary and does not allow rand() to generate its sequence as intended. Moreover if you call srand() with the same value, rand() will produce the same value, so using the current time as the seed is going to have an undesirable effect if you call it twice in the same second.

In many cases it is not necessary to call srand() at all - does it really matter if your ball uses the same random sequence every time your program runs - the "random" behaviour of the players will make every game different in any case.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • Virtual +1 here (since I can't actually upvote now). – πάντα ῥεῖ Apr 19 '14 at 21:05
  • @Clifford: I guess that's true. However, I still don't get how to make the ball move on its own (even after entering values in void viajar). – user3053042 Apr 19 '14 at 21:08
  • 1
    @user3053042: As I said in teh comment to your question, it is not celar wat you are asking other that *you* told us the problem was with the `srand()` call, but that is not the only serious problem (or even the most serious). Furthermore, modifying x and y correctly alone will not "move the ball", you presumably have to draw the ball at x, y, and how you do that is not within the scope of the question with the information given. The bast way to solve this kind of problem is to step the code in your debugger and observe the code flow and how the variable change. – Clifford Apr 19 '14 at 21:18
1

Although there are issues with the use of srand() as already noted, I don't think that is your biggest problem.

For example, if dir=='d', y may be incremented, but it will always be incremented in the else clause of dir=='i'. Equally when dir=='i', y will be decremented twice.

The control flow should perhaps be:

if (dir=='d') // if direction is to the right
{
    x++ ;
    if( ... )
    {
        y++;
    }
    else
    {
        y--;
    }
}
else if (dir=='i') // MUTUALLY EXCLUSIVE TO 'd'
{
    x--;
    if( ... )
    {
        y--;
    }
    else
    {
        y++;
    }
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
-1

Use this code for getting random value

srand (time(NULL)); cout << (rand() % 100 + 1)

I hope this will work

Sana Malik
  • 1
  • 1
  • 1
  • 3