Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

c# random problem

Started by Delemorte Apr 29, 2007 at 1:51 AM 4 replies 1.2k views
Original Post
Delemorte
Delemorte
I did a search and couldn't find any other posts. I'm using the following code to get a random number to pick the next block type, however everytime it gets to the 6th block, the block stays at the bottom of the screen and the random generator just goes nuts displaying all blocks in rapid succession. If i change rand.Next(0,7) i get the same results. If i lower rand.Next(4) then it goes nuts on the 3rd block. Is the random command bugged or am i missing something perhaps. Any help appreciated. I've commented out the areas where i've tried to run random and eventually moved it to a global variable. Random rand = new Random(); private int Rdm() { //Random rand = new Random(); int nextShapeNo = rand.Next(7); return nextShapeNo; } private void CurrentShape()//selects a shape to display on the screen. Will update to reflect next shape { //then current shape will be retrieved from the next shape variable. //Random rand = new Random(); //int currentShapeNo = rand.Next(0,6); Rdm(); int currentShapeNo = Rdm(); switch (currentShapeNo) { case 0: currentShape = sevenBlock; break; case 1: currentShape = rBlock; break; case 2: currentShape = tBlock; break; case 3: currentShape = sBlock; break; case 4: currentShape = zBlock; break; case 5: currentShape = lineBlock; break; case 6: currentShape = squareBlock; break; } blockSpeed = new Vector2(100.0f, 100.0f); }
Omega147
Omega147
It sounds like the error is elsewhere in your code, but your switch block may be messing something up since it has no default case. Try adding this to the switch block:

default: break;
Omid Ghavami
Omid Ghavami
I don't see anything wrong with your usage of the Random class, except the first line here is redundant:
Rdm();
int currentShapeNo = Rdm();

Are you sure the problem is with the Random generator? Have you monitored the values it produces? I would guess that you probably have a problem somewhere else in your code.
Best regards, Omid
Delemorte
Delemorte
I found the problem, thanks for pointing me in the right direction.

I was calling the random method more than once per array of blocks and it would cause it to enter an infinite loop due to the stopping position combined with the call to the random method.

for (int i = 0; i < 4; i++)
{
if (currentShape.Y > MaxY)//detects when the block shape hits the bottom of the screen then stops it
{
blockSpeed.Y = 0;
CurrentShape();//this contains the call to random
}
}

LJ
Omid Ghavami
Omid Ghavami
I believe I have located your problem Mr. Delemorte [smile]
At the top of the code you define your shapes and, the important part, you set them to the their initial position, which is at the top of the screen
#region Block shapesVector2[] sevenBlock = new Vector2[]//Define all 7 block shapes here using vector2 arrays{    new Vector2(200f,100f),    new Vector2(220f,100f),    new Vector2(220f,120f),    new Vector2(220f,140f)};Vector2[] rBlock = new Vector2[]{    new Vector2 (240f,100f),    new Vector2 (220f,100f),    new Vector2 (220f,120f),    new Vector2 (220f,140f)};Vector2[] tBlock = new Vector2[]{    new Vector2 (180f,100f),    new Vector2 (200f,100f),    new Vector2 (220f,100f),    new Vector2 (200f,120f)};Vector2[] sBlock = new Vector2[]{    new Vector2 (220f,100f),    new Vector2 (220f,120f),    new Vector2 (240f,120f),    new Vector2 (240f,140f)};Vector2[] zBlock = new Vector2[]{    new Vector2 (240f,100f),    new Vector2 (240f,120f),    new Vector2 (220f,120f),    new Vector2 (220f,140f)};Vector2[] lineBlock = new Vector2[]{    new Vector2 (200f,100f),    new Vector2 (200f,120f),    new Vector2 (200f,140f),    new Vector2 (200f,160f)};Vector2[] squareBlock = new Vector2[]{    new Vector2 (200f,100f),    new Vector2 (220f,100f),    new Vector2 (200f,120f),    new Vector2 (220f,120f)};#endregion


Then you set the currentShape variable to reference one of these. This means that when you alter the position of the currentShape, you are actually altering the position of sevenBlock, rBlock etc. So once a piece has been used, it's location will now be at the bottom of the screen. The next time that piece is used it will no longer be located at the position given when you initialized it, but at the position it ended up in after being used as the currentShape.
What you want to do is to explicitly copy the content of your pre-defined shapes into currentShape instead of setting the reference.
Best regards, Omid
Delemorte
Delemorte
Thanks for the quick reply. I'll look into how to do that. This is my first attempt at a game(tetris clone) and at this stage i'm just happy getting blocks to randomly generate and move down lol.
LJ

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.