Home > Blockchain >  C# Chess Board Printing the Rook in a for loop
C# Chess Board Printing the Rook in a for loop

Time:09-29

So on the chess board, the Rook has the option of moving up and down and left to right any spaces in that direction. I want to try and make a for loop that incriminates that. I wrote this and the Console printed out only the spaces near the piece, not any legal move. How can I reframe this so its shows all the moves it can move to?enter image description here

for(int up = 0; up < Size; up  )
{        
    theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber   0].LegalNextMove = true;        
}    
for (int left = 0; left < Size; left  )
{
    theGrid[currentCell.RowNumber   0, currentCell.ColumnNumber - 1].LegalNextMove = true;
}
for (int down = 0; down < Size; down  )
{
    theGrid[currentCell.RowNumber   1, currentCell.ColumnNumber   0].LegalNextMove = true;
}
for (int right = 0; right < Size; right  )
{
    theGrid[currentCell.RowNumber   0, currentCell.ColumnNumber   1].LegalNextMove = true;
}

CodePudding user response:

Your code is not using the loop variable, and so the body of the loop performs the exact same assignment each time.

Secondly, you could simplify this to one loop, containing two assignments: one for the horizontal direction, and one for the vertical one: making a cross. Then at the end, remove the possibility to stand still.

Code:

for (int i = 0; i < Size; i  )
{
    theGrid[i, currentCell.ColumnNumber].LegalNextMove = true;
    theGrid[currentCell.RowNumber, i].LegalNextMove = true;
}
theGrid[currentCell.RowNumber, currentCell.ColumnNumber].LegalNextMove = false;
  • Related