posted
I'm sure it could be much shorter, but even so it underscores the usefulness of logic programming, where the code for determining if someone is in check would be under a dozen lines, maybe just two or three (depending on how certain other parts were written).
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
Blayne Bradley
unregistered
posted
well I had to account for when white/black is playing after all a castle cant put you in check if its on your team.
IP: Logged |
posted
That should not take up significant numbers of lines.
There are a number of possible ways to check for being in check, but consider this one:
Iterate over, starting with the nearest, ever space in a row with the King. If you 'run into' an enemy piece, check if it could legally move to where the King is (and thus take it). If it can, the King is in check. If you 'run into' a piece that can't take the King, stop checking. Do the same for diagonals. Also, check knight positions relative to the King.
There are more efficient ways, but they would have required you to structure your other code differently. That way should work with how you're working currently, with minimal changes.
Check mate, now, that's a little harder, because you have to determine if any move could result in the King not being in check. However, the total number of possible moves for all pieces on one side in a single turn is really pretty small, and hopefully your code is structured such that you can get all current legal moves for a piece from an iterator. Then just check if the King is in check in the position that would result. If you wanted to be more sophisticated, start by checking if the piece that could previously check the King can still check the King, since most moves will be irrelevant to the original check situation.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
"But they're little castles, they're all castle shaped."
But the name of the piece is rook.
Blayne, when including rules about castling, one must remember that one cannot castle if one is in check, one cannot castle through check, and one cannot castle if either the rook or the king has moved. As well, don't forget both long castling and short castling.
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
Blayne Bradley
unregistered
posted
were not including castling into our chess game it is a class project not us trying to emulate the entire game of chess it wont award us extra marks it is not worth the work at this point of the year.
IP: Logged |
quote:it is a class project not us trying to emulate the entire game of chess it wont award us extra marks
Have you verified with your professor that a game that superficially resembles chess but is not actually chess will still receive full marks?
Posts: 37449 | Registered: May 1999
| IP: Logged |
posted
If there isn't castling, promotion, en passant, etc. then it isn't chess. It is a game that is akin to chess, not chess.
Oh, I am interested in how you handled pawns only moving forward, but capturing diagonally. As well, them being able to move two squares forward on the first move but only 1 on any other (and also being able to move up only once on the first move).
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
posted
Well, that's just plain wrong. There are three mistakes just in the handling of the white pawns.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
Beg your pardon, I shouldn't glance hastily at code like that. Six mistakes in the handling of white pawns.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
It isn't as bad as it might be, but your life will be much, much better if you learn how to really use object oriented and functional programming. You're never going to have to look at this code again after this project, but if you did, you would hate your previous self.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
I am ruining it now, but at the time I posted this the topic had 64 posts. Just thought I would point that out.
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
posted
Yeah, ok, he makes the same mistake in two different places, fair enough. I counted that as two.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
JH: he shouldn't be using open source code for a school project unless explicitly authorized to do so.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
It was a joke.
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
Blayne Bradley
unregistered
posted
There's a case above the code I posted that had a case select to make sure you werent taking your own pieces, testing has shown this works.
IP: Logged |
posted
Blayne: tell me, if a pawn tries to move one space forward into a space occupied by an enemy, when it isn't in its starting row, what happens?
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
It isn't nitpicking, it is showing you how reading your code with an eye to proper behavior can find bugs.
You would find your code much easier to read if it were organized more into polymorphic (in this case, that means implementing the same interface) classes (for each chess piece type, in particular). It would be much easier to debug if you wrote real test cases.
For instance, imagine if each piece had its own code that determined if a move was legal. It would probably be passed the board as an argument. Then you wouldn't have your monolithic move method of doom, and you could write simple checking code that constructed a contrived board and tested if a piece could make various moves.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
He apparently tackles the 'attack their own piece' with an earlier check, as he communicates above. I assume it just checks if the proposed destination space has a friendly piece.
I would hope he has a check against dy ever going over the size of the board (and the UI might not allow such a move, making the question essentially moot, good as it would be to guard against in the API).
However, it really isn't the game logic that's important. As we can see from his response, Blayne pretty much gets the game logic, it is translating all that knowledge into programming logic that is the issue. He seems to still be viewing programming flow as somewhat of a black box, albeit one he can tinker with and hope it works.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Blayne, I don't know what what level your class is... Have you studied OOP? Polymorphism and inheritance? Or is this a straight up pseudo-C++ assignment with only functions and scalar/vector object types?
*EDIT* This is gonna make me end up writing one just because I can... Thanks for distracting me, Blayne! ***sigh...***
quote:Originally posted by fugu13: However, it really isn't the game logic that's important. As we can see from his response, Blayne pretty much gets the game logic, it is translating all that knowledge into programming logic that is the issue.
I disagree, or perhaps I emphasize differently; the first step must be to really understand all the possible game-logic cases, which he clearly doesn't. At least, he hasn't thought about them thoroughly. Programming is relatively trivial if you really understand what's going on; so to translate into programming logic, you have to really think about the game logic, what's possible, and what's not. I think that's the important step that's missing, not knowledge of OOP and whatnot. If you can do X in procedural, it's pretty simple to pick up the OOP way of doing it. But to understand the procedural you have to learn the programming-oriented way of thinking about the problem, which is to understand all the edge cases and step through all the branches yourself. Not that attacking with an unmoved pawn is an 'edge' case, by any means!
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
Blayne Bradley
unregistered
posted
When my partner saw my check code he was like "jesus christ this is alot of code, its awesome"
followed by "wow it follows indentiation perfectly"
IP: Logged |
posted
Just because your partner says it is good does not mean that it is good.
No offense intended, but I suggest that you listen to what these people are advising you of, since it is evident that they know more about programming than you. You can't just ignore rules of chess and call it chess.
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
posted
I think he was saying that his partner was marveling only at its indentation (or "indentiation", if you prefer), not at its quality.
Posts: 3275 | Registered: May 2007
| IP: Logged |
posted
I think your partner is following the excellent rule of being quiet if you can't say anything nice. Praising the indentation is really the last resort of someone determined to find something to compliment.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
posted
I myself am known in some circles for writing long, utterly useless, but perfectly indented, code.
Posts: 5462 | Registered: Apr 2005
| IP: Logged |
posted
Since doing the indentation is really an absolute minimum requirement for an IDE, it doesn't exactly reflect well on the developer.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
quote:Originally posted by El JT de Spang: I myself am known in some circles for writing long, utterly useless, but perfectly indented, code.
That in your job description, too? I've got millions of lines of code that look immaculate!
And you didn't answer my question:
quote:Blayne, I don't know what what level your class is... Have you studied OOP? Polymorphism and inheritance? Or is this a straight up pseudo-C++ assignment with only functions and scalar/vector object types?
posted
Coulda swore I posted but we do some OOP, mostly structures/classes/function, next semester we do OOP in detail.
In related news I think I got screwed over by my partner, he's decided to bail and is refusing to send me the latest version of the project we were working on.
IP: Logged |
quote:Originally posted by Blayne Bradley: Coulda swore I posted but we do some OOP, mostly structures/classes/function, next semester we do OOP in detail.
Correct me if I'm wrong, but aren't you fairly close to graduating?
Posts: 2437 | Registered: Apr 2005
| IP: Logged |
quote:In related news I think I got screwed over by my partner, he's decided to bail and is refusing to send me the latest version of the project we were working on.
What reason does he give for bailing and refusing to send you the latest version.
Posts: 26071 | Registered: Oct 2003
| IP: Logged |
posted
Maybe he looked more closely at the splendid indentation, and noticed that the if-then constructs are inconsistent with how they use braces. I would definitely bail from any partner who couldn't keep his braces consistent. :nods:
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
Blayne Bradley
unregistered
posted
no he always had the impression I wasn't keeping up with my end of the work, (when codewise I wrote about twice as much code and was responsible for debugging his mistakes), now he didn't state this as his reason azs he just kinda yelled something about giving up and soemthing else I couldnt make it from down the hall way, I followed him not quite understanding asking if hes going to email me the laest version fo the header so I could fix the latest problem that came up, and he said no and asked for me to guess as to why.
I can only assume his reason is that I am somehow not keeping up when I have been.
OOP is a dedicated course next semester which is the final one as well.
I already talked to my teacher about this he said to do as much as I can do for tuesday and he'll figure something out.
IP: Logged |
posted
I suspect you two have had differing expectations, and that this is due to a gigantic communication gulf.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
So umm, how do I open it?
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
Blayne Bradley
unregistered
posted
you need linux. and I havent gotten a harddrive thats works yet so I havent been able to make my own, and I am a tad antsy about giving out my linux server account online.
IP: Logged |
posted
Did you fix all the problems mentioned earlier?
Posts: 2705 | Registered: Sep 2006
| IP: Logged |
Blayne Bradley
unregistered
posted
My biggest problem with getting networking to work, was that the code I wa susing would only send packets where the message was a string buf[MAXDATASIZE+1], so I had to make a whole new function that would convert the ASCII value of a char to its related integer.
I put it in a separate headerfile ascii.h because I felt it would be neater.
IP: Logged |
Blayne Bradley
unregistered
posted
If by problems you mean making it correspond to Official 10.6.7.8.9.1-c rule of Chess as you see it hell no, if you mean by odd things like a pawn taking a pawn right infront of it yes.
Although I have determined that I also lacked the time and energy to check if by moving a non-king piece will it put me in check because the way I wrote my check function I'ld need a roll back function or completely rewrite my 400 line of code blob to get it to work right.
Case 1:
Move pawn, castle now checks king, rollback move declare invalid.
case 2: adjust the valid move segment of every single possible case to include a new case of "if I move will it put the King in check?" in some way that manages to figure out that it shouldnt include itself as a blocking piece since it will move otherwise.
IP: Logged |