Helplife2.net: C problems, again

15357

Companion Cube
Joined
Jan 11, 2005
Messages
15,209
Reaction score
23
Hey guys. :p

Ok, I'm working on a C problem for a few hours now, and I can't seem to be able to work this one out. I never knew I sucked this much at C. :/

Basically, what I'm trying to do is to make it rain * randomly and pile up as they hit the ground.

Example:

Code:
   *
Code:
*
   *
Code:
     *
*
   *
Code:
   *
     *
*
   *
Code:
 *
   *
     *
*  *
Code:
   *
 *
   *    
*  * *

You understand, right? The asterisks have to fall down and pile up in order after they hit the "ground" or another asterisk that fell earlier.

I've been trying to do this by using

Code:
for (i = 0; i < 400; i++){
        b++;
	if (hor[i] == 1){
		printf("*");
					
	}
        else if (b == 41){
		printf("\n");
		b = 1;
	}
	else 
		printf(" ");
}

(well Kind of, just an example)

and system("cls"); to clear the screen after each loop.

The professor suggested using SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
and a set of COORD variables, but that didn't help much.

Any suggestions? I'll prolly get this working sooner or later, but I could really use the help.
 
I do recommend finding a good programmers forum. Back when I was still taking classes it certainly helped a lot.
 
It stays at the first line

like:

*
and then

* *

and so on.

I do recommend finding a good programmers forum. Back when I was still taking classes it certainly helped a lot.

Eh. I suppose so. :(
 
I'M NOT SAYING PEOPLE HERE CAN'T HELP YOU; IT'S JUST THAT
programming sucks balls
and you'd get more responses faster at specific sites too
 
I don't recall ever seeing a programming thread being a success in off-topic...plus most of us probably can't help. there are plenty of forums out there that can so thats your best option.
 
I don't recall ever seeing a programming thread being a success in off-topic...plus most of us probably can't help. there are plenty of forums out there that can so thats your best option.

Well, you know, I got my last irresolvable problem solved over here so I thought I'd try again. :)
 
I'm not too familiar with C but it looks to me like your logic isn't going to produce what you want.

It seems like you're just generating asterisks, which works to get them line by line down the page, but you won't be collecting them.

I think using a series of character arrays for each line and then copying them as they go down will give yo the desired effect.

For instance - you generate the five arrays and print them.
Line1-
Line2-
Line3-
Line4-
Line5-

Then "randomly" generate spaces and asterisks (I'm not sure how well rand would work for this but I don't know C well) for the first line.
Line1- * *
Line2-
Line3-
Line4-
Line5-

Then, copy Line1 to Line2 and generate another Line1.
Line1-*
Line2- * *
Line3-
Line4-
Line5-

Continue this to 5, but at 5 you'd have to have some sort of additional loop to parse through the array of line4 with an If statement to identify the location of each asterisk within it. Then you could just copy the asterisk to the same location in line 5. Ultimately though, line five would be all asterisks, but I assumed that's what you wanted.

I'm not entirely sure if it would work but it seems to make sense for me... let me try and make some psudocode

for (int i=0, i<400, i++)
{
Line1array = randomstring
for (int i=0, i<5, i++)
{
if line4array(i) = * Then
line5array(i) = *
}
Line4array = Line3array
Line3array = Line2array
Line2array = Line1 array
print Line1array... etc
}

If you do it in reverse then you'll be able to capture the arrays before they get new values. I have no idea if it will actually work, but it seems like it should. Good luck getting the syntax right though...

Also you'll prolly have to say something like "if (i != 0) to prevent line 2 from copying line 1 straight away.
 
Oh. Cool. That might actually work. But I don't understand how to randomly generate a space within those arrays? Could you explain it further?
 
I can't say I remember how random number generation works in C++, but if you can find that (prolly in a book or the cool internet place), you can basically just do another loop the length of the array that does a two number random generation. Something like -

for (int i=0, i<5, i++)
{
x= random(2)
If x = 1
Line1array(i) = " " //this will prolly have to be the ascii code for space now that I think about it
else
Line1array(i) = *
}

I'm sure there are better ways to do all of this but that's just what I'm coming up with early in the morning with little motivation.
 
Ah. Ok.

I think I get it now. Thanks a lot. :D


Of course, I need to actually convert this to C. :p
 
rainyl.jpg
 
no , i wrote my own algorithm.
EDIT: It more or less does what you described though. cant really think of another solution.
 
yeah , i used a 4X4 array to represent the map . everything is generalized so it can scale up, however you might want to think of other data structures for larger maps because right now it loops through every location in the array even though some of them are empty.
 
Everyone knows if you post on a C forum theyll just give you pointers to do the work yourself... but if you post on helplife2.net, someone will just do the work for you.
 
Everyone knows if you post on a C forum theyll just give you pointers to do the work yourself... but if you post on helplife2.net, someone will just do the work for you.

Nah , i'm not giving him any code this time. But i do enjoy these small little programming challenges it keeps me from getting rusty.
 
I'm a terrible C programmer but I'm pretty good in VB so I can supply only pseudocode.
 
D:

Yeah, that was what I was looking for. Shame you can't give me the code. :(


EDIT: I've been using 2 arrays to map the vertical and the horizontal ones together; I really don't know how to use a "2D array". The closest I suppose are COORD variables?
 
Code:
2d array: (notation may be incorrect for C, been a while since I did C)

int array[2][3]; 

first bracket has rows
2nd bracket is column

This creates a matrix like this. 

    0   1  2
0 [  ][  ][  ]
1 [  ][  ][  ]

This is an easy problem. Don't break your head coming up with complex solutions.
Use a 2d array. Array size can be set using a variable, you don't need to hardcode the array's size.

You could also use Lists in order to create a dynamically expandable example of this, but I doubt your teacher wants you to do this.

Using the 2d array:

Use it just like you would a 1D array. The only difference is that you now have 2 indexes instead of one.
 
Basically, what I'm trying to do is to make it rain * randomly and pile up as they hit the ground.
Code:
for (i = 0; i < 400; i++){
        b++;
	if (hor[i] == 1){
		printf("*");
					
	}
        else if (b == 41){
		printf("\n");
		b = 1;
	}
	else 
		printf(" ");
}
There are 2 issues here :-

1. Randomness. You have none. Use the srand(), rand() functions to get true random integers and use % to get valid coordinates.

2. Delay. You need it so your program isn't a blur. Use the appropriately named delay() function.

I won't code it here, but here's the pseudocode:

(a) MxN array holds the entire rectangular space.
(b) Move all rows down by 1 (so row 0 is free.)
(c) Create new row 0 by placing a random number of asterisks at random positions // eg:- * * *
(d) Call delay function. Loop back to (b)

Cheers! :cheers:
 
Dunno why I didn't think about 2D arrays... Prolly cause I hated them.
 
Use a timer to get the seed values for srand.

I implemented the delay by waiting in a while loop for a bit, i'm not sure if that delay function works in c.
 
int **array;
int M = 2, N = 3;

array = (int**) malloc(M*N);

...and done.

nice try, but he was referring to declaring an array with a variable, which the compiler will always throw an error.

and wtf would he go to all that trouble to use semantics that I highly doubt theyve covered in the class at this point. even if he did, he'd probably **** smthg up and leak memory all over the place (no offense, numbers)
 
This idea was too fun to not spend a few minutes on! I used a single array solution. This one scales too.

I didn't have a C environment ready, so I used Java. I tried to write it comprehensibly instead of efficiently or short, so that you might get something out of it. This idea would work fine in C.

http://pastebin.com/xyW9G4dZ (with syntax highlighting)

Code:
package gekut;

import java.util.Random;

public class sneeuw
{
	public static void main(String[] args)
	{
		snow(4,5);
	}
	
	public static void snow (int width, int height)
	{
		boolean[] box = new boolean[ width * height ];
		int number_of_flakes = 0;
		int empty_space = width*height;
		
		while( number_of_flakes < empty_space )
		{
			// update snowfall!
			for( int i = (box.length-1) - width; i>=0; i-- )
			{
				// walk through array backwards, skipping lowest layer!
				if( !box[i] )
				{
					if( i<width )
					{
						// generate new snowflake here!
						Random snowflake = new Random();
						box[i] = snowflake.nextBoolean() && snowflake.nextBoolean(); // 25% chance!
						if( box[i] ) number_of_flakes++;
					}
				}
				else
				{
					if( !box[i+width] )
					{
						// nothing below us; fall down!
						box[i] = false;
						box[i+width] = true;
					}
				}
			}
			
			// show it!
			for( int i=0; i<box.length; i++ )
			{
				if( i%width == 0 )
				{
					// new row!
					System.out.println();
					System.out.print("|");
				}
				
				if( box[i] )
				{
					// flake here!
					System.out.print("*");
				}
				else
				{
					System.out.print(" ");
				}
			}
			System.out.println("\n");
		}
	}
}
 
Back
Top