Jump to content
  • Advertisement
Sign in to follow this  
antone88

[Solved] Program hanging

This topic is 2704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, my program is hanging. I need a fresh pair of eyes to look at it and see where it's freezing up. The freeze occurs after I press "A", calling the setTiles method. I'm guessing I've got a loop running unchecked but I can't see it. I'm sure I'll feel dumb if someone can just point it out. Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Memaran_Checkers
{

public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
bool[] grid;
SpriteFont font;
SpriteFont font2;
int count;

public int getRandom(int x)
{
Random random = new Random();
return random.Next(x);
}

public int getOpenTiles()
{
int tilesOpen = 0;
for (int g = 0; g < count; g++)
{
if (grid[g] == false)
{
tilesOpen++;
}
}
return tilesOpen;
}

public int[] randomTiles()
{
int[] randomTiles = new int[5];
int count = randomTiles.Length;
for (int r = 0; r < count; r++)
{
randomTiles[r] = getRandom(getOpenTiles() - (r + 1));
}
return randomTiles;
}

public void setTiles()
{
int[] rTiles = randomTiles();
int tile;

for (int s = 0; s < 4; s++)
{
tile = rTiles[s];
if (grid[tile] == false)
{
grid[tile] = true;
}

else
{
int w = s;
int wTile = rTiles[(s + w)];
while (grid[wTile] == true)
{
w++;
if (w >= 15)
{
w = 0;
}
if (grid[wTile] == false)
{
grid[wTile] = true;
}
}
}
}
}


public void clearTiles()
{
for (int c = 0; c < count; c++)
{
grid[c] = false;
}
}

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}


protected override void Initialize()
{
grid = new bool[16];
count = grid.Length;
font2 = Content.Load<SpriteFont>("SpriteFont2");
font = Content.Load<SpriteFont>("SpriteFont1");
base.Initialize();
}


protected override void LoadContent()
{

spriteBatch = new SpriteBatch(GraphicsDevice);


}


protected override void UnloadContent()
{

}


protected override void Update(GameTime gameTime)
{

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
{
this.Exit();
}
bool pressedLastFrame = false;
if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed && pressedLastFrame == false)
{
clearTiles();
setTiles();
}

if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Released)
{
pressedLastFrame = false;
}
else
{
pressedLastFrame = true;
}



base.Update(gameTime);
}


protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Gray);

spriteBatch.Begin();
spriteBatch.DrawString(font2, "Tiles", new Vector2(0, 0), Color.White);
Vector2 stringSize = font2.MeasureString("Tiles");
for (int a = 0; a < count; a++)
{
spriteBatch.DrawString(font2, "" + a + " = " +grid[a], new Vector2(0, (stringSize.Y * (a+1))), Color.White);
}
spriteBatch.End();

base.Draw(gameTime);
}
}
}


Edit: I walked away and rethought what I was attempting to do with my code. It turns out that I didn't even need 2 of the methods. Now it works. :P

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!