@wio I broke chess again.
``` private bool checkcheck(Color c, Tile t) { List<Tile> allavailablemoves = new List<Tile>(); Tile ph; int c1 = 1; foreach (Piece p in pieces) { if (p.color != c) { switch (p.rank) { case Piece.type.pawn: if (p.color == Color.White) { if (p.tile.y == 6) { if (getpiece(gettile(p.tile.x, p.tile.y - 1)) == null) { allavailablemoves.Add(gettile(p.tile.x, p.tile.y - 1)); if (getpiece(gettile(p.tile.x, p.tile.y - 2)) == null) { allavailablemoves.Add(gettile(p.tile.x, p.tile.y - 2)); } } if (p.tile.x > 0) { if (getpiece(gettile(p.tile.x - 1, p.tile.y - 1)) != null) { if (getpiece(gettile(p.tile.x - 1, p.tile.y - 1)).color == Color.Black) { allavailablemoves.Add(gettile(p.tile.x - 1, p.tile.y - 1)); } } } if (p.tile.x < 7) { if (getpiece(gettile(p.tile.x + 1, p.tile.y - 1)) != null) { if (getpiece(gettile(p.tile.x + 1, p.tile.y - 1)).color == Color.Black) { allavailablemoves.Add(gettile(p.tile.x + 1, p.tile.y - 1)); } } } } else if (p.tile.y > 0) { if (getpiece(gettile(p.tile.x, p.tile.y - 1)) == null) { allavailablemoves.Add(gettile(p.tile.x, p.tile.y - 1)); } if (p.tile.x > 0) { if (getpiece(gettile(p.tile.x - 1, p.tile.y - 1)) != null) { if (getpiece(gettile(p.tile.x - 1, p.tile.y - 1)).color == Color.Black) { allavailablemoves.Add(gettile(p.tile.x - 1, p.tile.y - 1)); } } } if (p.tile.x < 7) { if (getpiece(gettile(p.tile.x + 1, p.tile.y - 1)) != null) { if (getpiece(gettile(p.tile.x + 1, p.tile.y - 1)).color == Color.Black) { allavailablemoves.Add(gettile(p.tile.x + 1, p.tile.y - 1)); } } } } } else { if (p.tile.y == 1) { if (getpiece(gettile(p.tile.x, p.tile.y + 1)) == null) { allavailablemoves.Add(gettile(p.tile.x, p.tile.y + 1)); if (getpiece(gettile(p.tile.x, p.tile.y + 2)) == null) { allavailablemoves.Add(gettile(p.tile.x, p.tile.y + 2)); } } if (p.tile.x > 0) { if (getpiece(gettile(p.tile.x - 1, p.tile.y + 1)) != null) { if (getpiece(gettile(p.tile.x - 1, p.tile.y + 1)).color == Color.White) { allavailablemoves.Add(gettile(p.tile.x - 1, p.tile.y + 1)); } } } if (p.tile.x < 7) { if (getpiece(gettile(p.tile.x + 1, p.tile.y + 1)) != null) { if (getpiece(gettile(p.tile.x + 1, p.tile.y + 1)).color == Color.White) { allavailablemoves.Add(gettile(p.tile.x + 1, p.tile.y + 1)); } } } } else if (p.tile.y < 7) { if (getpiece(gettile(p.tile.x, p.tile.y + 1)) == null) { gettile(p.tile.x, p.tile.y + 1).Image = Chess.Properties.Resources.yellow; allavailablemoves.Add(gettile(p.tile.x, p.tile.y + 1)); } if (p.tile.x > 0) { if (getpiece(gettile(p.tile.x - 1, p.tile.y + 1)) != null) { if (getpiece(gettile(p.tile.x - 1, p.tile.y + 1)).color == Color.White) { allavailablemoves.Add(gettile(p.tile.x - 1, p.tile.y + 1)); } } } if (p.tile.x < 7) { if (getpiece(gettile(p.tile.x + 1, p.tile.y + 1)) != null) { if (getpiece(gettile(p.tile.x + 1, p.tile.y + 1)).color == Color.White) { allavailablemoves.Add(gettile(p.tile.x + 1, p.tile.y + 1)); } } } } } break; case Piece.type.knight: if (p.tile.x > 0 & p.tile.y > 1) { ph = gettile(p.tile.x - 1, p.tile.y - 2); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x < 7 & p.tile.y > 1) { ph = gettile(p.tile.x + 1, p.tile.y - 2); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x > 0 & p.tile.y < 6) { ph = gettile(p.tile.x - 1, p.tile.y + 2); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x < 7 & p.tile.y < 6) { ph = gettile(p.tile.x + 1, p.tile.y + 2); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x > 1 & p.tile.y < 7) { ph = gettile(p.tile.x - 2, p.tile.y + 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x > 1 & p.tile.y > 0) { ph = gettile(p.tile.x - 2, p.tile.y - 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x < 6 & p.tile.y < 7) { ph = gettile(p.tile.x + 2, p.tile.y + 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.x < 6 & p.tile.y > 0) { ph = gettile(p.tile.x + 2, p.tile.y - 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } break; case Piece.type.rook: if (p.tile.x < 7) { for (int i = p.tile.x + 1; i <= 7; i++) { ph = gettile(i, p.tile.y); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } if (p.tile.x > 0) { for (int i = p.tile.x - 1; i >= 0; i--) { ph = gettile(i, p.tile.y); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } if (p.tile.y < 7) { for (int i = p.tile.y + 1; i <= 7; i++) { ph = gettile(p.tile.x, i); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } if (p.tile.y > 0) { for (int i = p.tile.y - 1; i >= 0; i--) { ph = gettile(p.tile.x, i); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } break; case Piece.type.bishop: while (!(p.tile.x + c1 > 7) & !(p.tile.y + c1 > 7)) { ph = gettile(p.tile.x + c1, p.tile.y + c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; while (!(p.tile.x + c1 > 7) & !(p.tile.y - c1 < 0)) { ph = gettile(p.tile.x + c1, p.tile.y - c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; while (!(p.tile.x - c1 < 0) & !(p.tile.y - c1 < 0)) { ph = gettile(p.tile.x - c1, p.tile.y - c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; while (!(p.tile.x - c1 < 0) & !(p.tile.y + c1 > 7)) { ph = gettile(p.tile.x - c1, p.tile.y + c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; break; case Piece.type.queen: while (!(p.tile.x + c1 > 7) & !(p.tile.y + c1 > 7)) { ph = gettile(p.tile.x + c1, p.tile.y + c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; while (!(p.tile.x + c1 > 7) & !(p.tile.y - c1 < 0)) { ph = gettile(p.tile.x + c1, p.tile.y - c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; while (!(p.tile.x - c1 < 0) & !(p.tile.y - c1 < 0)) { ph = gettile(p.tile.x - c1, p.tile.y - c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; while (!(p.tile.x - c1 < 0) & !(p.tile.y + c1 > 7)) { ph = gettile(p.tile.x - c1, p.tile.y + c1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } c1 += 1; } c1 = 1; if (p.tile.x < 7) { for (int i = p.tile.x + 1; i <= 7; i++) { ph = gettile(i, p.tile.y); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } if (p.tile.x > 0) { for (int i = p.tile.x - 1; i >= 0; i--) { ph = gettile(i, p.tile.y); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } if (p.tile.y < 7) { for (int i = p.tile.y + 1; i <= 7; i++) { ph = gettile(p.tile.x, i); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } if (p.tile.y > 0) { for (int i = p.tile.y - 1; i >= 0; i--) { ph = gettile(p.tile.x, i); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } break; } else { allavailablemoves.Add(ph); } } } break; case Piece.type.king: if (p.tile.x < 7) { ph = gettile(p.tile.x + 1, p.tile.y); if (!(checkcheck(p.color, ph))) { if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } if (p.tile.y < 7) { ph = gettile(p.tile.x + 1, p.tile.y + 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } ph = gettile(p.tile.x, p.tile.y + 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } if (p.tile.x > 0) { ph = gettile(p.tile.x - 1, p.tile.y + 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } } if (p.tile.y > 0) { ph = gettile(p.tile.x + 1, p.tile.y - 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } ph = gettile(p.tile.x, p.tile.y - 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } if (p.tile.x > 0) { ph = gettile(p.tile.x - 1, p.tile.y - 1); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } } if (p.tile.x > 0) { ph = gettile(p.tile.x - 1, p.tile.y); if (getpiece(ph) != null) { if (getpiece(ph).color != p.color) { allavailablemoves.Add(ph); } } else { allavailablemoves.Add(ph); } } } break; } } } ``` It gave me a stackoverflow at the very first bracket :/
What did you figure out so far?
It's absurd that you can't redraw the board
this one doesn't have to do with drawing anything on the board
it's giving me a stackoverflow at the first bracket, so I'm not sure what's actually causing the error
Overflow usually means run-away recurssion
Function calling itself
ok now that you mention that, I think I know exactly what's wrong
ok so I fixed it so that it's not giving me a stackoverflow, but now, that code isn't doing anything at all.
lol
Why did you use recursion to begin with?
didn't mean to :p forgot to take that part out
So what do you think it the problem now?
not sure. debugging right now.
ok so I fixed it now, so I can't move my king into check. However, there's another part that's kinda odd that I need to look into.
Ok, looks like everything is working as planned now. Gonna upload.
Cool.
Are you going to refactor?
probably yes the pawn movement code is super messy and can definitely be simplified, and almost everything after that uses the same block of code to add a tile to the list of moveable tiles
Here is a question, how would you encode a move into an object?
not sure what you mean
Supposed you gave very piece a 'move' object. Then supposed the game gave that move object the position. How would you generalize all chess moves into a class that takes a few parameters?
For example, maybe Knight moves would be encoded by 'V1H2' and 'V2H1' While rook moves might be encoded 'H*' and 'V*'
That is a good question. I think I was trying to do something like that with my last project. Not sure if this is what you mean, but my idea was to add a variable for each direction which gets the tile immediately surrounding one tile in the corresponding direction, and then check it the tile is open or not.
O that's not quite how I'm planning on doing moves. When a tile is clicked, a method is called that highlights all available moves, and then when a highlighted tile is clicked, the piece moves.
What would be the best way to do moves then?
Maybe with a predicate?
not sure what you mean again :p
Predicate is a function that returns yes or no after given some input
For example, you could take the difference in position as an input, and then return true or false if it is a legal move
idk man imo, if it ain't broke, don't fix it (unless it's super messy and an easy fix)
Then run that predicate on all 64 pieces
but why should I do that if what I have already works tho?
Ultimately it would allow you to make custom chess pieces.
but why would that be necessary
If it's just normal chess, people might get bored. For example, there is the game shogi
yeah I reckon but how I'm doing stuff now still allows for custom pieces/rules
That is true, however it makes the 'switch' longer.
#yolo
Join our real-time social learning platform and learn together with your friends!