Chess Low Level Design Code | Software Interview Question | System Design

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone this is sonmez of this side welcome to my channel in today's video we will be discussing about the low level design of a chess game so without further ado let's get started [Music] fearless [Music] hello everyone i am savajit baracharya here welcome to my channel today in this video we will be discussing about the low level design of a chess game it is a very very well known problem that has been asked several times in many of the low level design interviews for software development engineers today we will code the complete solution for the chess game so without wasting any time let's get started i have already mentioned several times in all of my previous videos the link to which i have given here in the cart we will start by defining the base case for a chess game and then after we have to find the base case of a chess game we will go ahead and start defining all the complex objects that we have used for defining in the base case and then go ahead and define any of the services that might be required to fulfill the requirements that we have in hand so for the chess game obviously we will start by defining the chess class the chess class will comprise of a chess board which is a complex object a list of players here the list of player will be containing only two players for the purpose of the game then another object that will hint us to who is the current player in that particular game the list of moves that has already been played in the game the game status these are the data members that the chess class will contain the game status will tell us the present status of the game whether or not the game has been finished whether or not the game has been forfeited if black has won if white has won or it's ongoing then we have the following three apis one the player move which consists of the starting cell position the ending cell position and the piece that a particular player is moving then we will have another api which is known as the end game api that will end the current game and we will have another api which is the change turn api this api if you notice is private in nature why this is private in nature you might ask this api is private in nature because this need not be called from outside the system instead this particular api the change turn api will be called from the player move api itself once the player move has been finished then we will call the change turn api which will change the current player inside the particular game now we will go ahead and start defining all the complex objects that we have used inside the chess class first and foremost we will start by defining the player class the player class basically represents the player who is playing that particular game of chess hench the player class will contain an account which will represent the actual person who is playing as that particular player second it will contain the color of that particular player whether it's white or whether it's black third it will contain the time left which will store the remaining time of that particular player inside the game now we will go ahead and define the account class the account class as usual will contain the username the password as well as the name phone number and email then we will go ahead and quickly define the enum color which i have already told will contain two values one is black and the other one is white then we will go ahead and define the time class which will contain again two values one is minutes and the other one is seconds now we will define the game status enum as i've already told before the game status in them will actually tell us the present status of the game whether or not the game is active whether or not the game is paused or if someone is forfeited if black has one or white hash one after that we will define the chessboard class if you see a traditional chessboard it will be coming up right now in the screen it is comprised of 8 cross 8 cells each cell can be black in color or white in color to represent such a scenario we have used a list of list of cells which is nothing but the entire representation of the chess board yes it is true that we could have used a matrix kind of a representation for it but using list of list gives us much more flexibility so that in future if we want to change the shape of the chessboard as well we can do so by using this list of lists instead of using a matrix your list of list essentially defines the entire chessboard apart from this particular data member it will also contain two other apis one to reset the board and two to update the board update board api will be called by the player move function inside the chess class because every move should be updating the chess board hence we have included the chessboard data member inside the chess class now every time a player moves based upon the move that has been suggested here the chessboard will be updated then we will go ahead and quickly define the cell class that we have introduced inside the chess board now we know that a chessboard is made up of 64 individual cells eight individual cells in each line these cells can be of black color or white color and these cells are represented by a specific position and they can have some peace or they might not have any peace in them hence we will use this information to see what are the data members that should be present inside the cell class so your the cell class as i have explained should contain the color object it should contain the piece information if there is any piece on that particular cell as well as it should contain a cell position that will uniquely identify one particular cell in the chess board then we will go ahead and quickly define the cell position as we know in chess each cell is identified by a character and a number hence this cell position class similarly will contain a character object as well as an integer number inside it we can see that we have not yet defined the move object here that we have introduced inside the chessboard class so we will quickly go ahead and define the move object the move object will actually tell us all the details of a particular node hence the move object will contain a player object which will tell us which player has played this particular move it will contain a piece object that will tell us which piece that particular player has moved in this particular move then it will contain another object which is the killed piece which is also a piece object which will tell us if any piece has been killed in this particular move then this particular object will be populated with that information if there is no p skill in this particular move then this particular object will be null then it will contain the start position of the move and the end position of the move this entire object contains all the necessary information about a particular move which will also help us in keeping the history of moves that has been played in this particular game which is present in the chess class now we will go ahead and define the piece class that we have used so extensively in all the other complex classes that we have created till now this piece class will contain one attribute one data member with which is the color of the fish this color is the same medium that we have used everywhere apart from this it will also contain three apis one will contain the move method that will be called when we will be updating the chessboard with a particular move information this particular method will take input the from cell position and the two cell position detailing out from which position to which position this particular piece needs to move there is another api which is the possible moves api which will return a list of all the final cell positions that can be reached from a particular position for a particular piece the third api will be a validate api that will take in as input the from position and the two position and will tell us if this particular move is valid or not as we know inches there are different kinds of pieces all the pieces that are present in chess are king the queen the knight the bishop the rook as well as the pawn all these are essentially chess pieces and they have very peculiar way of moving around the board for example a rook can move in straight lines either horizontally or vertically similarly a bishop can move only in diagonal lines on the other hand a queen can move in practically any direction whatsoever akin can also move in any direction but it can only move one step at a time and so on and so forth so all these different kinds of chess pieces have different kind of move implementation have different kind of possible moves that can be present and has different kinds of validations in place hence we can safely say that we can denote an s or relationship between these chess pieces and the piece abstract class that we have defined hence we will be using inheritance here and we will be defining all the chess pieces here and these classes will be extending this piece class and will define the methods present here so i will just quickly write down all the classes so we have the night class that extends the peace class the bishop class that extends the peace class the root class that extends the peace class the king class that extends the peace class and the queen class that extends the peace class and ultimately the pawn class that extends the peace class and all all the chess pieces will be overriding these methods which is move possible moves and validate method present inside the piece class now i can see one thing all the child classes that is the night bishop rook king queen and pawn essentially overwrite all the three methods here so we can safely make these three methods inside the piece abstract class to be abstract methods all these chess pieces will have their very own implementation of the move method of the possible moves method and of the validate method by this we come to an end of this low level design here we were able to come up with a robust solution for the low level design of chess game this is one of the most widely asked questions in software development in general low level design interviews one possible extension that i can think of this particular question is that to develop a complete gaming system like chess.com so probably i would suggest that you take this code to be the basis of designing a chess game and then extend this code to make the low level design of chess.com if you have any questions whatsoever regarding this code or regarding extending this code to chess.com then please mention it below i will make sure that i answer and solve all your queries that you have i really really hope that you like my content if you really liked it then please do share it among your friends so that it gets out to as many people as possible and i am able to help them by providing more such quality content so i please ask you to like this video and subscribe to my channel if you not have done already it really encourages me to make more such videos for you guys if you have any query whatsoever for this particular video or any video for that matter please comment down below if you have any query about anything comment down below i will make sure that i answer each and every one of your comments thank you so much for watching till the end of the video i hope you really enjoyed it as always i have mentioned my instagram profile in the description box below if you want to be a part of my marketing series then please follow me there and send me a message telling the same i will make sure to include you in my mock interview series not only this i have also uh given the link to download the source code of the chess eldi so you can do that please please do like and share this video and comment down below what more different kind of videos you want me to make and subscribe to my channel thank you have a nice day this is amazing breathing goodbye
Info
Channel: Soumyajit Bhattacharyay
Views: 33,682
Rating: undefined out of 5
Keywords: Software development, system design, LLD, HLD, Interview Preparation, Soumyajit, Soumyajit bhattacharyay, bhattacharya, Amazon, Facebook, Google, Netflix, FAANG, Low level Desgn
Id: koqj7xlkPNQ
Channel Id: undefined
Length: 13min 38sec (818 seconds)
Published: Sun Sep 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.