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?