Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (poopsiedoodle):

@wio I broke chess again.

OpenStudy (poopsiedoodle):

``` 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 :/

OpenStudy (anonymous):

What did you figure out so far?

OpenStudy (anonymous):

It's absurd that you can't redraw the board

OpenStudy (poopsiedoodle):

this one doesn't have to do with drawing anything on the board

OpenStudy (poopsiedoodle):

it's giving me a stackoverflow at the first bracket, so I'm not sure what's actually causing the error

OpenStudy (anonymous):

Overflow usually means run-away recurssion

OpenStudy (anonymous):

Function calling itself

OpenStudy (poopsiedoodle):

ok now that you mention that, I think I know exactly what's wrong

OpenStudy (poopsiedoodle):

ok so I fixed it so that it's not giving me a stackoverflow, but now, that code isn't doing anything at all.

OpenStudy (anonymous):

lol

OpenStudy (anonymous):

Why did you use recursion to begin with?

OpenStudy (poopsiedoodle):

didn't mean to :p forgot to take that part out

OpenStudy (anonymous):

So what do you think it the problem now?

OpenStudy (poopsiedoodle):

not sure. debugging right now.

OpenStudy (poopsiedoodle):

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.

OpenStudy (poopsiedoodle):

Ok, looks like everything is working as planned now. Gonna upload.

OpenStudy (anonymous):

Cool.

OpenStudy (anonymous):

Are you going to refactor?

OpenStudy (poopsiedoodle):

OpenStudy (poopsiedoodle):

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

OpenStudy (anonymous):

Here is a question, how would you encode a move into an object?

OpenStudy (poopsiedoodle):

not sure what you mean

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

For example, maybe Knight moves would be encoded by 'V1H2' and 'V2H1' While rook moves might be encoded 'H*' and 'V*'

OpenStudy (poopsiedoodle):

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.

OpenStudy (poopsiedoodle):

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.

OpenStudy (anonymous):

What would be the best way to do moves then?

OpenStudy (anonymous):

Maybe with a predicate?

OpenStudy (poopsiedoodle):

not sure what you mean again :p

OpenStudy (anonymous):

Predicate is a function that returns yes or no after given some input

OpenStudy (anonymous):

For example, you could take the difference in position as an input, and then return true or false if it is a legal move

OpenStudy (poopsiedoodle):

idk man imo, if it ain't broke, don't fix it (unless it's super messy and an easy fix)

OpenStudy (anonymous):

Then run that predicate on all 64 pieces

OpenStudy (poopsiedoodle):

but why should I do that if what I have already works tho?

OpenStudy (anonymous):

Ultimately it would allow you to make custom chess pieces.

OpenStudy (poopsiedoodle):

but why would that be necessary

OpenStudy (anonymous):

If it's just normal chess, people might get bored. For example, there is the game shogi

OpenStudy (poopsiedoodle):

yeah I reckon but how I'm doing stuff now still allows for custom pieces/rules

OpenStudy (anonymous):

That is true, however it makes the 'switch' longer.

OpenStudy (poopsiedoodle):

#yolo

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!