Microsoft Interview Question for Software Engineer in Tests


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

I am trying to get a feel of how to design and think in an Object Oriented manner and want to get some feedback from the community on this topic. The following is an example of a chess game that I wish to design in an OO manner. This is a very broad design and my focus at this stage is just to identify who is responsible for what messages and how the objects interact each other to simulate the game. Please point out if there are elements of bad design (high coupling, bad cohesion etc.) and how to improve on them.

The Chess game has the following classes

Board
Player
Piece
Square
ChessGame
The Board is made up of squares and so Board can be made responsible for creating and managing Square objects. Each piece also is on a square so each piece also has a reference to the square it is on. (Does this make sense?). Each piece then is responsible to move itself from one square to another. Player class holds references to all pieces he owns and is also responsible for their creation (Should player create Pieces?) . Player has a method takeTurn which in turn calls a method movePiece which belongs to the piece Class which changes the location of the piece from its current location to another location. Now I am confused on what exactly the Board class must be responsible for. I assumed it was needed to determine the current state of the game and know when the game is over. But when a piece changes it's location how should the board get updated? should it maintain a seperate array of squares on which pieces exist and that gets updates as pieces move?

Also, ChessGame intially creates the Board and player objects who in turn create squares and pieces respectively and start the simulation. Briefly, this might be what the code in ChessGame may look like

Player p1 =new Player();
Player p2 = new Player();

Board b = new Board();

while(b.isGameOver())
{
p1.takeTurn(); // calls movePiece on the Piece object
p2.takeTurn();

}
I am unclear on how the state of the board will get updated. Should piece have a reference to board? Where should be the responsibility lie? Who holds what references? Please help me with your inputs and point out problems in this design. I am deliberately not focusing on any algorithms or further details of game play as I am only interested in the design aspect. I hope this community can provide valuable insights.

- sathishkumar August 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I would think that instead of having squares, it would just be a 12*12 (or whatever the chess board dimensions are) array of pieces, as maintained by the board class.

I feel that instead of players holding references to each piece, board should be in charge of all the pieces, and each piece has a reference to which player owns it (or at least a string or something indicating the player who can move it). Thus when player wants to make a move, he interacts with the board class, which in turn interacts with the pieces, rather than player directly interacting with the pieces. The board should be in charge of maintaining the pieces, making sure the player is moving his or her own pieces only, and keeping the rules in check.

Thus I figure board should be the heart of this chess game. It contains the pieces, and players request the board to move their pieces. Board can also easily check to see if one side won, since it has contains the pieces.

Just my 2 cents on this

- Anon August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

So basically under my implementation there would only be 3 main classes at work:
board
player
piece
it would probably be good to also have a "game" class as well, though I feel that's one of those tasks which could be delegated to the board class.

- Anon August 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Sathish Are you the same person who posted on stack overflow - stackoverflow.com // questions 4168002 // object-oriented-design-for-a-chess-game ,If not please avoid plagiarising somebody else's idea.Only ask suggestions to improve your ideas.

- Ran August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Peice should be an base class then. King, Queen, etcc should be the classes. Every peice has a position, and it can move. Peice should be a abstract class and move should be method and position should be attributes. Then King, Queen, etc should inherit from this class and implement method move. This move method will take in Board object and change its state. It is the boards responsibility to return if the move was legal, if it was whether the move resulted into check or a end of game. Board will have method getState() so that the player can have knowledge of the state of the board and also the player will know if it is his turn to play. Player will have turn method which will do two things. It will get the state and depending on the state will move a peice. If getState returned EOG(End of game) the player will go home.

- Sandesh September 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL@ Sathish. cut & paste without even trying to read.

- Anonymous September 10, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More