✅ 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
Anchy4mo 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
Bandana Dee(z)
Bandana Dee(z)4mo 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
Anchy4mo ago
if you break at the first collision then it should not happen right?
Bandana Dee(z)
Bandana Dee(z)4mo 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
Anchy4mo 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
Bandana Dee(z)
Bandana Dee(z)4mo ago
yes ok i'll see if it works
Bandana Dee(z)
Bandana Dee(z)4mo ago
i think its working now
No description
Anchy
Anchy4mo ago
🥳
Bandana Dee(z)
Bandana Dee(z)4mo ago
nevermind
Bandana Dee(z)
Bandana Dee(z)4mo ago
yeah that's not right 😭
No description
Anchy
Anchy4mo ago
what does your loop look like
Bandana Dee(z)
Bandana Dee(z)4mo 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
Anchy4mo ago
you are just not looping the correct distance it looks like
Bandana Dee(z)
Bandana Dee(z)4mo ago
iooooh your right i should do x >= 0 not > 0 silly me
Anchy
Anchy4mo 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
Bandana Dee(z)
Bandana Dee(z)4mo ago
no wait
Anchy
Anchy4mo ago
that's for the left move
Bandana Dee(z)
Bandana Dee(z)4mo ago
how would I get distance from left side board
Anchy
Anchy4mo ago
take your current position I guess actually if the left side of the board = 0 depends on how your coordinate system works
Bandana Dee(z)
Bandana Dee(z)4mo ago
wouldnt distance from left side just be position.x
Anchy
Anchy4mo ago
yeah that's what I just said
Bandana Dee(z)
Bandana Dee(z)4mo ago
yeah
Anchy
Anchy4mo ago
you could technically use Board.Width because you dispose of any moves outside of the board, but that's really inefficient
Bandana Dee(z)
Bandana Dee(z)4mo ago
yeaah
Anchy
Anchy4mo ago
then for the right move to calculate the distance to check you can just take Board.Width and subtract your Position.x
Bandana Dee(z)
Bandana Dee(z)4mo 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
Anchy4mo ago
try it and see
Bandana Dee(z)
Bandana Dee(z)4mo ago
nothing i think tis catching myself
Anchy
Anchy4mo ago
yeah I was just going to mention the loop actually starts at 0
Bandana Dee(z)
Bandana Dee(z)4mo ago
because new Position(Position.x + x, Position.y); where x = 0 is just Position
Anchy
Anchy4mo ago
it's going to hit yourself
Bandana Dee(z)
Bandana Dee(z)4mo ago
💀
Anchy
Anchy4mo ago
just make it start at 1 my bad
Bandana Dee(z)
Bandana Dee(z)4mo ago
malding, seething, coping simultaneously rn
No description
Anchy
Anchy4mo ago
post your loop
Bandana Dee(z)
Bandana Dee(z)4mo 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
Anchy4mo 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
Bandana Dee(z)
Bandana Dee(z)4mo 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
More Posts
Is there a tool to view c# generated code but still at high level?Example, if I have: ``` public readonly record struct Point(double X, double Y, double Z); ``` I wStruggling with generic class... design?I have `BaseClass` that other classes will extend from: ```c# public class BaseClass { public Base✅ Not understanding Microsoft TutorialI'm following the microsoft tutorial on creating a cli application. https://learn.microsoft.com/en-uHello friends, is there anyone who can help me with C# selenium?I want to control an open Chrome page with Selenium. Is it possible to do this or how can I do it?Statement not printing to screenI have zero errors and I need to figure out why my code stops working after a certain pointStarting off aid?Hey all. I'm just starting off with trying to learn C# with game development intentions, and hopefulmulti threaded gate lock ORUsing the Monitor class, is there a way to create a lock with n input values, which creates a lock ftrying to search through a ReportviewerI am unsure of what is wrong but when i try to search for a specific value and try to show the reporTrying to get a game object to disappear and re-appear when a certain number of items are collected.I have a game object that when you collide with it, it takes you to the next level, but i would likeAdvice on how to structure a windows form application where it takes images to generate cards?I am making a win form for my card battling game but i'm unsure how to make it actually put the user