Some versions of the game doesn't permit two ships being side by side in which case using a boolean matrix followed by checking all four sides would be suffice to state hit or sink so it's worth asking the interviewer about this
@yissssss2 жыл бұрын
I think a better approach is to just store the ship objects with appropriate coordinates then remove the coordinates as they are hit. Once the coordinates are gone, the ship is sunk.
@walterloso8 жыл бұрын
I loved this. Could we please have more of this? More whiteboard solutions that are focused more on approaches to answer different technical questions.
@SamujjalMajumder5 жыл бұрын
Use a Map of Ship vs Integer Counter(for cells), reduce counter with everyhit and nullify corresponding cell value. If Counter == 0, its a SINK!
@madhumithabaskar98324 жыл бұрын
Please can you provide the program for this question?
@Cookiekeks4 жыл бұрын
Thank you so much! Im programming a battleship game currently and i struggelt but now i understand it more. Great Video!
@sunitgautam75476 жыл бұрын
Can somebody suggest more such content? Working out a solution to problem on a whiteboard and improving on it along the way.
@shawniscoolerthanyou7 жыл бұрын
I feel OOP would be pretty useful here.
@ancientelevator94 жыл бұрын
Right? I immediately started thinking of a player and ship class along with their properties and methods.
@xinli2169 Жыл бұрын
great video, i indeed encountered this problem during an interview
@IanDouglas7 жыл бұрын
Instead of searching the entire matrix to detect a sunk condition, you know that S2 can only be in positions left/right or up/down from the coordinate that you just hit. Wouldn't it be easier to store each ship in a smaller structure like ships = { 'player1': { 's1': {'x1,y1': false, 'x2,y2': true}, 's2': [{'x1,y1': true, 'x2,y2': true, 'x3,y3': false} etc} ... where the true/false values indicate if that piece of the ship is hit and if you hit s2, look it up in that other structure to detect how many other pieces were hit to mark is as sunk? I would think the complexity of maintaining that separate structure far easier than searching a 10x10 grid to find, at most, 5 other parts of the 6-piece boat, from 100 locations.
@zxcv98757 жыл бұрын
if you're assuming there is no repeats of firing, couldn't you also have a variable something like playerName.s2 which would hold the coordinates of ship 2 in an array and do somethingl ike if ((x,y) in playerName.s2) s2Life--; and if it's 0, you say sunk. Do the same for s1-s3 and for player1-2.
@leopalaciosgarcia63916 жыл бұрын
What if for a size-4 ship you first hit the 3rd position, then the 4th, if you check the positions left/right and up/down of the 4th you will be missing that positions 1 and 2 are still unhit. But sure, as he mentions, there are more elegant and efficient solutions.
@samuelmiller3 жыл бұрын
This was awesome
@T25de2 жыл бұрын
each game will have about two players... its a two player game... schitzo lol
@kengerfil3 жыл бұрын
3:30 - 3:47 The left guy pisses me off by the way he is acting, bruh
@nonconsensualopinion2 жыл бұрын
Might want to try some yoga or meditation. You seem high strung.