C
C#9mo ago
Ninja Kirby

✅ having stupidity. why can THE ROOOOOOOOOOOOOOOOK!!! move through the first pawn?

here is the valid moves code:
public override List<Position> ValidMoves()
{
// calculate the moves of THE ROOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOK

List<Position> validMoves = new List<Position>();

if (Board is null)
return validMoves;

int[] offsets = [1, -1];

for (int x = 0; x < Board.Width; x++)
{
foreach (var offset in offsets)
{
Position dir = new Position(Position.x + offset * x, Position.y);

if (Board.Pieces[dir] != null)
continue;

validMoves.Add(dir);
}
}

for (int y = 0; y < Board.Height; y++)
{
foreach (var offset in offsets)
{
Position dir = new Position(Position.x, Position.y + offset * y);

if (Board.Pieces[dir] != null)
continue;

validMoves.Add(dir);
}
}

validMoves.RemoveAll(x => x.Exceeds(Board.Width, Board.Height));

return validMoves;
}
public override List<Position> ValidMoves()
{
// calculate the moves of THE ROOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOK

List<Position> validMoves = new List<Position>();

if (Board is null)
return validMoves;

int[] offsets = [1, -1];

for (int x = 0; x < Board.Width; x++)
{
foreach (var offset in offsets)
{
Position dir = new Position(Position.x + offset * x, Position.y);

if (Board.Pieces[dir] != null)
continue;

validMoves.Add(dir);
}
}

for (int y = 0; y < Board.Height; y++)
{
foreach (var offset in offsets)
{
Position dir = new Position(Position.x, Position.y + offset * y);

if (Board.Pieces[dir] != null)
continue;

validMoves.Add(dir);
}
}

validMoves.RemoveAll(x => x.Exceeds(Board.Width, Board.Height));

return validMoves;
}
No description
No description
38 Replies
Anchy
Anchy9mo ago
shouldn't you break instead of continue, otherwise; yes you cannot move on top of another piece but you shouldn't be able to move through the piece I'm not really a chess player however, I prefer Sudoku verify this by adding two pawns at: F4, G4 then see if you can move your D4 to H4 you will see the problem is not that you can move through the first pawn but you can move through pieces in general
Ninja Kirby
Ninja KirbyOP9mo ago
i can infact move through pieces breaking instead of continuing has the same effect i feel like the only solution i can think of is having 4 for loops instead of 2
Anchy
Anchy9mo ago
if you break at the first collision then it should not happen right?
Ninja Kirby
Ninja KirbyOP9mo ago
that's the issue - if i break too early, then the other position wont be considered (the other side of the rook which is not blocked) so i think i should try 4 for loops
Anchy
Anchy9mo ago
oh I see what you mean, you are checking the horizontal / vertical moves in one pass from left / right and top/bottom yeah maybe if you check each side it may be more applicable
Ninja Kirby
Ninja KirbyOP9mo ago
yes ok i'll see if it works
Ninja Kirby
Ninja KirbyOP9mo ago
i think its working now
No description
Anchy
Anchy9mo ago
🥳
Ninja Kirby
Ninja KirbyOP9mo ago
nevermind
Ninja Kirby
Ninja KirbyOP9mo ago
yeah that's not right 😭
No description
Anchy
Anchy9mo ago
what does your loop look like
Ninja Kirby
Ninja KirbyOP9mo ago
this is pretty trash but
for (int x = Position.x+1; x < Board.Width; x++)
{
Position dir = new Position(x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

for (int x = Position.x - 1; x > 0; x--)
{
Position dir = new Position(x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

for (int y = Position.y + 1; y < Board.Height; y++)
{
Position dir = new Position(Position.x, y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

for (int y = Position.y - 1; y > 0; y--)
{
Position dir = new Position(Position.x, y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}
for (int x = Position.x+1; x < Board.Width; x++)
{
Position dir = new Position(x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

for (int x = Position.x - 1; x > 0; x--)
{
Position dir = new Position(x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

for (int y = Position.y + 1; y < Board.Height; y++)
{
Position dir = new Position(Position.x, y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

for (int y = Position.y - 1; y > 0; y--)
{
Position dir = new Position(Position.x, y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}
Anchy
Anchy9mo ago
you are just not looping the correct distance it looks like
Ninja Kirby
Ninja KirbyOP9mo ago
iooooh your right i should do x >= 0 not > 0 silly me
Anchy
Anchy9mo ago
for(int x = 0; x < this.Position.x; x++)
{
Position dir = new Position(this.Position.x - x, y);

if (Board.Pieces[dir] is not null)
{
break;
}

validMoves.Add(dir);
}
for(int x = 0; x < this.Position.x; x++)
{
Position dir = new Position(this.Position.x - x, y);

if (Board.Pieces[dir] is not null)
{
break;
}

validMoves.Add(dir);
}
maybe something like this
Ninja Kirby
Ninja KirbyOP9mo ago
no wait
Anchy
Anchy9mo ago
that's for the left move
Ninja Kirby
Ninja KirbyOP9mo ago
how would I get distance from left side board
Anchy
Anchy9mo ago
take your current position I guess actually if the left side of the board = 0 depends on how your coordinate system works
Ninja Kirby
Ninja KirbyOP9mo ago
wouldnt distance from left side just be position.x
Anchy
Anchy9mo ago
yeah that's what I just said
Ninja Kirby
Ninja KirbyOP9mo ago
yeah
Anchy
Anchy9mo ago
you could technically use Board.Width because you dispose of any moves outside of the board, but that's really inefficient
Ninja Kirby
Ninja KirbyOP9mo ago
yeaah
Anchy
Anchy9mo ago
then for the right move to calculate the distance to check you can just take Board.Width and subtract your Position.x
Ninja Kirby
Ninja KirbyOP9mo ago
// check left of rook
for (int x = 0; x < Position.x; x++)
{
Position dir = new Position(Position.x - x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

// check right of rook
for (int x = 0; x < Board.Width - Position.x; x++)
{
Position dir = new Position(Position.x + x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}
// check left of rook
for (int x = 0; x < Position.x; x++)
{
Position dir = new Position(Position.x - x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}

// check right of rook
for (int x = 0; x < Board.Width - Position.x; x++)
{
Position dir = new Position(Position.x + x, Position.y);

if (Board.Pieces[dir] != null)
break;

validMoves.Add(dir);
}
like that?
Anchy
Anchy9mo ago
try it and see
Ninja Kirby
Ninja KirbyOP9mo ago
nothing i think tis catching myself
Anchy
Anchy9mo ago
yeah I was just going to mention the loop actually starts at 0
Ninja Kirby
Ninja KirbyOP9mo ago
because new Position(Position.x + x, Position.y); where x = 0 is just Position
Anchy
Anchy9mo ago
it's going to hit yourself
Ninja Kirby
Ninja KirbyOP9mo ago
💀
Anchy
Anchy9mo ago
just make it start at 1 my bad
Ninja Kirby
Ninja KirbyOP9mo ago
malding, seething, coping simultaneously rn
No description
Anchy
Anchy9mo ago
post your loop
Ninja Kirby
Ninja KirbyOP9mo ago
its just that but with 1 as the initial value and I added the y axis as well
// check left of rook
for (int x = 1; x < Position.x; x++)
{
Position dir = new Position(Position.x - x, Position.y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}

// check right of rook
for (int x = 1; x < Board.Width - Position.x; x++)
{
Position dir = new Position(Position.x + x, Position.y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}

// check top of rook
for (int y = 1; y < Position.y; y++)
{
Position dir = new Position(Position.x, Position.y - y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}

// check top of rook
for (int y = 1; y < Board.Height - Position.y; y++)
{
Position dir = new Position(Position.x, Position.y + y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}
// check left of rook
for (int x = 1; x < Position.x; x++)
{
Position dir = new Position(Position.x - x, Position.y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}

// check right of rook
for (int x = 1; x < Board.Width - Position.x; x++)
{
Position dir = new Position(Position.x + x, Position.y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}

// check top of rook
for (int y = 1; y < Position.y; y++)
{
Position dir = new Position(Position.x, Position.y - y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}

// check top of rook
for (int y = 1; y < Board.Height - Position.y; y++)
{
Position dir = new Position(Position.x, Position.y + y);

if (Board.Pieces[dir] is not null)
break;

validMoves.Add(dir);
}
Anchy
Anchy9mo ago
I guess just debug it with a breakpoint and ensure each value you are seeing is what you expect step through the code using the debugger
Ninja Kirby
Ninja KirbyOP9mo ago
time to do random shit and hope it works edition (news flash - it worked!) adding one to position.x / ychanged it
Want results from more Discord servers?
Add your server