简体   繁体   中英

How do I pass an object from my main into my class?

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"?

Create a GameBoard class which creates Pieces in constructor or some Initialize method. You can then iterate over the pieces in your Main Form and draw the pictures.

See the implementation of Dragger Game on CodePlex

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 GameBoardMath class.

namespace GG
{
    public partial class frmPGame : Form
    {
        public frmPGame()
        {
            GameBoardMath.ClearGameBoard(gameBoard); // Clear the game board

            InitializeComponent();
        }

        Piece[,] gameBoard = new Piece[9, 8];    
    }

    public class GameBoardMath
    {
        public static void ClearGameBoard(Piece[,] gameBoard)
        {
            int lenX = gameBoard.GetLength(0);
            int lenY = gameBoard.GetLength(1);

            for (int y = 0; y < lenY; y++)
            {
                for (int x = 0; x < lenX; x++)
                {
                    gameBoard[x, y] = new Piece();
                    gameBoard[x, y].rank = -1;
                    gameBoard[x, y].player = 0;
                }
            }
        }
    }
}

Second variant: Here we consider the GameBoard to be a "full" class.

public class GameBoard
{
    public readonly Piece[,] Board;

    public GameBoard(int x, int y)
    {
        Board = new Piece[x, y];

        ClearGameBoard();
    }

    public void ClearGameBoard()
    {
        int lenX = Board.GetLength(0);
        int lenY = Board.GetLength(1);

        for (int y = 0; y < lenY; y++)
        {
            for (int x = 0; x < lenX; x++)
            {
                Board[x, y] = new Piece();
                Board[x, y].rank = -1;
                Board[x, y].player = 0;
            }
        }
    }
}

To use it, in your Form :

GameBoard gameBoard = new GameBoard(9, 8);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM