0

I'm making a chess game and for the pieces to move I need the variable board and it's located in the Form1 class. I was wondering if there is anyway to call the board variable in my base class so I can use it in my other classes that reference my base class.

This is how code looks (I only included key parts not everything)

public partial class Form1 : Form
{
    public string[,] board = new string[8, 8];
}

class Pieces
{
   //I want there to be a variable here that will always contain the exact data as the board variable
   //I also have other variables that are in the Form1 class that I need in here
}

class Rook : Pieces
{
   //This is just a small sample of how I set up the moves but its in a for loop
   if (board[x + i, y] == null && !board[x + i, y].Contains("King"))
         pieceMove.AddLast(placementBoard[x + i, y]);
}

This is what I've thought of but I want to know if there is a different approach

public partial class Form1 : Form
{
    public string[,] board = new string[8, 8];
    Rook[] whiteRook = new Rook[10];//I made an array of rooks so when a pawn gets to the opposite side of the board it can turn into a rook


    public Form1()
    {
         InitializeComponent();
         Rook[0] whiteRook = new Rook();
         whiteRook.board = board;//Everytime a piece moves I will call this with every piece to update it
    }
}

class Pieces
{
   public string[,] board = new string[8,8];
}

class Rook : Pieces
{
   //This is just a small sample of how I set up the moves but its in a for loop
   if (board[x + i, y] == null && !board[x + i, y].Contains("King"))
         pieceMove.AddLast(placementBoard[x + i, y]);
}
6
  • First of all try creating class PIECES a friend class of FORM1 that way you will have access of all variables of form1 in pieces. But the board is public so I will assume you want just a single instance of board so why dont make it static. But if you dont want to do that then what you can do is create a function in pieces class that receives a form1 object and since board is public it will be readily available in that functon through that object. Apart from this as BradleyDOTNET said you should consider changing your object model. Commented Dec 6, 2016 at 18:19
  • I am fairly new to programming so I really don't understand what static does or how to effectively use classes, I know this is a bad habit but I've been really avoiding using classes in that program, I really just hard coded all the moves and everything in the Form1 class but i put the board variable to public to try and see if I can access it from the pieces class and just couldn't be asked to remove it. Right now I have it as `public static string[,] board = new string[8,8]; and I copied the movement for the rook and put it in the rook class and it doesn't want to move now Commented Dec 7, 2016 at 14:20
  • I fixed it so now the rook moves, it was because of a null reference exception inside of an if statement Commented Dec 7, 2016 at 15:59
  • Note that you should use static carefully; because you have created a global you can get unexpected effects if multiple objects/threads interact with it. Its a good tool, but you need to know how to use it and what side-effects there are. Commented Dec 7, 2016 at 17:42
  • I've never used the static code at all and the way I see it is a key word that will change that one variable for the whole class instead of the individual class. So am i using it right? Commented Dec 7, 2016 at 19:47

1 Answer 1

2

Prefer composition over inheritance.

Your object model is all wrong right now. You are trying to control everything from the various piece classes when those should just contain piece specific logic, the containing class should control the state. You really want to have a Board object that contains Piece objects that you can then subclass

public class Board
{
    List<Piece> pieces;
}

public abstract class Piece
{
     //Some base behavior/interface
     public bool Move(int xTarget, int yTarget)
     {
         if (IsValidMove(xTarget, yTarget))
         {
            //Do stuff
         }
     }

     protected abstract bool IsValidMove(int xTarget, int yTarget);
}

And then subclass Rook, Bishop etc from "Piece". If necessary you can pass the Board into the Piece via constructor or a property but that dependency is very much the wrong way, the Board should be controlling its own state.

Sign up to request clarification or add additional context in comments.

16 Comments

Sorry, I'm not that advanced to I don't really know how that works
Or really what you mean by subclass and everything
@DoomedSpace Could you be a bit more specific? You already created a subclass in your attempt by writing Rook : Pieces I'm trying to put the beginnings of a correct OO design in my answer without writing the whole thing, so to help more I'll need a more specific question. Do you understand inheritance? Polymorphism? Do you just need "subclass" defined (if so, its just a class that derives from another)
I had an array named board which contained where the all the pieces where on the board, it was in the default class names Form1. I wanted to know how I could access and change the values in board from another class. I didn't want to inherit all the variables in the Form1 class I just wanted that specific variable. I found out that using the keyword static helped me achieve this purpose but you said to be careful when I use that keyword because of the "side effects". I hope this was a bit more specific
@DoomedSpace Right, my basic answer is that's the wrong approach. If you treat your pieces as real objects then they would never mutate the board state directly. The board would ask them applicable questions and mutate its own state accordingly. Apart from side-effects; using static here is just going to teach you bad habits regarding global state (which you should pretty much never have outside an IoC container, and you clearly aren't ready for that concept yet)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.