Hi, here is a class with the methods that i have created:

class BlackCheckAlgorithm
{
    bool checkCheckMeh;
    string checkOrpiece;
    bool checkOrPiece;
    string chessPiece;
    bool checkSafe;
    int kingRow;
    int kingColumn;
    int blackkingRow;
    int blackkingColumn;
    bool dynamicCheckBlack;
    int whitekingRow;
    int whitekingColumn;
    bool dynamicCheckWhite;
    bool mate;
    int kingRow2;
    int kingColumn2;
    bool pieceDoesntCollideKing;
    bool pieceMovesKing;
    Mate meh = new Mate();

    public int[] checkKingPosition(Pieces[,] pieces, int rowStart, int columnStart, int rowEnd, int columnEnd)
    {
        kingRow = 0;
        kingColumn = 0;
        int[] threatPosition = new int[2];
        chessPiece = "";
        bool WhiteKingSearch = false;
        bool BlackKingSearch = false;

        if (pieces[rowEnd, columnEnd].pieceName()[0] == 'W')
        {
            WhiteKingSearch = true;
        }
        else
        {
            BlackKingSearch = true;
        }

        if (WhiteKingSearch)
        {
            chessPiece = "WKing";
            for (int row = 0; row < pieces.Length; row++)
            {  //Find where the king.
                for (int column = 0; column < pieces.Length; column++)
                {
                    if (pieces[row, column] != null && pieces[row, column].pieceName() == chessPiece)
                    {
                        threatPosition[0] = row;
                        threatPosition[1] = column;
                    }
                }
            }
        }

        if (BlackKingSearch)
        {
            chessPiece = "BKing";
            for (int row = 0; row < pieces.Length; row++)
            {  //Find where the king.
                for (int column = 0; column < pieces.Length; column++)
                {
                    if (pieces[row, column] != null && pieces[row, column].pieceName() == chessPiece)
                    {
                        threatPosition[0] = row;
                        threatPosition[1] = column;
                    }
                }
            }
        }
        return threatPosition;
    }


    public bool checkCheck(Pieces[,] pieces, int blackkingRow, int blackkingColumn)
    {
        CheckManager manager = new CheckManager();

        for (int z1 = 0; z1 < pieces.Length  == false; z1++)
        {  //Find Threatening piece
            for (int j = 0; j < pieces.Length  == false; j++)
            {

                if (pieces[z1, j] != pieces[blackkingRow, blackkingColumn] && pieces[z1, j] != null && pieces[z1, j].pieceName()[0] == 'W' && pieces[z1, j].pieceCollision(pieces, z1, j, blackkingColumn, blackkingRow) == true && pieces[z1, j].pieceMovement(pieces, j, z1, blackkingColumn, blackkingRow) == true)
                {
                    dynamicCheckBlack = true;

                    Console.WriteLine("Piece Threat :  " + pieces[z1, j].pieceName());
                    if (meh.MateF(pieces, z1, j))
                    {
                        ConsoleApplication1.Chess.MATE = true;
                    }
                }
            }
        }
        return dynamicCheckBlack;
    }



    public void checkSafeCheck(Pieces[,] pieces)
    {
   do{
       checkCheckMeh = false;
        Console.WriteLine("//////////////////////////  check //////////////////////////////////");
        Console.WriteLine("//////////////////////////  check //////////////////////////////////");

        Console.WriteLine("Do you want to move a piece or the king ");
        checkOrpiece = Console.ReadLine();
        if (checkOrpiece == "Piece")
        {
            checkOrPiece = true;
        }
        else
        {
        
            Console.WriteLine("You are in check, move your king to a new position");
            Console.WriteLine("What row would you like to move your king?.Enter a NUMBER");
            kingRow2 = int.Parse(Console.ReadLine());
            Console.WriteLine("What column would you like to move your king.Enter a NUMBER");
            kingColumn2 = int.Parse(Console.ReadLine());
            for (int z1 = 0; z1 < pieces.Length; z1++)
            {  //Find Threatening piece
                for (int j = 0; j < pieces.Length; j++)
                {
                    if (pieces[z1, j] != null && pieces[z1, j].pieceName()[0] != pieces[kingRow2, kingColumn2].pieceName()[0] && pieces[z1, j].pieceCollision(pieces, z1, j, kingColumn2, kingRow2) == true && pieces[z1, j].pieceMovement(pieces, j, z1, kingColumn2, kingRow2) == true)
                    {
                        checkCheckMeh =true;


                        Console.WriteLine("Piece Threat :  " + pieces[z1, j].pieceName() + ". You cant move into that position. Try again");
                    }
                }
            }
        }
   } while (checkCheckMeh == true || checkOrPiece==true);
}



    public void checkValid(Pieces[,] pieces, int rowStart, int columnStart, int rowEnd, int columnEnd, int kingRow, int kingColumn)
    {
     

        //after getting a new king position checks if the move was valid.
        if (pieces[kingRow, kingColumn] != null && mate == false && pieces[kingRow, kingColumn].pieceMovement(pieces, kingRow, kingColumn, kingColumn2, kingRow2) == true)
        {
            pieceDoesntCollideKing = true;
        }

        if (pieces[kingRow, kingColumn] != null && pieces[kingRow, kingColumn].pieceMovement(pieces, kingColumn, kingRow, kingColumn2, kingRow2) == true)
        {
            pieceMovesKing = true;
        }

        if (pieces[kingRow, kingColumn] != null  && pieceMovesKing == true && pieceDoesntCollideKing == true)
        {
            checkSafe = true;
            pieces[kingRow2, kingColumn2] = pieces[kingRow, kingColumn];
            pieces[kingRow, kingColumn] = null;
        }
    
    }


}

My questions are:

1) Whether it is okay to put all your variables outside the methods, while keeping in mind that all those variables are used in all the methods!!

2) What happens if i use the same variables across several methods?

3) Is it wiser to declare all those variables in each of the methods?

4) Would the same be true if all my methods were static?

Variables which are declared outside the methods and not marked with static modifier are instance variables (fields).

Local variables are such variables, which are declared inside the method body or inside the block within the method.

In fact, instance variables constitute the object (real world object).

For example,

The class "Point" is an abstract idea which refers two values.

public class Point
 {
    private int _x; // instance variable
    private int _y; // instance variable

 }

Now consider the situation, if someone adds two more fields "Point" class then this class sounds "Rectangle".

public class Rectangle
{
    private int _x,_y,_width,_height;
}
commented: lame answers +0

That doesnt answer the question. i know what is instance and local variables

1) Whether it is okay to put all your variables outside the methods, while keeping in mind that all those variables are used in all the methods!!

Never.


2) What happens if i use the same variables across several methods?

Which category of variables?
- local
- static
- instance


3) Is it wiser to declare all those variables in each of the methods?
and
4) Would the same be true if all my methods were static?

It depends upon your problem definition.

why cant i put all my variables outside the methods? whats going to happen?
i trigger every method each time, and each of the methods are simply reused?

why cant i put all my variables outside the methods? whats going to happen?
i trigger every method each time, and each of the methods are simply reused?

Not sure if I understand your question completely so please tell me if I got something wrong.

If your variables are used by multiple methods and they need to store the changes between method calls e.g. a balance variable for a bank account, then you should put these variables outside of the any methods.

If your variables are used across all methods then they will keep their values unless you specifically change them.

public int num = 0;

public void addto()
{
     num++;
}
public void removefrom()
{
     num--;
}

In the code above number will retain its value between calls, if i called add twice and remove once, then the value would be 1;

public void addto()
{
     num = 1
}
public void removefrom()
{
     num--;
}

in this bit of code the add function sets the variable therefore calling add twice and remove once would give the value of 0.

As for putting all of your variables outside of methods, it does make your code a lot harder to read through and debug.

Hope some of this will help you.

-Matt

commented: Awesome +0

Awesome. that is the explanation that i was looking for!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.