0

This is a theoretical question, and excuse me if it is not clear, I am new to this field.

Thing is, I was asked to design a chess game in an interview (java/ android.) And my intuition was that P. Norvig is a very good and known designer, and that pocker is very similiar to chess. So I tried to imitate his pocker design. As far as it was possible for me to remember and understand it.

However, the interviewer said that it is the first time he saw an implmentation of this problem by enumeration. And that a proper implementation should have class for every piece (why?!). I actually asked why, and the answer was that every piece has very complicated rules (but to me these rules seem to be simple.)

Is it bad to design chess game like that (with enumeration)? What is the best approach to design a simple chess program in Java? And why?

I will thank very match anyone who can shed light on this topic.

closed as too broad by Doc Brown, user40980, user22815 Nov 13 '15 at 13:43

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    From the help center: Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you’re asking too much. - IMHO your question falls clearly under that category, people have written dozens of books on designing chess games. And in an interview, there is no "right" or "best" way to design a chess game. – Doc Brown Nov 13 '15 at 10:51
  • @DocBrown no it's not. It's definitely answerable. – SmallChess Nov 13 '15 at 11:01
  • 3
    I was asked to design a chess game in an interview - the interviewer isn't at all concerned with you actually designing a chess game. Or for that matter, that its fast or efficient. The interviewer is interested in finding out how you design software. Alternatively, the interviewer is interested in if you can come up with the same design as he or she has in their mind when the question is asked. Please read Why do interview questions make poor Programmers.SE questions? – user40980 Nov 13 '15 at 13:02
  • 2
    @StudentT if a question is "too broad" that does not mean it is not answerable: it means it is not answerable in a reasonable amount of space. – user22815 Nov 13 '15 at 13:43
  • 2
    @StudentT: you are correct, what the OP wants to know is not really how to design a chess game, but what to answer when someone in an interview asks that question. But interview questions will be closed here on Programmers as "too broad", too (see meta.programmers.stackexchange.com/questions/6360/…). – Doc Brown Nov 13 '15 at 13:56
7

I am a chess engine developer so I can tell you - neither approach is acceptable. A professional chess game always almost do it in some kind of special data structure such as a mailbox or bitboard. Why? For example to find out the moves for a bishop, you would have to loop through the squares that the piece can move on an empty board while checking for enemy pieces. This is a very slow operation and you will need to repeat the same looping for rook. What if you want to generate all the squares that your pieces can attack but your opponents can't? This would involve lots of unnecessary looping. Bitboard allows you to do all of that in O(1). In bitboard you can simple do an AND operation to two boards which are represented by two 64 bits integer.

Having said that, it was likely your interviewer was merely expecting for a object oriented solution. I dont think that's a right or wrong answer but I would go for classes. Allowing object oriented design would make future updates easier. You can change the underlying implementation without affecting the rest of the program. You can also make easy to read function names like p.move whereas not possible with enumeration. Furthermore you would probably have to copy and paste a switch block everytime you have to do something to your piece. OO avoided that with an abstract interface.

  • 1
    Good answer. I would add that enumeration means that everything gets handled in a single god class that has to know all positions of all pieces and handles all interaction between them. Putting a bit of work in the pieces themselves makes it a lot easier to deal with. You're right though. In practical terms, object oriented design would be quite a bit slower. – Neil Nov 13 '15 at 11:15
  • I actually used "Constants.java" class and fiew others (about 7), I also used Board.java class and MovePiece.java. So, it is not restrictive to use enumeration only with one big file. One can use enumeration in object oriented environment. Is it not so? – sivi Nov 13 '15 at 11:29
  • p.s. if you vote the answer vote the question so I can ask more, because the system here almost block me from asking. – sivi Nov 13 '15 at 11:30
  • 1
    @sivi I think the interviewer was more concern how you tell the program what to do with the pieces. You can do it with a switch block or some abstract methods. – SmallChess Nov 13 '15 at 11:32
  • 1
    +1, solving chess is not a problem where you should sacrifice speed for good design principles. But it should be stressed that game-playing is unusual in this respect: if we had unlimited processing power, it would be very easy (in fact, trivial) to write a program that is provably optimal. Almost no real-life business problems have that property. – Kilian Foth Nov 13 '15 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.