C
C#2y ago
FSHF

✅ How to make this loop

switch (chessPiece)
{
case "Knight":
if(isSafe(currentCell.RowNumber -2, currentCell.ColumnNumber -1))
theGrid[currentCell.RowNumber + 2, currentCell.ColumnNumber + 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber + 2, currentCell.ColumnNumber - 1))
theGrid[currentCell.RowNumber + 2, currentCell.ColumnNumber - 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber -2, currentCell.ColumnNumber +1))
theGrid[currentCell.RowNumber - 2, currentCell.ColumnNumber + 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber -2, currentCell.ColumnNumber -1))
theGrid[currentCell.RowNumber - 2, currentCell.ColumnNumber - 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber + 1, currentCell.ColumnNumber + 2))
theGrid[currentCell.RowNumber + 1, currentCell.ColumnNumber + 2].LegalNextMove = true;

if(isSafe(currentCell.RowNumber + 1, currentCell.ColumnNumber - 2))
theGrid[currentCell.RowNumber + 1, currentCell.ColumnNumber - 2].LegalNextMove = true;

if(isSafe(currentCell.RowNumber - 1, currentCell.ColumnNumber + 2))
theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber + 2].LegalNextMove = true;

if(isSafe(currentCell.RowNumber -1, currentCell.ColumnNumber -2))
theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber - 2].LegalNextMove = true;

break;
switch (chessPiece)
{
case "Knight":
if(isSafe(currentCell.RowNumber -2, currentCell.ColumnNumber -1))
theGrid[currentCell.RowNumber + 2, currentCell.ColumnNumber + 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber + 2, currentCell.ColumnNumber - 1))
theGrid[currentCell.RowNumber + 2, currentCell.ColumnNumber - 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber -2, currentCell.ColumnNumber +1))
theGrid[currentCell.RowNumber - 2, currentCell.ColumnNumber + 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber -2, currentCell.ColumnNumber -1))
theGrid[currentCell.RowNumber - 2, currentCell.ColumnNumber - 1].LegalNextMove = true;

if(isSafe(currentCell.RowNumber + 1, currentCell.ColumnNumber + 2))
theGrid[currentCell.RowNumber + 1, currentCell.ColumnNumber + 2].LegalNextMove = true;

if(isSafe(currentCell.RowNumber + 1, currentCell.ColumnNumber - 2))
theGrid[currentCell.RowNumber + 1, currentCell.ColumnNumber - 2].LegalNextMove = true;

if(isSafe(currentCell.RowNumber - 1, currentCell.ColumnNumber + 2))
theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber + 2].LegalNextMove = true;

if(isSafe(currentCell.RowNumber -1, currentCell.ColumnNumber -2))
theGrid[currentCell.RowNumber - 1, currentCell.ColumnNumber - 2].LegalNextMove = true;

break;
Instead of doing this, is there a way to simplyfy it
9 Replies
Omnissiah
Omnissiah2y ago
if you want a loop, you have to loop on the data somewhere you have to have an array of coordinates then you could for example
foreach (var knightMove in knightMovesCoordinates)
if (isSafe(knightMove))
theGrid[coordinate].Legal = true;
foreach (var knightMove in knightMovesCoordinates)
if (isSafe(knightMove))
theGrid[coordinate].Legal = true;
or something like that or you could for example first create an array with all the data and the apply it on theGrid
FSHF
FSHFOP2y ago
how would i set it up what would knightmove be?
Omnissiah
Omnissiah2y ago
well you need just two ints something like (int offsetX, int offsetY)[] knightMovesCoordinates = new... or maybe you could use an object if you already have it memorized somewhere as readonly
FSHF
FSHFOP2y ago
im using forms btw
FSHF
FSHFOP2y ago
would it work the same with this
FSHF
FSHFOP2y ago
case "Knight":
int[] rowOffsets = { 2, 2, -2, -2, 1, 1, -1, -1 };
int[] colOffsets = { 1, -1, 1, -1, 2, -2, 2, -2 };

for (int i = 0; i < rowOffsets.Length; i++)
{
int newRow = currentCell.RowNumber + rowOffsets[i];
int newCol = currentCell.ColumnNumber + colOffsets[i];

if (isSafe(newRow, newCol))
theGrid[newRow, newCol].LegalNextMove = true;
}

break;
case "Knight":
int[] rowOffsets = { 2, 2, -2, -2, 1, 1, -1, -1 };
int[] colOffsets = { 1, -1, 1, -1, 2, -2, 2, -2 };

for (int i = 0; i < rowOffsets.Length; i++)
{
int newRow = currentCell.RowNumber + rowOffsets[i];
int newCol = currentCell.ColumnNumber + colOffsets[i];

if (isSafe(newRow, newCol))
theGrid[newRow, newCol].LegalNextMove = true;
}

break;
this what i got
phaseshift
phaseshift2y ago
could get this down to something like,
int row = currentCell.RowNumber;
int col = currentCell.ColumnNumber;

foreach (var t in rowOffsets.Zip(colOffsets))
{
if (isSafe(row + t.Item1, col + t.Item2)
{...}
}
int row = currentCell.RowNumber;
int col = currentCell.ColumnNumber;

foreach (var t in rowOffsets.Zip(colOffsets))
{
if (isSafe(row + t.Item1, col + t.Item2)
{...}
}
FSHF
FSHFOP2y ago
I got it working Thx
phaseshift
phaseshift2y ago
close with /close 👍
Want results from more Discord servers?
Add your server