I’m a student doing an extra-grade project, and my prof has asked me to create a program that deals with classes.
So, I’ve created a class called “Piece” with the following code:
namespace GG
{
class Piece
{
public int rank;
public int player;
}
}
I instantiated it in my main program (the Form), like so:
namespace GG
{
public partial class frmPGame : Form
{
public frmPGame()
{
InitializeComponent();
}
Piece[,] gameBoard = new Piece[9, 8];
public void clearGameBoard()
{
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 9; x++)
{
gameBoard[x, y] = new Piece();
gameBoard[x, y].rank = -1;
gameBoard[x, y].player = 0;
}
}
}
}
}
Anyway, the form has pictures, and, depending on what’s inside the 2D array I’ve created, the picture can change. However, I also do some math in my main program, which makes it cluttered and long. I’d like to ask if I can, somehow, pass the gameBoard object to another class, along with its contents.
Basically the program flow right now is:
- Create Object
- Do math in Main using Object
- Change form pictures
And, I’d like to, if possible, change it into:
- Create Object
- Do math in Math class
- Return / Pass Object from math class to Main
- Change form pictures
I know it looks like I’m complicating stuff, but my professor told me the only thing the main should do is render the pictures. The class should handle the math-related stuff.
Could I have some advice on this, please? Do I instantiate the “Piece” Class in my “mathClass”?
I’ll propose two variants. Remember that arrays are reference types. If you pass one of them to a method, the method can modify it directly.
First variant:
Here the game board isn’t “described” as a full object. It’s an array with some (static) utility methods put in a
GameBoardMathclass.Second variant: Here we consider the
GameBoardto be a “full” class.To use it, in your
Form: