Hello! As the title says I'm struggling around with an array.
Basically I'm trying to make a tetris and I started off by making the board. (A multi dimensional array)
And I was just testing my method that would end the game if any block reached the last part of the array, and I didn't get it working.
I manage to edit the array fine, with the new values etc. But I don't manage to make the changes get outside it´s class. So the method doesn't fulfil it´s function.
Here is the class where I store the array:
[CODE] class Setup
{
public const int BoardWith = 10;
public const int BoardHeight = 10;
public int[,] BoardData = new int[BoardHeight, BoardWith];
public int Empty = 0;
public int Full = 1;
}[/CODE]
Here is the class that should edit the arrays value (in this case all values to 1)
[CODE] class TheEmptyBoard
{
public void CreateBoard(int BoardX, int BoardY)
{
Setup B = new Setup();
for (int i = 0; i < BoardY; i++)
{
for (int j = 0; j < BoardX; j++)
{
B.BoardData[i, j] = B.Full;
}
}
}
}[/CODE]
Here is the Main():
[CODE] class Program
{
static void Main()
{
Tetris.Board.TheEmptyBoard B = new Tetris.Board.TheEmptyBoard();
Setup S = new Setup();
B.CreateBoard(10, 10);
Console.WriteLine("Something is wrong since it the value is {0} and not 1", S.BoardData[1, 1]);
Console.ReadKey();
}
}[/CODE]
Any help with the "B.BoardData[i, j] = B.Full;" and how I could make it change the real array would be very well appreciated!
Thanks on advance!
[QUOTE=Calax;36458303]Hello! As the title says I'm struggling around with an array.
Basically I'm trying to make a tetris and I started off by making the board. (A multi dimensional array)
And I was just testing my method that would end the game if any block reached the last part of the array, and I didn't get it working.
I manage to edit the array fine, with the new values etc. But I don't manage to make the changes get outside it´s class. So the method doesn't fulfil it´s function.
Here is the class where I store the array:
[CODE] class Setup
{
public const int BoardWith = 10;
public const int BoardHeight = 10;
public int[,] BoardData = new int[BoardHeight, BoardWith];
public int Empty = 0;
public int Full = 1;
}[/CODE]
Here is the class that should edit the arrays value (in this case all values to 1)
[CODE] class TheEmptyBoard
{
public void CreateBoard(int BoardX, int BoardY)
{
Setup B = new Setup();
for (int i = 0; i < BoardY; i++)
{
for (int j = 0; j < BoardX; j++)
{
B.BoardData[i, j] = B.Full;
}
}
}
}[/CODE]
Here is the Main():
[CODE] class Program
{
static void Main()
{
Tetris.Board.TheEmptyBoard B = new Tetris.Board.TheEmptyBoard();
Setup S = new Setup();
B.CreateBoard(10, 10);
Console.WriteLine("Something is wrong since it the value is {0} and not 1", S.BoardData[1, 1]);
Console.ReadKey();
}
}[/CODE]
Any help with the "B.BoardData[i, j] = B.Full;" and how I could make it change the real array would be very well appreciated!
Thanks on advance![/QUOTE]
First of all you should change BoardData to be a static array rather than one created with new. I have no experience with C# but this would probably look like public int BoardData[10, 10]. Second of all that line should change the real array I don't understand how it wouldn't. Third you might want to not do this and just allocate based on size parameters in other words it would look something like this:
[code]
public int[,] BoardData;
...
public void CreateBoard(int BoardX, int BoardY)
{
Setup B = new Setup();
B.BoardData = new int[BoardHeight, BoardWith]
}
[/code]
You would also need to record the sizes used in that case. Sorry if I botched that syntax but you should be able to understand the general idea.
A general tip for you as well, don't name your variables with a capital start letter.
For example, B.BoardData looks like you're accessing a static BoardData class variable in the B class.
You're creating 2 different Setup instances. 1 in Main method and 1 in CreateBoard method. Instead of that, pass the setup instance you created in Main method to CreateBoard method and make CreateBoard use that instance.
Ok I got it working almost immedietly now! Thanks for the help guys!
[QUOTE=Frugle;36459481]You're creating 2 different Setup instances. 1 in Main method and 1 in CreateBoard method. Instead of that, pass the setup instance you created in Main method to CreateBoard method and make CreateBoard use that instance.[/QUOTE]
So in otherwords, I have to change the array always inside the same class? There is no way to change arrays defined in a certain class inside another class? I'm just curious. Problems at least solved! Thanks!
You could also make CreateBoard return a setup value.
[code]
class TheEmptyBoard
{
public static Setup CreateBoard(int BoardX, int BoardY)
{
Setup B = new Setup();
for (int i = 0; i < BoardY; i++)
{
for (int j = 0; j < BoardX; j++)
{
B.BoardData[i, j] = B.Full;
}
}
return B;
}
}
[/code]
Then use it like
[code]
Setup S = Tetris.Board.TheEmptyBoard.CreateBoard(10,10);
[/code]
[QUOTE=Calax;36465479]Ok I got it working almost immedietly now! Thanks for the help guys!
So in otherwords, I have to change the array always inside the same class? There is no way to change arrays defined in a certain class inside another class? I'm just curious. Problems at least solved! Thanks![/QUOTE]
No. In your CreateBoard method, you're making a new Setup instance, which is entirely separate from the Setup instance that's defined in your Main method.
Methinks you should read up a bit more on OOP.
You seem to be doing this in a strange way. It looks to me as though the contents of the Setup class belong in your TheEmptyBoard class. How I would do it:
Have one class called Board, which stores all the info and methods related to it (so it stores your board array, some constants and the createBoard method)
Then you can call anything related to changing the board with something like:
Board myBoard = new Board(10,10);
myBoard.Setup();
(or you could just have the Board constructor setup the board for you)
As geel9 mentioned, I think you're having trouble with the concepts of OOP, which is common if you've only read web tutorials. My advice is to get a good book and read through it if you really want a grasp of OOP - using it the way you're using it might slow you down and is not tidy at all
Sorry, you need to Log In to post a reply to this thread.