Sign in to follow this  
antone88

[Solved] Program hanging

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.

[code]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);
}
}
}
[/code]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this